fix(cli): share builtin argument ownership with MCP routing

This commit is contained in:
morluto
2026-10-24 02:30:51 +08:00
parent 254439750a
commit cd128795eb
6 changed files with 170 additions and 118 deletions
+1
View File
@@ -24,6 +24,7 @@ export const bundleRuntime = async (root) => {
{
cli: "cli.js",
cliOutput: "cliOutput.js",
cliBuiltinArguments: "cliBuiltinArguments.js",
cliResultOutput: "cli/cliResultOutput.js",
mcpDoctor: "mcpDoctor.js",
},
+1
View File
@@ -49,6 +49,7 @@ if (await pathExists(join(root, "src"))) {
"dist/mcp/main.js",
"dist/cli-runtime/cli.js",
"dist/cli-runtime/cliOutput.js",
"dist/cli-runtime/cliBuiltinArguments.js",
"dist/cli-runtime/cliResultOutput.js",
"dist/cli-runtime/mcpDoctor.js",
"dist/mcpDoctor.js",
+13 -7
View File
@@ -12,9 +12,15 @@ const { default: packageJson } = await import("../package.json", {
with: { type: "json" },
});
process.env.REA_PACKAGE_VERSION = packageJson.version;
const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const builtinArgumentRuntime = "dist/cli-runtime/cliBuiltinArguments.js";
const builtinArguments = (await compiledRuntimeExists([builtinArgumentRuntime]))
? await import("../dist/cli-runtime/cliBuiltinArguments.js")
: undefined;
const doctorArguments = builtinArguments?.productionMcpDoctorArguments(args);
const isMcpEntry = args[0] === "--mcp" || args[0] === "mcp";
const isMcpMode = isMcpEntry && args.length === 1;
const isMcpDoctorMode = args[0] === "mcp" && args[1] === "doctor";
const isMcpDoctorMode = doctorArguments !== undefined;
// Only flags may follow the entry: `mcp add --http` stays Incur's `mcp add`,
// while `mcp -h --http` reaches the HTTP entry's own help.
const isMcpHttpMode =
@@ -25,8 +31,6 @@ const isMcpHttpMode =
const SIGPIPE_EXIT_STATUS = 141;
// Set once the reader of CLI output has closed the pipe.
let stdoutClosed = false;
const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
// Windows home lookup throws for an empty USERPROFILE. Dependencies resolve
// home during import, before REA's own resolver can apply its account fallback.
if (process.platform === "win32" && process.env.USERPROFILE === "") {
@@ -47,8 +51,9 @@ if (
process.env.PLAYWRIGHT_BROWSERS_PATH = join(cacheRoot, "ms-playwright");
}
const runtimeFiles =
isMcpMode || isMcpHttpMode
const runtimeFiles = [
builtinArgumentRuntime,
...(isMcpMode || isMcpHttpMode
? ["dist/mcp/main.js"]
: isMcpDoctorMode
? ["dist/mcp/main.js", "dist/cli-runtime/mcpDoctor.js"]
@@ -56,7 +61,8 @@ const runtimeFiles =
"dist/cli-runtime/cli.js",
"dist/cli-runtime/cliOutput.js",
"dist/cli-runtime/cliResultOutput.js",
];
]),
];
if (!(await compiledRuntimeExists(runtimeFiles))) {
process.stderr.write(
@@ -73,7 +79,7 @@ if (!(await compiledRuntimeExists(runtimeFiles))) {
guardClosedStdout();
const { runProductionMcpDoctorCli } =
await import("../dist/cli-runtime/mcpDoctor.js");
const result = await runProductionMcpDoctorCli(args.slice(2), {
const result = await runProductionMcpDoctorCli(doctorArguments, {
dispatcherPath: fileURLToPath(import.meta.url),
packageRoot,
stdoutIsTerminal: process.stdout.isTTY === true,