问题描述
我正在研究一个包含我网站前端和后端任务的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)
frontendPath =(dirs ..)你可以看到我创建了一个帮助程序来提供正确的前端路径。 。) - > path.join.apply null,['frontend']。concat(dirs)
但是我必须请务必小心,我的任务(特别是.src和.dest)的所有步骤都在前端文件夹中执行。
我知道您可以使用 {cwd:'frontend'}
选项可以更改.src和.dest的工作目录。但有没有办法改变整个工作目录的任务?使用 process.chdir
解决方案
更改工作目录。我们可以在任何地方进行更改,或者在您的情况下,在任务中更改它。
gulp.task(' frontend',function(){
process.chdir('...');
gulp.src(...)
});
gulp.task('backend',function(){
process.chdir('...');
gulp.src(...)
});
确保使用最新版本的gulp,
这个功能似乎是。
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)
But I have to be really careful that all the steps of my task (especially .src and .dest) are executed in the frontend folder.
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?
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(...)
});
Make sure using the latest version of gulp,this feature seems to be added recently.
这篇关于Gulp更改整个任务的工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!