opencode-rag-plugin 2.5.2 → 2.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/index.d.ts +5 -0
- package/dist/cli/index.js +75 -5
- package/package.json +1 -1
package/dist/cli/index.d.ts
CHANGED
|
@@ -27,6 +27,11 @@ export declare function shouldAutoRunCli(moduleUrl: string, argv1?: string): boo
|
|
|
27
27
|
* This is the public API entry point for running the CLI from code
|
|
28
28
|
* (e.g. from tests or the library export).
|
|
29
29
|
*
|
|
30
|
+
* Never terminates the host process: Commander's exits are converted into
|
|
31
|
+
* errors. `--help` and `--version` resolve normally (their output was
|
|
32
|
+
* already printed); every other Commander error — a missing required
|
|
33
|
+
* argument, an unknown option — is rethrown so the caller can react.
|
|
34
|
+
*
|
|
30
35
|
* @param argv - The argument vector to parse (defaults to `process.argv`).
|
|
31
36
|
*/
|
|
32
37
|
export declare function runCli(argv?: string[]): Promise<void>;
|
package/dist/cli/index.js
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* referenced by the `bin` entry in `package.json`. The sibling file
|
|
11
11
|
* `dist/cli.js` is a backwards-compatibility re-export shim.
|
|
12
12
|
*/
|
|
13
|
-
import { Command } from "commander";
|
|
13
|
+
import { Command, CommanderError } from "commander";
|
|
14
14
|
import { realpathSync } from "node:fs";
|
|
15
15
|
import { basename, dirname } from "node:path";
|
|
16
16
|
import { fileURLToPath } from "node:url";
|
|
@@ -29,6 +29,15 @@ program
|
|
|
29
29
|
.name("opencode-rag")
|
|
30
30
|
.description(`Local-first RAG semantic code search v${pkg.version}`)
|
|
31
31
|
.version(pkg.version);
|
|
32
|
+
// Commander's default is to call `process.exit()` for `--help`, `--version`
|
|
33
|
+
// and argument-validation errors. That is fatal for `runCli()` — the
|
|
34
|
+
// programmatic API used by tests and library consumers: a `--help` would
|
|
35
|
+
// terminate the *host* process instead of returning. Override the exit so
|
|
36
|
+
// Commander throws a `CommanderError` that the caller can handle.
|
|
37
|
+
//
|
|
38
|
+
// This MUST be set before the sub-commands are registered: they inherit
|
|
39
|
+
// `_exitCallback` from the parent through `copyInheritedSettings()`.
|
|
40
|
+
program.exitOverride();
|
|
32
41
|
registerIndexCommand(program);
|
|
33
42
|
registerQueryCommand(program);
|
|
34
43
|
registerClearCommand(program);
|
|
@@ -73,8 +82,57 @@ export function shouldAutoRunCli(moduleUrl, argv1) {
|
|
|
73
82
|
return false;
|
|
74
83
|
}
|
|
75
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* Commander error codes that mean "the requested output was printed, exit
|
|
87
|
+
* successfully". `--help` and `--version` are successful outcomes, not
|
|
88
|
+
* failures — they just ask Commander to exit with code 0.
|
|
89
|
+
*/
|
|
90
|
+
const DISPLAY_EXIT_CODES = new Set([
|
|
91
|
+
"commander.helpDisplayed",
|
|
92
|
+
"commander.help",
|
|
93
|
+
"commander.version",
|
|
94
|
+
]);
|
|
95
|
+
/**
|
|
96
|
+
* Check whether a thrown value is Commander's "output was displayed" exit.
|
|
97
|
+
*
|
|
98
|
+
* @param err - The value thrown by `program.parseAsync()`.
|
|
99
|
+
* @returns `true` for `--help` / `--version` style exits.
|
|
100
|
+
*/
|
|
101
|
+
function isDisplayExit(err) {
|
|
102
|
+
const code = err?.code;
|
|
103
|
+
return typeof code === "string" && DISPLAY_EXIT_CODES.has(code);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Run the CLI as a real command-line process.
|
|
107
|
+
*
|
|
108
|
+
* Commander already wrote the error message to stderr before throwing, so
|
|
109
|
+
* the only thing left to do is map the error to an exit code. Setting
|
|
110
|
+
* `process.exitCode` (instead of calling `process.exit()`) lets pending
|
|
111
|
+
* async work drain first and avoids tearing down live handles mid-flight.
|
|
112
|
+
*
|
|
113
|
+
* @param err - The value thrown by `program.parseAsync()`.
|
|
114
|
+
*/
|
|
115
|
+
function handleCliExit(err) {
|
|
116
|
+
if (isDisplayExit(err)) {
|
|
117
|
+
// Commander's own `help()` exits with `process.exitCode || 0` — respect
|
|
118
|
+
// an exit code a command may already have set.
|
|
119
|
+
process.exitCode = process.exitCode || 0;
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (err instanceof CommanderError) {
|
|
123
|
+
process.exitCode = err.exitCode;
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
// Not a Commander control-flow error — rethrow so Node reports it as an
|
|
127
|
+
// unhandled rejection with a visible stack trace, exactly as before.
|
|
128
|
+
throw err;
|
|
129
|
+
}
|
|
130
|
+
/** Parse `process.argv` as a real CLI invocation. */
|
|
131
|
+
function runAsCli() {
|
|
132
|
+
void program.parseAsync(process.argv).catch(handleCliExit);
|
|
133
|
+
}
|
|
76
134
|
if (shouldAutoRunCli(import.meta.url, process.argv[1])) {
|
|
77
|
-
|
|
135
|
+
runAsCli();
|
|
78
136
|
}
|
|
79
137
|
else {
|
|
80
138
|
// Fallback: auto-run only when the script being executed IS the CLI entry
|
|
@@ -85,7 +143,7 @@ else {
|
|
|
85
143
|
const cliScript = basename(modulePath);
|
|
86
144
|
const runningScript = basename(argvRaw);
|
|
87
145
|
if (cliScript === runningScript) {
|
|
88
|
-
|
|
146
|
+
runAsCli();
|
|
89
147
|
}
|
|
90
148
|
else {
|
|
91
149
|
// Backwards-compatibility: when a shim points to the sibling cli.js
|
|
@@ -94,7 +152,7 @@ else {
|
|
|
94
152
|
try {
|
|
95
153
|
const resolvedArgv = realpathSync(argvRaw);
|
|
96
154
|
if (dirname(resolvedArgv) === dirname(dirname(modulePath))) {
|
|
97
|
-
|
|
155
|
+
runAsCli();
|
|
98
156
|
}
|
|
99
157
|
}
|
|
100
158
|
catch {
|
|
@@ -108,9 +166,21 @@ else {
|
|
|
108
166
|
* This is the public API entry point for running the CLI from code
|
|
109
167
|
* (e.g. from tests or the library export).
|
|
110
168
|
*
|
|
169
|
+
* Never terminates the host process: Commander's exits are converted into
|
|
170
|
+
* errors. `--help` and `--version` resolve normally (their output was
|
|
171
|
+
* already printed); every other Commander error — a missing required
|
|
172
|
+
* argument, an unknown option — is rethrown so the caller can react.
|
|
173
|
+
*
|
|
111
174
|
* @param argv - The argument vector to parse (defaults to `process.argv`).
|
|
112
175
|
*/
|
|
113
176
|
export async function runCli(argv = process.argv) {
|
|
114
|
-
|
|
177
|
+
try {
|
|
178
|
+
await program.parseAsync(argv);
|
|
179
|
+
}
|
|
180
|
+
catch (err) {
|
|
181
|
+
if (isDisplayExit(err))
|
|
182
|
+
return;
|
|
183
|
+
throw err;
|
|
184
|
+
}
|
|
115
185
|
}
|
|
116
186
|
//# sourceMappingURL=index.js.map
|