diff --git a/src/cli.ts b/src/cli.ts index 4ddfd83..2eceec0 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -73,28 +73,28 @@ async function printLightningInvoice(invoice: string): Promise { console.log(`${qr}\nInvoice:\n${invoice}`); } +async function installCocodOrExit(): Promise { + logger.log("cocod not found. Installing globally with bun..."); + + const installProc = Bun.spawn(["bun", "install", "--global", "cocod"], { + stdout: "inherit", + stderr: "inherit", + }); + + const installCode = await installProc.exited; + if (installCode !== 0 || !(await isCocodInstalled())) { + logger.error( + "Failed to install cocod. Please run 'bun install --global cocod' manually.", + ); + throw new Error("cocod installation failed"); + } + + logger.log("cocod installed successfully."); +} + async function initDaemon(): Promise { logger.log("Initializing routstrd..."); - if (!(await checkCocodInstalled())) { - logger.log("cocod not found. Installing globally with bun..."); - - const installProc = Bun.spawn(["bun", "install", "--global", "cocod"], { - stdout: "inherit", - stderr: "inherit", - }); - - const installCode = await installProc.exited; - if (installCode !== 0 || !(await checkCocodInstalled())) { - logger.error( - "Failed to install cocod. Please run 'bun install --global cocod' manually.", - ); - return; - } - - logger.log("cocod installed successfully."); - } - // Create config directory if (!existsSync(CONFIG_DIR)) { mkdirSync(CONFIG_DIR, { recursive: true }); @@ -112,7 +112,6 @@ async function initDaemon(): Promise { } const config = await loadConfig(); - const cocodExecutable = resolveCocodExecutable(config.cocodPath); if (!(await isCocodInstalled(config.cocodPath))) { if (config.cocodPath) { @@ -122,24 +121,11 @@ async function initDaemon(): Promise { return; } - logger.log("cocod not found. Installing globally with bun..."); - - const installProc = Bun.spawn(["bun", "install", "--global", "cocod"], { - stdout: "inherit", - stderr: "inherit", - }); - - const installCode = await installProc.exited; - if (installCode !== 0 || !(await isCocodInstalled(config.cocodPath))) { - logger.error( - "Failed to install cocod. Please run 'bun install --global cocod' manually.", - ); - return; - } - - logger.log("cocod installed successfully."); + await installCocodOrExit(); } + const cocodExecutable = resolveCocodExecutable(config.cocodPath); + console.log(`Database will be stored at: ${DB_PATH}`); console.log("\nInitializing cocod..."); @@ -216,19 +202,6 @@ async function initDaemon(): Promise { ); } -async function checkCocodInstalled(): Promise { - try { - const proc = Bun.spawn({ - cmd: ["which", "cocod"], - stdout: "pipe", - }); - const code = await proc.exited; - return code === 0; - } catch { - return false; - } -} - program .name("routstrd") .description("Routstr daemon - Manage routstr processes")