在gulpfile.js中设置工作目录? [英] Set working directory in gulpfile.js?

查看:114
本文介绍了在gulpfile.js中设置工作目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在gulpfile中设置Gulp的工作目录,这样我就可以从子目录运行gulp命令而不会遇到任何问题?为了澄清,我知道为我正在使用的文件添加前缀。然而,不是这样 -

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

我想这样做:

  var gulp = require('gulp'); 
var jshint = require('gulp-jshint');
...

gulp.cwd(__ dirname); //这会更容易理解,并且会使未来的编辑更安全一些。

var paths = {
js:['app / * / *。js','!app / lib / **'],
css:'app / * /*.styl',
img:'app / img / *',
index:'* .html',
dist:'dist'
};

我想知道Gulp是否公开此功能。也许节点本身允许这样做。



(我意识到在运行命令时可能有一种方法可以执行命令行,但我希望将它包含在我希望gulp的工作目录与gulpfile所在的目录相匹配。)



谢谢!

$ b $你应该使用 path.join ,因为它会照顾正确的斜杠,并且在该路径后面可以添加一个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'));

或者,您可以:

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

类似于:

  var src = function(globs,options){

options = options || {};
options.cwd = __dirname;

返回gulp.src(globs,options);
};

var dest =函数(文件夹,选项){

options = options || {};
options.cwd = __dirname;

返回gulp.dest(文件夹,选项);
};

此处此处


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!

解决方案

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.

这篇关于在gulpfile.js中设置工作目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆