-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
136 lines (111 loc) · 3.4 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*!
* Copyright 2015-2024 Josh Swan
* Released under the MIT license
* https://github.com/joshswan/gulp-merge/blob/master/LICENSE
*/
const cloneDeep = require('lodash.clonedeep');
const mergeWith = require('lodash.mergewith');
const JSON5 = require('json5');
const path = require('path');
const PluginError = require('plugin-error');
const through = require('through');
const Vinyl = require('vinyl');
// Polyfill structuredClone with lodash to support node versions < 17.
if (typeof global.structuredClone !== 'function') {
global.structuredClone = cloneDeep;
}
const PLUGIN_NAME = 'gulp-merge-json';
const mergeOrConcatArrays = (concatArrays, mergeArrays) => (objValue, srcValue) => {
// Handle array merging
if (Array.isArray(objValue) && Array.isArray(srcValue)) {
if (concatArrays) {
return objValue.concat(srcValue);
}
if (!mergeArrays) {
return srcValue;
}
}
return undefined;
};
function merge(a, b, options) {
const { customizer, concatArrays, mergeArrays } = options;
if (Array.isArray(a) && concatArrays) {
return a.concat(b);
}
return mergeWith(a, b, customizer || mergeOrConcatArrays(concatArrays, mergeArrays));
}
module.exports = function mergeJson(opts) {
const options = {
// Defaults
fileName: 'combined.json',
edit: (json) => json,
transform: (json) => json,
startObj: {},
endObj: null,
exportModule: false,
concatArrays: false,
mergeArrays: true,
customizer: null,
jsonReplacer: null,
jsonReviver: null,
jsonSpace: '\t',
json5: false,
...opts,
};
const jsonLib = (options.json5) ? JSON5 : JSON;
if ((options.startObj && typeof options.startObj !== 'object') || (options.endObj && typeof options.endObj !== 'object')) {
throw new PluginError(PLUGIN_NAME, `${PLUGIN_NAME}: Invalid start and/or end object!`);
}
let merged = structuredClone(options.startObj);
let firstFile = null;
function parseAndMerge(file) {
let parsed;
if (file.isNull()) {
return this.queue(file);
}
if (file.isStream()) {
return this.emit('error', new PluginError(PLUGIN_NAME, `${PLUGIN_NAME}: Streaming not supported!`));
}
if (!firstFile) {
firstFile = file;
}
try {
parsed = jsonLib.parse(file.contents.toString('utf8'), options.jsonReviver);
} catch (err) {
err.message = `Error while parsing ${file.path}: ${err.message}`;
return this.emit('error', new PluginError(PLUGIN_NAME, err));
}
try {
merged = merge(merged, options.edit(parsed, file), options);
} catch (err) {
return this.emit('error', new PluginError(PLUGIN_NAME, err));
}
}
function endStream() {
if (!firstFile) {
return this.emit('end');
}
if (options.endObj) {
merged = merge(merged, options.endObj, options);
}
let contents = jsonLib.stringify(
options.transform(merged),
options.jsonReplacer,
options.jsonSpace,
);
if (options.exportModule === true) {
contents = `module.exports = ${contents};`;
} else if (options.exportModule) {
contents = `${options.exportModule} = ${contents};`;
}
const output = new Vinyl({
cwd: firstFile.cwd,
base: firstFile.base,
path: path.join(firstFile.base, options.fileName),
contents: Buffer.from(contents),
});
this.emit('data', output);
this.emit('end');
}
return through(parseAndMerge, endStream);
};