Gulp更改整个任务的工作目录

Zyp*_*rax 13 frontend backend task working-directory gulp

我正在处理一个gulp文件,其中包含我的站点的前端和后端的任务.
例如,下面的任务将我的脚本连接到app.js:

gulp.task 'frontend:scripts', ->
    gulp.src frontendPath(scriptsFolder, scriptsPattern)
        .pipe sourcemaps.init()
        .pipe coffee()
        .pipe concat 'app.js'
        .pipe sourcemaps.write('.')
        .pipe gulp.dest frontendPath(tempFolder, scriptsFolder)
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我已经创建了一个帮助程序来提供正确的前端路径:

frontendPath = (dirs...) -> path.join.apply null, ['frontend'].concat(dirs)
Run Code Online (Sandbox Code Playgroud)

但我必须非常小心,我的任务的所有步骤(尤其是.src和.dest)都在前端文件夹中执行.

我知道您可以使用该{ cwd: 'frontend' }选项更改.src和.dest的工作目录.但有没有办法改变任务的整个工作目录?

gfa*_*ess 26

使用process.chdir更改工作目录.我们可以在gulpfile中的任何位置进行更改,或者在您的情况下,在任务中进行更改.

gulp.task('frontend', function(){
  process.chdir('...');
  gulp.src(...)
});

gulp.task('backend', function(){
  process.chdir('...');
  gulp.src(...)
});
Run Code Online (Sandbox Code Playgroud)

确保使用最新版本的gulp,最近似乎添加了此功能.