-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbuild.ts
318 lines (284 loc) · 9.7 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
import * as fs from "node:fs/promises";
import * as pathe from "pathe"
import { injectManifest } from "./scripts/inject/manifest";
import { injectXHTML, injectXHTMLDev } from "./scripts/inject/xhtml";
import { applyMixin } from "./scripts/inject/mixin-loader";
import { build as buildVite } from "vite";
import AdmZip from "adm-zip";
import { savePrefsForProfile } from "./scripts/launchDev/savePrefs";
import { applyPatches } from "./scripts/git-patches/git-patches-manager";
import { initializeBinGit } from "./scripts/git-patches/git-patches-manager";
import { genVersion } from "./scripts/launchDev/writeVersion";
import { writeBuildid2 } from "./scripts/update/buildid2";
import { $, ProcessPromise } from "zx";
import { usePwsh } from 'zx'
import chalk from "chalk";
switch (process.platform) {
case "win32":
usePwsh()
}
//? branding
const brandingBaseName = "noraneko";
const brandingName = "Noraneko";
//? 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/${brandingBaseName}`
: `_dist/bin/${brandingBaseName}/${brandingName}.app/Contents/Resources`;
const r = (dir: string) => {
return pathe.resolve(import.meta.dirname, dir);
};
const isExists = async (path: string) => {
return await fs
.access(path)
.then(() => true)
.catch(() => false);
};
const getBinArchive = async () => {
const arch = process.arch;
if (process.platform === "win32") {
return `${brandingBaseName}-win-amd64-moz-artifact.zip`;
} else if (process.platform === "linux") {
if (arch === "x64") {
return `${brandingBaseName}-linux-amd64-moz-artifact.tar.xz`;
} else if (arch === "arm64") {
return `${brandingBaseName}-linux-arm64-moz-artifact.tar.xz`;
}
} else if (process.platform === "darwin") {
return `${brandingBaseName}-macOS-universal-moz-artifact.dmg`;
}
throw new Error("Unsupported platform/architecture");
};
const binArchive = await getBinArchive();
try {
await fs.access("dist");
await fs.rename("dist", "_dist");
} catch {}
const binPath = pathe.join(binDir, brandingBaseName);
const binPathExe = process.platform !== "darwin" ?
binPath + (process.platform === "win32" ? ".exe" : ""):
`./_dist/bin/${brandingBaseName}/${brandingName}.app/Contents/MacOS/${brandingBaseName}`;
const binVersion = pathe.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 === "win32") {
//? windows
new AdmZip(binArchive).extractAllTo(binExtractDir);
await fs.writeFile(binVersion, VERSION);
}
if (process.platform === "darwin") {
//? macOS
const mountDir = "_dist/mount";
await fs.mkdir(mountDir, { recursive: true });
await $`hdiutil ${["attach", "-mountpoint", mountDir, binArchive]}`;
await fs.mkdir(binDir, { recursive: true });
await fs.cp(pathe.join(mountDir, `${brandingName}.app`), pathe.join(`./_dist/bin/${brandingBaseName}`, `${brandingName}.app`), { recursive: true });
await fs.writeFile(binVersion, VERSION);
await $`hdiutil ${["detach", mountDir]}`;
await fs.rm(mountDir, { recursive: true });
await $`chmod ${["-R", "777", `./_dist/bin/${brandingBaseName}/${brandingName}.app`]}`;
await $`xattr ${["-rc", `./_dist/bin/${brandingBaseName}/${brandingName}.app`]}`;
}
if (process.platform === "linux") {
//? linux
await $`tar -xf ${binArchive} -C ${binExtractDir}`
await $`chmod ${["-R", "755", `./${binDir}`]}`;
await $`chmod ${["755", binPathExe]}`;
}
console.log("decompress complete!");
} 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 devViteProcess: ProcessPromise | null = null;
let browserProcess: ProcessPromise | null = null;
let devInit = false;
async function run(mode: "dev" | "test" | "release" = "dev") {
await initBin();
await applyPatches();
//create version for dev
await genVersion();
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");
devViteProcess = $`node --import @swc-node/register/esm-register ./scripts/launchDev/child-dev.ts ${mode} ${buildid2 ?? ""}`.stdio("pipe").nothrow();
(async () => {for await (const temp of devViteProcess.stdout) {
process.stdout.write(temp)
}})();
(async () => {for await (const temp of devViteProcess.stderr) {
process.stdout.write(temp)
}})();
await $`node --import @swc-node/register/esm-register ./scripts/launchDev/child-build.ts ${mode} ${buildid2 ?? ""}`
// env
if (process.platform === "darwin") {
process.env.MOZ_DISABLE_CONTENT_SANDBOX = "1";
}
devInit = true;
}
await Promise.all([
injectManifest(binDir, true, "noraneko-dev"),
injectXHTMLDev(binDir),
])
} else {
await release("before");
try {
await fs.access(`_dist/bin/${brandingBaseName}/noraneko-dev`);
await fs.rm(`_dist/bin/${brandingBaseName}/noraneko-dev`, { recursive: true });
} catch {}
await fs.symlink(`../../${brandingBaseName}`,`./_dist/bin/${brandingBaseName}/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 {}
})(),
]);
//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");
browserProcess = $`node --import @swc-node/register/esm-register ./scripts/launchDev/child-browser.ts`.stdio("pipe").nothrow();
(async () => {for await (const temp of browserProcess.stdout) {
process.stdout.write(temp)
}})();
(async () => {for await (const temp of browserProcess.stderr) {
process.stdout.write(temp)
}})();
}
let runningExit = false
async function exit() {
if (runningExit) return;
runningExit = true;
if (browserProcess) {
console.log("[build] Start Shutdown browserProcess")
browserProcess.stdin.write("s")
try {
await browserProcess
} catch (e){
console.error(e)
}
console.log("[build] End Shutdown browserProcess")
}
if (devViteProcess) {
console.log("[build] Start Shutdown devViteProcess")
devViteProcess.stdin.write("s")
try {
await devViteProcess
} catch (e){
console.error(e)
}
console.log("[build] End Shutdown devViteProcess")
}
console.log(chalk.green("[build] Cleanup Complete!"))
process.exit(0)
}
process.on("SIGINT",async ()=>{
await exit()
})
/**
* * 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 $`node --import @swc-node/register/esm-register ./scripts/launchDev/child-build.ts production ${buildid2 ?? ""}`
await injectManifest("./_dist", false);
} else if (mode === "after") {
const binPath = "../obj-artifact-build-output/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 ?? "");
}
}
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":
await genVersion();
break;
}
}