-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
140 lines (111 loc) · 4.28 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
var PluginError = require('plugin-error'),
through = require('through2'),
PoFile = require('pofile'),
_merge = require('lodash/merge'),
exec = require('child_process').exec;
/**
* Builds shell command for GNU xgettext according to specified options.
*
* @param {Object} options List of options.
* @returns {String} Shell command with all needed flags.
*/
var buildCommand = function(options) {
var opt = options || {};
var command = opt.bin || 'xgettext';
command += ' --force-po -o -';
if (opt.language) {
command += ' --language="' + opt.language + '"';
}
if (opt.encoding) {
command += ' --from-code="' + opt.encoding + '"';
}
if (opt.comments === true) {
command += ' --add-comments';
} else if (typeof opt.comments == 'string') {
command += ' --add-comments="' + opt.comments + '"';
}
if (opt.keywords) {
for (var i = 0, l = opt.keywords.length; i < l; i++) {
var keyword = opt.keywords[i],
args = [];
if (!keyword.name || (typeof keyword.name !== 'string')) {
throw new PluginError('gulp-xgettext', 'Name of a keyword must be a not empty string');
}
if (keyword.singular) {
args.push(keyword.singular);
}
if (keyword.plural) {
if (!keyword.singular) {
// Using plural form without singular one has no sense.
throw new PluginError('gulp-xgettext', '"plural" cannot be set without "singular"');
}
args.push(keyword.plural);
}
if (keyword.context) {
if (!keyword.singular) {
// Using context without singular form has no sense.
throw new PluginError('gulp-xgettext', '"context" cannot be set without "singular"');
}
args.push(keyword.context + 'c');
}
command += ' --keyword="'
+ keyword.name + (args.length ? (':' + args.join(',')) : '')
+ '"';
}
}
// Use STDIN as input
command += ' -';
return command;
};
var xgettextPlugin = function(options) {
return through.obj(function(file, enc, callback) {
var stream = this;
if (file.isNull()) {
stream.push(file);
callback();
return;
}
if (file.isStream()) {
stream.emit('error', new PluginError('gulp-xgettext', 'Streams are not supported'));
callback();
return;
}
// Run xgettext
var xgettext = exec(buildCommand(options), function(error, stdout, stderr) {
if (error) {
// Something went wrong. Let Gulp know about that.
stream.emit('error', new PluginError('gulp-xgettext', error));
callback();
return;
}
var po = PoFile.parse(stdout);
// Remove file level comments
po.comments = [];
// Provide an ability to override headers.
po.headers = _merge(po.headers, (options.headers || {}));
// Use relative path instead of "standard input" string in reference
// comments.
for (var i = 0; i < po.items.length; i++) {
var matches,
updatedReferences = [],
lineRegExp = /[^:]+:(\d+)/g;
for (var j = 0; j < po.items[i].references.length; j++) {
// "pofile" module does not support more than one reference
// per line. Thus we must deal with it manually.
while (null !== (matches = lineRegExp.exec(po.items[i].references[j]))) {
updatedReferences.push(file.relative + ':' + matches[1]);
}
}
po.items[i].references = updatedReferences;
}
// Update file contents
file.contents = new Buffer(po.toString());
stream.push(file);
callback();
});
// Pass content of the file as STDIN to xgettext
xgettext.stdin.write(file.contents);
xgettext.stdin.end();
});
};
module.exports = xgettextPlugin;