This repository has been archived by the owner on Aug 25, 2020. It is now read-only.
forked from bmds/gulp-jsoncombine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
121 lines (98 loc) · 3.31 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const PLUGIN_NAME = 'gulp-tl-combine',
through = require('through'),
path = require('path'),
gutil = require('gulp-util'),
_ = require('lodash'),
PluginError = _.curry(gutil.PluginError)(PLUGIN_NAME),
File = gutil.File;
function fileParser(file) {
const fileExtname = path.extname(file.relative);
if (fileExtname === '.sql') {
let r = {};
r[`$${path.basename(file.relative, path.extname(file.relative))}`] = file.contents.toString();
return r;
} else {
// Assume this is a JSON file
return JSON.parse(file.contents.toString());
}
}
function dataConverter(data) {
const output = {
entities: [],
patches: [],
views: [],
queries: []
};
Object.keys(data).forEach((key)=> {
const entry = {
file: key,
content: data[key]
};
if (key.indexOf(`views${path.sep}`) > -1) {
output.views.push(entry);
} else if (key.indexOf(`entity-patches${path.sep}`) > -1) {
output.patches.push(entry);
} else if (key.indexOf(`entities${path.sep}`) > -1) {
output.entities.push(entry);
} else if (key.indexOf(`queries${path.sep}`) > -1) {
output.queries.push(entry);
} else {
gutil.log(`${key} is not a recognized data type. Skipping.`);
}
});
return new Buffer(JSON.stringify(output, null, 4));
}
module.exports = (fileName, options)=> {
const defaults = {
dataConverter,
pathConverter: (file)=> {
return path.dirname(file.relative) + path.sep + path.basename(file.relative, path.extname(file.relative));
},
fileParser
};
var config,
data = {},
firstFile = null,
skipConversion = false; // We keep track of when we should skip the conversion for error cases
if (!fileName) {
throw new PluginError('Missing fileName option.');
}
config = _.defaults({}, options, defaults);
function bufferContents(file) {
if (!firstFile) {
firstFile = file;
}
if (file.isNull()) {
return; // ignore
}
if (file.isStream()) {
skipConversion = true;
return this.emit('error', new PluginError('Streaming not supported', { showStack: false }));
}
try {
data[config.pathConverter(file)] = config.fileParser(file);
} catch (err) {
skipConversion = true;
return this.emit('error',
new PluginError(`Error parsing JSON: ${err}, file: ${file.path.slice(file.base.length)}`, { showStack: false }));
}
}
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(e, { showStack: true }));
}
}
this.emit('end');
}
return through(bufferContents, endStream);
};