最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Set working directory in gulpfile.js? - Stack Overflow

programmeradmin0浏览0评论

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!

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!

Share Improve this question edited Dec 1, 2014 at 21:15 asked Dec 1, 2014 at 20:11 user677526user677526 8
  • 3 __dirname stackoverflow.com/a/8131696/94144 ? – coma Commented Dec 1, 2014 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? – user677526 Commented Dec 1, 2014 at 21:00
  • Doesn't it already do that? – Evan Davis Commented Dec 1, 2014 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') nodejs.org/api/path.html#path_path_join_path1_path2 – coma Commented Dec 1, 2014 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. – user677526 Commented Dec 1, 2014 at 21:09
 |  Show 3 more comments

2 Answers 2

Reset to default 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.

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.

发布评论

评论列表(0)

  1. 暂无评论