@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 +1 -1
- package/src/cli.js +17 -8
- package/src/index.js +1 -0
- package/src/version.js +23 -0
package/package.json
CHANGED
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.
|
|
184
|
-
*
|
|
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}
|
|
189
|
-
* @param {import('@forwardimpact/libutil/runtime').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
|
-
|
|
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
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
|
+
}
|