-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (99 loc) · 2.95 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
var through = require('through');
var path = require('path');
var gutil = require('gulp-util');
var _ = require('lodash');
var PluginError = gutil.PluginError;
var File = gutil.File;
module.exports = function (fileName, options) {
if (!fileName) {
throw new PluginError('gulp-combine-languagefiles', 'Missing fileName option for gulp-combine-languagefiles');
}
if(typeof options !== 'object') {
options = {};
}
var data = [];
var result = [];
var LANGS = [];
var PATH, LANG = '';
var firstFile = null;
function bufferContents(file) {
if (!firstFile) {
firstFile = file;
}
if (file.isNull()) {
return; // ignore
}
if (file.isStream()) {
return this.emit('error', new PluginError('gulp-combine-languagefiles', 'Streaming not supported'));
}
var jsonData = JSON.parse(file.contents.toString());
/* Extract file name and language */
/* Expects files to be named en.json or en-EN.json or similar */
var fileName = file.relative.split('/').slice(-1)[0];
var lang = fileName.split('.').slice(0)[0];
/* Parse & include original path so it can be restored later */
var path = file.relative.toString().replace(fileName, '');
var json = {
path: path,
lang: lang,
value: jsonData
}
/* Gather all languages in the project */
if(!_.contains(LANGS, lang)) {
LANGS.push(lang);
}
data.push(json);
}
function endStream() {
var joinedPath = path.join(firstFile.base, fileName);
/* Loop through all found translations and combine result in desired format */
_.each(data, function(val) {
LANG = val.lang;
PATH = val.path;
iterateValues(val.value);
});
result = _.sortBy(result, 'path');
if(options.includeHeader) {
/* Include header as first item, for example if we are generating a CSV */
var header = [
'Path', 'Key'
].concat(LANGS);
result.unshift(header);
}
var joinedFile = new File({
cwd: firstFile.cwd,
base: firstFile.base,
path: joinedPath,
contents: new Buffer(JSON.stringify(result))
});
this.emit('data', joinedFile);
this.emit('end');
}
function iterateValues(val, key) {
_.each(val, function(v, k) {
/* Nested keys are joined using | . Resulting object is one dimentional */
if(key) k = key+'|'+k;
if(typeof v === 'object') {
iterateValues(v, k);
} else {
var obj = {
path: PATH,
key: k
};
var index = _.findIndex(result, obj);
if(index !== -1) {
/* Update translation for existing key */
result[index][LANG] = v;
} else {
/* No existing key found, insert it and initialize all languages as empty */
_.each(LANGS, function(l) {
obj[l] = '';
});
obj[LANG] = v;
result.push(obj);
}
}
});
}
return through(bufferContents, endStream);
};