gulp-json-editor
Advanced tools
Comparing version 1.2.0 to 2.0.0
25
index.js
/* jshint node: true */ | ||
var jsbeautify = require('js-beautify').js_beautify; | ||
var merge = require('deepmerge'); | ||
var through = require('through2'); | ||
@@ -9,11 +10,23 @@ var PluginError = require('gulp-util').PluginError; | ||
// check options | ||
if (!editor) | ||
throw new PluginError('gulp-json-editor', 'missing "editor function" option'); | ||
if (typeof editor !== 'function') | ||
throw new PluginError('gulp-json-editor', '"editor function" option must be function'); | ||
// edit JSON object by user specific function | ||
function editByFunction(json) { | ||
return JSON.stringify(editor(json)); | ||
} | ||
// edit JSON object by merging with user specific object | ||
function editByObject(json) { | ||
return JSON.stringify(merge(json, editor)); | ||
} | ||
// always beautify output | ||
var beautify = true; | ||
var editByXXX; | ||
// check options | ||
if (typeof editor === 'function') editByXXX = editByFunction; | ||
else if (typeof editor === 'object') editByXXX = editByObject; | ||
else if (typeof editor === 'undefined') throw new PluginError('gulp-json-editor', 'missing "editor function" option'); | ||
else throw new PluginError('gulp-json-editor', '"editor function" option must be function'); | ||
// create through object | ||
return through.obj(function (file, encoding, callback) { | ||
@@ -34,3 +47,3 @@ | ||
try { | ||
var json = JSON.stringify(editor(JSON.parse(file.contents.toString('utf8')))); | ||
var json = editByXXX(JSON.parse(file.contents.toString('utf8'))); | ||
if (beautify) { | ||
@@ -37,0 +50,0 @@ json = jsbeautify(json, { |
{ | ||
"name": "gulp-json-editor", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "A gulp plugin to edit JSON object", | ||
@@ -29,3 +29,4 @@ "license": "MIT", | ||
"through2": "~0.4.1", | ||
"js-beautify": "~1.4.2" | ||
"js-beautify": "~1.4.2", | ||
"deepmerge": "~0.2.7" | ||
}, | ||
@@ -32,0 +33,0 @@ "devDependencies": { |
@@ -9,8 +9,18 @@ # gulp-json-editor | ||
/* | ||
edit JSON object by merging with user specific object | ||
*/ | ||
gulp.src("./manifest.json") | ||
.pipe(jeditor({ | ||
'version': '1.2.3' | ||
})) | ||
.pipe(gulp.dest("./dest")); | ||
/* | ||
edit JSON object by using user specific function | ||
*/ | ||
gulp.src("./manifest.json") | ||
.pipe(jeditor(function(json) { | ||
// json is normal JSON object. You can modify / add / remove any properties. | ||
json.version = "1.2.3"; | ||
// must return JSON object. | ||
return json; | ||
return json; // must return JSON object. | ||
})) | ||
@@ -21,7 +31,15 @@ .pipe(gulp.dest("./dest")); | ||
### Note | ||
In case of such above situation, all of comment and whitespace in source file isn't kept in destination file. | ||
In case of such above situation, all of comment and whitespace in source file is **NOT** kept in destination file. | ||
## API | ||
### jeditor(editorFunction, [beautify]) | ||
### jeditor(editorObject) | ||
#### editorObject | ||
Type: `JSON object` | ||
JSON object to merge with. | ||
### jeditor(editorFunction) | ||
#### editorFunction | ||
Type: `function` | ||
The `editorFunction` must have the following signature: `function (json) {}`, and must return JSON object. | ||
@@ -28,0 +46,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
12084
8
309
46
4
1
+ Addeddeepmerge@~0.2.7
+ Addeddeepmerge@0.2.10(transitive)