forked from vilmire/adhdev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.mjs
More file actions
185 lines (164 loc) · 4.38 KB
/
Copy pathdev.mjs
File metadata and controls
185 lines (164 loc) · 4.38 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
#!/usr/bin/env node
import fs from 'fs';
import os from 'os';
import path from 'path';
import readline from 'readline';
import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const repoRoot = path.resolve(__dirname, '..');
const pidFile = path.join(repoRoot, '.adhdev-dev-pids.json');
const npmCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm';
const specs = [
{
name: 'daemon',
color: '\x1b[34m',
args: ['run', 'dev:daemon'],
port: 3847,
},
{
name: 'web',
color: '\x1b[32m',
args: ['run', 'dev:web'],
port: 3000,
},
];
const children = new Map();
let shuttingDown = false;
function log(line = '') {
process.stdout.write(`${line}\n`);
}
function label(name, color, text) {
const reset = '\x1b[0m';
return `${color}[${name}]${reset} ${text}`;
}
function writePidFile() {
const payload = {
pid: process.pid,
children: Array.from(children.entries()).map(([name, child]) => ({
name,
pid: child.pid ?? null,
})),
};
fs.writeFileSync(pidFile, JSON.stringify(payload, null, 2));
}
function removePidFile() {
try {
fs.unlinkSync(pidFile);
} catch {}
}
function processExists(pid) {
if (!pid) return false;
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
}
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function terminatePid(pid) {
if (!processExists(pid)) return;
try {
process.kill(pid, 'SIGTERM');
} catch {}
for (let i = 0; i < 20; i += 1) {
if (!processExists(pid)) return;
await delay(100);
}
try {
process.kill(pid, 'SIGKILL');
} catch {}
}
async function cleanupPreviousRun() {
if (!fs.existsSync(pidFile)) return;
try {
const data = JSON.parse(fs.readFileSync(pidFile, 'utf8'));
const pids = Array.isArray(data?.children)
? data.children.map((entry) => entry?.pid).filter(Boolean)
: [];
if (pids.length > 0) {
log('Cleaning up previous dev processes...');
}
for (const pid of pids) {
await terminatePid(pid);
}
} catch (error) {
log(`Warning: failed to clean previous dev processes: ${error instanceof Error ? error.message : String(error)}`);
}
removePidFile();
}
function forwardStream(spec, stream) {
const rl = readline.createInterface({ input: stream });
rl.on('line', (line) => {
log(label(spec.name, spec.color, line));
});
}
function describeFailure(spec, code, signal) {
if (spec.name === 'daemon') {
return `Standalone daemon failed. Port ${spec.port} may already be in use; stop the existing process and retry.`;
}
if (spec.name === 'web') {
return `Vite failed. Port ${spec.port} may already be in use; stop the existing process and retry.`;
}
return `${spec.name} exited with code ${code ?? 'unknown'}${signal ? ` (${signal})` : ''}.`;
}
async function shutdown(exitCode = 0) {
if (shuttingDown) return;
shuttingDown = true;
removePidFile();
const running = Array.from(children.values());
for (const child of running) {
if (child.pid && processExists(child.pid)) {
try {
child.kill('SIGTERM');
} catch {}
}
}
await delay(250);
for (const child of running) {
if (child.pid && processExists(child.pid)) {
try {
child.kill('SIGKILL');
} catch {}
}
}
process.exit(exitCode);
}
async function main() {
await cleanupPreviousRun();
for (const spec of specs) {
const child = spawn(npmCmd, spec.args, {
cwd: repoRoot,
env: process.env,
stdio: ['inherit', 'pipe', 'pipe'],
});
children.set(spec.name, child);
writePidFile();
forwardStream(spec, child.stdout);
forwardStream(spec, child.stderr);
child.on('exit', async (code, signal) => {
if (shuttingDown) return;
log('');
log(describeFailure(spec, code, signal));
await shutdown(code === 0 ? 1 : (code ?? 1));
});
}
log('');
log(`ADHDev dev runner started on ${os.hostname()}.`);
log('Expected ports: web 3000, standalone 3847.');
log('Press Ctrl+C to stop both processes.');
}
process.on('SIGINT', async () => {
await shutdown(0);
});
process.on('SIGTERM', async () => {
await shutdown(0);
});
main().catch(async (error) => {
log(error instanceof Error ? error.stack || error.message : String(error));
await shutdown(1);
});