javascript - 如何读取gulp.src的流

标签 javascript gulp

我正在尝试从使用 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

Returns a stream of Vinyl files that can be piped to plugins.

它没有 content 属性。并且您应该使用节点 Stream API 链接提取过程。并且 gulp 任务应该返回一个 stream.Readable

关于javascript - 如何读取gulp.src的流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31775108/

相关文章:

javascript - 防止单个表单项上的全局 jQuery 事件

javascript - 使用文档表单对象,如何引用按名称分组到数组中的表单元素?

gulp - ** 全局字符是什么?

javascript - 从 gulp 任务中排除文件不起作用

javascript - Gulp 重命名和基本路径

javascript - 为什么 ng-show 不能正常工作?

javascript - JavaScript API 是否公开用于压缩文件的内部解压器?

javascript - 使用when库理解javascript中的promise

node.js - Spriity(gulp spriting image/scss generator)运行任务时出现各种错误

javascript - 如何用 gulp 删除 templateCache?