-
-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathdev-server.mjs
More file actions
138 lines (113 loc) · 3.15 KB
/
Copy pathdev-server.mjs
File metadata and controls
138 lines (113 loc) · 3.15 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
import { spawn } from "node:child_process";
import { createRequire } from "node:module";
import { readdirSync, statSync, watch } from "node:fs";
import { join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const scriptPath = fileURLToPath(import.meta.url);
export const repoRoot = resolve(fileURLToPath(new URL("..", import.meta.url)));
const require = createRequire(import.meta.url);
const tsxCliPath = require.resolve("tsx/cli");
const watchRoots = ["src"].map((entry) => join(repoRoot, entry));
const restartDelayMs = 750;
const crashDelayMs = 1500;
let child;
let restartTimer;
let stoppingForRestart = false;
let shuttingDown = false;
function log(message) {
console.error(`[devspace:dev] ${message}`);
}
export function createServerCommand() {
return {
command: process.execPath,
args: [tsxCliPath, "src/cli.ts", "serve"],
};
}
function start() {
stoppingForRestart = false;
const { command, args } = createServerCommand();
child = spawn(command, args, {
cwd: repoRoot,
env: process.env,
stdio: "inherit",
});
child.on("exit", (code, signal) => {
child = undefined;
if (shuttingDown) return;
if (stoppingForRestart) return;
log(`server exited (${signal ?? code ?? "unknown"}); restarting in ${crashDelayMs}ms`);
scheduleRestart(crashDelayMs);
});
}
function scheduleRestart(delayMs = restartDelayMs) {
clearTimeout(restartTimer);
restartTimer = setTimeout(restart, delayMs);
}
function restart() {
if (shuttingDown) return;
clearTimeout(restartTimer);
if (!child) {
start();
return;
}
stoppingForRestart = true;
child.once("exit", () => {
if (!shuttingDown) start();
});
child.kill("SIGTERM");
setTimeout(() => {
if (child && stoppingForRestart) child.kill("SIGKILL");
}, 3000).unref();
}
function watchDirectory(root) {
const watchers = [];
const seen = new Set();
function addDirectory(dir) {
if (seen.has(dir)) return;
seen.add(dir);
const watcher = watch(dir, (event, filename) => {
if (!filename) {
scheduleRestart();
return;
}
const path = join(dir, filename.toString());
if (event === "rename") maybeAddDirectory(path);
scheduleRestart();
});
watchers.push(watcher);
for (const entry of readdirSync(dir)) {
maybeAddDirectory(join(dir, entry));
}
}
function maybeAddDirectory(path) {
try {
const stats = statSync(path);
if (stats.isDirectory()) addDirectory(path);
} catch {
// The file may have been deleted between the watch event and stat call.
}
}
addDirectory(root);
return watchers;
}
function shutdown() {
shuttingDown = true;
clearTimeout(restartTimer);
if (!child) return process.exit(0);
child.once("exit", () => process.exit(0));
child.kill("SIGTERM");
setTimeout(() => process.exit(1), 3000).unref();
}
function main() {
for (const signal of ["SIGINT", "SIGTERM"]) {
process.on(signal, shutdown);
}
for (const root of watchRoots) {
watchDirectory(root);
}
log("watching src; server restarts on changes and after crashes");
start();
}
if (resolve(process.argv[1] ?? "") === scriptPath) {
main();
}