-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
195 lines (170 loc) · 6.05 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
'use strict';
var through = require('through2');
var csvParse = require('csv-parse');
var gutil = require('gulp-util');
var File = require('vinyl');
module.exports = function (options) {
options = options || {};
// stringifies JSON and makes it pretty if asked
function stringify(jsonObj) {
if (options.pretty) {
return JSON.stringify(jsonObj, null, 4);
} else {
return JSON.stringify(jsonObj);
}
}
function filePath(jsonObj, lang, key) {
var savePath;
var writeObj = {};
// give default path if resPath not provided
if (options.resPath) {
savePath = options.resPath;
} else {
savePath = 'locales/__lng__/__ns__.json';
}
if (key) {
savePath = savePath.replace('__ns__', key);
writeObj[key] = jsonObj;
} else {
savePath = savePath.replace('__ns__', 'translation');
writeObj = jsonObj;
}
savePath = savePath.replace('__lng__', lang);
return new File({
cwd: '.',
path: savePath, // put each translation file in a folder
contents: new Buffer(stringify(writeObj)),
});
}
// split language files further into subsections (ie. help) if unused much
function splitFile(jsonObj, lang) {
var key;
var files = [];
if (options.split) {
// when true
if (options.split === true) {
Object.keys(jsonObj).forEach(function (key) {
files.push(filePath(jsonObj[key], lang, key));
delete jsonObj[key];
});
} else if (typeof options.split === 'string') {
Object.keys(jsonObj).forEach(function (key) {
if (key === options.split) {
files.push(filePath(jsonObj[key], lang, key));
delete jsonObj[key];
}
});
} else if (options.split instanceof Array) {
Object.keys(jsonObj).forEach(function (key) {
if (options.split.indexOf(key) !== -1) {
files.push(filePath(jsonObj[key], lang, key));
delete jsonObj[key];
}
});
}
}
if (options.split !== true) {
files.push(filePath(jsonObj, lang, key));
}
return files;
}
// parse array to JSON object and write to JSON file
function parseArray(csvArray, task) {
var csvErr = 'CSV poor format. Do not assign string value to key' +
' if there will be more subkeys nested within that key.';
var lang;
var jsonObj;
var i;
var j;
var key;
var subkeyArray;
var value;
var k;
var node;
var jsfile;
var x;
for (i = 2; i < csvArray[0].length; i++) {
lang = csvArray[0][i]; // get language from the CSV header row
jsonObj = {}; // JSON object to be created
for (j = 0; j < csvArray.length; j++) {
// append to JSON string
key = csvArray[j][1];
subkeyArray = key.split('.');
value = csvArray[j][i];
k = 0;
node = jsonObj;
while (node && (k < subkeyArray.length - 1)) {
if (!node[subkeyArray[k]]) {
node[subkeyArray[k]] = {};
} else {
if (typeof node[subkeyArray[k]] !== 'object') {
task.emit('error', new gutil.PluginError('gulp-i18n-csv',csvErr));
return;
}
}
node = node[subkeyArray[k]];
k++;
}
if (node) {
if (!node[subkeyArray[k]]) {
node[subkeyArray[k]] = value;
} else {
if (typeof node[subkeyArray[k]] === 'object') {
task.emit('error', new gutil.PluginError('gulp-i18n-csv',csvErr));
return;
}
}
}
}
jsfile = splitFile(jsonObj, lang);
// do not write files from the gulp plugin itself
// create a file object and push it back to through stream
// so main gulpfile
for (x = 0; x < jsfile.length; x++) {
task.push(jsfile[x]);
}
}
}
return through.obj(function (file, enc, cb) {
var self = this; // task is a reference to the through stream
var csvArray = [];
var parser;
if (file.isNull()) {
cb(null, file);
return;
}
// STREAM BLOCK
if (file.isStream()) {
parser = csvParse();
parser.on('readable', function () {
var record = parser.read();
while (record) {
csvArray.push(record);
record = parser.read();
}
});
parser.on('error', function (err) {
//console.log('on error', err.message);
this.emit('error', new gutil.PluginError('gulp-i18n-csv', err));
});
parser.on('finish', function () {
//console.log('on finish');
parseArray(csvArray, self);
parser.end();
cb();
});
file.contents.pipe(parser);
} else {
// BUFFER BLOCK
try {
csvParse(file.contents.toString('utf-8'),
function (err, output) {
parseArray(output, self);
cb();
});
} catch (err) {
this.emit('error', new gutil.PluginError('gulp-i18n-csv', err));
}
}
});
};