NodeJS & Gulp Vinyl File Streams and Objects - Gulp Wrapper for NPM package generating incorrect output

goal

I'm currently trying to write a Gulp wrapper for NPM Flat that can be easily used in Gulp tasks. I believe that this will be useful for the Node community, as well as to achieve my goal. The repository is here for everyone to browse, contribute, play and pull the request. I am trying to smooth out (using dotted notation) copies of multiple JSON files. Then I want to copy them into one folder and just change the file extension to switch from * .json to * .flat.json.

My problem

The results that I get in my JSON files look like vinyl files or byte code. For example, I expect "views.login.usernamepassword.login.text": "Login" , but I get something like {"0":123,"1":13,"2":10,"3":9,"4":34,"5":100,"6":105 ... etc.

My approach

I am new to developing Gulp tasks and Node modules, so definitely keep an eye on fundamentally wrong things.

The repository will be the most relevant code, but I will also try to take into account the issue in this matter as well.

Gulp -Task File

 var gulp = require('gulp'), plugins = require('gulp-load-plugins')({camelize: true}); var gulpFlat = require('gulp-flat'); var gulpRename = require('gulp-rename'); var flatten = require('flat'); gulp.task('language:file:flatten', function () { return gulp.src(gulp.files.lang_file_src) .pipe(gulpFlat()) .pipe(gulpRename( function (path){ path.extname = '.flat.json' })) .pipe(gulp.dest("App/Languages")); }); 

Node module index.js (Aka, which I hope will become gulp -flat)

 var through = require('through2'); var gutil = require('gulp-util'); var flatten = require('flat'); var PluginError = gutil.PluginError; // consts const PLUGIN_NAME = 'gulp-flat'; // plugin level function (dealing with files) function flattenGulp() { // creating a stream through which each file will pass var stream = through.obj(function(file, enc, cb) { if (file.isBuffer()) { //FIXME: I believe this is the problem line!! var flatJSON = new Buffer(JSON.stringify( flatten(file.contents))); file.contents = flatJSON; } if (file.isStream()) { this.emit('error', new PluginError(PLUGIN_NAME, 'Streams not supported! NYI')); return cb(); } // make sure the file goes through the next gulp plugin this.push(file); // tell the stream engine that we are done with this file cb(); }); // returning the file stream return stream; } // exporting the plugin main function module.exports = flattenGulp; 

Resources

+6
source share
1 answer

You are right where the error is. The fix is โ€‹โ€‹simple. You just need to parse file.contents , since the flatten function works with the object, not with the buffer.

 ... var flatJSON = new Buffer(JSON.stringify( flatten(JSON.parse(file.contents)))); file.contents = flatJSON; ... 

This should solve your problem.

And since you're new to the Gulp plugin, I hope you don't mind if I make an offer. You might want to consider giving users the opportunity to outperform JSON output. To do this, simply execute your main function with the options object, and then you can do something like this:

 ... var flatJson = flatten(JSON.parse(file.contents)); var jsonString = JSON.stringify(flatJson, null, options.pretty ? 2 : null); file.contents = new Buffer(jsonString); ... 

You may find that the options object is useful for other things if you plan on expanding your plugin in the future.

Remember to see the repository for the plugin I wrote called gulp-transform . I am happy to answer any questions about this. (For example, I could give you some recommendations on implementing your plugin version in streaming mode if you want).

Update

I decided to invite you to your invitation to participate. You can view my plug here and the question I opened here . You can use as much as you want, and if you really like it, I can always send a pull request. Hope this gives you some ideas.

Thank you for taking this project.

+1
source

All Articles