@forwardimpact/libcli 0.1.12 → 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.12",
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 {
@@ -180,17 +181,25 @@ export class Cli {
180
181
  }
181
182
 
182
183
  /**
183
- * Create a Cli instance. When `runtime` is provided, error,
184
- * usage-error, and help output route through `runtime.proc` instead of the
185
- * global `process`. The zero-arg form keeps reading the global `process` as a
186
- * deprecated alias for one migration cycle.
184
+ * Create a Cli instance. Error, usage-error, and help output route through the
185
+ * injected `runtime.proc`.
187
186
  * @param {object} definition - The CLI definition.
188
- * @param {object} [options]
189
- * @param {import('@forwardimpact/libutil/runtime').Runtime} [options.runtime]
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.
190
192
  * @returns {Cli}
191
193
  */
192
- export function createCli(definition, { runtime } = {}) {
193
- const proc = runtime ? runtime.proc : process;
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;
194
203
  const helpRenderer = new HelpRenderer({ process: proc });
195
204
  return new Cli(definition, { process: proc, helpRenderer });
196
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";
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
+ }