-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbuild.ts
420 lines (387 loc) · 12.2 KB
/
build.ts
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
414
415
416
417
418
419
420
import * as fs from "node:fs/promises";
import * as path from "node:path";
import { injectManifest } from "./scripts/inject/manifest";
import { injectXHTML, injectXHTMLDev } from "./scripts/inject/xhtml";
import { applyMixin } from "./scripts/inject/mixin-loader";
import puppeteer, { type Browser } from "puppeteer-core";
import { createServer, type ViteDevServer, build as buildVite } from "vite";
import AdmZip from "adm-zip";
import { execa, type ResultPromise } from "execa";
import { runBrowser } from "./scripts/launchBrowser/index";
import { savePrefsForProfile } from "./scripts/launchBrowser/savePrefs";
import { writeVersion } from "./scripts/update/version";
import { writeBuildid2 } from "./scripts/update/buildid2";
import { applyPatches } from "./scripts/git-patches/git-patches-manager";
import { initializeBinGit } from "./scripts/git-patches/git-patches-manager";
//? when the linux binary has published, I'll sync linux bin version
const VERSION = process.platform === "win32" ? "001" : "000";
const binExtractDir = "_dist/bin";
const binDir = process.platform !== "darwin" ? "_dist/bin/noraneko"
: "_dist/bin/noraneko/Noraneko.app/Contents/Resources";
const r = (dir: string) => {
return path.resolve(import.meta.dirname, dir);
};
const isExists = async (path: string) => {
return await fs
.access(path)
.then(() => true)
.catch(() => false);
};
const getBinArchive = async () => {
if (process.platform === "win32") {
for await (const x of fs.glob("noraneko-*.win64.zip")) {
return x
}
} if (process.platform === "linux") {
const arch = process.arch;
if (arch === "arm64") {
return "noraneko-linux-aarch64-dev.zip";
} if (arch === "x64") {
return "noraneko-linux-amd64-dev.zip";
}
} else {
if (process.platform === "darwin") {
return "noraneko-macOS-universal.dmg";
}
}
throw new Error("Unsupported platform/architecture");
};
const binArchive = await getBinArchive();
try {
await fs.access("dist");
await fs.rename("dist", "_dist");
} catch {}
const binPath = path.join(binDir, "noraneko");
const binPathExe = process.platform !== "darwin" ?
binPath + (process.platform === "win32" ? ".exe" : ""):
"./_dist/bin/noraneko/Noraneko.app/Contents/MacOS/noraneko";
const binVersion = path.join(binDir, "nora.version.txt");
async function decompressBin() {
try {
console.log(`decompressing ${binArchive}`);
if (!(await isExists(binArchive))) {
console.error(`${binArchive} not found`);
process.exit(1);
}
if (process.platform !== "darwin") {
new AdmZip(binArchive).extractAllTo(binExtractDir);
console.log("decompress complete!");
await fs.writeFile(binVersion, VERSION);
} else {
//? macOS
const mountDir = "_dist/mount";
await fs.mkdir(mountDir, { recursive: true });
await execa("hdiutil", [
"attach",
"-mountpoint",
mountDir,
binArchive,
]);
await fs.mkdir(binDir, { recursive: true });
await execa("cp", ["-R", path.join(mountDir, "Noraneko.app"), path.join("./_dist/bin/noraneko", "")]);
await fs.writeFile(binVersion, VERSION);
await execa("hdiutil", ["detach", mountDir]);
await fs.rm(mountDir, { recursive: true });
await execa("chmod", ["-R", "777", `./_dist/bin/noraneko/Noraneko.app`]);
await execa("xattr", ["-rc", `./_dist/bin/noraneko/Noraneko.app`]);
}
if (process.platform === "linux") {
try {
await execa("chmod", ["-R", "755", `./${binDir}`]);
await execa("chmod", ["755", binPathExe]);
} catch (chmodError) {
process.exit(1);
}
}
} catch (e) {
console.error(e);
process.exit(1);
}
}
async function initBin() {
const hasVersion = await isExists(binVersion);
const hasBin = await isExists(binPathExe);
if (hasVersion) {
const version = (await fs.readFile(binVersion)).toString();
const mismatch = VERSION !== version;
if (mismatch) {
console.log(`version mismatch ${version} !== ${VERSION}`);
await fs.rm(binDir, { recursive: true });
await fs.mkdir(binDir, { recursive: true });
await decompressBin();
return;
}
} else {
if (hasBin) {
console.log(`bin exists, but version file not found, writing ${VERSION}`);
await fs.mkdir(binDir, { recursive: true });
await fs.writeFile(binVersion, VERSION);
}
}
console.log("initBin");
if (!hasBin) {
console.log("There seems no bin. decompressing.");
await fs.mkdir(binDir, { recursive: true });
await decompressBin();
}
}
async function runWithInitBinGit() {
if (await isExists(binDir)) {
await fs.rm(binDir, { recursive: true, force: true });
}
await initBin();
await initializeBinGit();
await run();
}
let devViteProcesses: ViteDevServer[] | null = null;
let buildViteProcesses: any[];
const devExecaProcesses: ResultPromise[] = [];
let devInit = false;
import packageJson from "./package.json" assert { type: "json" };
async function run(mode: "dev" | "test" | "release" = "dev") {
await initBin();
await applyPatches();
//create version for dev
await version();
let buildid2: string | null = null;
try {
await fs.access("_dist/buildid2");
buildid2 = await fs.readFile("_dist/buildid2", { encoding: "utf-8" });
} catch {}
console.log(`[dev] buildid2: ${buildid2}`);
if (mode !== "release") {
if (!devInit) {
console.log("run dev servers");
devViteProcesses = [
await createServer({
mode,
configFile: r("./src/apps/main/vite.config.ts"),
root: r("./src/apps/main"),
define: {
"import.meta.env.__BUILDID2__": `"${buildid2 ?? ""}"`,
"import.meta.env.__VERSION2__": `"${packageJson.version}"`
},
}),
await createServer({
mode,
configFile: r("./src/apps/designs/vite.config.ts"),
root: r("./src/apps/designs"),
}),
];
buildViteProcesses = [
await buildVite({
mode,
configFile: r("./src/apps/designs/vite.config.ts"),
root: r("./src/apps/designs"),
}),
await buildVite({
configFile: r("./src/apps/modules/vite.config.ts"),
root:r("./src/apps/modules"),
define: {
"import.meta.env.__BUILDID2__": `"${buildid2 ?? ""}"`,
"import.meta.env.__VERSION2__": `"${packageJson.version}"`
}
})
];
devExecaProcesses.push(
execa({
preferLocal: true,
stdout: "inherit",
cwd: r("./src/apps/settings"),
})`pnpm dev`,
);
await execa({
preferLocal: true,
stdout: "inherit",
cwd: r("./src/apps/modules"),
})`pnpm genJarManifest`;
if (mode === "test") {
devExecaProcesses.push(
execa({
preferLocal: true,
cwd: r("./src/apps/test"),
})`node --import @swc-node/register/esm-register server.ts`,
);
}
// env
if (process.platform === "darwin") {
process.env.MOZ_DISABLE_CONTENT_SANDBOX = "1";
}
devInit = true;
}
devViteProcesses!.forEach((p) => {
p.listen();
});
await Promise.all([
injectManifest(binDir, true, "noraneko-dev"),
injectXHTMLDev(binDir),
])
} else {
await release("before");
try {
await fs.access("_dist/bin/noraneko/noraneko-dev");
await fs.rm("_dist/bin/noraneko/noraneko-dev", { recursive: true });
} catch {}
await fs.symlink("../../noraneko","./_dist/bin/noraneko/noraneko-dev",process.platform==="win32" ? "junction" : undefined);
}
await Promise.all([
buildVite({
mode,
root: r("./src/apps/startup"),
configFile: r("./src/apps/startup/vite.config.ts"),
}),
(async () => {
await injectXHTML(binDir);
})(),
applyMixin(binDir),
(async () => {
try {
await fs.access("_dist/profile");
await fs.rm("_dist/profile", { recursive: true });
} catch {}
})(),
]);
let browser: Browser | undefined = undefined;
//https://github.com/puppeteer/puppeteer/blob/c229fc8f9750a4c87d0ed3c7b541c31c8da5eaab/packages/puppeteer-core/src/node/FirefoxLauncher.ts#L123
await fs.mkdir("./_dist/profile/test", { recursive: true });
await savePrefsForProfile("./_dist/profile/test");
await runBrowser();
browser = await puppeteer.connect({
browserWSEndpoint: "ws://127.0.0.1:5180/session",
protocol: "webDriverBiDi",
});
//await (await browser.pages())[0].goto("https://google.com");
if (mode === "dev" && false) {
// const pages = await browser.pages();
// await pages[0].goto("about:newtab", {
// timeout: 0,
// waitUntil: "domcontentloaded",
// });
// //? We should not go to about:preferences
// //? Puppeteer cannot get the load event so when reload, this errors.
// // await pages[0].goto("about:preferences", {
// // timeout: 0,
// // waitUntil: "domcontentloaded",
// // });
} else if (mode === "test") {
browser.pages().then(async (page) => {
await page[0].goto("about:newtab", {
timeout: 0,
waitUntil: "domcontentloaded",
});
// await page[0].goto("about:preferences#csk", {
// timeout: 0,
// waitUntil: "domcontentloaded",
// });
});
}
// browser.on("disconnected", () => {
// process.exit();
// });
}
process.on("exit", () => {
devExecaProcesses.forEach((v) => {
v.kill();
});
devViteProcesses?.forEach((v) => {
v.close();
});
});
/**
* * Please run with NODE_ENV='production'
* @param mode
*/
async function release(mode: "before" | "after") {
let buildid2: string | null = null;
try {
await fs.access("_dist/buildid2");
buildid2 = await fs.readFile("_dist/buildid2", { encoding: "utf-8" });
} catch {}
console.log(`[build] buildid2: ${buildid2}`);
if (mode === "before") {
await Promise.all([
buildVite({
configFile: r("./src/apps/startup/vite.config.ts"),
root: r("./src/apps/startup"),
}),
buildVite({
configFile: r("./src/apps/main/vite.config.ts"),
root: r("./src/apps/main"),
define: {
"import.meta.env.__BUILDID2__": `"${buildid2 ?? ""}"`,
"import.meta.env.__VERSION2__": `"${packageJson.version}"`
},
base: "chrome://noraneko/content"
}),
buildVite({
configFile: r("./src/apps/designs/vite.config.ts"),
root: r("./src/apps/designs"),
}),
buildVite({
configFile: r("./src/apps/settings/vite.config.ts"),
root: r("./src/apps/settings"),
base: "chrome://noraneko-settings/content"
}),
buildVite({
configFile: r("./src/apps/modules/vite.config.ts"),
root:r("./src/apps/modules"),
define: {
"import.meta.env.__BUILDID2__": `"${buildid2 ?? ""}"`,
"import.meta.env.__VERSION2__": `"${packageJson.version}"`
}
})
//applyMixin(binPath),
]);
await execa({
preferLocal: true,
stdout: "inherit",
cwd: r("./src/apps/modules"),
})`pnpm genJarManifest`;
await injectManifest("./_dist", false);
} else if (mode === "after") {
const binPath = "../obj-x86_64-pc-windows-msvc/dist/bin";
injectXHTML(binPath);
let buildid2: string | null = null;
try {
await fs.access("_dist/buildid2");
buildid2 = await fs.readFile("_dist/buildid2", { encoding: "utf-8" });
} catch {}
await writeBuildid2(`${binPath}/browser`, buildid2 ?? "");
// await applyPatches(binPath);
}
}
import { v7 as uuidv7 } from "uuid";
async function version() {
await writeVersion(r("./gecko"));
try {
await fs.access("_dist");
} catch {
await fs.mkdir("_dist");
}
await writeBuildid2(r("./_dist"), uuidv7());
}
if (process.argv[2]) {
switch (process.argv[2]) {
case "--run":
run();
break;
case "--run-with-init-bin-git":
runWithInitBinGit();
break;
case "--test":
run("test");
break;
case "--run-prod":
run("release");
break;
case "--release-build-before":
release("before");
break;
case "--release-build-after":
release("after");
break;
case "--write-version":
version();
break;
}
}