[英]How read stream of gulp.src
我正在嘗試從gulp.src(content/*.md)
獲得的一組文件中提取數據。
這應該列出content
內的所有Markdown文件。 然后,我想遍歷文件路徑數組,處理每個文件,返回一個對象,該對象存儲提取的信息並將其保存到.json文件。
var gulp = require("gulp"),
jsonfile = require('jsonfile'),
gulp.task("myTask", function(){
// Contains JS objects that contain extracted information
// from the files; see processFile()
var extractions = [];
// Read from the stream???
gulp.src("content/*.md");
// An array of all filepaths found by gulp.src()
var files = [];
for (i = 0; i < files.length; i++) {
var information = processFile(files[i])
// Append information-object to extractions array
extractions.push(information);
}
// Save extractions as .json-file
jsonfile.writeFile("information.json", extractions, function (err) {
console.error(err);
});
});
function processFile(content) {
// Process each file's content one by one and return
//a JS-object with the extracted information.
}
在一種方法中,我嘗試使用console.log( gulp.src("content/*.md").content.toString() )
從gulp.src()
返回的流中輸出數據,但undefined
被輸出。
參見https://gulpjs.com/docs/en/api/src
gulp.src
返回可以通過管道傳輸到插件的Vinyl文件流。
它沒有content
屬性。 並且您應該使用節點Stream API鏈接提取過程。 並且gulp任務應該返回stream.Readable
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.