-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
45 lines (37 loc) · 1.22 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
'use strict';
var through = require('through2');
var objectPath = require('object-path');
var File = require('vinyl');
var deepAssign = require('deep-assign');
var path = require('path');
module.exports = function jsonBundler(opts) {
opts = opts || {};
var master = opts.master || '';
var omit = opts.omit || '';
var contents = {};
return through.obj(gatherJson, bundleJson);
function gatherJson(chunc, enc, cb) {
var localePath = path.relative(chunc.base, path.dirname(chunc.path)).replace(new RegExp(omit, 'g'), '');
// remove first and last slash
localePath = localePath.replace(/^\/|\/$/g, '');
var fileName = path.basename(chunc.path);
var content = {};
objectPath.set(content, localePath.replace(/\//g, '.'), JSON.parse(chunc.contents));
contents[fileName] = contents[fileName] || {};
deepAssign(contents[fileName], content);
cb();
}
function bundleJson(cb) {
Object
.keys(contents)
.map(fileName => {
var values = deepAssign({}, contents[master] || {}, contents[fileName]);
return new File({
path: fileName,
contents: new Buffer(JSON.stringify(values))
});
})
.forEach(file => this.push(file));
cb();
}
};