Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set working directory in gulpfile.js?

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!

like image 537
jedd.ahyoung Avatar asked Dec 01 '14 20:12

jedd.ahyoung


People also ask

Where do I put Gulpfile js?

Writing the First Gulp Task. All Gulp configuration goes in a file called gulpfile. js located at the root of the project. The pattern for writing tasks is that you first load a plugin you're about to use and then define a task that is based on that plugin.

What is Gulp SRC?

The src() and dest() methods are exposed by gulp to interact with files on your computer. src() is given a glob to read from the file system and produces a Node stream. It locates all matching files and reads them into memory to pass through the stream.

How do I run a gulp file in JavaScript?

In addition to making sure you have gulp globally installed, make sure your gulp file is named *gulpfile. js* and that is in the same directory as where you are running gulp . Then simply run gulp task1 .


2 Answers

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.

like image 147
gfaceless Avatar answered Sep 23 '22 07:09

gfaceless


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.

like image 29
coma Avatar answered Sep 24 '22 07:09

coma