Skip to content

Commit f4244f7

Browse files
authored
Support editor to return PromiseLike object (#48)
1 parent 20de079 commit f4244f7

File tree

4 files changed

+76
-13
lines changed

4 files changed

+76
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ This object is passed to deepmerge as its [option](https://github.com/TehShrike/
9696
#### editorFunction
9797
Type: `function`
9898

99-
The `editorFunction` must have the following signature: `function (json) {}`, and must return JSON object.
99+
The `editorFunction` must have the following signature: `function (json) {}`, and must return JSON object or PromiseLike object with JSON object as value.
100100

101101
#### jsBeautifyOptions
102102
Type: `object`

index.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module.exports = function(editor, jsbeautifyOptions, deepmergeOptions) {
3636
* create through object and return it
3737
*/
3838
return through.obj(function(file, encoding, callback) {
39+
var self = this;
3940

4041
// ignore it
4142
if (file.isNull()) {
@@ -50,6 +51,12 @@ module.exports = function(editor, jsbeautifyOptions, deepmergeOptions) {
5051
return callback();
5152
}
5253

54+
// when edit fail
55+
var onError = function(err) {
56+
self.emit('error', new PluginError('gulp-json-editor', err));
57+
callback();
58+
};
59+
5360
try {
5461
// try to get current indentation
5562
var indent = detectIndent(file.contents.toString('utf8'));
@@ -60,21 +67,34 @@ module.exports = function(editor, jsbeautifyOptions, deepmergeOptions) {
6067
beautifyOptions.indent_char = beautifyOptions.indent_char || (indent.type === 'tab' ? '\t' : ' ');
6168
beautifyOptions.beautify = !('beautify' in beautifyOptions && !beautifyOptions.beautify);
6269

70+
// when edit success
71+
var onSuccess = function(json) {
72+
json = JSON.stringify(json);
73+
74+
// beautify JSON
75+
if (beautifyOptions.beautify) {
76+
json = jsbeautify(json, beautifyOptions);
77+
}
78+
79+
// write it to file
80+
file.contents = Buffer.from(json);
81+
self.push(file);
82+
callback();
83+
};
84+
6385
// edit JSON object and get it as string notation
64-
var json = JSON.stringify(editBy(JSON.parse(file.contents.toString('utf8'))));
65-
66-
// beautify JSON
67-
if (beautifyOptions.beautify) {
68-
json = jsbeautify(json, beautifyOptions);
86+
var res = editBy(JSON.parse(file.contents.toString('utf8')));
87+
if (isPromiseLike(res)) {
88+
res.then(onSuccess, onError);
89+
} else {
90+
onSuccess(res);
6991
}
70-
71-
// write it to file
72-
file.contents = Buffer.from(json);
7392
} catch (err) {
74-
this.emit('error', new PluginError('gulp-json-editor', err));
93+
onError(err);
7594
}
76-
77-
this.push(file);
78-
callback();
7995
});
8096
};
97+
98+
function isPromiseLike(maybePromise) {
99+
return typeof maybePromise === 'object' && maybePromise !== null && typeof maybePromise.then === 'function';
100+
}

test/byFunction.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,29 @@ it('should multiple properties of JSON object (by function editor)', function(do
172172
done();
173173
});
174174
});
175+
176+
177+
it('should modify property asynchronous', function(done) {
178+
179+
var stream = gulp
180+
.src('test/test.json')
181+
.pipe(json(function(obj) {
182+
obj.version = '2.0.0';
183+
return Promise.resolve(obj);
184+
}));
185+
186+
stream.on('data', function(file) {
187+
var expected =
188+
'{\n' +
189+
' "name": "test object",\n' +
190+
' "version": "2.0.0",\n' +
191+
' "nested": {\n' +
192+
' "name": "nested object",\n' +
193+
' "version": "1.0.0"\n' +
194+
' },\n' +
195+
' "authors": ["tom"]\n' +
196+
'}';
197+
file.contents.toString().should.eql(expected);
198+
done();
199+
});
200+
});

test/error.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var json = require('../');
22
var fs = require('fs');
33
var File = require('vinyl');
44
var should = require('should');
5+
var gulp = require('gulp');
56

67
it('should raise error when missing option', function(done) {
78
should(function() {json();}).throw('missing "editor" option');
@@ -36,3 +37,19 @@ it('should raise error when streaming input', function(done) {
3637
contents: fs.createReadStream('test/test.json'),
3738
}));
3839
});
40+
41+
42+
it('should raise error when Promise.reject', function(done) {
43+
var msg = 'throw error in async editor';
44+
gulp
45+
.src('test/test.json')
46+
.pipe(
47+
json(function () {
48+
return Promise.reject(new Error(msg));
49+
})
50+
)
51+
.on('error', function (err) {
52+
err.message.should.equal(msg);
53+
done();
54+
});
55+
});

0 commit comments

Comments
 (0)