@pithy-sh/core 0.9.0 → 0.10.0
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/dist/asyncapi/document.d.ts +46 -0
- package/dist/asyncapi/document.d.ts.map +1 -0
- package/dist/asyncapi/document.js +115 -0
- package/dist/capability/capability.d.ts +7 -0
- package/dist/capability/capability.d.ts.map +1 -1
- package/dist/capability/composition.d.ts.map +1 -1
- package/dist/capability/composition.js +5 -1
- package/dist/capability/settings.d.ts +10 -2
- package/dist/capability/settings.d.ts.map +1 -1
- package/dist/capability/settings.js +5 -3
- package/dist/cli/command.d.ts +364 -0
- package/dist/cli/command.d.ts.map +1 -0
- package/dist/cli/command.js +418 -0
- package/dist/cli/commandCatalog.generated.d.ts +2275 -0
- package/dist/cli/commandCatalog.generated.d.ts.map +1 -0
- package/dist/cli/commandCatalog.generated.js +2390 -0
- package/dist/cli/commandFacts.d.ts +164 -0
- package/dist/cli/commandFacts.d.ts.map +1 -0
- package/dist/cli/commandFacts.js +215 -0
- package/dist/controlPlane/error/errors.d.ts.map +1 -1
- package/dist/controlPlane/error/errors.js +4 -3
- package/dist/controlPlane/http/guard.d.ts.map +1 -1
- package/dist/controlPlane/http/guard.js +11 -1
- package/dist/controlPlane/http/routes.d.ts +1 -1
- package/dist/controlPlane/http/routes.d.ts.map +1 -1
- package/dist/controlPlane/http/routes.js +34 -7
- package/dist/createBackend.d.ts +7 -0
- package/dist/createBackend.d.ts.map +1 -1
- package/dist/createBackend.js +34 -4
- package/dist/entitlement/require.js +1 -1
- package/dist/error/cause.d.ts +16 -116
- package/dist/error/cause.d.ts.map +1 -1
- package/dist/error/cause.js +19 -153
- package/dist/error/http.d.ts +40 -0
- package/dist/error/http.d.ts.map +1 -1
- package/dist/error/payload.d.ts +80 -0
- package/dist/error/payload.d.ts.map +1 -1
- package/dist/error/payload.js +26 -0
- package/dist/error/pithyError.d.ts +44 -0
- package/dist/error/pithyError.d.ts.map +1 -1
- package/dist/error/pithyError.js +56 -1
- package/dist/error/unresolved.d.ts +265 -0
- package/dist/error/unresolved.d.ts.map +1 -0
- package/dist/error/unresolved.js +599 -0
- package/dist/error/wire.d.ts +20 -0
- package/dist/error/wire.d.ts.map +1 -0
- package/dist/error/wire.js +19 -0
- package/dist/http/oidcWebhook.d.ts +3 -3
- package/dist/http/oidcWebhook.js +3 -3
- package/dist/http/routeContract.js +1 -1
- package/dist/http/routeSpec.d.ts +66 -0
- package/dist/http/routeSpec.d.ts.map +1 -0
- package/dist/http/routeSpec.js +40 -0
- package/dist/http/sameOrigin.d.ts +1 -1
- package/dist/http/sameOrigin.js +1 -1
- package/dist/http/signedWebhook.d.ts +3 -3
- package/dist/http/signedWebhook.js +3 -3
- package/dist/http/socket.d.ts +52 -0
- package/dist/http/socket.d.ts.map +1 -0
- package/dist/http/socket.js +2 -0
- package/dist/http/subprotocol.d.ts +29 -0
- package/dist/http/subprotocol.d.ts.map +1 -0
- package/dist/http/subprotocol.js +37 -0
- package/dist/http/validation.d.ts +40 -8
- package/dist/http/validation.d.ts.map +1 -1
- package/dist/http/validation.js +25 -1
- package/dist/index.js +2 -2
- package/dist/logger/record.d.ts +23 -0
- package/dist/logger/record.d.ts.map +1 -1
- package/dist/migrations/bookkeeping.d.ts +21 -0
- package/dist/migrations/bookkeeping.d.ts.map +1 -1
- package/dist/migrations/bookkeeping.js +32 -3
- package/dist/migrations/groups.d.ts +140 -0
- package/dist/migrations/groups.d.ts.map +1 -0
- package/dist/migrations/groups.js +191 -0
- package/dist/migrations/migrateStep.d.ts +13 -0
- package/dist/migrations/migrateStep.d.ts.map +1 -0
- package/dist/migrations/migrateStep.js +20 -0
- package/dist/migrations/owner.d.ts +7 -0
- package/dist/migrations/owner.d.ts.map +1 -1
- package/dist/migrations/owner.js +11 -7
- package/dist/migrations/retained.js +2 -2
- package/dist/migrations/runner.d.ts +76 -5
- package/dist/migrations/runner.d.ts.map +1 -1
- package/dist/migrations/runner.js +130 -19
- package/dist/naming/domains.d.ts +17 -0
- package/dist/naming/domains.d.ts.map +1 -1
- package/dist/naming/domains.js +62 -4
- package/dist/naming/environment.d.ts.map +1 -1
- package/dist/naming/environment.js +2 -1
- package/dist/naming/packageName.d.ts +17 -0
- package/dist/naming/packageName.d.ts.map +1 -0
- package/dist/naming/packageName.js +22 -0
- package/dist/naming/provisionScope.d.ts.map +1 -1
- package/dist/naming/provisionScope.js +2 -1
- package/dist/openapi/document.d.ts +106 -0
- package/dist/openapi/document.d.ts.map +1 -0
- package/dist/openapi/document.js +314 -0
- package/dist/openapi/routeGroups.d.ts +8 -0
- package/dist/openapi/routeGroups.d.ts.map +1 -0
- package/dist/openapi/routeGroups.js +23 -0
- package/dist/text/comments.js +66 -4
- package/dist/text/literals.d.ts +43 -0
- package/dist/text/literals.d.ts.map +1 -0
- package/dist/text/literals.js +293 -0
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.d.ts.map +1 -1
- package/dist/version.generated.js +1 -1
- package/dist/workflow/dispatchRoute.d.ts.map +1 -1
- package/dist/workflow/dispatchRoute.js +12 -6
- package/dist/workflow/faults.d.ts.map +1 -1
- package/dist/workflow/faults.js +2 -3
- package/dist/workflow/hostEnv.d.ts +1 -0
- package/dist/workflow/hostEnv.d.ts.map +1 -1
- package/dist/workflow/hostEnv.js +4 -2
- package/dist/workflow/loopback.d.ts.map +1 -1
- package/dist/workflow/loopback.js +2 -1
- package/package.json +3 -3
- package/src/asyncapi/document.ts +164 -0
- package/src/capability/capability.ts +7 -0
- package/src/capability/composition.ts +2 -1
- package/src/capability/settings.ts +19 -2
- package/src/cli/command.ts +865 -0
- package/src/cli/commandCatalog.generated.ts +2533 -0
- package/src/cli/commandFacts.ts +219 -0
- package/src/controlPlane/error/errors.ts +10 -3
- package/src/controlPlane/http/guard.ts +14 -2
- package/src/controlPlane/http/routes.ts +57 -9
- package/src/createBackend.ts +35 -2
- package/src/entitlement/require.ts +1 -1
- package/src/error/cause.ts +18 -181
- package/src/error/payload.ts +59 -0
- package/src/error/pithyError.ts +76 -0
- package/src/error/unresolved.ts +853 -0
- package/src/error/wire.ts +40 -0
- package/src/http/oidcWebhook.ts +3 -3
- package/src/http/routeContract.ts +1 -1
- package/src/http/routeSpec.ts +81 -0
- package/src/http/sameOrigin.ts +1 -1
- package/src/http/signedWebhook.ts +3 -3
- package/src/http/socket.ts +55 -0
- package/src/http/subprotocol.ts +47 -0
- package/src/http/validation.ts +50 -8
- package/src/migrations/bookkeeping.ts +39 -2
- package/src/migrations/groups.ts +295 -0
- package/src/migrations/migrateStep.ts +19 -0
- package/src/migrations/owner.ts +10 -6
- package/src/migrations/retained.ts +2 -2
- package/src/migrations/runner.ts +214 -22
- package/src/naming/domains.ts +75 -16
- package/src/naming/environment.ts +2 -1
- package/src/naming/packageName.ts +20 -0
- package/src/naming/provisionScope.ts +2 -1
- package/src/openapi/document.ts +412 -0
- package/src/openapi/routeGroups.ts +25 -0
- package/src/text/comments.ts +56 -4
- package/src/text/literals.ts +294 -0
- package/src/version.generated.ts +1 -1
- package/src/workflow/dispatchRoute.ts +13 -6
- package/src/workflow/faults.ts +5 -3
- package/src/workflow/hostEnv.ts +9 -1
- package/src/workflow/loopback.ts +2 -1
|
@@ -0,0 +1,865 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { InternalError } from "../error/pithyError";
|
|
5
|
+
import type { DEFAULT_ENVIRONMENTS, LOCAL_ENVIRONMENT } from "../naming/environment";
|
|
6
|
+
import { COMMANDS, GLOBAL_FLAGS, GROUPS } from "./commandCatalog.generated";
|
|
7
|
+
import {
|
|
8
|
+
CONFIRM_FLAGS,
|
|
9
|
+
CONFIRMATIONS,
|
|
10
|
+
CREDENTIAL_FLAGS,
|
|
11
|
+
DESTRUCTIVE_COMMANDS,
|
|
12
|
+
DESTRUCTIVE_FLAGS,
|
|
13
|
+
type DestructiveFlag,
|
|
14
|
+
type DestructivePath,
|
|
15
|
+
EVERY_ENVIRONMENT_ALWAYS,
|
|
16
|
+
EVERY_ENVIRONMENT_WITHOUT_ENV,
|
|
17
|
+
MEANING_FLAGS,
|
|
18
|
+
MEANING_ONLY_WITH,
|
|
19
|
+
NO_WORKER_MODES,
|
|
20
|
+
PRODUCTION_CONFIRMS,
|
|
21
|
+
PRODUCTION_ENVIRONMENTS,
|
|
22
|
+
type ProductionPath,
|
|
23
|
+
REQUIRED_BESIDE,
|
|
24
|
+
WORKER_SCOPED,
|
|
25
|
+
type WorkerScopedPath,
|
|
26
|
+
} from "./commandFacts";
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* **The one place a `pithy` command line is spelled (#711).**
|
|
30
|
+
*
|
|
31
|
+
* #697 held every `fix` to one rule: it runs as written. The sentences beside it were held to nothing, and three
|
|
32
|
+
* review rounds each found new ones that did not run — a `--worker` dropped, an `--env` left to default to `dev`,
|
|
33
|
+
* a placeholder an agent pasted verbatim, a command that destroyed far more than the fault it was offered for.
|
|
34
|
+
* Every one was a command spelled by hand at its own site, so every fix was one site, and the next site was
|
|
35
|
+
* still spelled by hand.
|
|
36
|
+
*
|
|
37
|
+
* So no site spells one. A line that names a command gets it from here, and this module knows the tree
|
|
38
|
+
* (`commandCatalog.generated.ts`, projected from the CLI's own parser) and the facts the parser cannot state
|
|
39
|
+
* (`commandFacts.ts`). It refuses to build:
|
|
40
|
+
*
|
|
41
|
+
* - **a command that does not exist**, or a flag the command does not declare — at compile time for a literal
|
|
42
|
+
* path, at run time for one assembled from a capability's name;
|
|
43
|
+
* - **a command missing a flag it needs**: one the parser requires, one a rule over several flags requires, an
|
|
44
|
+
* `--env` whose default would change what the command acts on, or the `--worker` a command acting on one
|
|
45
|
+
* Worker is refused without — and `--worker` takes a {@link WorkerDir}, the Worker's `apps/` directory, never
|
|
46
|
+
* its deployed script name;
|
|
47
|
+
* - **a command that destroys something**, or a production provision. Those are a person's decision, and
|
|
48
|
+
* {@link personDecision} says so in a sentence that carries no command an agent would run.
|
|
49
|
+
*
|
|
50
|
+
* ## A builder reached at run time never throws because of data
|
|
51
|
+
*
|
|
52
|
+
* Every refusal above is about what **the program spells** — a command path, a flag name, an enum choice, the
|
|
53
|
+
* shape of the line — and a value is one of two kinds:
|
|
54
|
+
*
|
|
55
|
+
* - **Spelled by the program**: the path, each flag's name, a choice from a flag's fixed set, a Worker directory
|
|
56
|
+
* (a {@link WorkerDir}, which only {@link workerDirOf} makes). These are checked, at compile time where the path
|
|
57
|
+
* is a literal.
|
|
58
|
+
* - **Supplied by a caller**: a `--set` value, a secret's name, free text, anything a run read. These are
|
|
59
|
+
* shell-quoted as they are and never checked — `fromName=Acme <hi@acme.io>` is a value, not a placeholder, and a
|
|
60
|
+
* builder that read it as one turned `pithy add email` into `core/internal` after it had installed the package
|
|
61
|
+
* (#711). A `<placeholder>` is a fault of **spelling**, so the place that refuses one is the gate that reads
|
|
62
|
+
* spellings, `packages/cli/src/ci/runnableCommands.test.ts`, which reads every literal handed to a builder too.
|
|
63
|
+
*
|
|
64
|
+
* So whether a line is a person's decision never turns on a value at run time either. An environment a run reads —
|
|
65
|
+
* a deployed Worker's `ENVIRONMENT`, a caller's `--env` — is data: {@link pithyCommand} takes no `--env` on a
|
|
66
|
+
* command a production run makes a person's decision except a literal non-production one, {@link pithyCommandOf}
|
|
67
|
+
* takes none there at all, and a line that names one it read is {@link remedy}'s or {@link rerunRemedy}'s, which
|
|
68
|
+
* say a production one as a person's decision. {@link personDecision} takes only a command or flag that destroys
|
|
69
|
+
* something, which is shape. What is left to throw is a program that spelled a line wrong, every time it runs.
|
|
70
|
+
*
|
|
71
|
+
* `packages/cli/src/ci/runnableCommands.test.ts` reads every string in every package's shipped source and fails
|
|
72
|
+
* on a `pithy …` spelled anywhere but here, wherever it sits in the sentence.
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
type Catalog = typeof COMMANDS;
|
|
76
|
+
|
|
77
|
+
/** The CLI itself: the first word of every line built here, and what a line names when it means no command. */
|
|
78
|
+
const CLI = "pithy";
|
|
79
|
+
|
|
80
|
+
/** A command the CLI runs, as typed after `pithy`. */
|
|
81
|
+
export type CommandPath = keyof Catalog;
|
|
82
|
+
|
|
83
|
+
/** A group, which only lists its subcommands. A sentence may name one in prose. */
|
|
84
|
+
export type GroupPath = (typeof GROUPS)[number];
|
|
85
|
+
|
|
86
|
+
declare const WORKER_DIR: unique symbol;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* **A Worker as `--worker` takes it: its `apps/` directory.** Branded, so the deployed script name — a `string`
|
|
90
|
+
* every Worker also carries — cannot be passed where this is wanted. Made by {@link workerDirOf} from the
|
|
91
|
+
* Worker's directory.
|
|
92
|
+
*/
|
|
93
|
+
export type WorkerDir = string & { readonly [WORKER_DIR]: true };
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The Worker a directory holds, as `--worker` names it: the directory's own name. Takes the directory rather
|
|
97
|
+
* than a name, because a name in hand is as often the deployed script name, which `--worker` does not resolve.
|
|
98
|
+
*/
|
|
99
|
+
export function workerDirOf(worker: { readonly dir: string }): WorkerDir {
|
|
100
|
+
const name = worker.dir
|
|
101
|
+
.split(/[\\/]/)
|
|
102
|
+
.filter((part) => part.length > 0)
|
|
103
|
+
.at(-1);
|
|
104
|
+
if (name === undefined) throw new InternalError({ message: "A Worker directory has a name.", detail: worker.dir });
|
|
105
|
+
return name as WorkerDir;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
declare const BUILT: unique symbol;
|
|
109
|
+
|
|
110
|
+
/** A command line this module built, and so one that runs as written. The only thing a `fix` may carry. */
|
|
111
|
+
export type Command = string & { readonly [BUILT]: true };
|
|
112
|
+
|
|
113
|
+
type FlagDefs<P extends CommandPath> = Catalog[P]["flags"];
|
|
114
|
+
type FlagName<P extends CommandPath> = Extract<keyof FlagDefs<P>, string>;
|
|
115
|
+
type ConfirmFlag = (typeof CONFIRM_FLAGS)[number];
|
|
116
|
+
type MeaningFlag = (typeof MEANING_FLAGS)[number];
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* An environment no project's production run is: the local one, and the deployed one that is not `prod`. The only
|
|
120
|
+
* `--env` a literal line may give a command whose production run is a person's decision — so it is spelled, never
|
|
121
|
+
* read, and an environment a run read goes through {@link remedy}, which decides by its value.
|
|
122
|
+
*/
|
|
123
|
+
export type QuietEnvironment =
|
|
124
|
+
| typeof LOCAL_ENVIRONMENT
|
|
125
|
+
| Exclude<(typeof DEFAULT_ENVIRONMENTS)[number], (typeof PRODUCTION_ENVIRONMENTS)[number]>;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The value one flag takes: `true` for a boolean, a {@link WorkerDir} for `--worker`, one of a fixed set's choices,
|
|
129
|
+
* a {@link QuietEnvironment} for the `--env` of a command a production run makes a person's decision, else a string.
|
|
130
|
+
*/
|
|
131
|
+
type FlagValue<P, D, F> = F extends "--worker"
|
|
132
|
+
? WorkerDir
|
|
133
|
+
: D extends { type: "boolean" }
|
|
134
|
+
? true
|
|
135
|
+
: F extends "--env"
|
|
136
|
+
? P extends ProductionPath
|
|
137
|
+
? QuietEnvironment
|
|
138
|
+
: string
|
|
139
|
+
: D extends { enum: readonly (infer E)[] }
|
|
140
|
+
? D extends { repeatable: true }
|
|
141
|
+
? E | readonly E[]
|
|
142
|
+
: E
|
|
143
|
+
: D extends { repeatable: true }
|
|
144
|
+
? string | readonly string[]
|
|
145
|
+
: string;
|
|
146
|
+
|
|
147
|
+
/** Whether a line naming `P` must carry `F`. `add`'s Worker is its own builder's to name (`addCommand.ts`). */
|
|
148
|
+
type IsNeeded<P extends CommandPath, F extends FlagName<P>> = FlagDefs<P>[F] extends { required: true }
|
|
149
|
+
? true
|
|
150
|
+
: F extends MeaningFlag
|
|
151
|
+
? FlagDefs<P>[F] extends { default: string }
|
|
152
|
+
? P extends keyof typeof MEANING_ONLY_WITH
|
|
153
|
+
? false
|
|
154
|
+
: true
|
|
155
|
+
: false
|
|
156
|
+
: F extends "--worker"
|
|
157
|
+
? P extends Exclude<WorkerScopedPath, "add">
|
|
158
|
+
? true
|
|
159
|
+
: false
|
|
160
|
+
: false;
|
|
161
|
+
|
|
162
|
+
type NeededFlag<P extends CommandPath> = { [F in FlagName<P>]: IsNeeded<P, F> extends true ? F : never }[FlagName<P>];
|
|
163
|
+
|
|
164
|
+
/** The flags a line naming `P` passes: the ones it needs, and any other it declares that destroys nothing. */
|
|
165
|
+
export type CommandFlags<P extends CommandPath> = { [F in NeededFlag<P>]: FlagValue<P, FlagDefs<P>[F], F> } & {
|
|
166
|
+
[F in Exclude<FlagName<P>, NeededFlag<P> | DestructiveFlag | ConfirmFlag>]?: FlagValue<P, FlagDefs<P>[F], F>;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
type HasRequiredPositional<P extends CommandPath> = [
|
|
170
|
+
Extract<Catalog[P]["positionals"][number], { required: true }>,
|
|
171
|
+
] extends [never]
|
|
172
|
+
? false
|
|
173
|
+
: true;
|
|
174
|
+
|
|
175
|
+
/** What a line naming `P` passes: its positional arguments, and its flags, in the order they are written. */
|
|
176
|
+
export type CommandParts<P extends CommandPath> = (HasRequiredPositional<P> extends true
|
|
177
|
+
? { args: readonly [string, ...string[]] }
|
|
178
|
+
: { args?: readonly string[] }) &
|
|
179
|
+
([NeededFlag<P>] extends [never] ? { flags?: CommandFlags<P> } : { flags: CommandFlags<P> });
|
|
180
|
+
|
|
181
|
+
type PartsArgument<P extends CommandPath> =
|
|
182
|
+
HasRequiredPositional<P> extends true
|
|
183
|
+
? [parts: CommandParts<P>]
|
|
184
|
+
: [NeededFlag<P>] extends [never]
|
|
185
|
+
? [parts?: CommandParts<P>]
|
|
186
|
+
: [parts: CommandParts<P>];
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* A command that is built like any other: one that destroys nothing, and that does not act on every environment on
|
|
190
|
+
* every run — a line names that one through {@link remedy}, which says it as a person's decision.
|
|
191
|
+
*/
|
|
192
|
+
export type BuildablePath = Exclude<CommandPath, DestructivePath | (typeof EVERY_ENVIRONMENT_ALWAYS)[number]>;
|
|
193
|
+
|
|
194
|
+
/** Parts as a builder holds them when the path is a value rather than a literal. */
|
|
195
|
+
export interface LooseParts {
|
|
196
|
+
readonly args?: readonly string[];
|
|
197
|
+
readonly flags?: Readonly<Record<string, string | true | readonly string[] | undefined>>;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** What {@link commandFaults} is asked to allow. */
|
|
201
|
+
export interface FaultOptions {
|
|
202
|
+
/**
|
|
203
|
+
* A person's decision: a destructive command or flag, or a production run, is named for a person to choose,
|
|
204
|
+
* and the confirmation flags are theirs to add. {@link personDecision} asks for this; nothing else does.
|
|
205
|
+
*/
|
|
206
|
+
readonly person?: boolean;
|
|
207
|
+
/** The project's `seed.productionEnvironments`, beyond the {@link PRODUCTION_ENVIRONMENTS} every project has. */
|
|
208
|
+
readonly productionEnvironments?: readonly string[];
|
|
209
|
+
/**
|
|
210
|
+
* A line said by a **deployed** Worker — into its own log, or to an HTTP caller — which cannot know the `apps/`
|
|
211
|
+
* directory that provisioned it. Every rule holds but `--worker`. Only the files
|
|
212
|
+
* `packages/cli/src/ci/runnableCommands.test.ts` lists as runtime lines may pass it.
|
|
213
|
+
*/
|
|
214
|
+
readonly runtime?: boolean;
|
|
215
|
+
/**
|
|
216
|
+
* **Every value on the line was spelled by the program** — the gate, reading a line written in a literal — so a
|
|
217
|
+
* `<placeholder>` or a blank among them is a fault. No builder says this: a value handed to a builder is a
|
|
218
|
+
* caller's, and is shell-quoted as it is (see the module comment).
|
|
219
|
+
*/
|
|
220
|
+
readonly spelled?: boolean;
|
|
221
|
+
/** Treat `<x>`, the gate's mark for interpolation, as a value rather than a placeholder. */
|
|
222
|
+
readonly interpolated?: boolean;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* What a builder adds to {@link FaultOptions}: that every value is the caller's own run, handed back as it was
|
|
227
|
+
* given, so not even a fixed set of choices is held against it — the run's own parser has had its say.
|
|
228
|
+
*/
|
|
229
|
+
interface BuildOptions extends FaultOptions {
|
|
230
|
+
readonly given?: boolean;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Whether `path` is a command the CLI runs. */
|
|
234
|
+
export function isCommandPath(path: string): path is CommandPath {
|
|
235
|
+
return Object.hasOwn(COMMANDS, path);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Whether `path` is a group. */
|
|
239
|
+
export function isGroupPath(path: string): path is GroupPath {
|
|
240
|
+
return (GROUPS as readonly string[]).includes(path);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* What the catalog says about one flag of one command — its type, whether it is required, its default — or
|
|
245
|
+
* `undefined` when the command does not declare it.
|
|
246
|
+
*/
|
|
247
|
+
export function commandFlag(
|
|
248
|
+
path: string,
|
|
249
|
+
flag: string,
|
|
250
|
+
): { readonly type: "boolean" | "string"; readonly required: boolean; readonly default?: string } | undefined {
|
|
251
|
+
if (!isCommandPath(path)) return undefined;
|
|
252
|
+
return (COMMANDS[path] as unknown as CommandDef).flags[flag];
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** A flag's catalog entry, read loosely. */
|
|
256
|
+
interface FlagDef {
|
|
257
|
+
readonly type: "boolean" | "string";
|
|
258
|
+
readonly required: boolean;
|
|
259
|
+
readonly repeatable: boolean;
|
|
260
|
+
readonly default?: string;
|
|
261
|
+
readonly enum?: readonly string[];
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** A rule over several flags, read loosely. */
|
|
265
|
+
interface GroupDef {
|
|
266
|
+
readonly kind: "exactlyOne" | "atMostOne" | "allOrNone" | "requires";
|
|
267
|
+
readonly flags?: readonly string[];
|
|
268
|
+
readonly flag?: string;
|
|
269
|
+
readonly needs?: readonly string[];
|
|
270
|
+
readonly any?: boolean;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** One command's catalog entry, read loosely. */
|
|
274
|
+
interface CommandDef {
|
|
275
|
+
readonly positionals: readonly { readonly name: string; readonly required: boolean; readonly variadic: boolean }[];
|
|
276
|
+
readonly flags: Readonly<Record<string, FlagDef>>;
|
|
277
|
+
readonly groups: readonly GroupDef[];
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** A value nobody can run: a `<placeholder>`, or nothing at all. */
|
|
281
|
+
function isPlaceholder(value: string, options: FaultOptions): boolean {
|
|
282
|
+
if (value.trim().length === 0) return true;
|
|
283
|
+
const marks = value.match(/<[^>]*>/g) ?? [];
|
|
284
|
+
return marks.some((mark) => !(options.interpolated === true && mark === "<x>"));
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** Whether the environment a line names is a production one. */
|
|
288
|
+
function isProduction(env: string, options: FaultOptions): boolean {
|
|
289
|
+
const name = env.trim().toLowerCase();
|
|
290
|
+
return [...PRODUCTION_ENVIRONMENTS, ...(options.productionEnvironments ?? [])].some(
|
|
291
|
+
(one) => one.trim().toLowerCase() === name,
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* **Every reason a command line would not run as written, or would do more than a remedy may.** Empty when it
|
|
297
|
+
* runs. The builder throws on any; `runnableCommands.test.ts` holds every hand-spelled line it reads to the same
|
|
298
|
+
* function, so the gate and the builder cannot disagree about a rule.
|
|
299
|
+
*/
|
|
300
|
+
export function commandFaults(path: string, parts: LooseParts = {}, options: BuildOptions = {}): string[] {
|
|
301
|
+
const shown = `${CLI} ${path}`;
|
|
302
|
+
if (!isCommandPath(path)) {
|
|
303
|
+
return [isGroupPath(path) ? `\`${shown}\` is a group, which runs nothing.` : `\`${shown}\` is not a command.`];
|
|
304
|
+
}
|
|
305
|
+
const def = COMMANDS[path] as unknown as CommandDef;
|
|
306
|
+
const faults: string[] = [];
|
|
307
|
+
const args = parts.args ?? [];
|
|
308
|
+
const flags = Object.entries(parts.flags ?? {}).filter(
|
|
309
|
+
(entry): entry is [string, string | true | readonly string[]] => entry[1] !== undefined,
|
|
310
|
+
);
|
|
311
|
+
const named = new Set(flags.map(([flag]) => flag));
|
|
312
|
+
|
|
313
|
+
const required = def.positionals.filter((one) => one.required).length;
|
|
314
|
+
const variadic = def.positionals.some((one) => one.variadic);
|
|
315
|
+
if (args.length < required) faults.push(`\`${shown}\` needs its ${def.positionals[args.length]?.name} argument.`);
|
|
316
|
+
if (!variadic && args.length > def.positionals.length)
|
|
317
|
+
faults.push(`\`${shown}\` takes ${def.positionals.length} arguments.`);
|
|
318
|
+
// A value is checked only where the program spelled it; a caller's is quoted as it is.
|
|
319
|
+
for (const arg of options.spelled === true ? args : []) {
|
|
320
|
+
if (isPlaceholder(arg, options)) faults.push(`\`${shown}\` is given "${arg}", which nobody can run.`);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const global = new Set<string>(GLOBAL_FLAGS);
|
|
324
|
+
for (const [flag, value] of flags) {
|
|
325
|
+
const flagDef = def.flags[flag];
|
|
326
|
+
if (flagDef === undefined) {
|
|
327
|
+
if (!global.has(flag)) faults.push(`\`${shown}\` has no ${flag}.`);
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
const values = typeof value === "string" ? [value] : value === true ? [] : [...value];
|
|
331
|
+
if (flagDef.type === "boolean" && value !== true) faults.push(`\`${shown} ${flag}\` takes no value.`);
|
|
332
|
+
if (flagDef.type === "string" && (value === true || values.length === 0)) {
|
|
333
|
+
faults.push(`\`${shown} ${flag}\` needs a value.`);
|
|
334
|
+
}
|
|
335
|
+
if (values.length > 1 && !flagDef.repeatable) faults.push(`\`${shown} ${flag}\` is passed once.`);
|
|
336
|
+
for (const one of values) {
|
|
337
|
+
if (options.spelled === true && isPlaceholder(one, options)) {
|
|
338
|
+
faults.push(`\`${shown} ${flag}\` is given "${one}", which nobody can run.`);
|
|
339
|
+
} else if (
|
|
340
|
+
options.given !== true &&
|
|
341
|
+
flagDef.enum !== undefined &&
|
|
342
|
+
!flagDef.enum.includes(one) &&
|
|
343
|
+
!(options.interpolated && one === "<x>")
|
|
344
|
+
) {
|
|
345
|
+
faults.push(`\`${shown} ${flag}\` takes ${flagDef.enum.join(", ")}, not ${one}.`);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const confirms = new Set<string>(CONFIRM_FLAGS);
|
|
351
|
+
for (const flag of named) {
|
|
352
|
+
if (confirms.has(flag)) faults.push(`\`${shown} ${flag}\` carries a confirmation only a person gives.`);
|
|
353
|
+
if ((CREDENTIAL_FLAGS as readonly string[]).includes(flag)) {
|
|
354
|
+
faults.push(`\`${shown} ${flag}\` carries a credential, which no line repeats.`);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
for (const [flag, flagDef] of Object.entries(def.flags)) {
|
|
358
|
+
if (named.has(flag)) continue;
|
|
359
|
+
if (flagDef.required && !(options.person === true && confirms.has(flag))) {
|
|
360
|
+
faults.push(`\`${shown}\` is refused without ${flag}.`);
|
|
361
|
+
}
|
|
362
|
+
const onlyWith = (MEANING_ONLY_WITH as Readonly<Record<string, string>>)[path];
|
|
363
|
+
const meaningful = onlyWith === undefined || named.has(onlyWith);
|
|
364
|
+
if ((MEANING_FLAGS as readonly string[]).includes(flag) && flagDef.default !== undefined && meaningful) {
|
|
365
|
+
faults.push(`\`${shown}\` without ${flag} acts on ${flagDef.default}; a line names the one it is about.`);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
for (const group of def.groups) faults.push(...groupFaults(shown, group, named));
|
|
369
|
+
for (const rule of REQUIRED_BESIDE) {
|
|
370
|
+
if (rule.path === path && named.has(rule.with) && !named.has(rule.unless) && !named.has(rule.needs)) {
|
|
371
|
+
faults.push(`\`${shown} ${rule.with}\` is refused without ${rule.needs}.`);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const noWorker = (NO_WORKER_MODES[path] ?? []).some((flag) => named.has(flag));
|
|
376
|
+
if ((WORKER_SCOPED as readonly string[]).includes(path) && !noWorker && !named.has("--worker") && !options.runtime) {
|
|
377
|
+
faults.push(`\`${shown}\` acts on one Worker and names none.`);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
if (options.person !== true) {
|
|
381
|
+
for (const consequence of consequencesOf(path, parts, options))
|
|
382
|
+
faults.push(`\`${shown}\` ${consequence}: a person's decision.`);
|
|
383
|
+
}
|
|
384
|
+
return faults;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* **What a command does that only a person may decide**: what it deletes, or that it runs against production. Empty
|
|
389
|
+
* for a command a remedy may name.
|
|
390
|
+
*/
|
|
391
|
+
export function consequencesOf(path: string, parts: LooseParts = {}, options: FaultOptions = {}): string[] {
|
|
392
|
+
const consequences: string[] = [];
|
|
393
|
+
if (Object.hasOwn(DESTRUCTIVE_COMMANDS, path)) {
|
|
394
|
+
consequences.push(`deletes ${DESTRUCTIVE_COMMANDS[path as DestructivePath]}`);
|
|
395
|
+
}
|
|
396
|
+
for (const [flag, value] of Object.entries(parts.flags ?? {})) {
|
|
397
|
+
if (value !== undefined && Object.hasOwn(DESTRUCTIVE_FLAGS, flag)) {
|
|
398
|
+
consequences.push(`deletes ${DESTRUCTIVE_FLAGS[flag as DestructiveFlag]}`);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
const env = parts.flags?.["--env"];
|
|
402
|
+
if (Object.hasOwn(PRODUCTION_CONFIRMS, path) && typeof env === "string" && isProduction(env, options)) {
|
|
403
|
+
consequences.push(`runs against ${env}, a production environment`);
|
|
404
|
+
}
|
|
405
|
+
// Left off, `--env` is every environment for such a command, production included — and one with no `--env` to give
|
|
406
|
+
// is every environment on every run (#711).
|
|
407
|
+
if ((env === undefined && actsOnEveryEnvironment(path)) || actsOnEveryEnvironmentAlways(path)) {
|
|
408
|
+
consequences.push("runs against every environment, production included");
|
|
409
|
+
}
|
|
410
|
+
return consequences;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Whether `path` acts on every environment, production among them, when its `--env` is left off — `pithy testers
|
|
415
|
+
* provision` (`commandFacts.ts`'s {@link EVERY_ENVIRONMENT_WITHOUT_ENV}). A line about one environment names it.
|
|
416
|
+
*/
|
|
417
|
+
export function actsOnEveryEnvironment(path: string): boolean {
|
|
418
|
+
return (EVERY_ENVIRONMENT_WITHOUT_ENV as readonly string[]).includes(path);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Whether `path` acts on every environment, production among them, on every run, with no `--env` to narrow it —
|
|
423
|
+
* `pithy secrets provision` (`commandFacts.ts`'s {@link EVERY_ENVIRONMENT_ALWAYS}). A line naming it is a person's
|
|
424
|
+
* decision, always.
|
|
425
|
+
*/
|
|
426
|
+
export function actsOnEveryEnvironmentAlways(path: string): boolean {
|
|
427
|
+
return (EVERY_ENVIRONMENT_ALWAYS as readonly string[]).includes(path);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/** What one rule over several flags says about the flags a line names. */
|
|
431
|
+
function groupFaults(shown: string, group: GroupDef, named: ReadonlySet<string>): string[] {
|
|
432
|
+
const among = (group.flags ?? []).filter((flag) => named.has(flag));
|
|
433
|
+
const list = (group.flags ?? []).join(", ");
|
|
434
|
+
switch (group.kind) {
|
|
435
|
+
case "exactlyOne":
|
|
436
|
+
return among.length === 1 ? [] : [`\`${shown}\` takes exactly one of ${list}.`];
|
|
437
|
+
case "atMostOne":
|
|
438
|
+
return among.length <= 1 ? [] : [`\`${shown}\` takes at most one of ${list}.`];
|
|
439
|
+
case "allOrNone":
|
|
440
|
+
return among.length === 0 || among.length === (group.flags ?? []).length
|
|
441
|
+
? []
|
|
442
|
+
: [`\`${shown}\` takes all of ${list}, or none.`];
|
|
443
|
+
case "requires": {
|
|
444
|
+
if (group.flag === undefined || !named.has(group.flag)) return [];
|
|
445
|
+
const needs = group.needs ?? [];
|
|
446
|
+
const met = group.any === true ? needs.some((flag) => named.has(flag)) : needs.every((flag) => named.has(flag));
|
|
447
|
+
return met ? [] : [`\`${shown} ${group.flag}\` needs ${needs.join(group.any === true ? " or " : " and ")}.`];
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/** A word as a shell reads it back: quoted only when it has to be. */
|
|
453
|
+
function shellWord(word: string): string {
|
|
454
|
+
return /^[A-Za-z0-9_@%+=:,./-]+$/.test(word) ? word : `'${word.replaceAll("'", `'\\''`)}'`;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/** The line, spelled: arguments first, then each flag in the order the caller wrote it. */
|
|
458
|
+
function spell(path: string, parts: LooseParts): string {
|
|
459
|
+
const words = [CLI, path, ...(parts.args ?? []).map(shellWord)];
|
|
460
|
+
for (const [flag, value] of Object.entries(parts.flags ?? {})) {
|
|
461
|
+
if (value === undefined) continue;
|
|
462
|
+
if (value === true) words.push(flag);
|
|
463
|
+
else for (const one of typeof value === "string" ? [value] : value) words.push(flag, shellWord(one));
|
|
464
|
+
}
|
|
465
|
+
return words.join(" ");
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** Throw what {@link commandFaults} found, or hand back nothing. */
|
|
469
|
+
function refuseFaults(path: string, parts: LooseParts, options: BuildOptions): void {
|
|
470
|
+
const faults = commandFaults(path, parts, options);
|
|
471
|
+
if (faults.length > 0) {
|
|
472
|
+
throw new InternalError({
|
|
473
|
+
message: `A line names a command that does not run as written: ${faults.join(" ")}`,
|
|
474
|
+
detail: spell(path, parts),
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* **A command line that runs as written**, from a literal path: the tree checks its command, its flags and the
|
|
481
|
+
* flags it needs at compile time, and {@link commandFaults} checks the rest when it is built.
|
|
482
|
+
*/
|
|
483
|
+
export function pithyCommand<P extends BuildablePath>(path: P, ...parts: PartsArgument<P>): Command {
|
|
484
|
+
// The compiler has held an `--env` here to a quiet one, so no value it carries can be a person's decision.
|
|
485
|
+
return build(path, (parts[0] ?? {}) as LooseParts, {});
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/** Check a line's shape, and spell it. */
|
|
489
|
+
function build(path: string, parts: LooseParts, options: BuildOptions): Command {
|
|
490
|
+
refuseFaults(path, parts, options);
|
|
491
|
+
return spell(path, parts) as Command;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/** Whether a production run of `path` is a person's decision, so its `--env` decides what kind of line it is. */
|
|
495
|
+
function isProductionPath(path: string): path is ProductionPath {
|
|
496
|
+
return Object.hasOwn(PRODUCTION_CONFIRMS, path);
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/** The parts of a line a deployed Worker builds: no `--env`, which it reads as data and names through {@link remedy}. */
|
|
500
|
+
export interface RuntimeParts extends LooseParts {
|
|
501
|
+
readonly flags?: LooseParts["flags"] & { readonly "--env"?: never };
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* **The same, from a path that is a value** — `` `${capability} provision` ``. Nothing is checked at compile
|
|
506
|
+
* time, so everything is checked here, and a path the CLI does not have is refused rather than spelled.
|
|
507
|
+
*
|
|
508
|
+
* Whether a command is a person's decision never turns on a value here. A runtime line names no `--env`: a deployed
|
|
509
|
+
* Worker's environment is data, and whether its command is a person's decision depends on it, so that line is
|
|
510
|
+
* {@link remedy}'s. Nor does any line name the `--env` of a command a production run makes a person's decision —
|
|
511
|
+
* `provision`, `seed`, `vector provision`. Each is refused for every value, so no environment is a surprise.
|
|
512
|
+
*/
|
|
513
|
+
export function pithyCommandOf(path: string, parts: RuntimeParts, options: { readonly runtime: true }): Command;
|
|
514
|
+
export function pithyCommandOf(path: string, parts?: LooseParts): Command;
|
|
515
|
+
export function pithyCommandOf(
|
|
516
|
+
path: string,
|
|
517
|
+
parts: LooseParts = {},
|
|
518
|
+
options: Pick<FaultOptions, "runtime"> = {},
|
|
519
|
+
): Command {
|
|
520
|
+
if (parts.flags?.["--env"] !== undefined && (options.runtime === true || isProductionPath(path))) {
|
|
521
|
+
throw new InternalError({
|
|
522
|
+
message: `A line names an environment it read through remedy, never through pithyCommandOf: \`${spell(path, parts)}\`.`,
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
return build(path, parts, options);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* **A command a person decides on, said as one.** For a fault whose only remedy destroys something, or runs
|
|
530
|
+
* against production: the sentence names the command, says what it does and that the choice is a person's, and
|
|
531
|
+
* leaves every confirmation flag for them to add. It returns a sentence, never a {@link Command}, so it cannot
|
|
532
|
+
* become a `fix`.
|
|
533
|
+
*
|
|
534
|
+
* It takes a command that destroys something, or a line naming a flag that does — shape, never a value. A command
|
|
535
|
+
* whose production run is a person's decision is a decision only for a production `--env`, which is data, so that
|
|
536
|
+
* line is {@link remedy}'s, which decides by the value; here it is refused for every value.
|
|
537
|
+
*/
|
|
538
|
+
export function personDecision(
|
|
539
|
+
path: PersonPath,
|
|
540
|
+
parts: LooseParts,
|
|
541
|
+
options: Pick<FaultOptions, "productionEnvironments" | "runtime"> = {},
|
|
542
|
+
): string {
|
|
543
|
+
const destroys =
|
|
544
|
+
Object.hasOwn(DESTRUCTIVE_COMMANDS, path) ||
|
|
545
|
+
Object.entries(parts.flags ?? {}).some(
|
|
546
|
+
([flag, value]) => value !== undefined && Object.hasOwn(DESTRUCTIVE_FLAGS, flag),
|
|
547
|
+
);
|
|
548
|
+
if (!destroys) {
|
|
549
|
+
throw new InternalError({
|
|
550
|
+
message: `\`${spell(path, parts)}\` destroys nothing; a person's decision names what it destroys, and a production run is remedy's to say.`,
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
return decision(path, parts, options);
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/** A command that destroys something, or one declaring a flag that does: what {@link personDecision} takes. */
|
|
557
|
+
export type PersonPath = {
|
|
558
|
+
[P in CommandPath]: P extends DestructivePath
|
|
559
|
+
? P
|
|
560
|
+
: [Extract<FlagName<P>, DestructiveFlag>] extends [never]
|
|
561
|
+
? never
|
|
562
|
+
: P;
|
|
563
|
+
}[CommandPath];
|
|
564
|
+
|
|
565
|
+
/** The sentence {@link personDecision} says, for any command with something a person must decide. */
|
|
566
|
+
function decision(path: string, parts: LooseParts, options: BuildOptions): string {
|
|
567
|
+
refuseFaults(path, parts, { ...options, person: true });
|
|
568
|
+
const consequences = consequencesOf(path, parts, options);
|
|
569
|
+
if (consequences.length === 0) {
|
|
570
|
+
throw new InternalError({
|
|
571
|
+
message: `\`${spell(path, parts)}\` has nothing a person must decide; build it as a command.`,
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
const named = Object.keys(parts.flags ?? {});
|
|
575
|
+
const env = parts.flags?.["--env"];
|
|
576
|
+
const asked = CONFIRMATIONS.filter(
|
|
577
|
+
(one) =>
|
|
578
|
+
one.path === path &&
|
|
579
|
+
(one.when === undefined || named.includes(one.when)) &&
|
|
580
|
+
!(typeof env === "string" && (one.unlessEnv ?? []).includes(env)),
|
|
581
|
+
);
|
|
582
|
+
const flags = asked.map((one) => one.flag);
|
|
583
|
+
const production = (PRODUCTION_CONFIRMS as Readonly<Record<string, string | null>>)[path];
|
|
584
|
+
const everyEnvironment = (env === undefined && actsOnEveryEnvironment(path)) || actsOnEveryEnvironmentAlways(path);
|
|
585
|
+
if (typeof production === "string" && ((typeof env === "string" && isProduction(env, options)) || everyEnvironment)) {
|
|
586
|
+
flags.push(production);
|
|
587
|
+
}
|
|
588
|
+
const confirm =
|
|
589
|
+
flags.length === 0
|
|
590
|
+
? ""
|
|
591
|
+
: ` with its ${[...new Set(flags)].join(" and ")} ${flags.length === 1 ? "phrase" : "phrases"}, which only a person gives,`;
|
|
592
|
+
// "deletes A and deletes B" reads as "deletes A and B".
|
|
593
|
+
const deleted = consequences.filter((one) => one.startsWith("deletes ")).map((one) => one.slice("deletes ".length));
|
|
594
|
+
const said = [
|
|
595
|
+
...(deleted.length === 0 ? [] : [`deletes ${deleted.join(" and ")}`]),
|
|
596
|
+
...consequences.filter((one) => !one.startsWith("deletes ")),
|
|
597
|
+
];
|
|
598
|
+
return `\`${spell(path, parts)}\`${confirm} ${said.join(" and ")}. That is a person's decision, not a fix.`;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/** **A remedy**: the command a `fix` carries when one may, and the words a line says about it. */
|
|
602
|
+
export interface Remedy {
|
|
603
|
+
/** The command, when it runs as written and a `fix` may carry it. `undefined` for a person's decision. */
|
|
604
|
+
readonly fix: Command | undefined;
|
|
605
|
+
/** The words: `wrap` around the command, or — for a person's decision — that sentence in its place. */
|
|
606
|
+
say(wrap: (command: string) => string): string;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* **The remedy a line names**, decided by the command rather than by its site: a command a remedy may name is a
|
|
611
|
+
* `fix` and goes into the sentence; one that destroys something or runs against production is a person's
|
|
612
|
+
* decision, said as one, with no `fix`. A command that does not run is refused either way.
|
|
613
|
+
*/
|
|
614
|
+
export function remedy(
|
|
615
|
+
path: string,
|
|
616
|
+
parts: LooseParts = {},
|
|
617
|
+
options: Pick<FaultOptions, "productionEnvironments" | "runtime"> = {},
|
|
618
|
+
): Remedy {
|
|
619
|
+
return remedyOf(path, parts, options);
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/** {@link remedy}, with what a builder knows about where the values came from. */
|
|
623
|
+
function remedyOf(path: string, parts: LooseParts, options: BuildOptions): Remedy {
|
|
624
|
+
if (isCommandPath(path) && consequencesOf(path, parts, options).length > 0) {
|
|
625
|
+
const sentence = decision(path, parts, options);
|
|
626
|
+
return { fix: undefined, say: () => sentence };
|
|
627
|
+
}
|
|
628
|
+
const command = build(path, parts, options);
|
|
629
|
+
return { fix: command, say: (wrap) => wrap(command) };
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
declare const CALLER: unique symbol;
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* **The caller's own run (#711): the one input every rerun and every fix naming it again is built from.**
|
|
636
|
+
*
|
|
637
|
+
* Each review round of #711 found another run again that dropped what the caller gave — `--json`, `--worker`,
|
|
638
|
+
* `--dry-run`, a `--set`, an option the Worker's config already answers — because each was rebuilt at its own site
|
|
639
|
+
* from whatever flags that site held. So a rerun builder ({@link rerunCommand}, {@link rerunRemedy}, `pithy add`'s
|
|
640
|
+
* `addAgain`) takes this and nothing else, and this is made only by {@link callerOf}, from a run's parsed arguments.
|
|
641
|
+
* Branded, so loose flags do not type-check where one is wanted; `packages/cli/src/ci/runnableCommands.test.ts`
|
|
642
|
+
* fails on a cast to it and on a {@link callerOf} handed anything but a run's own arguments.
|
|
643
|
+
*/
|
|
644
|
+
export interface Caller<P extends CommandPath = CommandPath> {
|
|
645
|
+
/** The command the run was. */
|
|
646
|
+
readonly path: P;
|
|
647
|
+
/** Its parsed arguments, keyed as the parser keys them (`worker`, `routing-zone`, `dry-run`). */
|
|
648
|
+
readonly given: Readonly<Record<string, unknown>>;
|
|
649
|
+
/** Its `--set` values, by key, and every option it settled by a prompt ({@link settledBy}). */
|
|
650
|
+
readonly set: Readonly<Record<string, string>>;
|
|
651
|
+
/**
|
|
652
|
+
* The options the Worker's config already answers, for a command that reads one (`pithy add`): settled, so a
|
|
653
|
+
* rerun names no `--set` for them, which could contradict the config.
|
|
654
|
+
*/
|
|
655
|
+
readonly answered: ReadonlySet<string>;
|
|
656
|
+
readonly [CALLER]: true;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/** `key=value` entries, by key; an entry with no `=` names nothing. */
|
|
660
|
+
function setEntries(entries: unknown): Record<string, string> {
|
|
661
|
+
const list = typeof entries === "string" ? [entries] : Array.isArray(entries) ? entries : [];
|
|
662
|
+
const values: Record<string, string> = {};
|
|
663
|
+
for (const entry of list) {
|
|
664
|
+
if (typeof entry !== "string") continue;
|
|
665
|
+
const at = entry.indexOf("=");
|
|
666
|
+
if (at > 0) values[entry.slice(0, at)] = entry.slice(at + 1);
|
|
667
|
+
}
|
|
668
|
+
return values;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
/**
|
|
672
|
+
* **A run's own context**, from its parsed arguments. `set` is the raw `--set` entries where the parser keeps only the
|
|
673
|
+
* last (`collectSetFlags`); absent, they are read from `given.set`. `answered` is what the Worker's config states.
|
|
674
|
+
*/
|
|
675
|
+
export function callerOf<P extends CommandPath>(
|
|
676
|
+
path: P,
|
|
677
|
+
given: Readonly<Record<string, unknown>>,
|
|
678
|
+
context: { readonly set?: readonly string[] | undefined; readonly answered?: Iterable<string> | undefined } = {},
|
|
679
|
+
): Caller<P> {
|
|
680
|
+
return {
|
|
681
|
+
path,
|
|
682
|
+
given,
|
|
683
|
+
set: setEntries(context.set ?? given.set),
|
|
684
|
+
answered: new Set(context.answered ?? []),
|
|
685
|
+
} as unknown as Caller<P>;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/** The same caller, with what the run settled on its way — a prompt's answers — carried as `--set` too. */
|
|
689
|
+
export function settledBy<P extends CommandPath>(
|
|
690
|
+
caller: Caller<P>,
|
|
691
|
+
values: Readonly<Record<string, string>>,
|
|
692
|
+
): Caller<P> {
|
|
693
|
+
return { ...caller, set: { ...caller.set, ...values } };
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/** The same caller, with the options its Worker's config answers. */
|
|
697
|
+
export function answeredBy<P extends CommandPath>(caller: Caller<P>, keys: Iterable<string>): Caller<P> {
|
|
698
|
+
return { ...caller, answered: new Set([...caller.answered, ...keys]) };
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
/** What a run-again is told beyond the arguments the run was given. */
|
|
702
|
+
export interface RerunOptions {
|
|
703
|
+
/** The positional arguments, as the command takes them. */
|
|
704
|
+
readonly args?: readonly string[];
|
|
705
|
+
/** What the run resolved for itself — the Worker it picked when it was not named — or `undefined` to drop one. */
|
|
706
|
+
readonly flags?: LooseParts["flags"];
|
|
707
|
+
/** The project's `seed.productionEnvironments`, beyond the ones every project has. */
|
|
708
|
+
readonly productionEnvironments?: readonly string[] | undefined;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/**
|
|
712
|
+
* A run again, as parts: each flag the command declares, carried when it was given — a boolean when true, a value
|
|
713
|
+
* when it differs from its default or when its default would change the meaning (`--env`, where the command reads
|
|
714
|
+
* it). A confirmation or a credential is never carried: each is a person's to give again.
|
|
715
|
+
*/
|
|
716
|
+
function rerunParts(caller: Caller, options: RerunOptions): LooseParts {
|
|
717
|
+
const { path, given } = caller;
|
|
718
|
+
const def = COMMANDS[path] as unknown as CommandDef;
|
|
719
|
+
const flags: Record<string, string | true | readonly string[]> = {};
|
|
720
|
+
for (const [flag, flagDef] of Object.entries(def.flags)) {
|
|
721
|
+
if ((CONFIRM_FLAGS as readonly string[]).includes(flag)) continue;
|
|
722
|
+
if ((CREDENTIAL_FLAGS as readonly string[]).includes(flag)) continue;
|
|
723
|
+
// `--set` is the caller's by key: every value it passed, and every option it settled on the way.
|
|
724
|
+
const value =
|
|
725
|
+
flag === "--set" ? Object.entries(caller.set).map(([key, one]) => `${key}=${one}`) : given[flag.slice(2)];
|
|
726
|
+
if (flagDef.type === "boolean") {
|
|
727
|
+
if (value === true) flags[flag] = true;
|
|
728
|
+
continue;
|
|
729
|
+
}
|
|
730
|
+
if (Array.isArray(value)) {
|
|
731
|
+
const values = value.filter((one): one is string => typeof one === "string" && one.length > 0);
|
|
732
|
+
if (values.length > 0) flags[flag] = values;
|
|
733
|
+
continue;
|
|
734
|
+
}
|
|
735
|
+
if (typeof value !== "string" || value.length === 0) continue;
|
|
736
|
+
// A default `--env` names nothing on a command that reads it only under another flag (`upgrade` without
|
|
737
|
+
// `--migrate`), and is carried as soon as that flag is.
|
|
738
|
+
const onlyWith = (MEANING_ONLY_WITH as Readonly<Record<string, string>>)[path];
|
|
739
|
+
const meaning =
|
|
740
|
+
(MEANING_FLAGS as readonly string[]).includes(flag) &&
|
|
741
|
+
(onlyWith === undefined || given[onlyWith.slice(2)] === true);
|
|
742
|
+
if (value !== flagDef.default || meaning) flags[flag] = value;
|
|
743
|
+
}
|
|
744
|
+
return { args: options.args ?? [], flags: { ...flags, ...options.flags } };
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
/** A command whose run again is always a command: it destroys nothing, and no production run of it is a person's. */
|
|
748
|
+
export type RerunPath = Exclude<CommandPath, DestructivePath | ProductionPath>;
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* **The caller's own command, to run again (#711)**, as a {@link Command} — rebuilt from the run's {@link Caller}, so
|
|
752
|
+
* a rerun asks for nothing the first run did not. `options.flags` adds what the run resolved for itself (the Worker it
|
|
753
|
+
* picked) or drops a flag whose work is done (`undefined`).
|
|
754
|
+
*
|
|
755
|
+
* It takes no command whose run again could be a person's decision — one that destroys something, or one a
|
|
756
|
+
* production `--env` makes a person's: that run again is {@link rerunRemedy}'s, which says so rather than throwing.
|
|
757
|
+
*/
|
|
758
|
+
export function rerunCommand(
|
|
759
|
+
caller: Caller<RerunPath>,
|
|
760
|
+
options: Omit<RerunOptions, "productionEnvironments"> = {},
|
|
761
|
+
): Command {
|
|
762
|
+
const path: string = caller.path;
|
|
763
|
+
if (!isCommandPath(path)) return pithyCommandOf(path);
|
|
764
|
+
if (Object.hasOwn(DESTRUCTIVE_COMMANDS, path) || Object.hasOwn(PRODUCTION_CONFIRMS, path)) {
|
|
765
|
+
throw new InternalError({
|
|
766
|
+
message: `\`${commandName(path)}\` may be a person's decision to run again; name it with rerunRemedy.`,
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
return build(path, rerunParts(caller, options), { given: true });
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/**
|
|
773
|
+
* **The caller's own command, as a line names it to run again (#711)** — the {@link Remedy} for
|
|
774
|
+
* {@link rerunCommand}'s parts, for any command. A run again that a production `--env` makes a person's decision is
|
|
775
|
+
* said as one, with no `fix`, rather than thrown after the run did its work. `say` hands `wrap` the command in code
|
|
776
|
+
* and — where the run was given a credential by flag, which no line repeats — the flags to pass again beside it.
|
|
777
|
+
*/
|
|
778
|
+
export function rerunRemedy(caller: Caller, options: RerunOptions = {}): Remedy {
|
|
779
|
+
const { path, given } = caller;
|
|
780
|
+
if (!isCommandPath(path)) return remedy(path);
|
|
781
|
+
const parts = rerunParts(caller, options);
|
|
782
|
+
const found = remedyOf(path, parts, { productionEnvironments: options.productionEnvironments ?? [], given: true });
|
|
783
|
+
const withheld = (CREDENTIAL_FLAGS as readonly string[]).filter((flag) => {
|
|
784
|
+
const value = given[flag.slice(2)];
|
|
785
|
+
return typeof value === "string" && value.length > 0;
|
|
786
|
+
});
|
|
787
|
+
const credentials = withheld.join(" and ");
|
|
788
|
+
if (found.fix === undefined) {
|
|
789
|
+
const sentence = found.say((command) => command);
|
|
790
|
+
return {
|
|
791
|
+
fix: undefined,
|
|
792
|
+
say: () => (withheld.length === 0 ? sentence : `${sentence} It takes the ${credentials} this run was given.`),
|
|
793
|
+
};
|
|
794
|
+
}
|
|
795
|
+
const line =
|
|
796
|
+
withheld.length === 0 ? `\`${found.fix}\`` : `\`${found.fix}\` with the ${credentials} this run was given`;
|
|
797
|
+
return { fix: found.fix, say: (wrap) => wrap(line) };
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
/**
|
|
801
|
+
* **A command named in prose, not given to run** — "as `pithy add` wrote it". Checked to exist, and spelled with
|
|
802
|
+
* nothing after it. A line that tells somebody to run a command builds it with {@link pithyCommand}.
|
|
803
|
+
*/
|
|
804
|
+
export function commandName(path: CommandPath | GroupPath): string {
|
|
805
|
+
return `${CLI} ${path}`;
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
/** {@link commandName}, for a path that is a value: refused when the CLI has no such command or group. */
|
|
809
|
+
export function commandNameOf(path: string): string {
|
|
810
|
+
if (!isCommandPath(path) && !isGroupPath(path)) {
|
|
811
|
+
throw new InternalError({ message: `A line names \`pithy ${path}\`, which is not a command.` });
|
|
812
|
+
}
|
|
813
|
+
return `${CLI} ${path}`;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
/**
|
|
817
|
+
* {@link commandNameOf}, for a path as a parser walked it, word by word. Empty is the root, which is the CLI
|
|
818
|
+
* itself rather than a command, and is named as such.
|
|
819
|
+
*/
|
|
820
|
+
export function commandNameOfPath(path: readonly string[]): string {
|
|
821
|
+
return path.length === 0 ? CLI : commandNameOf(path.join(" "));
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
/**
|
|
825
|
+
* **The CLI at a version** — `pithy 0.9.5`, as a line names a release rather than a command. A line never spells
|
|
826
|
+
* `pithy` before an interpolation: the gate reads that as a command it cannot check.
|
|
827
|
+
*/
|
|
828
|
+
export function cliAtVersion(version: string): string {
|
|
829
|
+
return `${CLI} ${version}`;
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
/**
|
|
833
|
+
* **How each package manager runs a project's own binary**: `bun x`, `pnpm exec`, `yarn`, `npx`. The one table
|
|
834
|
+
* both a spawned process (`packages/cli/src/project/packageManager.ts`'s `execArgs`) and a line naming one read.
|
|
835
|
+
*/
|
|
836
|
+
export const PACKAGE_EXEC = {
|
|
837
|
+
bun: ["bun", "x"],
|
|
838
|
+
pnpm: ["pnpm", "exec"],
|
|
839
|
+
yarn: ["yarn"],
|
|
840
|
+
npm: ["npx"],
|
|
841
|
+
} as const satisfies Readonly<Record<string, readonly [string, ...string[]]>>;
|
|
842
|
+
|
|
843
|
+
/** A package manager a project runs its own binaries through. */
|
|
844
|
+
export type ExecPackageManager = keyof typeof PACKAGE_EXEC;
|
|
845
|
+
|
|
846
|
+
/**
|
|
847
|
+
* **A built command, run through the project's own package manager** — the `pithy` the project installed, never
|
|
848
|
+
* a global one, which may be an older CLI than the line is about. Takes a {@link Command}, so what follows the
|
|
849
|
+
* prefix was built and runs as written.
|
|
850
|
+
*/
|
|
851
|
+
export function viaPackageManager(pm: ExecPackageManager, command: Command): Command {
|
|
852
|
+
return [...PACKAGE_EXEC[pm], command].join(" ") as Command;
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
/** `a`, `a or b`, `a, b or c`, each in code: commands a reader picks one of. */
|
|
856
|
+
export function commandAlternatives(commands: readonly string[]): string {
|
|
857
|
+
const coded = commands.map((command) => `\`${command}\``);
|
|
858
|
+
return coded.length <= 1 ? coded.join("") : `${coded.slice(0, -1).join(", ")} or ${coded.at(-1)}`;
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
/** `a`, `a and b`, `a, b and c`, each in code: commands that all run. */
|
|
862
|
+
export function commandSequence(commands: readonly string[]): string {
|
|
863
|
+
const coded = commands.map((command) => `\`${command}\``);
|
|
864
|
+
return coded.length <= 1 ? coded.join("") : `${coded.slice(0, -1).join(", ")} and ${coded.at(-1)}`;
|
|
865
|
+
}
|