一个for循环中的gulpstream不能正常工作
我有一个类似于以下的吞咽任务。 我试图循环,并追加一个string到页面,并连接一个相应的文件。
gulp.task('loop', function() { for(i =0; i < 10; i++) { gulp.src('./build/ross/templates/pages/_page_home.liquid') .pipe(plugins.insert.append('Loop ' + i)) // real example has concat here .pipe(plugins.addSrc('./src/page/_page' + i + '.liquid')) .pipe(plugins.concat('_page_home.liquid')) .pipe(gulp.dest('./build/ross/templates/pages/')); } });
预期的输出将是类似的
Loop 1 // _page1.liquid contents Loop 2 // _page2.liquid contents Loop 3 // _page3.liquid contents and so on...
实际产出看起来更像
// Original Page Contents here Loop 9 // _page9.liquid contents
我在循环中的原始文件上做了一个fs.readFile
并输出数据,它只输出原始页面内容,而不是连接文件或我试图追加的string。
我也做了一个console.log(i)
里面,它显示正确的数字1,2,3,4,5,6,7,8,9
。 我想知道这是否与gulp.src()有关
更新:有时看起来输出内容实际上会有_page#.liquid内容的一部分。 这有点不一致。
有没有更好的方法来做到这一点吞咽或我只是做错了什么?
在freenode irc的#gulpjs上得到它的帮助。
这使用stream-to-promise
和bluebird
var s2p = require('stream-to-promise'); var Promise = require('bluebird'); gulp.task('loop2', function() { // don't forget your `var`s! var buildIt = function(i){ return gulp.src('./build/ross/templates/pages/_page_home.liquid') .pipe(plugins.insert.append('Loop ' + i)) .pipe(gulp.dest('./build/ross/templates/pages/')) .on('end', function(){ console.log(i + ' -- done')}); } var indexes = []; for(var i = 0; i < 10; i++) { indexes.push(i); } // // sequence them indexes.reduce(function(p, index){ return p.then(function(){ return s2p(buildIt(index)); }); }, Promise.resolve()); });