【问题标题】:Gulp: How to read folder name?Gulp:如何读取文件夹名称?
【发布时间】:2016-03-25 18:32:34
【问题描述】:

我正在重构我的 Gulp 构建过程,因此用户不必输入 V=1.2.88 等。

我希望用户只输入major gulp buildminor gulp buildpatch gulp build。然后当然会迭代版本号。

为此,我需要 gulp 来读取上次创建的构建的文件夹名称:

生成版本号的我当前的 Gulp 任务:

var version = '';
var env = process.env.V; // V={version number} ie: V=1.0.1 gulp build

gulp.task('version', function() {
    return printOut(env);
});

function errorlog(err) {
    console.log(err.message);
    this.emit('end');
}

function printOut(ver) {
    gutil.log(gutil.colors.blue.bold('Last build: '+paths.last));
    version = ver;
    if (version === undefined) {
        version = '0.0.0';
    }
    gutil.log(gutil.colors.blue.bold('##################################################'));
    gutil.log(gutil.colors.blue.bold('         Building Dashboard version '+version));
    gutil.log(gutil.colors.green.bold('~~           All change is detectable           ~~'));
    gutil.log(gutil.colors.blue.bold('##################################################'));
}

有人知道如何在 Gulp 中完成此任务吗?

这是我目前发现的Gulp-folders

所以使用Gulp-folders 插件我创建了以下首先运行的任务:

    gulp.task('build:getLastBuild', folders(paths.lastBuild, function(folder) {
    console.log( 'Last version number is: '+folder);
    return lastVersion = folder;
    //This will loop over all folders inside pathToFolder main, secondary
    //Return stream so gulp-folders can concatenate all of them
    //so you still can use safely use gulp multitasking
    // return gutil.colors.blue.bold('Last build folder: '+folder);
    // return gulp.src(path.join(paths.lastBuild, folder))
    //     .pipe(console.log(' Getting last version number: '+folder))
    //     .pipe(lastVersion = folder);
}));

现在,当我运行构建时,请在下面查看!我在 console.log 中获得了文件夹的名称,但是我的进程出错了:(

TypeError: e.pipe 不是函数

【问题讨论】:

  • 我不明白“读取文件夹名称”和您的构建任务之间的关系。但是,我认为您应该使用yargs (npmjs.com/package/yargs),这样您就可以执行类似gulp --major 2.2.2 build 的操作。如果major是最新版本,也许你只需要使用node的path模块,带有排序功能来查找最新版本
  • 这个想法是确保用户不需要知道或输入任何版本号。我想读取文件夹的名称,在上面的示例中:1.2.8 然后如果它是 Major 1+1 或 Minor 2+1 等...
  • npmjs.com/package/semver-utils 检查版本和fs.readdir()fs.readdirSync() 读取目录怎么样?
  • 啊是的fs.readdir 我会检查一下,还要检查我发布到我的问题的更新。我找到了一个可以检查文件夹名称的任务,但是遇到了错误。
  • 关于你的空操作,结帐gulp-ifgithub.com/robrich/gulp-if,尤其是gulpIgnore

标签: javascript build gulp build-process gulp-folders


【解决方案1】:

我不完全了解未成年人/专业的部分,但关于目录列表,您可以执行以下操作:

var fs = require('fs'),
    gulp = require('gulp');

gulp.task('default', function() {
    var dirs = fs.readdirSync('./build/assets');
    console.log(dirs);
    // do something with your directories
})

// and the async version:
gulp.task('async', function() {
    var dirs = [];
    var print = function(err, files) {
        // do something with your directories
        console.log(files)
    };

    fs.readdir('./build/assets', print);
})

【讨论】:

  • 好吧,这正是我解决它的方法:) 检查这个,虽然因为我的build/assets 中有 2 个文件夹,我必须在下面的代码中检查以避免迭代 @987654323 @文件夹。
【解决方案2】:

知道了!虽然我承认这有点粗糙,但我搜索了 node readdir 并找到了__dirname console.log(__dirname);

因此,我在下面创建了变量和任务:

var fs   = require('fs'),
    path = require('path');

gulp.task('build:getLastBuild', function() {
    return fs.readdirSync(paths.lastBuild).filter(function(file) {
        console.log(' build:getLastBuild: '+file);
        if (file != 'static') {
            lastVersion = file;
        }
        else {
            console.log('  lastVersion: '+lastVersion);
        }
    });
}); 

所以现在得到这个!现在我有一个字符串,我可以在用户运行构建过程时对其进行操作以增加版本号。

【讨论】:

  • 啊,我的回答太慢了:S。干得好;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-26
  • 2016-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多