Gulp change working directory for entire task

时光毁灭记忆、已成空白 提交于 2019-11-29 09:22:41

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.

With the way that gulp works, it doesn't make sense to "change directories" in the steps between src and dest.

gulp.src reads files from disk into memory. The .pipes thereafter just pipe the contents of those files (and metadata related to the files - see Vinyl file objects for more info) to other javascript functions, each of which just operates on the in-memory versions of the file. That means they don't know about a working directory, or care about one. They just see incoming data about files come in through pipe, do stuff to that data, and send it out through the next .pipe.

So I think that if you're truly paranoid about being in the parent director, a cwd to src and dest is the best you can do.

All of that to say, you could look at shelljs, which allows you to change directories for the current node process. I think this might be considered an anti-pattern when piping things around, but might be worth a shot.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!