// Node-side entry point for the icon launcher. The .app's shell stub // hands over here as soon as it has recovered a usable PATH. // // Everything user-visible happens on a browser page (launcher-page.mjs); // this file only decides which page to show and when. The order matters: // an already-running server is answered before any prerequisite check, // so clicking the icon while the app is open never stalls behind a // `claude --version` probe. import { execFileSync, spawn } from "node:child_process"; import { appendFileSync, closeSync, mkdirSync, openSync, statSync, truncateSync, writeFileSync } from "node:fs"; import { homedir } from "node:os"; import { dirname, join } from "node:path"; import { findRunningServerPort } from "./detect-server.mjs"; import { fillPlaceholders, launcherMessages, pickLauncherLocale } from "./messages.mjs"; import { renderErrorPage, renderLauncherPage } from "./launcher-page.mjs"; import { browserOpenArgv, launcherPaths, npxCommand } from "./platform.mjs"; import { runPreflight } from "./preflight.mjs"; import { findAvailablePort } from "../port.mjs"; import { LAUNCHED_FROM_ICON, LAUNCHED_FROM_VAR } from "../launch-vars.mjs"; const DEFAULT_PORT = 3001; const LOG_SIZE_CAP_BYTES = 1_000_000; /** macOS keeps per-app logs here, which is also where Console.app looks. */ export function launcherLogPath(home = homedir(), platform = process.platform) { return launcherPaths({ home, platform }).logPath; } function log(logPath, message) { const line = `[${new Date().toISOString()}] ${message}\n`; try { mkdirSync(dirname(logPath), { recursive: true }); capLogSize(logPath); appendFileSync(logPath, line); } catch { // A launcher that dies because it could not write its own log has // failed at the only thing it exists to do. } } function capLogSize(logPath) { try { if (statSync(logPath).size > LOG_SIZE_CAP_BYTES) truncateSync(logPath, 0); } catch { // No log yet — nothing to cap. } } // A GUI launch inherits no LANG, so the system preference is the only // reliable source. Falls back to the env vars for a terminal run. function readAppleLocale() { try { return execFileSync("defaults", ["read", "-g", "AppleLocale"], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }); } catch { return ""; } } // Node resolves this from the OS, so it answers on every platform — // including a Windows GUI launch, which has neither `defaults` nor LANG. function readIntlLocale() { try { return Intl.DateTimeFormat().resolvedOptions().locale; } catch { return ""; } } /** * The OS UI language. macOS is asked through `defaults` because a GUI * launch inherits no LANG and AppleLocale is the only reliable source * there; everywhere else the env vars come first (a terminal run) and * Intl closes the gap. * * Windows has neither source, which is how its pages ended up English * on a Japanese machine while the .vbs no-node dialog spoke Japanese — * the two halves disagreeing again, in a new place. */ export function detectLocale({ env = process.env, run = readAppleLocale, platform = process.platform, intl = readIntlLocale } = {}) { const candidates = platform === "darwin" ? [run(), env.LANG, env.LC_ALL, intl()] : [env.LANG, env.LC_ALL, intl()]; const first = candidates.find((value) => typeof value === "string" && value.trim().length > 0); return pickLauncherLocale(first?.trim().split(".")[0]); } function openInBrowser(target, platform = process.platform) { const { command, args } = browserOpenArgv(target, platform); spawn(command, args, { stdio: "ignore", detached: true }).unref(); } function showPage(html, { tmpDir, name }) { const path = join(tmpDir, name); mkdirSync(tmpDir, { recursive: true }); writeFileSync(path, html); openInBrowser(path); return path; } function showFailure({ failure, messages, locale, logPath, tmpDir }) { const html = renderErrorPage({ messages, failure, logPath, locale }); showPage(html, { tmpDir, name: "mulmoclaude-error.html" }); } // The prerequisite failures carry `{required}` / `{found}`, and the // Claude Code one carries the commands to run. Fold the catalog entry // and the measured values into what the page renders. function toPageFailure(messages, { key, values }) { const entry = messages[key]; return { title: entry.title, body: fillPlaceholders(entry.body, values), action: entry.action, steps: entry.steps, stepsNote: entry.stepsNote, hint: entry.hint, }; } // `npx mulmoclaude@latest` is spawned detached so the server outlives // this process: there is no window to keep open and nothing to keep the // launcher alive for. Clicking the icon again finds the running server // and just opens the browser, so a lingering server is harmless. // // Its output goes straight to the log file by descriptor, NOT through // pipes. Piping would keep the launcher's event loop alive forever // holding the read ends — the process never exits, which is the exact // opposite of detaching — and killing it would then break the server's // stdout mid-write. /** * What to spawn, and from where. * * The working directory is set explicitly because a GUI launch runs with * cwd `/` (measured), and the CLI loads `/.env` to pick up keys like * `GEMINI_API_KEY`. Left alone, an icon user's `.env` could only ever be * `/.env` — a path they cannot write — so the documented way to supply a * key would silently do nothing for exactly the people who never open a * terminal. Home is the one directory that is theirs and always exists. * * @param {{ port: number, home?: string }} options * @returns {{ command: string, args: string[], cwd: string }} */ export function serverSpawnPlan({ port, home = homedir(), platform = process.platform }) { return { // `npx.cmd` on Windows — a batch file, which spawn cannot resolve // from the bare name. The plan owns it so the unit test pins the // command as well as the cwd. command: npxCommand(platform), args: ["mulmoclaude@latest", "--port", String(port), "--no-open"], cwd: home, }; } /** * The environment the server is started with: whatever the launcher was * handed, plus a mark saying this came from the icon. * * The mark is what lets the server tell the user the truth about where a * key goes. A shell `export` cannot reach here — `resolve-path.sh` * harvests PATH from the login shell and deliberately nothing else — so * the advice "or export it before starting" is wrong on this route and * right on the other, and nothing downstream can tell the two apart * without being told (#2626). * * @param {Record} env * @returns {Record} */ export function serverSpawnEnv(env) { return { ...env, [LAUNCHED_FROM_VAR]: LAUNCHED_FROM_ICON }; } function spawnServer({ port, logPath, env }) { mkdirSync(dirname(logPath), { recursive: true }); const logFd = openSync(logPath, "a"); const { command, args, cwd } = serverSpawnPlan({ port }); try { const child = spawn(command, args, { env: serverSpawnEnv(env), cwd, detached: true, stdio: ["ignore", logFd, logFd], }); child.unref(); return child; } finally { closeSync(logFd); } } /** * @param {import("./start.d.mts").StartLauncherOptions} [options] * @returns {Promise<"opened-existing" | "preflight-failed" | "starting" | "no-port">} */ export async function startLauncher({ env = process.env, tmpDir, localeRunner } = {}) { const logPath = launcherLogPath(); const locale = detectLocale({ env, run: localeRunner }); const messages = launcherMessages(locale); const pageDir = tmpDir ?? launcherPaths().pageDir; log(logPath, `launcher start (locale=${locale})`); const runningPort = await findRunningServerPort(DEFAULT_PORT); if (runningPort !== null) { log(logPath, `already running on ${runningPort} — opening browser`); openInBrowser(`http://localhost:${runningPort}/`); return "opened-existing"; } const failure = runPreflight({ env }); if (failure !== null) { log(logPath, `preflight failed: ${failure.key}`); showFailure({ failure: toPageFailure(messages, failure), messages, locale, logPath, tmpDir: pageDir }); return "preflight-failed"; } const port = await findAvailablePort(DEFAULT_PORT); if (port === null) { log(logPath, `no free port from ${DEFAULT_PORT}`); const { noPort } = messages; const body = fillPlaceholders(noPort.body, { port: DEFAULT_PORT }); showFailure({ failure: { ...noPort, body }, messages, locale, logPath, tmpDir: pageDir }); return "no-port"; } log(logPath, `starting server on ${port}`); showPage(renderLauncherPage({ messages, port, logPath, locale }), { tmpDir: pageDir, name: "mulmoclaude-starting.html" }); spawnServer({ port, logPath, env }); return "starting"; }