-
Notifications
You must be signed in to change notification settings - Fork 676
Expand file tree
/
Copy pathrun-site-server.mjs
More file actions
296 lines (242 loc) · 6.51 KB
/
Copy pathrun-site-server.mjs
File metadata and controls
296 lines (242 loc) · 6.51 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
import { spawn, spawnSync } from "node:child_process";
import { rm } from "node:fs/promises";
import path from "node:path";
import process from "node:process";
const repoRoot = process.cwd();
const mode = process.argv[2] ?? "dev";
const forwardedArgs = process.argv.slice(3);
const allowedModes = new Set(["dev", "preview"]);
if (!allowedModes.has(mode)) {
throw new Error(
`Expected site server mode to be dev or preview, got ${mode}.`
);
}
const sitePort = String(process.env.CHART_KIT_SITE_PORT ?? "4321");
const forwardedPort =
readArgValue(forwardedArgs, "--port") ?? readArgValue(forwardedArgs, "-p");
const siteHost = String(
readArgValue(forwardedArgs, "--host") ??
readArgValue(forwardedArgs, "-h") ??
process.env.CHART_KIT_SITE_HOST ??
"127.0.0.1"
);
const siteViteCacheDir = String(
process.env.CHART_KIT_SITE_VITE_CACHE_DIR ??
path.join("node_modules", ".vite", "site-server")
);
const cleanupPorts = parsePortList(
process.env.CHART_KIT_SITE_CLEAN_PORTS ?? "4321-4330"
);
const sleep = (milliseconds) =>
new Promise((resolve) => {
setTimeout(resolve, milliseconds);
});
function readArgValue(args, longName, shortName) {
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
const names = [longName, shortName].filter(Boolean);
for (const name of names) {
if (arg === name) {
return args[index + 1];
}
if (arg.startsWith(`${name}=`)) {
return arg.slice(name.length + 1);
}
}
}
return undefined;
}
function stripArgs(args, namesToStrip) {
const stripped = [];
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
const name = [...namesToStrip].find(
(candidate) => arg === candidate || arg.startsWith(`${candidate}=`)
);
if (!name) {
stripped.push(arg);
continue;
}
if (arg === name) {
index += 1;
}
}
return stripped;
}
function parsePortList(value) {
const ports = new Set();
for (const part of value.split(",")) {
const range = part.trim();
if (!range) {
continue;
}
if (range.includes("-")) {
const [rawStart, rawEnd] = range.split("-");
const start = Number(rawStart);
const end = Number(rawEnd);
if (Number.isInteger(start) && Number.isInteger(end)) {
for (let port = start; port <= end; port += 1) {
ports.add(String(port));
}
}
continue;
}
const port = Number(range);
if (Number.isInteger(port)) {
ports.add(String(port));
}
}
ports.add(sitePort);
return [...ports];
}
function pidsForPort(port) {
const result = spawnSync(
"lsof",
["-nP", `-iTCP:${port}`, "-sTCP:LISTEN", "-Fp"],
{
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"]
}
);
if (result.status !== 0) {
return [];
}
return [
...new Set(
result.stdout
.split(/\r?\n/)
.map((line) => line.trim())
.filter((line) => line.startsWith("p"))
.map((line) => Number(line.slice(1)))
.filter((pid) => Number.isInteger(pid) && pid > 0)
)
];
}
function commandForPid(pid) {
const result = spawnSync("ps", ["-p", String(pid), "-o", "command="], {
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"]
});
return result.status === 0 ? result.stdout.trim() : "";
}
function isChartKitSiteProcess(command) {
const normalizedCommand = command.replaceAll("\\", "/");
const normalizedRoot = repoRoot.replaceAll("\\", "/");
return (
!command ||
(normalizedCommand.includes(normalizedRoot) &&
/\b(astro|vite|run-site-server\.mjs)\b/.test(normalizedCommand))
);
}
function isPidAlive(pid) {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
}
async function stopExistingSiteServers() {
const pidPorts = new Map();
for (const port of cleanupPorts) {
for (const pid of pidsForPort(port)) {
if (pid === process.pid) {
continue;
}
const ports = pidPorts.get(pid) ?? [];
ports.push(port);
pidPorts.set(pid, ports);
}
}
const stoppedPids = [];
for (const [pid, ports] of pidPorts) {
const command = commandForPid(pid);
if (!isChartKitSiteProcess(command)) {
console.log(
`Leaving PID ${pid} on port ${ports.join(",")} because it is not this site.`
);
continue;
}
try {
process.kill(pid, "SIGTERM");
stoppedPids.push(pid);
console.log(
`Stopped stale site server PID ${pid} on port ${ports.join(",")}.`
);
} catch (error) {
console.warn(`Unable to stop PID ${pid}: ${error.message}`);
}
}
if (stoppedPids.length === 0) {
return;
}
await sleep(800);
for (const pid of stoppedPids) {
if (!isPidAlive(pid)) {
continue;
}
try {
process.kill(pid, "SIGKILL");
console.log(`Force-stopped stale site server PID ${pid}.`);
} catch {
// The process may have exited between the liveness check and SIGKILL.
}
}
}
async function clearSiteViteCache() {
const cacheDir = path.isAbsolute(siteViteCacheDir)
? siteViteCacheDir
: path.join(repoRoot, "apps", "site", siteViteCacheDir);
await rm(cacheDir, { force: true, recursive: true });
console.log(`Cleared site Vite cache at ${cacheDir}.`);
}
const strippedArgs = stripArgs(
forwardedArgs,
new Set(["--host", "-h", "--port", "-p"])
).filter((arg) => arg !== "--force");
const astroArgs = [
"--host",
siteHost,
"--port",
sitePort,
"--force",
...strippedArgs
];
await stopExistingSiteServers();
await clearSiteViteCache();
if (forwardedPort && String(forwardedPort) !== sitePort) {
console.warn(
`Ignoring requested --port ${forwardedPort}; set CHART_KIT_SITE_PORT=${forwardedPort} to intentionally move the single docs server.`
);
}
console.log(`Starting site ${mode} server on http://${siteHost}:${sitePort}/`);
const child = spawn(
"npm",
["--workspace", "@chart-kit/site", "run", mode, "--", ...astroArgs],
{
cwd: repoRoot,
env: {
...process.env,
CHART_KIT_SITE_VITE_CACHE_DIR: siteViteCacheDir
},
stdio: "inherit"
}
);
const forwardSignal = (signal) => {
if (!child.killed) {
child.kill(signal);
}
};
process.on("SIGINT", () => forwardSignal("SIGINT"));
process.on("SIGTERM", () => forwardSignal("SIGTERM"));
child.on("exit", (code, signal) => {
if (signal) {
const signalExitCodes = {
SIGINT: 130,
SIGTERM: 143
};
process.exit(signalExitCodes[signal] ?? 1);
return;
}
process.exit(code ?? 0);
});