-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare-node-runtime.mjs
More file actions
166 lines (145 loc) · 4.31 KB
/
prepare-node-runtime.mjs
File metadata and controls
166 lines (145 loc) · 4.31 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
import fs from "node:fs";
import path from "node:path";
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
const rootDir = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"..",
".."
);
const outputDir = path.join(rootDir, ".desktop-node");
const outputPath = path.join(
outputDir,
process.platform === "win32" ? "node.exe" : "node"
);
const version = process.version.replace(/^v/, "");
const supportedTargets = {
"darwin-arm64": {
archiveName: `node-v${version}-darwin-arm64.tar.gz`,
extractedDir: `node-v${version}-darwin-arm64`,
binaryPath: ["bin", "node"],
extract: {
kind: "tar",
args: ["-xzf"],
},
},
"darwin-x64": {
archiveName: `node-v${version}-darwin-x64.tar.gz`,
extractedDir: `node-v${version}-darwin-x64`,
binaryPath: ["bin", "node"],
extract: {
kind: "tar",
args: ["-xzf"],
},
},
"linux-arm64": {
archiveName: `node-v${version}-linux-arm64.tar.xz`,
extractedDir: `node-v${version}-linux-arm64`,
binaryPath: ["bin", "node"],
extract: {
kind: "tar",
args: ["-xJf"],
},
},
"linux-x64": {
archiveName: `node-v${version}-linux-x64.tar.xz`,
extractedDir: `node-v${version}-linux-x64`,
binaryPath: ["bin", "node"],
extract: {
kind: "tar",
args: ["-xJf"],
},
},
"win32-arm64": {
archiveName: `node-v${version}-win-arm64.zip`,
extractedDir: `node-v${version}-win-arm64`,
binaryPath: ["node.exe"],
extract: {
kind: "zip",
},
},
"win32-x64": {
archiveName: `node-v${version}-win-x64.zip`,
extractedDir: `node-v${version}-win-x64`,
binaryPath: ["node.exe"],
extract: {
kind: "zip",
},
},
};
const targetKey = `${process.platform}-${process.arch}`;
const target = supportedTargets[targetKey];
if (!target) {
throw new Error(
`Unsupported desktop Node runtime target: ${targetKey}. Add an official Node distribution mapping first.`
);
}
const markerPath = path.join(outputDir, ".version");
function run(command, args) {
const result = spawnSync(command, args, {
cwd: rootDir,
stdio: "inherit",
});
if (typeof result.status === "number" && result.status !== 0) {
throw new Error(`Command failed: ${command} ${args.join(" ")}`);
}
if (result.error) {
throw result.error;
}
}
function escapePowerShellString(value) {
return value.replace(/'/g, "''");
}
function extractArchive(archivePath, destination) {
if (target.extract.kind === "tar") {
run("tar", [...target.extract.args, archivePath, "-C", destination]);
return;
}
if (target.extract.kind === "zip") {
run("powershell.exe", [
"-NoProfile",
"-NonInteractive",
"-Command",
`Expand-Archive -LiteralPath '${escapePowerShellString(
archivePath
)}' -DestinationPath '${escapePowerShellString(destination)}' -Force`,
]);
return;
}
throw new Error(`Unsupported archive extractor for ${targetKey}`);
}
async function downloadArchive(url, destination) {
const response = await fetch(url);
if (!response.ok || !response.body) {
throw new Error(`Failed to download Node runtime from ${url} (${response.status})`);
}
const chunks = [];
for await (const chunk of response.body) {
chunks.push(chunk);
}
fs.writeFileSync(destination, Buffer.concat(chunks));
}
fs.mkdirSync(outputDir, { recursive: true });
const markerValue = JSON.stringify({ version, target: targetKey });
if (
fs.existsSync(outputPath) &&
fs.existsSync(markerPath) &&
fs.readFileSync(markerPath, "utf8") === markerValue
) {
console.log(`[desktop] Bundled Node runtime: ${outputPath}`);
process.exit(0);
}
const cacheDir = path.join(outputDir, ".cache");
const archivePath = path.join(cacheDir, target.archiveName);
const extractDir = path.join(cacheDir, target.extractedDir);
const url = `https://nodejs.org/dist/v${version}/${target.archiveName}`;
fs.rmSync(cacheDir, { recursive: true, force: true });
fs.mkdirSync(cacheDir, { recursive: true });
await downloadArchive(url, archivePath);
extractArchive(archivePath, cacheDir);
fs.copyFileSync(path.join(extractDir, ...target.binaryPath), outputPath);
if (process.platform !== "win32") {
fs.chmodSync(outputPath, 0o755);
}
fs.writeFileSync(markerPath, markerValue);
console.log(`[desktop] Bundled Node runtime: ${outputPath}`);