forked from adrium/easypass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulp-utils.js
263 lines (229 loc) · 6.11 KB
/
gulp-utils.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* This Source Code is subject to the terms of the Mozilla Public License
* version 2.0 (the "License"). You can obtain a copy of the License at
* http://mozilla.org/MPL/2.0/.
*/
"use strict";
let fs = require("fs");
let path = require("path");
let {spawn} = require("child_process");
let {Duplex, Transform} = require("stream");
exports.readArg = function(prefix, defaultValue)
{
for (let arg of process.argv)
if (arg.startsWith(prefix))
return arg.substr(prefix.length);
return defaultValue;
};
function transform(modifier, opts)
{
if (!opts)
opts = {};
let stream = new Transform({objectMode: true});
stream._transform = function(file, encoding, callback)
{
if (!file.isBuffer())
throw new Error("Unexpected file type");
if (opts.files && opts.files.indexOf(file.path) < 0)
{
callback(null, file);
return;
}
Promise.resolve().then(() =>
{
let contents = opts.raw ? file.contents : file.contents.toString("utf-8");
return modifier(file.path, contents);
}).then(([filepath, contents]) =>
{
file.path = filepath;
file.contents = Buffer.from(contents, "utf-8");
callback(null, file);
}).catch(e =>
{
console.error(e);
callback(e);
});
};
return stream;
}
exports.transform = transform;
exports.jsonModify = function(modifier, newName)
{
return transform((filepath, contents) =>
{
let data = JSON.parse(contents);
data = modifier(data) || data;
if (newName)
filepath = path.resolve(filepath, "..", newName);
return [filepath, JSON.stringify(data, null, 2)];
});
};
exports.combineLocales = function()
{
let rootDir = path.join(process.cwd(), "locale");
let locales = {};
let files = {};
let stream = new Duplex({objectMode: true});
stream._write = (file, encoding, callback) =>
{
if (!file.isBuffer())
throw new Error("Unexpected file type");
let parts = path.relative(rootDir, file.path).split(path.sep);
let locale = parts.shift();
if (!locales.hasOwnProperty(locale))
{
locales[locale] = {};
files[locale] = file;
}
let fileName = parts.pop();
if (!fileName.startsWith("_"))
parts.push(path.basename(fileName, ".json"));
let prefix = "";
if (parts.length)
prefix = parts.join("@") + "@";
let data = JSON.parse(file.contents.toString("utf-8"));
for (let name of Object.keys(data))
locales[locale][prefix + name] = data[name];
callback(null);
};
stream._read = (...params) =>
{
};
stream.on("finish", () =>
{
for (let locale of Object.keys(locales))
{
let file = files[locale];
file.contents = Buffer.from(JSON.stringify(locales[locale], null, 2), "utf-8");
file.path = path.join(process.cwd(), "locale", locale + ".json");
stream.push(file);
}
stream.push(null);
});
return stream;
};
exports.toChromeLocale = function()
{
return transform((filepath, contents) =>
{
let strings = JSON.parse(contents);
let data = {};
for (let key of Object.keys(strings))
data[key] = {message: strings[key]};
let locale = path.basename(filepath, ".json");
let manifest = require("./package.json");
data.name = {"message": manifest.title};
data.description = {"message": manifest.description};
if ("locales" in manifest && locale in manifest.locales)
{
let localized = manifest.locales[locale];
if ("title" in localized)
data.name.message = localized.title;
if ("description" in localized)
data.description.message = localized.description;
}
return [
path.join(path.dirname(filepath), locale, "messages.json"),
JSON.stringify(data, null, 2)
];
});
};
exports.runTests = function()
{
function escape_string(str)
{
return str.replace(/(["'\\])/g, "\\$1");
}
function* readdir(dir, prefix = "")
{
for (let file of fs.readdirSync(dir))
{
let stats = fs.statSync(path.join(dir, file));
if (stats.isDirectory())
yield* readdir(path.join(dir, file), prefix + file + "/");
else if (path.extname(file) == ".js")
yield prefix + file;
}
}
let {TextEncoder, TextDecoder} = require("util");
class WorkerEventTarget
{
constructor(other)
{
if (other)
{
this.other = other;
this.other.other = this;
}
let listeners = [];
this.addEventListener = (type, listener) =>
{
if (type != "message")
return;
listeners.push(listener);
};
this.removeEventListener = (type, listener) =>
{
if (type != "message")
return;
let index = listeners.indexOf(listener);
if (index >= 0)
listeners.splice(index, 1);
};
this.onmessage = null;
this.triggerListeners = function(data)
{
let event = {type: "message", data};
if (typeof this.onmessage == "function")
this.onmessage(event);
for (let listener of listeners)
listener(event);
};
this.postMessage = data =>
{
Promise.resolve().then(() =>
{
this.other.triggerListeners(data);
});
};
}
}
class FakeWorker extends WorkerEventTarget
{
constructor(url)
{
super();
require("sandboxed-module").require(url, {
globals: {
self: new WorkerEventTarget(this)
}
});
}
}
let atob = str => Buffer.from(str, "base64").toString("binary");
let btoa = str => Buffer.from(str, "binary").toString("base64");
let {URL} = require("url");
let nodeunit = require("sandboxed-module").require("nodeunit", {
globals: {
console, process, Buffer, TextEncoder, TextDecoder, atob, btoa, URL,
Worker: FakeWorker,
navigator: {
onLine: true
}
}
});
let reporter = nodeunit.reporters.default;
return transform((filepath, contents) =>
{
return new Promise((resolve, reject) =>
{
reporter.run([filepath], null, error =>
{
if (error)
reject(error);
else
resolve([filepath, contents]);
});
});
});
};