fix: daemon spawn uses CLI entry path instead of command module
The runDaemon function was using import.meta.url (pointing to start.js) as the script for the spawned child process. This meant the child ran `node start.js start` which has no CLI entry logic and exits immediately. Added cliEntryScript() that resolves to the correct CLI entry (cli.js) regardless of whether the code is bundled or split into separate files. Closes #27
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { createWriteStream } from "node:fs";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { mkdir } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { basename, dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
import { parseNerveConfig } from "@uncaged/nerve-core";
|
||||
@@ -85,6 +85,22 @@ async function runForeground(nerveRoot: string): Promise<void> {
|
||||
await kernel.ready;
|
||||
}
|
||||
|
||||
/** Path to the CLI entry script (for spawning `start` without `-d`). */
|
||||
function cliEntryScript(): string {
|
||||
const argv1 = process.argv[1];
|
||||
if (argv1) {
|
||||
const base = basename(argv1);
|
||||
if (base === "cli.js" || base === "cli.ts") return argv1;
|
||||
}
|
||||
const here = fileURLToPath(import.meta.url);
|
||||
const base = basename(here);
|
||||
if (base === "cli.js" || base === "cli.ts") return here;
|
||||
if (base === "start.js" || base === "start.ts") {
|
||||
return join(dirname(here), "..", base === "start.ts" ? "cli.ts" : "cli.js");
|
||||
}
|
||||
return here;
|
||||
}
|
||||
|
||||
async function runDaemon(nerveRoot: string): Promise<void> {
|
||||
if (isRunning()) {
|
||||
const pid = readPidFile();
|
||||
@@ -108,9 +124,9 @@ async function runDaemon(nerveRoot: string): Promise<void> {
|
||||
else resolve();
|
||||
});
|
||||
|
||||
const selfPath = fileURLToPath(import.meta.url);
|
||||
const cliPath = cliEntryScript();
|
||||
|
||||
const child = spawn(process.execPath, [selfPath, "start"], {
|
||||
const child = spawn(process.execPath, [cliPath, "start"], {
|
||||
detached: true,
|
||||
stdio: ["ignore", logStream.fd, logStream.fd],
|
||||
env: { ...process.env, NERVE_DAEMON_MODE: "1" },
|
||||
|
||||
Reference in New Issue
Block a user