Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,38 @@ gulp.src('jsonFiles/**/*.json')
gulp.src('jsonFiles/**/*.json')
.pipe(merge('dataModule.js', false, false, false, 'var my.var'))
.pipe(gulp.dest('./dist'));


/*
Provide options as an object
*/
gulp.src('jsonFiles/**/*.json')
.pipe(merge({
fileName: 'dataModule.js',
edit: function(parsedJson) {
if (parsedJson.someValue) {
delete parsedJson.otherValue;
}
},
startObj: {someKey: 'defaultValue'},
endObj: {someKey: 'specialValue'},
exportModule: false,
})
.pipe(gulp.dest('./dist'));

/*
Provide replacer and space options for JSON.stringify
*/
gulp.src('jsonFiles/**/*.json')
.pipe(merge({
fileName: 'dataModule.js',
jsonSpace = ' ',
jsonReplacer = function() {/*...*/}
})
.pipe(gulp.dest('./dist'));
```


## Example Input
```JSON
/*
Expand Down
19 changes: 18 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ var through = require('through');
var PLUGIN_NAME = 'gulp-merge-json';

module.exports = function(fileName, edit, startObj, endObj, exportModule) {

var jsonReplacer, jsonSpace;
if (typeof fileName === 'object') {
// use first argument as opts
var opts = fileName;
fileName = opts.fileName;
edit = opts.edit;
startObj = opts.startObj;
endObj = opts.endObj;
exportModule = opts.exportModule;
jsonReplacer = opts.jsonReplacer || null;
jsonSpace = opts.jsonSpace || '\t';
} else {
jsonReplacer = null;
jsonSpace = '\t';
}

if ((startObj && typeof startObj !== 'object') || (endObj && typeof endObj !== 'object')) {
throw new gutil.PluginError(PLUGIN_NAME, PLUGIN_NAME + ': Invalid start and/or end object!');
}
Expand Down Expand Up @@ -59,7 +76,7 @@ module.exports = function(fileName, edit, startObj, endObj, exportModule) {
merged = merge(merged, endObj);
}

var contents = JSON.stringify(merged, null, '\t');
var contents = JSON.stringify(merged, jsonReplacer, jsonSpace);

if (exportModule === true) {
contents = 'module.exports = ' + contents + ';';
Expand Down
80 changes: 80 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,84 @@ it('should error on stream', function(done) {
stream.end();
});

it('should merge when file is passed in options object', function(done) {
var stream = gulp.src('test/json/*.json').pipe(merge({fileName: 'combined.json'}));

stream.on('data', function(file) {
var expected = ['{', '\t"name": "Josh",', '\t"pet": {', '\t\t"name": "Indy"', '\t},', '\t"place": "San Francisco",', '\t"settings": {', '\t\t"likesSleep": true', '\t}', '}'].join('\n');

file.contents.toString().should.eql(expected);

done();
});
});


it('should allow the editor function in options object', function(done) {
var stream = gulp.src('test/json/*.json').pipe(merge({
fileName: 'combined.json',
edit: function(json) {
if (json.place) {
json.place = 'New York';
}

return json;
}
}));

stream.on('data', function(file) {
var expected = ['{', '\t"name": "Josh",', '\t"pet": {', '\t\t"name": "Indy"', '\t},', '\t"place": "New York",', '\t"settings": {', '\t\t"likesSleep": true', '\t}', '}'].join('\n');

file.contents.toString().should.eql(expected);

done();
});
});

it('should use supplied start object as base when passed in options object', function(done) {
var stream = gulp.src('test/json/*.json').pipe(merge({
fileName: 'combined.json',
startObj: {'initial': 'value'}
}));

stream.on('data', function(file) {
var expected = ['{', '\t"initial": "value",', '\t"name": "Josh",', '\t"pet": {', '\t\t"name": "Indy"', '\t},', '\t"place": "San Francisco",', '\t"settings": {', '\t\t"likesSleep": true', '\t}', '}'].join('\n');

file.contents.toString().should.eql(expected);

done();
});
});

it('should use supplied final object to overwrite when passed in options object', function(done) {
var stream = gulp.src('test/json/*.json').pipe(merge({
fileName: 'combined.json',
endObj: {'place': 'Las Vegas'}
}));

stream.on('data', function(file) {
var expected = ['{', '\t"name": "Josh",', '\t"pet": {', '\t\t"name": "Indy"', '\t},', '\t"place": "Las Vegas",', '\t"settings": {', '\t\t"likesSleep": true', '\t}', '}'].join('\n');

file.contents.toString().should.eql(expected);

done();
});
});

it('should output a node module when exportModule is true in options object', function(done) {
var stream = gulp.src('test/json/*.json').pipe(merge({
fileName: 'combined.json',
exportModule: true
}));

stream.on('data', function(file) {
var expected = ['{', '\t"name": "Josh",', '\t"pet": {', '\t\t"name": "Indy"', '\t},', '\t"place": "San Francisco",', '\t"settings": {', '\t\t"likesSleep": true', '\t}', '}'].join('\n');

expected = 'module.exports = ' + expected + ';';

file.contents.toString().should.eql(expected);

done();
});
});