我想将拼写检查结果输出到控制台而不是文件中,我认为这应该可行,因为据我所知,gulp会返回一个流。
相反,我收到错误:
TypeError: Object #<Stream> has no method 'read'
这是我的代码
gulp.task('spellcheck', function() {
var patterns = [{
// Strip tags from HTML
pattern: /(<([^>]+)>)/ig,
replacement: ''
}];
var spellSuggestions = [{
pattern: / [^ ]+? \(suggestions:[A-z, ']+\)/g,
replacement: function(match) {
return '<<<' + match + '>>>';
}
}];
var nonSuggestions = [{
pattern: /<<<.+>>>|([^\s]+[^<]+)/g,
replacement: function(match) {
if (match.indexOf('<') == 0) {
return '\n' + match + '\n';
}
return '';
}
}];
var toConsole = gulp.src('./_site/**/*.html')
.pipe(frep(patterns))
.pipe(spellcheck())
.pipe(frep((spellSuggestions)))
.pipe(frep((nonSuggestions)));
var b = toConsole.read();
console.log(b);
});
答案 0 :(得分:31)
流上没有读取方法。你有两个选择:
在代码中实施:
gulp.task('spellcheck', function () {
var patterns = [
{
// Strip tags from HTML
pattern: /(<([^>]+)>)/ig,
replacement: ''
}];
var nonSuggestions = [
{
pattern: /<<<.+>>>|([^\s]+[^<]+)/g,
replacement: function(match) {
if (match.indexOf('<')==0) {
return '\n' + match +'\n';
}
return '';
}
}];
var a = gulp.src('./_site/**/*.html')
.pipe(frep(patterns))
.pipe(spellcheck(({replacement: '<<<%s (suggestions: %s)>>>'})))
.pipe(frep(nonSuggestions))
;
a.on('data', function(chunk) {
var contents = chunk.contents.toString().trim();
var bufLength = process.stdout.columns;
var hr = '\n\n' + Array(bufLength).join("_") + '\n\n'
if (contents.length > 1) {
process.stdout.write(chunk.path + '\n' + contents + '\n');
process.stdout.write(chunk.path + hr);
}
});
});