@rhythmjs/cli 0.0.14 → 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 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 `provide()` or `register()`, and it can't be served on its own: a `Rhythm` app is always the host that owns the lifecycle.
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
 
@@ -28,10 +28,11 @@ A fuller runnable version, including nested command groups and interactive promp
28
28
  - **`ctx.args`**: captured `:name` command tokens, added once a command matches.
29
29
  - **`ctx.flags`**: parsed `--foo` / `--foo=bar` / `-f` options. Schema-less: a bare `--foo` is boolean `true`, `--foo bar` takes the next token as its value unless the token itself looks like a flag, the same default behavior as `minimist`.
30
30
  - **`ctx.stdin`**: a `ReadableStream`, or `null` when stdin is a TTY (nothing piped in).
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 a `.provide()` on the host `Rhythm` app; see the example.
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
- - **A cli is a controller, not a module**: it has no `provide()` 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())`.
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
 
@@ -41,7 +42,7 @@ A fuller runnable version, including nested command groups and interactive promp
41
42
  - `.middleware()`: this CLI as a plain middleware, for mounting into a `Rhythm` app via `.use()`; the cli's only way onto a runtime. Note: mounting a _cli_ into a _cli_ must use `.use(child)`, not `.use(child.commands())`, because an opaque middleware can't have the parent's prefix applied to its commands.
42
43
  - `.entries`: a read-only snapshot of registered middlewares and commands, in order. `.middleware()` is tagged with the cli as its source, so a parent module lists it in `sources`.
43
44
  - `toCliHandler(app)`: bridges a `Rhythm` app to `(argv: string[]) => Promise<number>`.
44
- - `createPrompt()`: a `{ text, confirm, select, multiSelect }` prompt reading lines through Bun's async-iterable `console`, for use inside a `.provide()` on the host app.
45
+ - `createPrompt()`: a `{ text, confirm, select, multiSelect }` prompt reading lines through Bun's async-iterable `console`, for use with `app.context` on the host app.
45
46
 
46
47
  ## Running on Bun
47
48
 
@@ -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>;
@@ -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);
@@ -44,6 +44,20 @@ function assertValidPattern(path, segments) {
44
44
  throw new TypeError(`command "${path}" has more than one catch-all`);
45
45
  }
46
46
  }
47
+ function mountedCommands(cli, out, seen = new Set) {
48
+ if (seen.has(cli))
49
+ return;
50
+ seen.add(cli);
51
+ for (const entry of cli.entries) {
52
+ if (entry.kind === "command")
53
+ out.push({ segments: [...entry.segments] });
54
+ else {
55
+ const child = sourceOf(entry.fn);
56
+ if (child instanceof RhythmCli)
57
+ mountedCommands(child, out, seen);
58
+ }
59
+ }
60
+ }
47
61
 
48
62
  class RhythmCli {
49
63
  #options;
@@ -57,10 +71,14 @@ class RhythmCli {
57
71
  get #prefixSegments() {
58
72
  return this.#options.prefix ? toSegments(this.#options.prefix) : [];
59
73
  }
60
- use(fn) {
74
+ use(fn, condition) {
61
75
  if (typeof fn !== "function")
62
76
  throw new TypeError("middleware must be a function!");
63
- this.#entries.push({ kind: "middleware", fn });
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 });
64
82
  return this;
65
83
  }
66
84
  command(path, ...handlers) {
@@ -98,8 +116,18 @@ class RhythmCli {
98
116
  const entry = this.#entries[i];
99
117
  if (entry.kind === "middleware") {
100
118
  const { fn } = entry;
101
- const from = groups.length;
102
- stack.push(sourceOf(fn) ? fn : (ctx, next) => reaches(ctx, from) ? fn(ctx, next) : next());
119
+ const source = sourceOf(fn);
120
+ if (source) {
121
+ if (source instanceof RhythmCli) {
122
+ const commands = [];
123
+ mountedCommands(source, commands);
124
+ groups.push(commands);
125
+ }
126
+ stack.push(fn);
127
+ } else {
128
+ const from = groups.length;
129
+ stack.push((ctx, next) => reaches(ctx, from) ? fn(ctx, next) : next());
130
+ }
103
131
  i++;
104
132
  continue;
105
133
  }
package/dist/run.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { Rhythm } from "@rhythmjs/rhythm";
2
2
  import { type RhythmPrompt } from "./prompt";
3
3
  import { type RhythmCliContext } from "./context";
4
- export declare function toCliHandler<TContext extends RhythmCliContext, TProviders extends object = {}>(app: Rhythm<RhythmCliContext, TContext, TProviders>): (argv: string[]) => Promise<number>;
4
+ export declare function toCliHandler<TContext extends RhythmCliContext>(app: Rhythm<RhythmCliContext, any, TContext>): (argv: string[]) => Promise<number>;
5
5
  export declare function createPrompt(): {
6
6
  prompt: RhythmPrompt;
7
7
  close: () => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rhythmjs/cli",
3
- "version": "0.0.14",
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.14"
52
+ "@rhythmjs/rhythm": "0.0.16"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@types/bun": "^1.4.2",