-
Notifications
You must be signed in to change notification settings - Fork 30
/
build.mjs
413 lines (347 loc) · 12.7 KB
/
build.mjs
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import {glob} from './miniglob.mjs';
import fs from 'fs';
import path from 'path';
import * as url from 'url';
import webExt from 'web-ext';
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
const builtDir = path.resolve(__dirname, 'built');
const chromeSourceDir = path.resolve(__dirname, 'chrome');
const chromeLibreBuildDir = path.resolve(__dirname, 'build_chrome_libre');
const chromeDistBuildDir = path.resolve(__dirname, 'build_chrome_dist');
const firefoxLibreBuildDir = path.resolve(__dirname, 'build_firefox_libre');
const firefoxDistBuildDir = path.resolve(__dirname, 'build_firefox_dist');
const webBuildDir = path.resolve(__dirname, 'built/web');
const licenseText = fs.readFileSync(path.resolve(__dirname, 'LICENSE.md'), 'utf8');
const packageJson = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'package.json'), 'utf8'));
fs.mkdirSync(builtDir, {recursive: true});
glob(builtDir + '/*.zip').forEach((file) => {
fs.unlinkSync(file);
});
removeBuildDirs();
deleteDirectoryRecursively(webBuildDir);
function removeBuildDirs() {
deleteDirectoryRecursively(chromeDistBuildDir);
deleteDirectoryRecursively(firefoxLibreBuildDir);
deleteDirectoryRecursively(firefoxDistBuildDir);
deleteDirectoryRecursively(chromeLibreBuildDir);
}
function deleteDirectoryRecursively(dirPath) {
if (fs.existsSync(dirPath)) {
fs.readdirSync(dirPath).forEach((file, index) => {
const curPath = path.join(dirPath, file);
if (fs.lstatSync(curPath).isDirectory()) {
deleteDirectoryRecursively(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(dirPath);
}
}
function extractImports(fileText) {
const lines = fileText.split('\n');
const newLines = [];
const imports = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.trim().startsWith('import')) {
const match = line.match(/import\s+\{{0,1}(.*?)\}{0,1}\s+(?:as\s+(.*)\s+){0,1}from\s+'(.*)'/);
if (!match) {
throw new Error('Invalid import: ' + line);
}
const importName = match[1];
const importAs = match[3] ? match[2] : null;
const importFrom = match[3] || match[2];
imports.push({
name: importName,
as: importAs,
from: importFrom,
});
} else {
newLines.push(line);
}
}
return {
imports: imports,
fileText: newLines.join('\n'),
};
}
function extractExports(fileText) {
const lines = fileText.split('\n');
const newLines = [];
const exports = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.trim().startsWith('export')) {
const match = line.match(/export\s+(?:default\s+){0,1}(const|let|var|class|function)\s+([^= ]*)/);
if (!match) {
throw new Error('Invalid export: ' + line);
}
const exportType = match[1];
const exportName = match[2];
exports.push({
type: exportType,
name: exportName,
});
newLines.push(line.replace('export', '').trim());
continue;
}
newLines.push(line);
}
return {
exports: exports,
fileText: newLines.join('\n'),
};
}
// eslint-disable-next-line no-unused-vars
function generateScriptWithAllImports(sourceDir, filePath, resolvedPaths = []) {
const text = extractExports(fs.readFileSync(filePath, 'utf8')).fileText;
const {imports, fileText} = extractImports(text);
const importScripts = [];
imports.forEach((importData) => {
const importPath = path.resolve(path.dirname(filePath), importData.from);
if (resolvedPaths.includes(importPath)) {
return;
}
resolvedPaths.push(importPath);
const importScript = generateScriptWithAllImports(sourceDir, importPath, resolvedPaths);
importScripts.push(importScript.trim());
});
return importScripts.join('\n') + '\n' + fileText;
}
function splice(fileText, target, relativePath) {
const lines = fileText.split('\n');
let newLines = [];
let inSplicerRemove = false;
let removedLines = 0;
const initiatorStr = `SPLICER:${target}:`;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const index = line.indexOf(initiatorStr);
if (index >= 0) {
const command = line.substring(index + initiatorStr.length).trim();
if (command === 'REMOVE_FILE') {
console.log(`[Splicer-${target}] Removing file`, relativePath);
return '';
} else if (command === 'REMOVE_LINE') {
console.log(`[Splicer-${target}] Removing line ${i + 1}`, relativePath);
continue;
} else if (command === 'REMOVE_START') {
inSplicerRemove = true;
removedLines = 0;
continue;
} else if (command === 'REMOVE_END') {
if (!inSplicerRemove) {
console.error(`[Splicer-${target}] Unmatched SPLICER:${target}:REMOVE_END`, relativePath);
throw new Error('Unmatched SPLICER');
}
inSplicerRemove = false;
console.log(`[Splicer-${target}] Removing lines ${i - removedLines}-${i + 1}`, relativePath);
continue;
} else if (command === 'INSERT_LOCALE') {
const localesPath = path.join(chromeSourceDir, '_locales/');
// get all locale folders
const localeFolders = fs.readdirSync(localesPath);
const locales = [];
localeFolders.forEach((localeFolder) => {
const localePath = path.join(localesPath, localeFolder, 'messages.json');
// check if file exists
if (!fs.existsSync(localePath)) {
return;
}
const messages = JSON.parse(fs.readFileSync(localePath, 'utf8'));
const translationMap = {};
Object.keys(messages).forEach((key) => {
translationMap[key] = messages[key].message;
});
locales.push({
translationMap,
code: localeFolder,
});
});
// put english first
const defaultLanguage = 'en';
locales.sort((a, b) => {
if (a.code === defaultLanguage) {
return -1;
} else if (b.code === defaultLanguage) {
return 1;
} else {
return a.code.localeCompare(b.code);
}
});
const newLocales = {};
newLocales['LANGUAGES'] = locales.map((locale) => locale.code);
Object.keys(locales[0].translationMap).forEach((key) => {
const translations = locales.map((locale) => locale.translationMap[key]);
newLocales[key] = translations;
});
const localeText = JSON.stringify(newLocales, null, 2);
newLines.push(localeText.substring(1, localeText.length - 1).trim());
console.log(`[Splicer-${target}] Inserting locale`, relativePath);
continue;
} else if (command === 'INSERT_VERSION') {
newLines.push(`version = '${packageJson.version}';`);
console.log(`[Splicer-${target}] Inserting version`, relativePath);
continue;
}
}
if (inSplicerRemove) {
removedLines++;
continue;
}
newLines.push(line);
}
if (inSplicerRemove) {
console.error(`[Splicer-${target}] Unmatched SPLICER:${target}:REMOVE_START`, relativePath);
throw new Error('Unmatched SPLICER');
}
newLines = newLines.filter((line) => {
return line.trim().length;
});
return newLines.join('\n');
}
function spliceAndCopy(sourceDir, buildDir, spliceTargets = [], excludeFiles = []) {
glob(sourceDir + '/**')
.forEach((file) => {
const fileExtension = path.extname(file);
const relativePath = path.relative(sourceDir, file);
const targetPath = path.resolve(buildDir, relativePath);
const fileText = fs.readFileSync(file, 'utf8');
// See if exclude files shares a prefix with the file
for (let i = 0; i < excludeFiles.length; i++) {
const excludeFile = excludeFiles[i];
if (relativePath.startsWith(excludeFile)) {
return;
}
}
if (fileExtension === '.mjs' || fileExtension === '.js') {
let spliced = fileText;
spliceTargets.forEach((target) => {
spliced = splice(spliced, target, relativePath);
});
if (spliced.length) {
fs.mkdirSync(path.dirname(targetPath), {recursive: true});
fs.writeFileSync(targetPath, spliced + '\n');
}
} else {
fs.mkdirSync(path.dirname(targetPath), {recursive: true});
fs.copyFileSync(file, targetPath);
}
});
}
async function runWebExtBuild(sourceDir, artifactsDir) {
return new Promise((resolve, reject) => {
// run web-ext build
webExt.cmd.build({
sourceDir: sourceDir,
artifactsDir: artifactsDir,
overwriteDest: true,
}).then((result) => {
resolve(result.extensionPath);
});
});
}
function insertLicense(buildDir) {
const newLicensePath = path.join(buildDir, 'LICENSE.md');
fs.writeFileSync(newLicensePath, licenseText);
}
async function buildChromeDist() {
spliceAndCopy(chromeSourceDir, chromeDistBuildDir, ['EXTENSION', 'CENSORYT', 'NO_UPDATE_CHECKER']);
insertLicense(chromeDistBuildDir);
const builtPath = await runWebExtBuild(chromeDistBuildDir, path.join(chromeDistBuildDir, 'dist'));
const name = path.basename(builtPath);
const finalPath = path.join(builtDir, 'chrome-dist-' + name);
fs.renameSync(builtPath, finalPath);
return finalPath;
}
async function buildChromeLibre() {
spliceAndCopy(chromeSourceDir, chromeLibreBuildDir, ['EXTENSION', 'NO_PROMO']);
insertLicense(chromeLibreBuildDir);
const builtPath = await runWebExtBuild(chromeLibreBuildDir, path.join(chromeLibreBuildDir, 'libre'));
const name = path.basename(builtPath);
const finalPath = path.join(builtDir, 'chrome-libre-' + name);
fs.renameSync(builtPath, finalPath);
return finalPath;
}
async function buildFirefoxLibre() {
spliceAndCopy(chromeSourceDir, firefoxLibreBuildDir, ['EXTENSION', 'FIREFOX', 'NO_PROMO']);
insertLicense(firefoxLibreBuildDir);
const manifestPath = path.join(firefoxLibreBuildDir, 'manifest.json');
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
manifest.permissions.push('downloads', 'cookies', 'contextualIdentities');
manifest.browser_specific_settings = {
gecko: {
id: 'faststream@andrews',
strict_min_version: '113.0',
},
};
manifest.background = {
scripts: ['background/background.mjs'],
type: 'module',
};
delete manifest.incognito;
delete manifest.minimum_chrome_version;
delete manifest.key;
delete manifest.sandbox;
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
const builtPath = await runWebExtBuild(firefoxLibreBuildDir, path.join(firefoxLibreBuildDir, 'libre'));
const name = path.basename(builtPath);
const finalPath = path.join(builtDir, 'firefox-libre-' + name);
fs.renameSync(builtPath, finalPath);
return finalPath;
}
async function buildFirefoxDist() {
spliceAndCopy(chromeSourceDir, firefoxDistBuildDir, ['EXTENSION', 'FIREFOX', 'NO_UPDATE_CHECKER']);
insertLicense(firefoxDistBuildDir);
const manifestPath = path.join(firefoxDistBuildDir, 'manifest.json');
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
manifest.browser_specific_settings = {
gecko: {
id: 'faststream@andrews',
strict_min_version: '113.0',
},
};
manifest.background = {
scripts: ['background/background.mjs'],
type: 'module',
};
manifest.permissions.push('downloads', 'cookies', 'contextualIdentities');
delete manifest.incognito;
delete manifest.minimum_chrome_version;
delete manifest.key;
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
const builtPath = await runWebExtBuild(firefoxDistBuildDir, path.join(firefoxDistBuildDir, 'dist'));
const name = path.basename(builtPath);
const finalPath = path.join(builtDir, 'firefox-dist-' + name);
fs.renameSync(builtPath, finalPath);
return finalPath;
}
async function buildWeb() {
spliceAndCopy(chromeSourceDir, webBuildDir, ['WEB', 'NO_UPDATE_CHECKER'], [
'manifest.json',
'content.js',
'background',
'_locales',
'perms.html',
'perms.mjs',
'welcome.html',
'icon2_128.png',
'icon16.png',
'icon48.png',
'keyboard.png',
'custom',
]);
insertLicense(webBuildDir);
}
async function runAll() {
// update manifest version
const manifestPath = path.join(chromeSourceDir, 'manifest.json');
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
manifest.version = packageJson.version;
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
console.log(`Building version ${manifest.version}`);
await Promise.all([buildChromeLibre(), buildChromeDist(), buildFirefoxLibre(), buildFirefoxDist(), buildWeb()]);
removeBuildDirs();
}
runAll();