Files
OmniRoute/bin/cli/commands/runtime.mjs
Zius a5e0a96f8a fix: prevent false 'Failed to save connection' error when adding providers (#8912)
Validated in local merge-train (devbox-vm-06-dev002) @ combined-tip (FAST gates green: static + changed tests + vitest — only pre-existing audit.test.ts flake). Evidence: /home/diegosouzapw/dev/proxys/OmniRoute/.claude/worktrees/merge-train-20260805-213228-suite.log
2026-08-05 21:41:33 -03:00

106 lines
3.6 KiB
JavaScript

import { rmSync } from "node:fs";
import { t } from "../i18n.mjs";
import { emit } from "../output.mjs";
import { withSpinner } from "../spinner.mjs";
import {
getRuntimeNodeModules,
hasModule,
isBetterSqliteBinaryValid,
ensureBetterSqliteRuntime,
} from "../runtime/nativeDeps.mjs";
async function confirm(msg) {
const readline = await import("node:readline");
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const answer = await new Promise((r) => rl.question(`${msg} [y/N] `, r));
rl.close();
return /^y(es)?$/i.test(answer);
}
/**
* Shared repair action used by both `omniroute runtime repair` and the
* top-level `omniroute repair` alias. Reinstalls better-sqlite3 into the
* user-writable runtime directory via the existing engine — no hand-rolled
* npm-rebuild spawn. Exits with code 1 on failure.
*/
async function runRepairAction(opts, cmd) {
const globalOpts = cmd.optsWithGlobals();
await withSpinner(
"Repairing native deps",
async () => ensureBetterSqliteRuntime({ silent: true, force: opts.force }),
globalOpts
);
const ok = hasModule("better-sqlite3") && isBetterSqliteBinaryValid();
if (ok) {
process.stdout.write("✓ better-sqlite3 repaired OK\n");
} else {
process.stderr.write("✗ Repair failed\n");
process.stderr.write(
" Possible causes:\n" +
" • npm not available — check that Node.js/npm are on your PATH\n" +
" • npm install scripts are blocked — run: npm install-scripts approve better-sqlite3\n" +
" • Network issue — check your internet connection\n" +
" Try: npm install-scripts ls (to see if better-sqlite3 is blocked)\n"
);
process.exit(1);
}
}
export function registerRuntime(program) {
const runtime = program
.command("runtime")
.description(t("runtime.description") || "Manage native runtime dependencies");
runtime
.command("check")
.description("Check status of native deps in runtime directory")
.action(async (opts, cmd) => {
const globalOpts = cmd.optsWithGlobals();
const status = {
runtimeDir: getRuntimeNodeModules(),
betterSqlite3: {
installed: hasModule("better-sqlite3"),
valid: isBetterSqliteBinaryValid(),
},
};
emit(status, globalOpts);
});
runtime
.command("repair")
.description("Reinstall native deps in runtime directory")
.option("--force", "Force reinstall even if valid")
.action(runRepairAction);
// Top-level discoverability alias: `omniroute repair` invokes the SAME action
// as `omniroute runtime repair` (no duplicated logic). Surfaced in the
// native-error / startup hints so users with a broken better-sqlite3 binding
// have a single self-heal command that works without a C++ toolchain.
program
.command("repair")
.description("Repair native deps (alias for `runtime repair`)")
.option("--force", "Force reinstall even if valid")
.action(runRepairAction);
runtime
.command("clean")
.description("Remove runtime directory (frees disk space)")
.option("--yes", "Skip confirmation")
.action(async (opts) => {
if (!opts.yes) {
const ok = await confirm(`Remove ${getRuntimeNodeModules()}?`);
if (!ok) {
process.stdout.write("Cancelled.\n");
return;
}
}
try {
rmSync(getRuntimeNodeModules(), { recursive: true, force: true });
process.stdout.write("Cleaned runtime directory.\n");
} catch (e) {
process.stderr.write(`Failed: ${e instanceof Error ? e.message : String(e)}\n`);
process.exit(1);
}
});
}