Files
OmniRoute/tests/unit/cli-version-fastpath.test.ts
Diego Rodrigues de Sa e Souza 54f31f79d8 fix(cli): fast-path --version to skip full CLI bootstrap
`omniroute --version` ran the entire CLI bootstrap before printing the
version: the tsx/esm + polyfill imports, env-file loading, and
Commander's ~70-command registration (importing DB, providers, OAuth,
and other heavy modules). That took ~1.5s just to print a version
string.

Add isVersionFastPath() (bin/cli/utils/versionFastPath.mjs) and check it
at the very top of bin/omniroute.mjs, before any of that work runs. It
only trips for an unambiguous bare `--version`/`-V` invocation (no other
args), so it never changes behavior for real commands or for `--help`
(whose output is generated dynamically from every registered
subcommand, so it still needs full registration and is deliberately not
fast-pathed).

`--version` now returns in ~0.3s instead of ~1.5s locally.

Co-authored-by: Sutarto Jordan Chrisfivo <Jordannst@users.noreply.github.com>
Inspired-by: https://github.com/decolua/9router/pull/2414
2026-07-14 23:09:44 -03:00

57 lines
2.4 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { isVersionFastPath } from "../../bin/cli/utils/versionFastPath.mjs";
const execFileAsync = promisify(execFile);
// argv shape is [node, script, ...args]
const argv = (...args: string[]) => ["node", "omniroute", ...args];
test("fast-path selector: bare --version/-V select the fast path", () => {
assert.equal(isVersionFastPath(argv("--version")), true);
assert.equal(isVersionFastPath(argv("-V")), true);
});
test("fast-path selector: --help does NOT select the fast path (help text is dynamic)", () => {
assert.equal(isVersionFastPath(argv("--help")), false);
assert.equal(isVersionFastPath(argv("-h")), false);
});
test("fast-path selector: extra args or a subcommand alongside --version fall through", () => {
assert.equal(isVersionFastPath(argv("serve", "--version")), false);
assert.equal(isVersionFastPath(argv("--version", "extra")), false);
assert.equal(isVersionFastPath(argv("--lang", "en", "--version")), false);
});
test("fast-path selector: no args or a real command do not select the fast path", () => {
assert.equal(isVersionFastPath(argv()), false);
assert.equal(isVersionFastPath(argv("serve")), false);
});
test("fast-path selector: defensive on non-array input", () => {
// @ts-expect-error intentional bad input
assert.equal(isVersionFastPath(undefined), false);
});
test("omniroute CLI --version fast-path prints ONLY the version, skipping bootstrap output", async () => {
const pkg = JSON.parse(
readFileSync(join(process.cwd(), "package.json"), "utf8")
) as { version: string };
const { stdout } = await execFileAsync(process.execPath, ["bin/omniroute.mjs", "--version"], {
cwd: process.cwd(),
env: { ...process.env, DATA_DIR: "" },
});
// Before the fast-path, env-file loading (loadEnvFile) runs ahead of Commander and
// prints "Loaded env from ..." lines interleaved with the version — proving the full
// bootstrap (tsx/esm polyfill, env loading, ~70-command Commander registration) ran
// for a plain --version query. The fast-path must short-circuit before any of that,
// so stdout is EXACTLY the version string and nothing else.
assert.equal(stdout.trim(), pkg.version);
});