从孩子目录中运行gulp? [英] Run gulp from child directories?

查看:123
本文介绍了从孩子目录中运行gulp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  SASS 
gulpfile.js
node_modules
站点
示例站点
scss
css
示例站点二
scss
css
示例站点三
scss
css

我在主父SASS文件夹中安装了gulp 'sass-all'可以通过每个单独的站点scss文件夹并将其编译为css。

我正在尝试编写一个名为'sass-single'的新任务,该任务可以从任何示例站点文件夹运行。假设我在文件夹example-site-two中,我希望能够cmd并执行gulp sass-single,并且只需要在此网站上编译SASS。



问题是,每当我从站点文件夹运行此任务时,它都会将我的工作目录更改为父SASS文件夹。我不想为每个不同的站点执行100个不同的任务,我宁愿只做一个'sass-single'任务,这个任务足够聪明,只能在我运行脚本时从我所在的文件夹中编译文件。 / p>

当前Gulp任务尝试

  gulp .task('sass-single',function(){
process.chdir('./');

// SCSS文件在哪里?
var input = './scss/*.scss';

//你要保存编译CSS文件的位置?
var output ='./css';

return gulp
.src(input)
.pipe(sourcemaps.init())
.pipe(sass(s​​assOptions).on('error',sass.logError))
.pipe(postcss(processors))
.pipe(sourcemaps.write('./ maps'))
.pipe(gulp.dest(output));
}) ;

然而,这可以回到主SASS文件夹,然后什么都不做。
我该如何去修改它,以便能够从任何站点文件夹运行并让它只为该站点执行?

解决方案如果您想将当前工作目录(CWD)更改回到您调用 gulp 的目录,那么这将不起作用:

  process.chdir('./'); 

这是一条相对路径。相对路径与CWD有关。但是当你执行 process.chdir('./')时,Gulp已经将CWD更改为你的Gulpfile.js所在的目录。所以你只是把CWD改成CWD。

你可以明确地将CWD传递给 gulp 在命令行上:

  SASS / sites / example-site>吞噬--cwd。 

但是这会让人很烦恼。

幸运的是,咕嘟店存储原始CWD process.env.INIT_CWD 在改变之前。因此,您可以在任务中使用以下内容将CWD更改回原始:

  process.chdir(process.env。 INIT_CWD); 


I currently have a file structure like this

SASS
    gulpfile.js
    node_modules
    sites
        example-site
            scss
            css
        example-site-two
            scss
            css
        example-site-three
            scss
            css

I have gulp installed in the main parent SASS folder with a task 'sass-all' that can go through every single sites scss folder and compile it into css.

I'm trying to write a new task called 'sass-single' which can be run from any of the example-site folders. So let's say I'm in the folder "example-site-two", I want to be able to cmd and do 'gulp sass-single' and ONLY have it compile the SASS in this site. Same thing for a watch-single task I'd like to setup.

Problem is whenever I run this task from a site folder, it changes my working directory up to the parent SASS folder. I don't want to have 100 different tasks for every different site, I'd prefer to just have one 'sass-single' task thats smart enough to only compile the files from the folder I was in when I ran the script.

current Gulp task attempt

    gulp.task('sass-single', function () {
      process.chdir('./');

      // Where are the SCSS files?
      var input = './scss/*.scss';

      // Where do you want to save the compiles CSS file?
      var output = './css';

      return gulp
        .src(input)
        .pipe(sourcemaps.init())
        .pipe(sass(sassOptions).on('error', sass.logError))
        .pipe(postcss(processors))
        .pipe(sourcemaps.write('./maps'))
        .pipe(gulp.dest(output));
    });

However this goes back to the main SASS folder and then just does nothing. How would I go about modifying this to be able to run from any site folder and have it only do it for that site?

解决方案

If you want to change the current working directory (CWD) back to the directory where you invoked gulp then this won't work:

process.chdir('./');

That's a relative path. Relative paths are relative to the CWD. But by the time you execute process.chdir('./') Gulp has already changed the CWD to the directory where your Gulpfile.js is located. So you're just changing the CWD to ... the CWD.

You could explicitly pass a CWD to gulp on the command line:

SASS/sites/example-site> gulp --cwd .

But that would get annoying pretty quickly.

Luckily for you Gulp stores the original CWD in process.env.INIT_CWD before changing it. So you can use the following in your task to change the CWD back to the original:

process.chdir(process.env.INIT_CWD);

这篇关于从孩子目录中运行gulp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆