-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbuilder.ts
More file actions
396 lines (335 loc) · 13.7 KB
/
builder.ts
File metadata and controls
396 lines (335 loc) · 13.7 KB
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
import type { Context } from "../types/index.js";
import type { Manifest } from "../types/manifest.js";
import type { UpdateJSON } from "../types/update-json.js";
import { existsSync } from "node:fs";
import { readFile, writeFile } from "node:fs/promises";
import { basename, dirname, join, resolve } from "node:path";
import process from "node:process";
import AdmZip from "adm-zip";
import { escapeRegExp, toMerged } from "es-toolkit";
import { build as buildAsync } from "esbuild";
import { copy, emptyDir, move, outputFile, outputJSON, readJSON, writeJson } from "fs-extra/esm";
import styleText from "node-style-text";
import { glob } from "tinyglobby";
import { generateHash } from "../utils/crypto.js";
import { PrefsManager, renderPluginPrefsDts } from "../utils/prefs-manager.js";
import { dateFormat, replaceInFile, toArray } from "../utils/string.js";
import { Base } from "./base.js";
export default class Build extends Base {
private buildTime: string;
private isPreRelease: boolean;
constructor(ctx: Context) {
super(ctx);
process.env.NODE_ENV ??= "production";
this.buildTime = "";
this.isPreRelease = this.ctx.version.includes("-");
}
/**
* Default build runner
*/
async run() {
const { dist, version } = this.ctx;
const t = new Date();
this.buildTime = dateFormat("YYYY-mm-dd HH:MM:SS", t);
this.logger.info(
`Building version ${styleText.blue(version)} to ${styleText.blue(dist)} at ${styleText.blue(this.buildTime)} in ${styleText.blue(process.env.NODE_ENV)} mode.`,
);
await this.ctx.hooks.callHook("build:init", this.ctx);
await emptyDir(dist);
await this.ctx.hooks.callHook("build:mkdir", this.ctx);
this.logger.tip("Preparing static assets", { space: 1 });
await this.makeAssets();
await this.ctx.hooks.callHook("build:copyAssets", this.ctx);
this.logger.debug("Preparing manifest", { space: 2 });
await this.makeManifest();
await this.ctx.hooks.callHook("build:makeManifest", this.ctx);
this.logger.debug("Preparing locale files", { space: 2 });
await this.prepareLocaleFiles();
await this.ctx.hooks.callHook("build:fluent", this.ctx);
await this.preparePrefs();
this.logger.tip("Bundling scripts", { space: 1 });
await this.esbuild();
await this.ctx.hooks.callHook("build:bundle", this.ctx);
/** ======== build resolved =========== */
if (process.env.NODE_ENV === "production") {
this.logger.tip("Packing plugin", { space: 1 });
await this.pack();
await this.ctx.hooks.callHook("build:pack", this.ctx);
await this.makeUpdateJson();
await this.ctx.hooks.callHook("build:makeUpdateJSON", this.ctx);
}
await this.ctx.hooks.callHook("build:done", this.ctx);
this.logger.success(
`Build finished in ${(new Date().getTime() - t.getTime()) / 1000} s.`,
);
}
/**
* Copys files in `Config.build.assets` to `Config.dist`
*/
async makeAssets() {
const { source, dist, build } = this.ctx;
const { assets, define } = build;
// We should ignore node_modules/ by default, glob this folder will be very slow
const paths = await glob(assets, { ignore: ["node_modules", ".git", dist] });
const newPaths = paths.map(p => `${dist}/addon/${p.replace(new RegExp(toArray(source).join("|")), "")}`);
// Copys files in `Config.build.assets` to `Config.dist`
await Promise.all(paths.map(async (file, i) => {
await copy(file, newPaths[i]);
this.logger.debug(`Copy ${file} to ${newPaths[i]}`);
}));
// Replace all `placeholder.key` to `placeholder.value` for all files in `dist`
const replaceMap = new Map(
Object.keys(define).map(key => [
new RegExp(`__${key}__`, "g"),
define[key],
]),
);
this.logger.debug(`replace map: ${replaceMap}`);
await replaceInFile({
files: newPaths,
from: Array.from(replaceMap.keys()),
to: Array.from(replaceMap.values()),
isGlob: false,
});
}
/**
* Override user's manifest
*
*/
async makeManifest() {
if (!this.ctx.build.makeManifest.enable)
return;
const { name, id, updateURL, dist, version } = this.ctx;
const userData = await readJSON(
`${dist}/addon/manifest.json`,
) as Manifest;
const template: Manifest = {
...userData,
...((!userData.name && name) && { name }),
...(version && { version }),
manifest_version: 2,
applications: {
zotero: {
id,
update_url: updateURL,
},
},
};
const data: Manifest = toMerged(userData, template);
this.logger.debug(`manifest: ${JSON.stringify(data, null, 2)}`);
outputJSON(`${dist}/addon/manifest.json`, data, { spaces: 2 });
}
async prepareLocaleFiles() {
const { dist, namespace, build } = this.ctx;
const ignores = toArray(build.fluent.ignore);
// https://regex101.com/r/lQ9x5p/1
// eslint-disable-next-line regexp/no-super-linear-backtracking
const FTL_MESSAGE_PATTERN = /^(?<message>[a-z]\S*)( *= *)(?<pattern>.*)$/gim;
const HTML_DATAI10NID_PATTERN = new RegExp(`(data-l10n-id)="((?!${namespace})\\S*)"`, "g");
// Get locale names
const localePaths = await glob(`${dist}/addon/locale/*`, { onlyDirectories: true });
const localeNames = localePaths.map(locale => basename(locale));
this.logger.debug(`Locale names:", ${localeNames}`);
const allMessages = new Set<string>();
const messagesByLocale = new Map<string, Set<string>>();
for (const localeName of localeNames) {
// Prefix Fluent messages in each ftl, add message to set.
const localeMessages = new Set<string>();
const ftlPaths = await glob(`${dist}/addon/locale/${localeName}/**/*.ftl`);
await Promise.all(ftlPaths.map(async (ftlPath: string) => {
let ftlContent = await readFile(ftlPath, "utf-8");
const matches = [...ftlContent.matchAll(FTL_MESSAGE_PATTERN)];
for (const match of matches) {
const [matched, message, _pattern] = match;
if (message) {
localeMessages.add(message);
allMessages.add(message);
ftlContent = ftlContent.replace(new RegExp(`^${escapeRegExp(matched)}`, "gm"), `${namespace}-${matched}`);
}
}
// If prefixFluentMessages===true, we save the changed ftl file,
// otherwise discard the changes
if (build.fluent.prefixFluentMessages)
await writeFile(ftlPath, ftlContent);
// rename *.ftl to addonRef-*.ftl
if (build.fluent.prefixLocaleFiles === true) {
await move(ftlPath, `${dirname(ftlPath)}/${namespace}-${basename(ftlPath)}`);
this.logger.debug(`FTL file '${ftlPath}' is renamed to '${namespace}-${basename(ftlPath)}'.`);
}
}));
messagesByLocale.set(localeName, localeMessages);
}
// Prefix Fluent messages in xhtml
const messagesInHTML = new Set<string>();
const htmlPaths = await glob([
`${dist}/addon/**/*.xhtml`,
`${dist}/addon/**/*.html`,
]);
await Promise.all(htmlPaths.map(async (htmlPath) => {
let htmlContent = await readFile(htmlPath, "utf-8");
const matches = [...htmlContent.matchAll(HTML_DATAI10NID_PATTERN)];
for (const match of matches) {
const [matched, attrKey, attrVal] = match;
if (ignores.includes(attrVal)) {
this.logger.debug(`HTML data-i10n-id ${attrVal} is in ignore list, skip to namespace`);
continue;
}
if (!allMessages.has(attrVal)) {
this.logger.warn(`HTML data-i10n-id '${styleText.blue(attrVal)}' in ${styleText.gray(htmlPath)} do not exist in any FTL message, skip to namespace it.`);
continue;
}
messagesInHTML.add(attrVal);
const namespacedAttr = `${namespace}-${attrVal}`;
htmlContent = htmlContent.replace(matched, `${attrKey}="${namespacedAttr}"`);
this.logger.debug(`HTML data-i10n-id '${styleText.blue(attrVal)}' in ${styleText.gray(htmlPath)} is namespaced to ${styleText.blue(namespacedAttr)}.`);
}
if (build.fluent.prefixFluentMessages)
await writeFile(htmlPath, htmlContent);
}));
// Check miss 1: Cross check in diff locale - seems no need
// messagesByLocale.forEach((messageInThisLang, lang) => {
// // Needs Nodejs 22
// const diff = allMessages.difference(messageInThisLang);
// if (diff.size)
// this.logger.warn(`FTL messages '${Array.from(diff).join(", ")}' don't exist the locale '${lang}'`);
// });
// Check miss 2: Check ids in HTML but not in ftl
messagesInHTML.forEach((messageInHTML) => {
if (ignores.includes(messageInHTML))
return;
const missingLocales = [...messagesByLocale.entries()]
.filter(([_, messages]) => !messages.has(messageInHTML))
.map(([locale]) => locale);
if (missingLocales.length > 0) {
this.logger.warn(`HTML data-l10n-id '${styleText.blue(messageInHTML)}' is missing in locales: ${missingLocales.join(", ")}.`);
}
});
}
async preparePrefs() {
const { dts, prefixPrefKeys, prefix } = this.ctx.build.prefs;
const { dist } = this.ctx;
// Skip if not enable this builder
if (!prefixPrefKeys && !dts)
return;
// Skip if no prefs.js
const prefsFilePath = join(dist, "addon", "prefs.js");
if (!existsSync(prefsFilePath))
return;
// Parse prefs.js
const prefsManager = new PrefsManager("pref");
await prefsManager.read(prefsFilePath);
const prefsWithPrefix = prefsManager.getPrefsWithPrefix(prefix);
const prefsWithoutPrefix = prefsManager.getPrefsWithoutPrefix(prefix);
// Generate prefs.d.ts
if (dts) {
const dtsContent = renderPluginPrefsDts(prefsWithoutPrefix);
await outputFile(dts, dtsContent, "utf-8");
}
// Generate prefixed prefs.js
if (prefixPrefKeys) {
prefsManager.clearPrefs();
prefsManager.setPrefs(prefsWithPrefix);
await prefsManager.write(prefsFilePath);
}
// Prefix pref keys in xhtml
if (prefixPrefKeys) {
const HTML_PREFERENCE_PATTERN = /preference="(\S*)"/g;
const xhtmlPaths = await glob(`${dist}/addon/**/*.xhtml`);
await Promise.all(xhtmlPaths.map(async (path) => {
let content = await readFile(path, "utf-8");
const matchs = [...content.matchAll(HTML_PREFERENCE_PATTERN)];
for (const match of matchs) {
const [matched, key] = match;
if (key.startsWith(prefix)) {
this.logger.debug(`Pref key '${styleText.blue(key)}' is already starts with '${prefix}', skip prefixing it.`);
continue;
}
else if (key.startsWith("extensions.")) {
this.logger.warn(`Pref key '${styleText.blue(key)}' in ${styleText.gray(path)} starts with 'extensions.' but not '${styleText.blue(prefix)}', skip prefixing it.`);
continue;
}
else if (!(key in prefsWithPrefix) && !(key in prefsWithoutPrefix)) {
this.logger.warn(`Pref key '${styleText.blue(key)}' in ${styleText.gray(path)} is not found in prefs.js, skip prefixing it.`);
continue;
}
else {
const prefixed = `${prefix}.${key}`;
this.logger.debug(`Pref key '${styleText.blue(key)}' in ${styleText.gray(path)} is prefixed to ${styleText.blue(prefixed)}.`);
content = content.replace(matched, `preference="${prefixed}"`);
}
}
await outputFile(path, content, "utf-8");
}));
}
}
esbuild() {
const { dist, build: { esbuildOptions } } = this.ctx;
const distAbsolute = resolve(dist);
if (esbuildOptions.length === 0)
return;
// ensure outfile and outdir are in dist folder
esbuildOptions.map((option, i) => {
if (option.outfile && !resolve(option.outfile).startsWith(distAbsolute)) {
this.logger.debug(`'outfile' of esbuildOptions[${i}] is not in dist folder, it will be overwritten.`);
option.outfile = `${dist}/${option.outfile}`;
}
if (option.outdir && !resolve(option.outdir).startsWith(distAbsolute)) {
this.logger.debug(`'outdir' of esbuildOptions[${i}] is not in dist folder, it will be overwritten.`);
option.outdir = `${dist}/${option.outdir}`;
}
return option;
});
return Promise.all(
esbuildOptions.map(esbuildOption =>
buildAsync(esbuildOption),
),
);
}
async makeUpdateJson() {
const { dist, xpiName, id, version, xpiDownloadLink, build } = this.ctx;
const manifest = await readJSON(
`${dist}/addon/manifest.json`,
) as Manifest;
const min = manifest.applications?.zotero?.strict_min_version;
const max = manifest.applications?.zotero?.strict_max_version;
const updateHash = await generateHash(`${dist}/${xpiName}.xpi`, "sha512");
const data: UpdateJSON = {
addons: {
[id]: {
updates: [
...build.makeUpdateJson.updates,
{
version,
update_link: xpiDownloadLink,
...(build.makeUpdateJson.hash && {
update_hash: updateHash,
}),
applications: {
zotero: {
...(min && { strict_min_version: min }),
...(max && { strict_max_version: max }),
},
},
},
],
},
},
};
await writeJson(`${dist}/update-beta.json`, data, { spaces: 2 });
if (!this.isPreRelease)
await writeJson(`${dist}/update.json`, data, { spaces: 2 });
this.logger.debug(
`Prepare Update.json for ${
this.isPreRelease
? "\u001B[31m Prerelease \u001B[0m"
: "\u001B[32m Release \u001B[0m"
}`,
);
}
async pack() {
const { dist, xpiName } = this.ctx;
const zip = new AdmZip();
zip.addLocalFolder(`${dist}/addon`);
zip.writeZip(`${dist}/${xpiName}.xpi`);
}
}