gulp-json-editor
Advanced tools
Comparing version 2.5.7 to 2.6.0
@@ -0,1 +1,12 @@ | ||
## 2.6.0 | ||
### Added | ||
- Support editor to return PromiseLike object (#48) | ||
### Changed | ||
- Test on Node.js 18 and upwards | ||
- Update dependencies | ||
## 2.5.7 | ||
@@ -2,0 +13,0 @@ |
44
index.js
@@ -39,2 +39,3 @@ var jsbeautify = require('js-beautify').js_beautify; | ||
return through.obj(function(file, encoding, callback) { | ||
var self = this; | ||
@@ -54,2 +55,8 @@ // ignore it | ||
// when edit fail | ||
var onError = function(err) { | ||
self.emit('error', new PluginError('gulp-json-editor', err)); | ||
callback(); | ||
}; | ||
try { | ||
@@ -65,19 +72,32 @@ // try to get current indentation | ||
// when edit success | ||
var onSuccess = function(json) { | ||
json = JSON.stringify(json); | ||
// beautify JSON | ||
if (beautifyOptions.beautify) { | ||
json = jsbeautify(json, beautifyOptions); | ||
} | ||
// write it to file | ||
file.contents = Buffer.from(json); | ||
self.push(file); | ||
callback(); | ||
}; | ||
// edit JSON object and get it as string notation | ||
var json = JSON.stringify(editBy(JSON.parse(file.contents.toString('utf8')))); | ||
// beautify JSON | ||
if (beautifyOptions.beautify) { | ||
json = jsbeautify(json, beautifyOptions); | ||
var res = editBy(JSON.parse(file.contents.toString('utf8'))); | ||
if (isPromiseLike(res)) { | ||
res.then(onSuccess, onError); | ||
} else { | ||
onSuccess(res); | ||
} | ||
// write it to file | ||
file.contents = Buffer.from(json); | ||
} catch (err) { | ||
this.emit('error', new PluginError('gulp-json-editor', err)); | ||
onError(err); | ||
} | ||
this.push(file); | ||
callback(); | ||
}); | ||
}; | ||
function isPromiseLike(maybePromise) { | ||
return typeof maybePromise === 'object' && maybePromise !== null && typeof maybePromise.then === 'function'; | ||
} |
{ | ||
"name": "gulp-json-editor", | ||
"version": "2.5.7", | ||
"version": "2.6.0", | ||
"description": "A gulp plugin to edit JSON objects", | ||
@@ -17,3 +17,3 @@ "license": "MIT", | ||
"detect-indent": "^6.1.0", | ||
"js-beautify": "^1.14.8", | ||
"js-beautify": "^1.14.11", | ||
"plugin-error": "^2.0.1", | ||
@@ -23,3 +23,3 @@ "through2": "^4.0.2" | ||
"devDependencies": { | ||
"eslint": "^8.45.0", | ||
"eslint": "^8.56.0", | ||
"eslint-config-gulp": "^5.0.1", | ||
@@ -26,0 +26,0 @@ "gulp": "^4.0.2", |
@@ -99,3 +99,3 @@ # gulp-json-editor | ||
The `editorFunction` must have the following signature: `function (json) {}`, and must return JSON object. | ||
The `editorFunction` must have the following signature: `function (json) {}`, and must return JSON object or PromiseLike object with JSON object as value. | ||
@@ -102,0 +102,0 @@ #### jsBeautifyOptions |
@@ -175,1 +175,27 @@ var json = require('../'); | ||
}); | ||
it('should modify property asynchronous', function(done) { | ||
var stream = gulp | ||
.src('test/test.json') | ||
.pipe(json(function(obj) { | ||
obj.version = '2.0.0'; | ||
return Promise.resolve(obj); | ||
})); | ||
stream.on('data', function(file) { | ||
var expected = | ||
'{\n' + | ||
' "name": "test object",\n' + | ||
' "version": "2.0.0",\n' + | ||
' "nested": {\n' + | ||
' "name": "nested object",\n' + | ||
' "version": "1.0.0"\n' + | ||
' },\n' + | ||
' "authors": ["tom"]\n' + | ||
'}'; | ||
file.contents.toString().should.eql(expected); | ||
done(); | ||
}); | ||
}); |
@@ -5,2 +5,3 @@ var json = require('../'); | ||
var should = require('should'); | ||
var gulp = require('gulp'); | ||
@@ -40,1 +41,17 @@ it('should raise error when missing option', function(done) { | ||
}); | ||
it('should raise error when Promise.reject', function(done) { | ||
var msg = 'throw error in async editor'; | ||
gulp | ||
.src('test/test.json') | ||
.pipe( | ||
json(function () { | ||
return Promise.reject(new Error(msg)); | ||
}) | ||
) | ||
.on('error', function (err) { | ||
err.message.should.equal(msg); | ||
done(); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
23919
551
Updatedjs-beautify@^1.14.11