Repository navigation
Expand file tree
/
Copy pathspec.ts
More file actions
69 lines (62 loc) · 2.45 KB
/
Copy pathspec.ts
File metadata and controls
69 lines (62 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { Command } from "effect/cli";
/**
* A declarative command tree. Specs carry only the shape of a command -- its
* name, parameters, description, and children -- so the whole CLI surface can
* be read in one file. Handlers are attached separately by {@link Runtime}, which
* keeps their imports (and the harness they pull in) off the startup path.
*/
export interface Example {
readonly command: string;
readonly description: string;
}
interface Options<
Config extends Command.Command.Config,
Shared extends Command.Command.FlagConfig,
Commands extends ReadonlyArray<Any>,
> {
readonly description?: string;
readonly params?: Config;
/** Flags declared here are also parsed by every descendant command. */
readonly shared?: Shared;
readonly examples?: ReadonlyArray<Example>;
readonly commands?: Commands;
}
export interface Node<
Name extends string,
// The tree is heterogeneous, so a node has to accept any command shape here.
// Concrete types are recovered per-command through `Runtime.Input`.
// biome-ignore lint/suspicious/noExplicitAny: heterogeneous command tree
Spec extends Command.Command<Name, any, any, any, any>,
Commands extends Children,
> {
readonly name: Name;
readonly spec: Spec;
readonly commands: Commands;
}
// The command name is invariant on `Command`, so the erased node type has to
// admit any name for concrete nodes to be assignable to it.
// biome-ignore lint/suspicious/noExplicitAny: heterogeneous command tree
export type Any = Node<string, Command.Command<any, any, any, any, any>, Children>;
export type Children = Readonly<Record<string, Any>>;
type ChildrenOf<Commands extends ReadonlyArray<Any>> = {
readonly [Node in Commands[number] as Node["name"]]: Node;
};
export function make<
const Name extends string,
const Config extends Command.Command.Config = {},
const Shared extends Command.Command.FlagConfig = {},
const Commands extends ReadonlyArray<Any> = [],
>(name: Name, options: Options<Config, Shared, Commands> = {}) {
let spec = Command.make(name, options.params ?? ({} as Config)).pipe(
Command.withSharedFlags(options.shared ?? ({} as Shared)),
);
if (options.description !== undefined) spec = spec.pipe(Command.withDescription(options.description));
if (options.examples !== undefined) spec = spec.pipe(Command.withExamples(options.examples));
return {
name,
spec,
commands: Object.fromEntries(
(options.commands ?? []).map((command) => [command.name, command]),
) as ChildrenOf<Commands>,
};
}