Skip to content

Add the ability to alter the filenames of the merged JSON files #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
71 changes: 57 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,61 @@
var through = require('through');
var path = require('path');
var gutil = require('gulp-util');
var merge = require('merge');
var PluginError = gutil.PluginError;
var File = gutil.File;

module.exports = function (fileName, converter) {
if (!fileName) {
throw new PluginError('gulp-jsoncombine', 'Missing fileName option for gulp-jsoncombine');
var config;
var defaults = {
dataConverter: dataConverter,
pathConverter: pathConverter,
fileConverter: fileConverter
};

function dataConverter(data) {
return new Buffer(JSON.stringify(data, null, '\t'));
}
if (!converter) {
throw new PluginError('gulp-jsoncombine', 'Missing converter option for gulp-jsoncombine');

function pathConverter(file) {
return file.relative.substr(0,file.relative.length-5);
}

function fileConverter(file) {
return JSON.parse(file.contents.toString());
}

var data = {};
var firstFile = null;
//We keep track of when we should skip the conversion for error cases
var skipConversion = false;

if (!fileName) {
throw new PluginError('gulp-jsoncombine', 'Missing fileName option for gulp-jsoncombine');
}

if(typeof converter === 'object') {
config = merge.recursive(defaults, converter);
} else {
config = defaults;

if(typeof converter === 'function') {
config.dataConverter = converter;
}
}

if (!config.hasOwnProperty('dataConverter') && typeof config.dataConverter !== 'function') {
throw new PluginError('gulp-jsoncombine', 'Missing data converter option for gulp-jsoncombine');
}

if (!config.hasOwnProperty('pathConverter') && typeof config.pathConverter !== 'function') {
throw new PluginError('gulp-jsoncombine', 'Missing path converter option for gulp-jsoncombine');
}

if (!config.hasOwnProperty('fileConverter') && typeof config.fileConverter !== 'function') {
throw new PluginError('gulp-jsoncombine', 'Missing file converter option for gulp-jsoncombine');
}

function bufferContents(file) {
if (!firstFile) {
firstFile = file;
Expand All @@ -26,35 +65,39 @@ module.exports = function (fileName, converter) {
if (file.isNull()) {
return; // ignore
}

if (file.isStream()) {
skipConversion = true;
skipConversion = true;
return this.emit('error', new PluginError('gulp-jsoncombine', 'Streaming not supported'));
}

try {
data[file.relative.substr(0,file.relative.length-5)] = JSON.parse(file.contents.toString());
data[config.pathConverter(file)] = config.fileConverter(file);
} catch (err) {
skipConversion = true;
return this.emit('error',
new PluginError('gulp-jsoncombine', 'Error parsing JSON: ' + err + ', file: ' + file.path.slice(file.base.length)));
new PluginError('gulp-jsoncombine', 'Error parsing JSON: ' + err + ', file: ' + file.path.slice(file.base.length)));
}
}

function endStream() {
if (firstFile && !skipConversion) {
var joinedPath = path.join(firstFile.base, fileName);

try {
var joinedFile = new File({
try {
var joinedFile = new File({
cwd: firstFile.cwd,
base: firstFile.base,
path: joinedPath,
contents: converter(data)
contents: config.dataConverter(data)
});
this.emit('data', joinedFile);
} catch (e) {
return this.emit('error', new PluginError('gulp-jsoncombine', e, { showStack: true }));
}

this.emit('data', joinedFile);
} catch (e) {
return this.emit('error', new PluginError('gulp-jsoncombine', e, { showStack: true }));
}
}

this.emit('end');
}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

{
"name": "gulp-jsoncombine",
"version": "1.0.3",
Expand All @@ -17,8 +16,9 @@
"coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
},
"dependencies": {
"through": "*",
"gulp-util": "~2.2.0"
"gulp-util": "~2.2.0",
"merge": "~1.2.0",
"through": "*"
},
"devDependencies": {
"mocha": "*",
Expand Down