-
Notifications
You must be signed in to change notification settings - Fork 134
/
gulp-i18n.js
47 lines (39 loc) · 1.21 KB
/
gulp-i18n.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
var through = require('through2')
, path = require('path')
, gutil = require('gulp-util')
, File = gutil.File
;
module.exports = function (file){
var locales = {};
var latestFile;
var latestMod;
var joinedFile;
function endStream(cb){
joinedFile = latestFile.clone({contents: false});
joinedFile.path = path.join(latestFile.base, file);
joinedFile.contents = new Buffer(JSON.stringify(locales, null, 2));
this.push(joinedFile);
cb();
}
function localesToJSON(file, encoding, callback){
if ( file.isNull() ) {
this.push(file); // Do nothing if no contents
return callback();
}
if ( file.isStream() ) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Stream not supported!'));
return callback();
}
if ( file.isBuffer() ) {
locales[file.path.split("/").slice(-2, -1)[0]] = JSON.parse(file.contents.toString());
}
// set latest file if not already set,
// or if the current file was modified more recently.
if ( !latestMod || file.stat && file.stat.mtime > latestMod ) {
latestFile = file;
latestMod = file.stat && file.stat.mtime;
}
return callback();
}
return through.obj(localesToJSON, endStream);
};