@sharedots/cli 0.1.0 → 0.1.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/daemon-runner.d.ts +32 -0
- package/dist/daemon-runner.d.ts.map +1 -0
- package/dist/daemon-runner.js +132 -0
- package/dist/daemon-runner.js.map +1 -0
- package/dist/index.d.ts +1 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +114 -156
- package/dist/index.js.map +1 -1
- package/dist/parser.d.ts +5 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/parser.js +81 -0
- package/dist/parser.js.map +1 -0
- package/dist/up-wizard.d.ts +21 -0
- package/dist/up-wizard.d.ts.map +1 -0
- package/dist/up-wizard.js +297 -0
- package/dist/up-wizard.js.map +1 -0
- package/package.json +11 -11
- package/src/cli.test.ts +50 -0
- package/src/daemon-runner.ts +165 -0
- package/src/index.ts +361 -415
- package/src/parser.ts +77 -0
- package/src/up-wizard.ts +338 -0
- package/LICENSE +0 -195
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import {
|
|
3
|
+
closeSync,
|
|
4
|
+
existsSync,
|
|
5
|
+
mkdirSync,
|
|
6
|
+
openSync,
|
|
7
|
+
readFileSync,
|
|
8
|
+
rmSync,
|
|
9
|
+
writeFileSync,
|
|
10
|
+
} from "node:fs";
|
|
11
|
+
import { homedir } from "node:os";
|
|
12
|
+
import { dirname, join, resolve } from "node:path";
|
|
13
|
+
import { fileURLToPath } from "node:url";
|
|
14
|
+
|
|
15
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
16
|
+
|
|
17
|
+
export interface LocalWorkerConfig {
|
|
18
|
+
coordinator: string;
|
|
19
|
+
roomId: string;
|
|
20
|
+
token: string;
|
|
21
|
+
joinedAt: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function getHomeDir(): string {
|
|
25
|
+
const dir = process.env.SHAREDOTS_HOME
|
|
26
|
+
? resolve(process.env.SHAREDOTS_HOME)
|
|
27
|
+
: join(homedir(), ".sharedots");
|
|
28
|
+
mkdirSync(dir, { recursive: true });
|
|
29
|
+
return dir;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function configPath(): string {
|
|
33
|
+
return join(getHomeDir(), "worker-config.json");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function pidPath(): string {
|
|
37
|
+
return join(getHomeDir(), "worker.pid");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function logPath(): string {
|
|
41
|
+
return join(getHomeDir(), "worker.log");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function readConfig(): LocalWorkerConfig | null {
|
|
45
|
+
const p = configPath();
|
|
46
|
+
if (!existsSync(p)) return null;
|
|
47
|
+
try {
|
|
48
|
+
return JSON.parse(readFileSync(p, "utf8")) as LocalWorkerConfig;
|
|
49
|
+
} catch {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function writeConfig(cfg: LocalWorkerConfig): void {
|
|
55
|
+
writeFileSync(configPath(), `${JSON.stringify(cfg, null, 2)}\n`, "utf8");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function resolveDaemonBinary(): string {
|
|
59
|
+
if (process.env.SHAREDOTS_DAEMON_BIN && existsSync(process.env.SHAREDOTS_DAEMON_BIN)) {
|
|
60
|
+
return process.env.SHAREDOTS_DAEMON_BIN;
|
|
61
|
+
}
|
|
62
|
+
const exe = process.platform === "win32" ? "sharedots-daemon.exe" : "sharedots-daemon";
|
|
63
|
+
const repoRoot = resolve(__dirname, "../../..");
|
|
64
|
+
const candidates = [
|
|
65
|
+
join(repoRoot, "target", "release", exe),
|
|
66
|
+
join(repoRoot, "target", "debug", exe),
|
|
67
|
+
join(process.cwd(), "target", "release", exe),
|
|
68
|
+
join(process.cwd(), "target", "debug", exe),
|
|
69
|
+
];
|
|
70
|
+
for (const c of candidates) {
|
|
71
|
+
if (existsSync(c)) return c;
|
|
72
|
+
}
|
|
73
|
+
return exe; // fallback to PATH
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function isProcessRunning(pid: number): boolean {
|
|
77
|
+
try {
|
|
78
|
+
process.kill(pid, 0);
|
|
79
|
+
return true;
|
|
80
|
+
} catch {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface StartDaemonOptions {
|
|
86
|
+
coordinator: string;
|
|
87
|
+
roomId: string;
|
|
88
|
+
token: string;
|
|
89
|
+
cpuLimit?: string | number;
|
|
90
|
+
memoryLimit?: string | number;
|
|
91
|
+
gpu?: boolean;
|
|
92
|
+
foreground?: boolean;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function startDaemonProcess(options: StartDaemonOptions): { pid?: number; foreground: boolean } {
|
|
96
|
+
const bin = resolveDaemonBinary();
|
|
97
|
+
const home = getHomeDir();
|
|
98
|
+
const args = [
|
|
99
|
+
"--coordinator",
|
|
100
|
+
options.coordinator,
|
|
101
|
+
"--room-id",
|
|
102
|
+
options.roomId,
|
|
103
|
+
"--token",
|
|
104
|
+
options.token,
|
|
105
|
+
"--state-dir",
|
|
106
|
+
join(home, "state"),
|
|
107
|
+
"--work-dir",
|
|
108
|
+
join(home, "work"),
|
|
109
|
+
];
|
|
110
|
+
|
|
111
|
+
if (options.cpuLimit !== undefined) {
|
|
112
|
+
args.push("--cpu-limit", String(options.cpuLimit));
|
|
113
|
+
}
|
|
114
|
+
if (options.memoryLimit !== undefined) {
|
|
115
|
+
args.push("--memory-limit", String(options.memoryLimit));
|
|
116
|
+
}
|
|
117
|
+
if (options.gpu === true) {
|
|
118
|
+
args.push("--gpu");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (options.foreground === true) {
|
|
122
|
+
const child = spawn(bin, args, { stdio: "inherit" });
|
|
123
|
+
child.on("exit", (code) => process.exit(code ?? 0));
|
|
124
|
+
return { pid: child.pid, foreground: true };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const logFd = openSync(logPath(), "a");
|
|
128
|
+
const child = spawn(bin, args, {
|
|
129
|
+
detached: true,
|
|
130
|
+
stdio: ["ignore", logFd, logFd],
|
|
131
|
+
});
|
|
132
|
+
child.on("error", (err) => {
|
|
133
|
+
console.error(`Failed to spawn daemon process (${bin}): ${err.message}`);
|
|
134
|
+
});
|
|
135
|
+
child.unref();
|
|
136
|
+
closeSync(logFd);
|
|
137
|
+
|
|
138
|
+
if (child.pid) {
|
|
139
|
+
writeFileSync(pidPath(), String(child.pid), "utf8");
|
|
140
|
+
}
|
|
141
|
+
return { pid: child.pid, foreground: false };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function stopDaemonProcess(): { stopped: boolean; pid?: number } {
|
|
145
|
+
const pPath = pidPath();
|
|
146
|
+
if (!existsSync(pPath)) {
|
|
147
|
+
return { stopped: false };
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
const pid = Number.parseInt(readFileSync(pPath, "utf8").trim(), 10);
|
|
151
|
+
if (Number.isFinite(pid) && isProcessRunning(pid)) {
|
|
152
|
+
try {
|
|
153
|
+
process.kill(pid, "SIGTERM");
|
|
154
|
+
} catch {
|
|
155
|
+
// ignore
|
|
156
|
+
}
|
|
157
|
+
rmSync(pPath, { force: true });
|
|
158
|
+
return { stopped: true, pid };
|
|
159
|
+
}
|
|
160
|
+
} catch {
|
|
161
|
+
// ignore
|
|
162
|
+
}
|
|
163
|
+
rmSync(pPath, { force: true });
|
|
164
|
+
return { stopped: false };
|
|
165
|
+
}
|