@forwardimpact/libcli 0.1.11 → 0.1.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forwardimpact/libcli",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "description": "Agent-friendly CLIs — self-documenting entry points that humans and agents reach through the same interface.",
5
5
  "keywords": [
6
6
  "cli",
package/src/cli.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { parseArgs } from "node:util";
2
2
  import { HelpRenderer } from "./help.js";
3
3
  import { freezeInvocationContext } from "./invocation-context.js";
4
+ import { resolveVersion } from "./version.js";
4
5
 
5
6
  /** Command-line interface that parses argv against a definition of commands, options, and help. */
6
7
  export class Cli {
@@ -134,8 +135,8 @@ export class Cli {
134
135
  return null;
135
136
  }
136
137
 
137
- /** Match parsed positionals to a subcommand and invoke its handler with a frozen invocation context. */
138
- dispatch(parsed, { data }) {
138
+ /** Match parsed positionals to a subcommand and invoke its handler with a frozen invocation context. `deps` carries host-injected collaborators (the runtime bag); `data` carries host-loaded domain values. */
139
+ dispatch(parsed, { data, deps } = {}) {
139
140
  const command = this.#findCommand(parsed.positionals);
140
141
  if (!command) {
141
142
  throw new Error(`${this.#definition.name}: no matching subcommand`);
@@ -156,6 +157,7 @@ export class Cli {
156
157
  data,
157
158
  args,
158
159
  options: parsed.values,
160
+ deps,
159
161
  });
160
162
  return command.handler(ctx);
161
163
  }
@@ -178,8 +180,26 @@ export class Cli {
178
180
  }
179
181
  }
180
182
 
181
- /** Create a Cli instance wired to the real process and a default HelpRenderer. */
182
- export function createCli(definition) {
183
- const helpRenderer = new HelpRenderer({ process });
184
- return new Cli(definition, { process, helpRenderer });
183
+ /**
184
+ * Create a Cli instance. Error, usage-error, and help output route through the
185
+ * injected `runtime.proc`.
186
+ * @param {object} definition - The CLI definition.
187
+ * @param {object} options
188
+ * @param {import('@forwardimpact/libutil/runtime').Runtime} options.runtime
189
+ * @param {URL|string} [options.packageJsonUrl] - When provided and the definition
190
+ * carries no explicit `version`, resolve it via {@link resolveVersion} so every
191
+ * bin shares one compile-time version literal. Explicit `definition.version` wins.
192
+ * @returns {Cli}
193
+ */
194
+ export function createCli(definition, { runtime, packageJsonUrl } = {}) {
195
+ if (!runtime) throw new Error("runtime is required");
196
+ if (definition.version === undefined && packageJsonUrl !== undefined) {
197
+ definition = {
198
+ ...definition,
199
+ version: resolveVersion({ packageJsonUrl, runtime }),
200
+ };
201
+ }
202
+ const proc = runtime.proc;
203
+ const helpRenderer = new HelpRenderer({ process: proc });
204
+ return new Cli(definition, { process: proc, helpRenderer });
185
205
  }
package/src/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export { Cli, createCli } from "./cli.js";
2
+ export { resolveVersion } from "./version.js";
2
3
  export { freezeInvocationContext } from "./invocation-context.js";
3
4
  export { HelpRenderer } from "./help.js";
4
5
  export { SummaryRenderer } from "./summary.js";
@@ -35,14 +35,20 @@
35
35
  * empty-valued query parameter), or an array of strings (when the same
36
36
  * key appears more than once). Absent options are not present in the
37
37
  * object — 'foo' in ctx.options is the membership test.
38
+ *
39
+ * @property {Readonly<Object>} deps
40
+ * Host-injected ambient collaborators (the `runtime` bag and typed
41
+ * clients). The handler treats deps as immutable input.
42
+ * Distinct from `data` (host-loaded domain values). Defaults to
43
+ * `undefined` for hosts that do not inject collaborators.
38
44
  */
39
45
 
40
46
  /**
41
47
  * Deep-freeze an invocation context so handlers may assume immutability.
42
- * @param {{ data: Object, args: Object<string,string>, options: Object<string,string|boolean|string[]> }} raw
48
+ * @param {{ data: Object, args: Object<string,string>, options: Object<string,string|boolean|string[]>, deps?: Object }} raw
43
49
  * @returns {InvocationContext}
44
50
  */
45
- export function freezeInvocationContext({ data, args, options }) {
51
+ export function freezeInvocationContext({ data, args, options, deps }) {
46
52
  for (const v of Object.values(options)) {
47
53
  if (Array.isArray(v)) Object.freeze(v);
48
54
  }
@@ -50,5 +56,6 @@ export function freezeInvocationContext({ data, args, options }) {
50
56
  data,
51
57
  args: Object.freeze({ ...args }),
52
58
  options: Object.freeze({ ...options }),
59
+ deps: deps === undefined ? undefined : Object.freeze(deps),
53
60
  });
54
61
  }
package/src/version.js ADDED
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Resolve a CLI's version string. In a `bun build --compile` binary,
3
+ * `process.env.LIBCLI_VERSION` is replaced at build time by `just build-binary`'s
4
+ * `--define`, so this returns the injected literal and the package.json read below
5
+ * is dead code (it never executes — and tree-shakes). In source/npx execution the
6
+ * env var is normally unset and the read supplies the version; setting LIBCLI_VERSION
7
+ * in the environment overrides it (used by bin-smoke integration tests).
8
+ *
9
+ * The read must be written as the literal member expression `process.env.LIBCLI_VERSION`
10
+ * — that is the token `bun build --define` substitutes across the whole bundle,
11
+ * including this bundled library. A dynamic `process.env[name]` would not be replaced.
12
+ *
13
+ * @param {object} args
14
+ * @param {URL|string} args.packageJsonUrl - `new URL("../package.json", import.meta.url)`
15
+ * @param {import('@forwardimpact/libutil/runtime').Runtime} args.runtime
16
+ * @returns {string}
17
+ */
18
+ export function resolveVersion({ packageJsonUrl, runtime }) {
19
+ const injected = process.env.LIBCLI_VERSION; // literal — the --define target
20
+ if (injected) return injected;
21
+ const text = runtime.fsSync.readFileSync(packageJsonUrl, "utf8");
22
+ return JSON.parse(text).version;
23
+ }