dev-server-manager.ts
8.44 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
import * as http from 'http';
import * as net from 'net';
import { spawn, execSync, type ChildProcess } from 'child_process';
import { buildDevServerUrl, DEFAULT_PORT } from '../../../nodes/validation/constants';
import { logger } from '../../../utils/logger';
export interface DevServerHandle {
child: ChildProcess;
port: number;
url: string;
/**
* Last tail of stdout/stderr for diagnosing startup/runtime issues.
*/
outputTail: () => string;
}
function terminateChildProcess(child: ChildProcess): void {
// Windows 上 dev server 以 `npm → vite` 的孙进程方式运行,child.kill() 只杀到
// npm 外壳,杀不到 vite 本体,导致 dev server 泄漏、持续锁住项目目录(典型表现:
// 下一次生成在 initial 阶段写 package.json 时抛 EPERM)。因此统一用 taskkill /f /t
// 杀掉整个进程树(含孙进程)。POSIX 下按进程组 SIGKILL。
try {
const pid = child.pid;
if (!pid) {
child.kill();
return;
}
if (process.platform === 'win32') {
execSync(`taskkill /pid ${pid} /f /t`, { stdio: 'ignore' });
} else {
try {
process.kill(-pid, 'SIGKILL');
} catch {
child.kill('SIGKILL');
}
}
} catch {
// ignore
}
}
function normalizeExecutable(executable: string): string {
if (process.platform !== 'win32') {
return executable;
}
const lower = executable.toLowerCase();
if (lower === 'npm' || lower === 'pnpm' || lower === 'yarn' || lower === 'npx') {
return `${lower}.cmd`;
}
return executable;
}
function parseCommandWithPortSupport(commandStr: string): { executable: string; args: string[]; needsDoubleDash: boolean } {
const parts = commandStr.trim().split(/\s+/);
const executable = parts[0];
if (!executable) {
throw new Error('Failed to parse command: empty command string');
}
const args = parts.slice(1);
const needsDoubleDash = ['npm', 'pnpm', 'yarn'].includes(executable);
return { executable, args, needsDoubleDash };
}
async function isPortAvailable(port: number): Promise<boolean> {
return await new Promise(resolve => {
const server = net.createServer();
server.unref();
server.on('error', () => resolve(false));
server.listen(port, () => {
server.close(() => resolve(true));
});
});
}
/**
* Calculate deterministic port from workspace path hash.
* Same workspace always gets the same port for consistency.
*
* @param workspacePath - Absolute path to workspace root
* @param basePort - Base port for calculation (default: 5200, avoids common 5173-5180)
* @returns Hash-based port (basePort to basePort + 999)
*/
function calculateHashPort(workspacePath: string, basePort: number = 5200): number {
// Simple hash function - same input always produces same output
let hash = 0;
for (let i = 0; i < workspacePath.length; i++) {
hash = (hash << 5) - hash + workspacePath.charCodeAt(i);
hash = hash & hash; // Convert to 32bit integer
}
// Map to port range (basePort to basePort + 999)
const offset = Math.abs(hash) % 1000;
return basePort + offset;
}
/**
* Get available port using hash-based selection strategy.
* Tries hash-based port first, then nearby ports (±10 range).
*
* @param workspacePath - Absolute path to workspace (used for port calculation)
* @param _startPort - Unused (kept for API compatibility)
* @returns Available port number
* @throws Error if no ports available in ±10 range around hash-based port
*/
export async function getFreeHashPort(workspacePath: string, _startPort: number = DEFAULT_PORT): Promise<number> {
// Calculate this workspace's hash-based port
const preferredPort = calculateHashPort(workspacePath);
// Try preferred port first
if (await isPortAvailable(preferredPort)) {
logger.printInfoLog(`Using workspace-preferred port: ${preferredPort}`);
return preferredPort;
}
// If preferred port is taken, try nearby ports (±10 range)
// This handles edge case where same workspace runs twice simultaneously
logger.printInfoLog(`Preferred port ${preferredPort} occupied, trying nearby ports...`);
for (let offset = 1; offset <= 10; offset++) {
const portUp = preferredPort + offset;
const portDown = preferredPort - offset;
if (await isPortAvailable(portUp)) {
logger.printInfoLog(`Using nearby port: ${portUp} (preferred was ${preferredPort})`);
return portUp;
}
if (portDown > 1024 && (await isPortAvailable(portDown))) {
logger.printInfoLog(`Using nearby port: ${portDown} (preferred was ${preferredPort})`);
return portDown;
}
}
// All ports in ±10 range are occupied
throw new Error(`No available ports found in workspace-preferred range (${preferredPort - 10} to ${preferredPort + 10})`);
}
async function waitForServerReady(url: string, timeoutMs: number): Promise<boolean> {
const start = Date.now();
const intervalMs = 400;
while (Date.now() - start < timeoutMs) {
const ok = await new Promise<boolean>(resolve => {
const req = http.get(url, res => {
res.resume();
resolve(res.statusCode !== undefined && res.statusCode >= 200 && res.statusCode < 500);
});
req.on('error', () => resolve(false));
req.setTimeout(500, () => req.destroy());
});
if (ok) return true;
await new Promise(r => setTimeout(r, intervalMs));
}
return false;
}
export class DevServerManager {
private handle: DevServerHandle | null = null;
constructor(private readonly params: { appPath: string; runCommand: string }) {}
get current(): DevServerHandle | null {
return this.handle;
}
async start(options: { timeoutMs: number } = { timeoutMs: 60_000 }): Promise<DevServerHandle> {
if (this.handle) {
return this.handle;
}
const port = await getFreeHashPort(this.params.appPath);
const url = buildDevServerUrl(port);
const { executable, args, needsDoubleDash } = parseCommandWithPortSupport(this.params.runCommand);
const normalizedExecutable = normalizeExecutable(executable);
const commandArgs = needsDoubleDash ? [...args, '--', '--port', String(port)] : [...args, '--port', String(port)];
logger.printInfoLog(`Starting dev server: ${normalizedExecutable} ${commandArgs.join(' ')}`);
const child = spawn(normalizedExecutable, commandArgs, {
cwd: this.params.appPath,
stdio: ['ignore', 'pipe', 'pipe'],
env: {
...process.env,
PORT: String(port),
FORCE_COLOR: '1',
},
});
const maxBytes = 1_000_000;
let out = '';
const push = (chunk: Buffer) => {
out += chunk.toString('utf-8');
if (out.length > maxBytes) {
out = out.slice(out.length - maxBytes);
}
};
child.stdout?.on('data', push);
child.stderr?.on('data', push);
// If our process exits, try to stop the child to avoid orphans.
process.once('exit', () => {
terminateChildProcess(child);
});
const ready = await waitForServerReady(url, options.timeoutMs);
if (!ready) {
const tail = out.trim();
terminateChildProcess(child);
throw new Error(`Dev server did not become ready at ${url} within ${options.timeoutMs}ms.\n${tail}`);
}
this.handle = {
child,
port,
url,
outputTail: () => out.trim(),
};
logger.printSuccessLog(`Dev server ready at ${url}`);
return this.handle;
}
async stop(): Promise<void> {
if (!this.handle) return;
const child = this.handle.child;
this.handle = null;
await new Promise<void>(resolve => {
try {
child.once('close', () => resolve());
terminateChildProcess(child);
} catch {
resolve();
}
setTimeout(() => resolve(), 1500);
});
}
async restart(options: { timeoutMs: number } = { timeoutMs: 60_000 }): Promise<DevServerHandle> {
await this.stop();
return await this.start(options);
}
}