Gulp 更改整个任务的工作目录 [英] Gulp change working directory for entire task
问题描述
我正在处理一个 gulp 文件,其中包含我网站前端和后端的任务.
例如,下面的任务会将我的脚本连接到 app.js:
I'm working on a gulp file that contains tasks for both the frontend and the backend of my site.
The task below for example will concat my scripts into 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)
如您所见,我创建了一个帮助程序来提供正确的前端路径:
As you can see I've created a helper to provide the correct frontend path:
frontendPath = (dirs...) -> path.join.apply null, ['frontend'].concat(dirs)
但我必须非常小心,我的任务的所有步骤(尤其是 .src 和 .dest)都在前端文件夹中执行.
But I have to be really careful that all the steps of my task (especially .src and .dest) are executed in the frontend folder.
我知道您可以使用 { cwd: 'frontend' }
选项来更改 .src 和 .dest 的工作目录.但是有没有办法更改任务的整个工作目录?
I know that you can use the { cwd: 'frontend' }
option to change the working directory for .src and .dest. But is there a way to change the whole working directory for a task?
推荐答案
使用process.chdir
来改变工作目录.我们可以在 gulpfile 中的任何位置进行更改,或者根据您的情况,在任务中进行更改.
Use process.chdir
to change the working directory. We can make the change anywhere in a gulpfile, or in your situation, change it within a task.
gulp.task('frontend', function(){
process.chdir('...');
gulp.src(...)
});
gulp.task('backend', function(){
process.chdir('...');
gulp.src(...)
});
确保使用最新版本的 gulp,此功能似乎是最近添加的.
Make sure using the latest version of gulp, this feature seems to be added recently.
这篇关于Gulp 更改整个任务的工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!