我想你想要下面这样的东西。我简化了一些事情,以便测试它。
第一点:你的
getFolders()
功能可以很容易地用
glob.sync()
见电话
glob options
.由于您显然只需要文件夹,因此您的全局调用的全局模式必须以“
/
“,即a
path.sep
.
这里返回流的关键点是使用
gulp-merge
它允许您返回每个文件夹的合并流。绘制各个流的地图。
const gulp = require('gulp');
const path = require('path');
const merge = require('gulp-merge');
const gulpif = require('gulp-if');
const glob = require("glob");
const inject = require('gulp-inject-string');
function injectAfterEach(src) {
const folders = glob.sync(src);
console.log(folders);
return merge(folders.map(function (folder) {
console.log("folder basename = " + path.basename(folder));
if (path.basename(folder) !== 'base') {
let fullPath = path.join("RemotePath", path.sep, path.basename(folder), path.sep);
console.log('fullPath: ' + fullPath);
return gulp.src(path.join(folder, '*.html') )
.pipe(inject.afterEach('<script src="', fullPath))
.pipe(gulp.dest('dist'))
}
}));
}
gulp.task('default', function() {
return gulp.src('./htmlFolder')
.pipe(gulpif(true, injectAfterEach('./htmlFolders/**/')))
// just commented out for testing
// .pipe(gulpif(process.env.NODE_ENV == 'production', removeCode({ production: true })))
// .pipe(gulpif(process.env.NODE_ENV == 'production', htmlmin(config.tasks.html.htmlmin)))
.pipe(gulp.dest('dist'))
// .pipe(browserSync.stream());
});