@minus-ai/dev-vite-plugin 0.1.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cleanup.js +74 -0
- package/dist/index.js +653 -0
- package/package.json +26 -0
- package/src/.minus/session-counter +1 -0
- package/src/cleanup.ts +65 -0
- package/src/index.ts +647 -0
package/src/cleanup.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// 启动前清理同项目残留 dev server 进程
|
|
4
|
+
// Rails 风格:读 .minus/dev.pid → kill 进程组 → 删 PID 文件
|
|
5
|
+
|
|
6
|
+
import { existsSync, readFileSync, writeFileSync, unlinkSync, mkdirSync } from 'node:fs';
|
|
7
|
+
import { join, resolve } from 'node:path';
|
|
8
|
+
|
|
9
|
+
const PID_FILE = '.minus/dev.pid';
|
|
10
|
+
|
|
11
|
+
function killOldProcess(pidFile: string): void {
|
|
12
|
+
if (!existsSync(pidFile)) return;
|
|
13
|
+
|
|
14
|
+
let pid: number;
|
|
15
|
+
try {
|
|
16
|
+
pid = parseInt(readFileSync(pidFile, 'utf8').trim(), 10);
|
|
17
|
+
} catch {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
if (!pid || pid <= 0) return;
|
|
21
|
+
|
|
22
|
+
// 检查进程是否还活着
|
|
23
|
+
try { process.kill(pid, 0); } catch {
|
|
24
|
+
try { unlinkSync(pidFile); } catch { /* ok */ }
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
console.log(`[minus-dev] killing residual dev server (PID ${pid})`);
|
|
29
|
+
try { process.kill(-pid, 'SIGTERM'); } catch {
|
|
30
|
+
try { process.kill(pid, 'SIGTERM'); } catch { /* gone */ }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const deadline = Date.now() + 3000;
|
|
34
|
+
while (Date.now() < deadline) {
|
|
35
|
+
try { process.kill(pid, 0); } catch { break; }
|
|
36
|
+
const wait = Date.now() + 100;
|
|
37
|
+
while (Date.now() < wait) { /* busy wait */ }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// 还活着就 SIGKILL
|
|
41
|
+
try { process.kill(-pid, 'SIGKILL'); } catch {
|
|
42
|
+
try { process.kill(pid, 'SIGKILL'); } catch { /* gone */ }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
try { unlinkSync(pidFile); } catch { /* ok */ }
|
|
46
|
+
console.log('[minus-dev] cleanup done');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function writePid(projectDir: string): void {
|
|
50
|
+
const dir = join(projectDir, '.minus');
|
|
51
|
+
mkdirSync(dir, { recursive: true });
|
|
52
|
+
writeFileSync(join(projectDir, PID_FILE), String(process.pid) + '\n');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function cleanupDev(projectDir?: string): void {
|
|
56
|
+
const dir = resolve(projectDir ?? process.cwd());
|
|
57
|
+
killOldProcess(join(dir, PID_FILE));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// CLI entry: minus-dev-cleanup
|
|
61
|
+
if (process.argv[1]?.endsWith('minus-dev-cleanup') ||
|
|
62
|
+
process.argv[1]?.endsWith('cleanup.ts') ||
|
|
63
|
+
process.argv[1]?.endsWith('cleanup.js')) {
|
|
64
|
+
cleanupDev();
|
|
65
|
+
}
|