Javascript 在 gulpfile.js 中设置工作目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27236437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set working directory in gulpfile.js?
提问by jedd.ahyoung
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.
有没有办法在 gulpfile 中设置 Gulp 的工作目录,以便我可以从子目录运行 gulp 命令而不会遇到任何问题?我对此进行了搜索,但没有找到我要找的东西。
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.
我想知道 Gulp 是否公开了这个功能。也许节点本身允许这样做。
(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.)
(我意识到在运行命令时可能有一种方法可以自己执行命令行,但我想将它包含在 gulp 文件中,尤其是出于分发目的。我希望 gulp 的工作目录与其中的目录相匹配gulpfile 驻留。)
Thanks!
谢谢!
采纳答案by coma
Instead of concatenating strings by yourself, you should be using path.joinsince it will take care of the proper slash, and following that path you can add a shorcut:
您应该使用path.join而不是自己连接字符串,因为它会处理正确的斜杠,并且在该路径之后您可以添加一个快捷方式:
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);
};
回答by gfaceless
Besides option.cwd, you can also use process.chdir(yourDir)
此外option.cwd,您还可以使用process.chdir(yourDir)
it could be used anywhere in a gulpfile. e.g.
它可以在 gulpfile 中的任何地方使用。例如
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.
确保您的 gulp 是最新的(> 3.8.10),这可能不适用于较旧的 gulp。

