7

Is there a way to set the working directory for Gulp within a gulpfile, so that I can run a gulp command from a subdirectory without running into any issues? I ran a search for this and didn't find what I was looking for.

To clarify, I'm aware of adding a prefix to the files I'm using. However, instead of this -

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'd 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'm wondering if Gulp exposes this functionality. Perhaps node itself allows this.

(I realize that there is likely a way to do command line itself when I run the command, but I would like to include it in the gulp file, especially for distribution purposes. I want the working directory for gulp to match the directory in which the gulpfile resides.)

Thanks!

  • 3
    __dirname http://stackoverflow.com/a/8131696/94144 ? – coma Dec 01 '14 at 20:34
  • 1
    That's one way to get the script directory, sure, but how can I set that directory as my working directory? There's `process.cwd()`, but it doesn't look as though I can assign anything through that. Does gulp expose something to let me accomplish this, or is it something that only a node command can offer? –  Dec 01 '14 at 21:00
  • Doesn't it already do that? – Evan Davis Dec 01 '14 at 21:03
  • I'm not sure how you are using Gulp, but it comes with two main methods, src and dest, and you can set both using, let's say path.join(__diename, 'src', 'styles') http://nodejs.org/api/path.html#path_path_join_path1_path2 – coma Dec 01 '14 at 21:06
  • 2
    @coma I do know about that possibility, but I was wondering if I could avoid prefixing every directory with `__dirname` and instead do something like `process.cwd = __dirname`. Bower offers this capability in a config file (which, admittedly, is not really the same thing) and so I was wondering if there's a way to be less verbose. –  Dec 01 '14 at 21:09
  • @coma I've edited the question to hopefully make it a bit clearer. –  Dec 01 '14 at 21:15
  • What is the particular issue you're running into? By default gulp will set your cwd to the gulpfile.js directory. – Justin Howard Dec 02 '14 at 01:15
  • @justinhoward From what I've seen so far, the cwd isn't set to the gulp file directory if you run gulp from a subdirectory in your project. Admittedly, I've probably messed it up somehow.... –  Dec 02 '14 at 04:40

2 Answers2

9

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

it could be used anywhere in a gulpfile. e.g.

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

Make sure your gulp is up-to-date( > 3.8.10), this may not work in older gulp.

gfaceless
  • 1,529
  • 1
  • 17
  • 22
4

Instead of concatenating strings by yourself, you should be using path.join since it will take care of the proper slash, and following that 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 just:

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

Something like:

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);
};

Look here and here.

coma
  • 16,429
  • 4
  • 51
  • 76
  • Hmm. This might fit my needs - and it's good to know about the `cwd` argument to `gulp.src` and `gulp.dest`. I generally like to try to keep my gulpfiles short (but readable), but in the event of larger ones I will likely end up using your final methods. Thanks! –  Dec 02 '14 at 04:37
  • @jedd.ahyoung, you can split your Gulp flows into several files and combine them using closures like https://coderwall.com/p/yzvbvw/defining-and-running-gulp-tasks-for-each-environment-on-the-fly?p=1&q=author%3Acoma – coma Dec 02 '14 at 08:11