@rhythmjs/cli 0.0.15 → 0.0.16
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/README.md +3 -2
- package/dist/rhythm-cli.d.ts +2 -2
- package/dist/rhythm-cli.js +7 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
The command-line layer of Rhythm, the Bun-native backend framework: CLI command routing on top of the `@rhythmjs/rhythm` kernel. `RhythmCli` matches commands against argv positional tokens with a simple linear scan (appropriate for the handful-to-dozens of commands a real CLI has), supports prefixes and nested command groups, and mounts flat into a parent `Rhythm` app via `.use(cli.middleware())`, so an unmatched command correctly falls through to whatever's registered after it.
|
|
4
4
|
|
|
5
|
-
`RhythmCli` is not an app and does not extend `Rhythm`; it is a controller that compiles commands and middleware down to a single middleware (`.middleware()`). It shares the core middleware contract (`compose`, `Middleware`, `derive`; `next()` takes no arguments, extend the context with `derive()`), but has no
|
|
5
|
+
`RhythmCli` is not an app and does not extend `Rhythm`; it is a controller that compiles commands and middleware down to a single middleware (`.middleware()`). It shares the core middleware contract (`compose`, `Middleware`, `derive`; `next()` takes no arguments, extend the context with `derive()`), but has no startup `context` or `register()`, and it can't be served on its own: a `Rhythm` app is always the host that owns the lifecycle.
|
|
6
6
|
|
|
7
7
|
## Example
|
|
8
8
|
|
|
@@ -31,7 +31,8 @@ A fuller runnable version, including nested command groups and interactive promp
|
|
|
31
31
|
- **Interactive prompts**: `createPrompt()` gives `text()`/`confirm()`/`select()`/`multiSelect()` (the latter two support an `allowCustom` option that adds a "type your own" choice). Wire it in via `app.context.prompt = ...` on the host `Rhythm` app; see the example.
|
|
32
32
|
- **Prefixes compose across nesting**: a child cli mounted into a prefixed parent via `.use(child)` gets the parent's prefix segments joined onto every one of its commands, at any nesting depth. Mounting copies the child's commands and middleware at that moment; commands added to the child afterwards don't appear in the parent, and the child keeps working standalone.
|
|
33
33
|
- **Registration order is execution order**: a `.use()` middleware wraps only the commands registered after it, and runs only when one of them matches the argv (so a guard never answers `help` or another cli's commands); a mounted cli (`.use(child.middleware())`) always runs; commands registered before it are untouched, and a matched command that doesn't call `next()` returns without reaching anything registered later. An unmatched command falls through, entry by entry, to the outer `next()`.
|
|
34
|
-
- **
|
|
34
|
+
- **Conditional middleware**: `.use(fn, condition)` also needs `condition(ctx)` to return true (sync or async); the second callback only narrows, so a middleware registered after every command still never runs.
|
|
35
|
+
- **A cli is a controller, not a module**: it has no startup `context` or `register()`, and it cannot be `register()`ed into a `Rhythm` app either; `register()` composes `Rhythm` modules only. A cli mounts into an app exactly one way: koa-style, via `.use(cli.middleware())`.
|
|
35
36
|
|
|
36
37
|
## API
|
|
37
38
|
|
package/dist/rhythm-cli.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DeriveMiddleware, Middleware } from "@rhythmjs/rhythm/types";
|
|
1
|
+
import type { Condition, DeriveMiddleware, Middleware } from "@rhythmjs/rhythm/types";
|
|
2
2
|
import type { RhythmCliContext } from "./context";
|
|
3
3
|
export interface RhythmCliCommandContext {
|
|
4
4
|
readonly args: Readonly<Record<string, string>>;
|
|
@@ -19,7 +19,7 @@ export declare class RhythmCli<TContext extends RhythmCliContext = RhythmCliCont
|
|
|
19
19
|
constructor(options?: RhythmCliOptions);
|
|
20
20
|
get entries(): readonly CliEntry[];
|
|
21
21
|
use<TExtra extends object>(fn: DeriveMiddleware<TContext, TExtra>): RhythmCli<TContext & TExtra, TInput>;
|
|
22
|
-
use(fn: Middleware<TContext>): this;
|
|
22
|
+
use(fn: Middleware<TContext>, condition?: Condition<TContext>): this;
|
|
23
23
|
command<TExtra extends object>(path: string, middleware: DeriveMiddleware<TContext & RhythmCliCommandContext, TExtra>, ...handlers: Middleware<TContext & RhythmCliCommandContext & TExtra>[]): this;
|
|
24
24
|
command(path: string, ...handlers: Middleware<TContext & RhythmCliCommandContext>[]): this;
|
|
25
25
|
middleware(): Middleware<TInput>;
|
package/dist/rhythm-cli.js
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
} from "./rhythm-cli-qdf1cds9.js";
|
|
5
5
|
|
|
6
6
|
// src/rhythm-cli.ts
|
|
7
|
-
import { compose } from "@rhythmjs/rhythm/compose";
|
|
7
|
+
import { compose, gate } from "@rhythmjs/rhythm/compose";
|
|
8
8
|
import { sourceOf, withSource } from "@rhythmjs/rhythm/source";
|
|
9
9
|
function toSegments(command) {
|
|
10
10
|
return command.trim().split(/\s+/).filter(Boolean);
|
|
@@ -71,10 +71,14 @@ class RhythmCli {
|
|
|
71
71
|
get #prefixSegments() {
|
|
72
72
|
return this.#options.prefix ? toSegments(this.#options.prefix) : [];
|
|
73
73
|
}
|
|
74
|
-
use(fn) {
|
|
74
|
+
use(fn, condition) {
|
|
75
75
|
if (typeof fn !== "function")
|
|
76
76
|
throw new TypeError("middleware must be a function!");
|
|
77
|
-
|
|
77
|
+
if (condition !== undefined && typeof condition !== "function")
|
|
78
|
+
throw new TypeError("condition must be a function!");
|
|
79
|
+
const source = sourceOf(fn);
|
|
80
|
+
const wrapped = condition ? gate(fn, condition) : fn;
|
|
81
|
+
this.#entries.push({ kind: "middleware", fn: condition && source ? withSource(wrapped, source) : wrapped });
|
|
78
82
|
return this;
|
|
79
83
|
}
|
|
80
84
|
command(path, ...handlers) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rhythmjs/cli",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.16",
|
|
4
4
|
"description": "Command-line argument parsing, prompts, and command routing for the Rhythm middleware kernel, running on Bun.",
|
|
5
5
|
"homepage": "https://rhythm.js.org/cli",
|
|
6
6
|
"license": "ISC",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"access": "public"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@rhythmjs/rhythm": "0.0.
|
|
52
|
+
"@rhythmjs/rhythm": "0.0.16"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@types/bun": "^1.4.2",
|