gulp-data icon indicating copy to clipboard operation
gulp-data copied to clipboard

How can i append the data to an existing JSON file?

Open talvasconcelos opened this issue 8 years ago • 1 comments

Hi, how can i append the data i get from front-matter to an existing json file? Here's what i have:

`gulp.task('meta', function(){ return gulp.src(path.posts) .pipe(plumber({ handleError: onError })) .pipe(frontMatter({ property: 'meta'})) //.pipe(data(function(file){ // file.meta.path = file.path // })) .pipe(pluck('meta', 'meta.json')) .pipe(data(function(file){ file.contents = new Buffer(JSON.stringify({'articles': file.meta}, null, ' ')); //var obj = JSON.parse(require './config.json'); //obj.articles = file.contents;

}))
.pipe(gulp.dest('dev/articles/'))

});`

At the moment it outputs to a meta.json file, i'd like for it to append the data to an existing config.json under an "artilcles" key.

Thanks, Tiago

talvasconcelos avatar Jul 18 '16 15:07 talvasconcelos

This is from my project and works well.

  1. Append static json
  2. Append currFile
  3. Append GrayMatter Data (works same as FrontMatter)
      .pipe(plugins.data(function (file) {
        let data = JSON.parse(fs.readFileSync(cacheFile));
        data.currFile = path.basename(file.path);
        return data;
      }))
      .pipe(plugins.grayMatter())

ghost avatar Feb 08 '17 15:02 ghost