forked from PaGury/gulp-resx2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (62 loc) · 2.1 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
var xmldoc = require("xmldoc");
var through2 = require('through2');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var Buffer = require('buffer').Buffer;
var path = require('path');
const PLUGIN_NAME = "gulp-resx2jreact-localize-redux";
module.exports = function (opt, args) {
opt = opt || {};
// Convert XML to JSON
var doConvert = function (file, args) {
var xml = file.contents;
var doc = new xmldoc.XmlDocument(xml);
var resourceObject = {};
var valueNodes = doc.childrenNamed("data");
if (!args || args && Object.keys(args).length == 0) {
valueNodes.forEach(function (element) {
var name = element.attr.name;
var value = element.firstChild.val;
resourceObject[name] = value;
});
}
else {
valueNodes.forEach(function (element) {
var resourceCategory = '';
var resourceName = '';
var resourceValue = '';
var resourceKey = element.attr.name;
if (args.resourcePrefix && resourceKey.startsWith(args.resourcePrefix)) {
resourceKey = resourceKey.replace(args.resourcePrefix, '');
}
if (args.resourceSeparator) {
var arrayChunks = resourceKey.split(args.resourceSeparator)
if (arrayChunks.length == 2) {
if (!arrayChunks[0])
return;
resourceCategory = arrayChunks[0];
resourceName = arrayChunks[1];
resourceValue = element.firstChild.val;
}
if (resourceValue) {
resourceObject[resourceCategory] = resourceObject[resourceCategory] || {};
resourceObject[resourceCategory][resourceName] = resourceValue;
}
}
});
}
return JSON.stringify(resourceObject);
};
var throughCallback = function (file, enc, cb) {
if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streaming not supported'));
return cb();
}
if (file.isBuffer()) {
file.contents = new Buffer(doConvert(file, args));
}
this.push(file);
return cb();
};
return through2.obj(throughCallback);
};