forked from reflog/gulp-jsoncombine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
105 lines (85 loc) · 2.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"use strict";
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) {
var config;
var defaults = {
dataConverter: dataConverter,
pathConverter: pathConverter,
fileConverter: fileConverter
};
function dataConverter(data) {
return new Buffer(JSON.stringify(data, null, '\t'));
}
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;
}
if (file.isNull()) {
return; // ignore
}
if (file.isStream()) {
skipConversion = true;
return this.emit('error', new PluginError('gulp-jsoncombine', 'Streaming not supported'));
}
try {
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)));
}
}
function endStream() {
if (firstFile && !skipConversion) {
var joinedPath = path.join(firstFile.base, fileName);
try {
var joinedFile = new File({
cwd: firstFile.cwd,
base: firstFile.base,
path: joinedPath,
contents: config.dataConverter(data)
});
this.emit('data', joinedFile);
} catch (e) {
return this.emit('error', new PluginError('gulp-jsoncombine', e, { showStack: true }));
}
}
this.emit('end');
}
return through(bufferContents, endStream);
};