我正在重构我的 Gulp 构建过程,以便用户不必输入 V=1.2.88
等。
我希望用户只需输入major gulp build
、minor gulp build
或patch 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 is not a function
最佳答案
我不太清楚有关次要/专业的部分,但关于目录列表,您可以执行以下操作:
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);
})
关于javascript - Gulp:如何读取文件夹名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36225793/