Install working directory in gulpfile.js?

Is there a way to set the working directory for Gulp in the gulpfile so that I can run the Gulp command from a subdirectory without any problems? I started the search and did not find what I was looking for.

To clarify, I know about adding a prefix to the files that I use. However, instead -

var gulp = require('gulp'); var jshint = require('gulp-jshint'); ... var paths = { js: [__dirname + 'app/*/*.js', __dirname + '!app/lib/**'], css: __dirname + 'app/*/*.styl', img: __dirname + 'app/img/*', index: __dirname + '*.html', dist: __dirname + 'dist' }; 

I would like to do something like this:

 var gulp = require('gulp'); var jshint = require('gulp-jshint'); ... gulp.cwd(__dirname); // This would be much easier to understand, and would make future edits a bit safer. var paths = { js: ['app/*/*.js', '!app/lib/**'], css: 'app/*/*.styl', img: 'app/img/*', index: '*.html', dist: 'dist' }; 

I am wondering if this function reveals Gulp. Perhaps node itself allows this.

(I understand that when I run the command, there is a way to make the command itself, but I would like to include it in the Gulp file, especially for distribution purposes. I need a working directory for Gulp to match the directory where the gulpfile is located.)

Thanks!

+5
source share
2 answers

Instead of joining the strings yourself, you should use path.join , as it takes care of the correct slash, and after this path you can add a shorcut:

 var path = require('path'), p = function () { Array .prototype .unshift .call(arguments, __dirname); return path.join.apply(path, arguments); }; console.log(p('a', 'b', 'c')); 

Or, well, you can simply:

 gulp.src(..., {cwd: __dirname}) gulp.dest(..., {cwd: __dirname}) 

Sort of:

 var src = function (globs, options) { options = options || {}; options.cwd = __dirname; return gulp.src(globs, options); }; var dest = function (folder, options) { options = options || {}; options.cwd = __dirname; return gulp.dest(folder, options); }; 

See here and here .

+4
source

Besides option.cwd , you can also use process.chdir(yourDir)

it can be used anywhere in the gulpfile. eg.

 process.chdir(yourDir); var gulp = require('gulp'); 

Make sure your gulp is updated (> 3,8.10), this may not work with older gulp.

+9
source

Source: https://habr.com/ru/post/1208075/


All Articles