feat: cli launcher npx kandev (#39)

* feat: cli launcher

* feat: improved debug
This commit is contained in:
Carlos Florêncio
2026-01-18 20:42:03 +00:00
committed by GitHub
parent 16b3fb5b92
commit bc6a5f28d9
29 changed files with 1682 additions and 33 deletions
+57
View File
@@ -0,0 +1,57 @@
import { spawn } from "node:child_process";
import { createProcessSupervisor } from "./process";
export type WebLaunchOptions = {
command: string;
args: string[];
cwd: string;
env: NodeJS.ProcessEnv;
url: string;
supervisor: ReturnType<typeof createProcessSupervisor>;
label: string;
};
export function openBrowser(url: string) {
if (process.env.KANDEV_NO_BROWSER === "1") {
return;
}
const opener =
process.platform === "darwin"
? "open"
: process.platform === "win32"
? "cmd"
: "xdg-open";
const args =
process.platform === "win32"
? ["/c", "start", "", url]
: [url];
try {
const child = spawn(opener, args, { stdio: "ignore", detached: true });
child.unref();
} catch {
// ignore browser launch errors
}
}
export function launchWebApp({
command,
args,
cwd,
env,
url,
supervisor,
label,
}: WebLaunchOptions) {
const proc = spawn(command, args, { cwd, env, stdio: "inherit" });
supervisor.children.push(proc);
proc.on("exit", (code, signal) => {
console.error(`[kandev] ${label} exited (code=${code}, signal=${signal})`);
const exitCode = signal ? 0 : code ?? 1;
void supervisor.shutdown(`${label} exit`).then(() => process.exit(exitCode));
});
openBrowser(url);
return proc;
}