繁体   English   中英

如何读取gulp.src的流

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM