1

I'm trying to extract data from a set of files that I get with gulp.src(content/*.md).

This should list all Markdown-files inside content. Then I wanted to iterate over an array of file-paths, process each file, return an object that stores the extracted information and save this to a .json-file.

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.
    }

In one approach I tried to use console.log( gulp.src("content/*.md").content.toString() ) to output the data from the stream that is returned by gulp.src(), but undefined will be outputted.

user3147268
  • 1,814
  • 7
  • 26
  • 39
  • 1
    Would you like to post a working implementation as an answer? The API documentation mentioned by @gzc does not mean much to me; I am stuck with the stream stuff... – Matze Aug 26 '16 at 13:55
  • For one approach to this issue see https://stackoverflow.com/questions/46605923/gulp-merge-json-files-from-different-folders-while-keeping-folder-structure/46617523#46617523. Also good using gulp-foreach see https://stackoverflow.com/questions/39007624/get-current-file-name-in-gulp-stream – Mark Oct 11 '17 at 02:57

1 Answers1

0

see https://gulpjs.com/docs/en/api/src

gulp.src

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

It doesn't have content attribute. And you should chain extraction process with node Stream API. And gulp task should return a stream.Readable.

Igor Zinov'yev
  • 3,676
  • 1
  • 33
  • 49
gzc
  • 8,180
  • 8
  • 42
  • 62