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.
source share