我一直在尝试使用 gulp 找到一种仅根据我在管道中收集的 yaml 数据写出某些文件的方法。我已经能够看到文件的数据,但无法获得我期望的输出。
在这个任务中,我收集了大量的降价文件,并将它们传递到使用 gulp-data 读取 yaml 的管道中,然后向其中添加一些其他数据。然后我通过 Swig 管道传输它。
我试图在输入 gulp.dest 之前添加某种条件元素。我找到了这个例子,它让我到达了现在的位置。
我得到的最接近的是:
.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/'));
}
}))
我从 console.log 命令得到的结果是正确的(它显示了 50 个文件中的 2 个)。但是没有任何东西被写入目的地。如果我将 gulp.dest 移到这个管道之外,所有文件都会被写入。
我尝试使用 gulp-if 或 gulp-ignore,但无法将 file.data.date 放入其中任何一个模块中。
编辑:这是完整的任务
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/'));
});
}