[英]Gulp control output based on data from an earlier pipe
I have been trying to find a way with gulp to only write out certain files based on yaml data I am collecting in the pipe. 我一直在尝试使用gulp方法,仅根据我在管道中收集的yaml数据写出某些文件。 I have been able to see the file's data, but not able to get the output I expect. 我已经能够看到文件的数据,但是无法获得我期望的输出。
In this task, I am collecting a glob of markdown files, and passing them into a pipe that reads the yaml using gulp-data, and then adds some other data to it. 在此任务中,我将收集一组markdown文件,并将它们传递到使用gulp-data读取yaml的管道中,然后向其中添加其他数据。 I then pipe it through Swig. 然后,我将其通过Swig传递。
I'm trying to add some sort of conditional element before I pipe into gulp.dest. 我试图在插入gulp.dest之前添加某种条件元素。 I found this example which got me to where I am currently. 我发现了这个例子 ,使我进入了目前的位置。
The closest I've gotten is below: 我最近得到的是:
.pipe(tap(function(file) {
if (new Date(file.data.date) >= new Date(buildRange)) {
console.log(file.path);
gulp.src(file.path)
.pipe(gulp.dest(config.paths.dest + '/underreview/'));
}
}))
What I've gotten from the console.log command is correct (it shows 2 of 50 files). 我从console.log命令获得的信息是正确的(显示50个文件中的2个)。 But nothing gets written to the destination. 但是什么都没有写到目的地。 If I move the gulp.dest outside of this pipe, all files get written. 如果我将gulp.dest移动到此管道之外,则会写入所有文件。
I've tried using gulp-if or gulp-ignore, but have been unable to get the file.data.date into either of those modules. 我尝试使用gulp-if或gulp-ignore,但无法将file.data.date放入这些模块中的任何一个中。
Edited: Here's the complete task 编辑:这是完整的任务
module.exports = function(gulp, config, env) {
var gulpSwig = require('gulp-swig'),
swig = require('swig'),
data = require('gulp-data'),
matter = require('gray-matter'),
runSequence = require('run-sequence'),
// BrowserSync
reload = config.browserSync.reload,
_ = require('lodash'),
Path = require('path'),
requireDir = require('require-dir'),
marked = require('marked'),
readingTime = require('reading-time'),
postsData = [],
postsTags = [],
pdate = null,
buildRange = new Date(new Date().setDate(new Date().getDate()-14));
sitebuilddate = null,
through = require('through2'),
gutil = require('gulp-util'),
rename = require('gulp-rename'),
File = require('vinyl'),
$if = require('gulp-if'),
ignore = require('gulp-ignore'),
tap = require('gulp-tap');
var opts = {
defaults: {
cache: false
},
setup: function(Swig) {
Swig.setDefaults({
loader: Swig.loaders.fs(config.paths.source + '/templates')});
}
};
// Full of the compiled HTML file
function targetPathFull(path, data) {
return Path.join(Path.dirname(path), targetPath(data));
}
gulp.task('templates2:under', function() {
return gulp.src(config.paths.source + '/content/**/*.md')
.pipe(data(function(file) {
postData = [];
var matterObject = matter(String(file.contents)), // extract front matter data
type = matterObject.data.type, // page type
body = matterObject.content,
postData = matterObject.data,
moreData = requireDir(config.paths.data),
data = {},
bodySwig;
bodySwig = swig.compile(body, opts);
// Use swig to render partials first
body = bodySwig(data);
// Process markdown
if (Path.extname(file.path) === '.md') {
body = marked(body);
}
// Inherit the correct template based on type
if (type) {
var compiled = _.template(
"{% extends 'pages/${type}.html' %}{% block body %}${body}{% endblock %}"
// Always use longform until a different template for different types is needed
//"{% extends 'pages/longform.html' %}{% block body %}${body}{% endblock %}"
);
body = compiled({
"type": type,
"body": body
});
}
file.path = targetPathFull(file.path, postData);
moreData.path = targetPath(postData);
_.merge(data, postData, moreData);
data.url = data.site.domain + "/" + data.slug;
// Copy the processed body text back into the file object so Gulp can keep piping
file.contents = new Buffer(body);
return data;
}))
.pipe(gulpSwig(opts))
.pipe(tap(function(file) {
if (new Date(file.data.date) >= new Date(buildRange)) {
console.log(file.path);
gulp.src(file.path)
.pipe(gulp.dest(config.paths.dest + '/underreview/'));
}
}))
.pipe(gulp.dest(config.paths.dest + '/underreview/'));
});
}
So, possibly not the best solution, but after some re-factoring I came up with this: 因此,可能不是最好的解决方案,但是经过一些重构后,我想到了:
.pipe(gulp.dest(config.paths.dest + '/underreview/'))
.pipe(tap(function(file) {
if (new Date(file.data.date) < new Date(buildRange)) {
console.log(file.data.path);
del(config.paths.dest + '/underreview/' + file.data.path)
}
}))
I moved the gulp-tap to after the output, and then I am deleting the file that was just written. 我将gulp-tap移至输出之后,然后删除刚刚写入的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.