@kjanat/dreamcli 3.0.0-rc.1 → 3.0.0-rc.11
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/CHANGELOG.md +227 -1
- package/dist/core/cli/dispatch.d.mts +6 -5
- package/dist/core/cli/dispatch.mjs +205 -1
- package/dist/core/cli/help-links.d.mts +0 -1
- package/dist/core/cli/help-links.mjs +41 -1
- package/dist/core/cli/index.d.mts +81 -8
- package/dist/core/cli/index.mjs +945 -1
- package/dist/core/cli/planner.d.mts +5 -1
- package/dist/core/cli/planner.mjs +300 -1
- package/dist/core/cli/plugin.d.mts +1 -1
- package/dist/core/cli/plugin.mjs +41 -1
- package/dist/core/cli/propagate.d.mts +1 -1
- package/dist/core/cli/propagate.mjs +57 -1
- package/dist/core/cli/root-help.d.mts +2 -1
- package/dist/core/cli/root-help.mjs +226 -4
- package/dist/core/cli/root-surface.d.mts +12 -1
- package/dist/core/cli/root-surface.mjs +28 -1
- package/dist/core/cli/runtime-preflight.d.mts +14 -2
- package/dist/core/cli/runtime-preflight.mjs +169 -1
- package/dist/core/completion/index.d.mts +0 -1
- package/dist/core/completion/index.mjs +90 -1
- package/dist/core/completion/shells/bash.d.mts +0 -1
- package/dist/core/completion/shells/bash.mjs +308 -3
- package/dist/core/completion/shells/fish.d.mts +0 -1
- package/dist/core/completion/shells/fish.mjs +162 -2
- package/dist/core/completion/shells/powershell.d.mts +0 -1
- package/dist/core/completion/shells/powershell.mjs +358 -2
- package/dist/core/completion/shells/shared.d.mts +13 -2
- package/dist/core/completion/shells/shared.mjs +176 -1
- package/dist/core/completion/shells/zsh.d.mts +0 -1
- package/dist/core/completion/shells/zsh.mjs +251 -2
- package/dist/core/config/index.d.mts +2 -3
- package/dist/core/config/index.mjs +246 -1
- package/dist/core/config/package-json.d.mts +27 -4
- package/dist/core/config/package-json.mjs +386 -3
- package/dist/core/errors/index.d.mts +3 -3
- package/dist/core/errors/index.mjs +87 -1
- package/dist/core/execution/index.d.mts +0 -1
- package/dist/core/execution/index.mjs +201 -1
- package/dist/core/help/ansi.d.mts +11 -5
- package/dist/core/help/ansi.mjs +121 -2
- package/dist/core/help/index.d.mts +22 -3
- package/dist/core/help/index.mjs +341 -7
- package/dist/core/help/theme.d.mts +68 -0
- package/dist/core/help/theme.mjs +46 -0
- package/dist/core/json-schema/index.d.mts +15 -2
- package/dist/core/json-schema/index.mjs +536 -12
- package/dist/core/json-schema/meta-descriptions.generated.d.mts +55 -38
- package/dist/core/json-schema/meta-descriptions.generated.mjs +102 -28
- package/dist/core/output/activity.d.mts +0 -1
- package/dist/core/output/activity.mjs +452 -1
- package/dist/core/output/bind.mjs +56 -1
- package/dist/core/output/contracts.d.mts +4 -5
- package/dist/core/output/contracts.mjs +57 -1
- package/dist/core/output/display-value.mjs +32 -1
- package/dist/core/output/index.d.mts +21 -1
- package/dist/core/output/index.mjs +442 -2
- package/dist/core/output/renderers.d.mts +0 -1
- package/dist/core/output/renderers.mjs +46 -1
- package/dist/core/output/writer.mjs +12 -1
- package/dist/core/parse/index.d.mts +40 -7
- package/dist/core/parse/index.mjs +727 -1
- package/dist/core/prompt/index.d.mts +1 -1
- package/dist/core/prompt/index.mjs +348 -6
- package/dist/core/resolve/args.d.mts +1 -1
- package/dist/core/resolve/args.mjs +105 -1
- package/dist/core/resolve/coerce.d.mts +1 -1
- package/dist/core/resolve/coerce.mjs +336 -1
- package/dist/core/resolve/config.mjs +19 -1
- package/dist/core/resolve/contracts.d.mts +9 -4
- package/dist/core/resolve/errors.d.mts +0 -1
- package/dist/core/resolve/errors.mjs +140 -2
- package/dist/core/resolve/flags.d.mts +7 -2
- package/dist/core/resolve/flags.mjs +251 -1
- package/dist/core/resolve/index.d.mts +2 -2
- package/dist/core/resolve/index.mjs +85 -1
- package/dist/core/resolve/property.d.mts +4 -2
- package/dist/core/resolve/property.mjs +53 -1
- package/dist/core/schema/activity.d.mts +31 -11
- package/dist/core/schema/arg.d.mts +3 -4
- package/dist/core/schema/arg.mjs +463 -1
- package/dist/core/schema/command.d.mts +26 -6
- package/dist/core/schema/command.mjs +726 -1
- package/dist/core/schema/flag.d.mts +485 -19
- package/dist/core/schema/flag.mjs +705 -1
- package/dist/core/schema/index.d.mts +4 -2
- package/dist/core/schema/middleware.d.mts +4 -3
- package/dist/core/schema/middleware.mjs +41 -1
- package/dist/core/schema/number-constraints.mjs +60 -1
- package/dist/core/schema/prompt.d.mts +6 -3
- package/dist/core/schema/run.d.mts +20 -1
- package/dist/core/schema/string-constraints.d.mts +97 -0
- package/dist/core/schema/string-constraints.mjs +69 -0
- package/dist/core/schema/value-parsers.d.mts +78 -0
- package/dist/core/schema/value-parsers.mjs +142 -0
- package/dist/core/schema-dsl/runtime.mjs +271 -2
- package/dist/core/schema-dsl/to-json-schema.d.mts +0 -1
- package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
- package/dist/core/testkit/index.d.mts +0 -31
- package/dist/core/testkit/index.mjs +83 -1
- package/dist/index.d.mts +11 -6
- package/dist/index.mjs +22 -1
- package/dist/runtime/adapter.d.mts +38 -2
- package/dist/runtime/adapter.mjs +87 -1
- package/dist/runtime/auto.d.mts +2 -3
- package/dist/runtime/auto.mjs +45 -1
- package/dist/runtime/deno.d.mts +24 -5
- package/dist/runtime/deno.mjs +262 -2
- package/dist/runtime/detect.mjs +45 -1
- package/dist/runtime/index.d.mts +3 -4
- package/dist/runtime/node.d.mts +8 -2
- package/dist/runtime/node.mjs +220 -2
- package/dist/runtime/paths.mjs +48 -1
- package/dist/runtime.d.mts +4 -4
- package/dist/runtime.mjs +6 -1
- package/dist/strings.mjs +22 -1
- package/dist/testkit.d.mts +1 -0
- package/dist/testkit.mjs +5 -1
- package/dist/version.d.mts +7 -3
- package/dist/version.mjs +7 -1
- package/dreamcli.schema.json +36 -7
- package/package.json +45 -42
- package/dist/runtime/bun.d.mts +0 -28
- package/dist/runtime/bun.mjs +0 -1
- package/src/core/cli/dispatch.ts +0 -355
- package/src/core/cli/help-links.ts +0 -82
- package/src/core/cli/index.ts +0 -1614
- package/src/core/cli/planner.ts +0 -656
- package/src/core/cli/plugin.ts +0 -117
- package/src/core/cli/propagate.ts +0 -97
- package/src/core/cli/root-help.ts +0 -342
- package/src/core/cli/root-surface.ts +0 -87
- package/src/core/cli/runtime-preflight.ts +0 -456
- package/src/core/completion/index.ts +0 -138
- package/src/core/completion/shells/bash.ts +0 -459
- package/src/core/completion/shells/fish.ts +0 -259
- package/src/core/completion/shells/powershell.ts +0 -439
- package/src/core/completion/shells/shared.ts +0 -310
- package/src/core/completion/shells/zsh.ts +0 -381
- package/src/core/config/index.ts +0 -417
- package/src/core/config/package-json.ts +0 -560
- package/src/core/errors/index.ts +0 -193
- package/src/core/execution/index.ts +0 -265
- package/src/core/help/ansi.ts +0 -144
- package/src/core/help/index.ts +0 -563
- package/src/core/json-schema/index.ts +0 -910
- package/src/core/json-schema/meta-descriptions.generated.ts +0 -202
- package/src/core/output/activity.ts +0 -575
- package/src/core/output/bind.ts +0 -59
- package/src/core/output/contracts.ts +0 -194
- package/src/core/output/display-value.ts +0 -48
- package/src/core/output/index.ts +0 -704
- package/src/core/output/renderers.ts +0 -125
- package/src/core/output/writer.ts +0 -46
- package/src/core/parse/index.ts +0 -742
- package/src/core/prompt/index.ts +0 -623
- package/src/core/resolve/args.ts +0 -144
- package/src/core/resolve/coerce.ts +0 -500
- package/src/core/resolve/config.ts +0 -26
- package/src/core/resolve/contracts.ts +0 -135
- package/src/core/resolve/errors.ts +0 -227
- package/src/core/resolve/flags.ts +0 -269
- package/src/core/resolve/index.ts +0 -108
- package/src/core/resolve/property.ts +0 -99
- package/src/core/schema/activity.ts +0 -240
- package/src/core/schema/arg.ts +0 -841
- package/src/core/schema/command.ts +0 -1500
- package/src/core/schema/flag.ts +0 -917
- package/src/core/schema/index.ts +0 -96
- package/src/core/schema/middleware.ts +0 -171
- package/src/core/schema/number-constraints.ts +0 -153
- package/src/core/schema/prompt.ts +0 -171
- package/src/core/schema/run.ts +0 -198
- package/src/core/schema-dsl/index.ts +0 -109
- package/src/core/schema-dsl/parse.ts +0 -262
- package/src/core/schema-dsl/runtime.ts +0 -609
- package/src/core/schema-dsl/to-json-schema.ts +0 -102
- package/src/core/testkit/index.ts +0 -147
- package/src/index.ts +0 -181
- package/src/runtime/adapter.ts +0 -303
- package/src/runtime/auto.ts +0 -86
- package/src/runtime/bun.ts +0 -48
- package/src/runtime/deno.ts +0 -367
- package/src/runtime/detect.ts +0 -105
- package/src/runtime/index.ts +0 -21
- package/src/runtime/node-builtins.d.ts +0 -27
- package/src/runtime/node.ts +0 -289
- package/src/runtime/paths.ts +0 -64
- package/src/runtime/support.ts +0 -77
- package/src/runtime.ts +0 -31
- package/src/schema.ts +0 -16
- package/src/strings.ts +0 -34
- package/src/testkit.ts +0 -25
- package/src/version.ts +0 -20
|
@@ -1 +1,251 @@
|
|
|
1
|
-
import{ValidationError
|
|
1
|
+
import { ValidationError } from "../errors/index.mjs";
|
|
2
|
+
import { resolvePromptConfig } from "../prompt/index.mjs";
|
|
3
|
+
import { coerceValue } from "./coerce.mjs";
|
|
4
|
+
import { isNonEmpty, throwAggregatedErrors } from "./errors.mjs";
|
|
5
|
+
import { resolveConfigPath } from "./config.mjs";
|
|
6
|
+
//#region src/core/resolve/flags.ts
|
|
7
|
+
/**
|
|
8
|
+
* Internal flag resolution helpers.
|
|
9
|
+
*
|
|
10
|
+
* @module dreamcli/core/resolve/flags
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
/** Walk every declared flag through the resolution chain (cli -> env -> config -> prompt -> default), collecting deprecations and throwing aggregated errors. */
|
|
14
|
+
async function resolveFlags(flagSchemas, parsedFlags, env, config, prompter, interactive, deprecations, stat) {
|
|
15
|
+
const resolved = {};
|
|
16
|
+
const errors = [];
|
|
17
|
+
const hardErrorFlags = /* @__PURE__ */ new Set();
|
|
18
|
+
for (const [name, schema] of Object.entries(flagSchemas)) {
|
|
19
|
+
const hasParsedValue = Object.hasOwn(parsedFlags, name);
|
|
20
|
+
const parsedValue = parsedFlags[name];
|
|
21
|
+
if (hasParsedValue && parsedValue !== void 0) {
|
|
22
|
+
if (schema.deprecated !== void 0) deprecations.push({
|
|
23
|
+
kind: "flag",
|
|
24
|
+
name,
|
|
25
|
+
message: schema.deprecated
|
|
26
|
+
});
|
|
27
|
+
resolved[name] = parsedValue;
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
if (schema.envVar !== void 0) {
|
|
31
|
+
const envValue = env[schema.envVar];
|
|
32
|
+
if (envValue !== void 0) {
|
|
33
|
+
const coerced = coerceValue(name, {
|
|
34
|
+
kind: "env",
|
|
35
|
+
envVar: schema.envVar
|
|
36
|
+
}, envValue, schema);
|
|
37
|
+
if (coerced.ok) {
|
|
38
|
+
if (schema.deprecated !== void 0) deprecations.push({
|
|
39
|
+
kind: "flag",
|
|
40
|
+
name,
|
|
41
|
+
message: schema.deprecated
|
|
42
|
+
});
|
|
43
|
+
resolved[name] = coerced.value;
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
errors.push(coerced.error);
|
|
47
|
+
hardErrorFlags.add(name);
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (schema.configPath !== void 0) {
|
|
52
|
+
const configValue = resolveConfigPath(config, schema.configPath);
|
|
53
|
+
if (configValue !== void 0) {
|
|
54
|
+
const coerced = coerceValue(name, {
|
|
55
|
+
kind: "config",
|
|
56
|
+
configPath: schema.configPath
|
|
57
|
+
}, configValue, schema);
|
|
58
|
+
if (coerced.ok) {
|
|
59
|
+
if (schema.deprecated !== void 0) deprecations.push({
|
|
60
|
+
kind: "flag",
|
|
61
|
+
name,
|
|
62
|
+
message: schema.deprecated
|
|
63
|
+
});
|
|
64
|
+
resolved[name] = coerced.value;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
errors.push(coerced.error);
|
|
68
|
+
hardErrorFlags.add(name);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const interactiveConfigs = interactive !== void 0 ? interactive({ flags: resolved }) : void 0;
|
|
73
|
+
for (const [name, schema] of Object.entries(flagSchemas)) {
|
|
74
|
+
if (Object.hasOwn(resolved, name) || hardErrorFlags.has(name)) continue;
|
|
75
|
+
const interactiveConfig = interactiveConfigs?.[name];
|
|
76
|
+
let effectivePromptConfig;
|
|
77
|
+
if (interactiveConfig !== void 0 && interactiveConfig !== null && interactiveConfig !== false && interactiveConfig !== 0 && interactiveConfig !== "") effectivePromptConfig = interactiveConfig;
|
|
78
|
+
else if (interactiveConfig === false) effectivePromptConfig = void 0;
|
|
79
|
+
else effectivePromptConfig = schema.prompt;
|
|
80
|
+
if (effectivePromptConfig !== void 0 && prompter !== void 0) {
|
|
81
|
+
const promptResult = await resolvePromptValueWithConfig(name, schema, effectivePromptConfig, prompter);
|
|
82
|
+
if (promptResult.ok) {
|
|
83
|
+
if (schema.deprecated !== void 0) deprecations.push({
|
|
84
|
+
kind: "flag",
|
|
85
|
+
name,
|
|
86
|
+
message: schema.deprecated
|
|
87
|
+
});
|
|
88
|
+
resolved[name] = promptResult.value;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (promptResult.error !== void 0) {
|
|
92
|
+
errors.push(promptResult.error);
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (schema.defaultValue !== void 0) {
|
|
97
|
+
resolved[name] = schema.defaultValue;
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (schema.kind === "array" && schema.presence !== "required") {
|
|
101
|
+
resolved[name] = [];
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (schema.kind === "keyValue" && schema.presence !== "required") {
|
|
105
|
+
resolved[name] = {};
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (schema.presence === "required") {
|
|
109
|
+
const details = {
|
|
110
|
+
flag: name,
|
|
111
|
+
kind: schema.kind
|
|
112
|
+
};
|
|
113
|
+
if (schema.envVar !== void 0) details.envVar = schema.envVar;
|
|
114
|
+
if (schema.configPath !== void 0) details.configPath = schema.configPath;
|
|
115
|
+
errors.push(new ValidationError(`Missing required flag --${name}`, {
|
|
116
|
+
code: "REQUIRED_FLAG",
|
|
117
|
+
details,
|
|
118
|
+
suggest: buildRequiredFlagSuggest(name, schema)
|
|
119
|
+
}));
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
resolved[name] = void 0;
|
|
123
|
+
}
|
|
124
|
+
for (const [name, schema] of Object.entries(flagSchemas)) {
|
|
125
|
+
const value = resolved[name];
|
|
126
|
+
if (schema.kind === "array" && schema.unique && Array.isArray(value)) resolved[name] = [...new Set(value)];
|
|
127
|
+
if (schema.pathChecks !== void 0 && typeof value === "string" && stat !== void 0) {
|
|
128
|
+
const violation = await validatePathChecks(name, value, schema.pathChecks, stat);
|
|
129
|
+
if (violation !== void 0) errors.push(violation);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (isNonEmpty(errors)) throwAggregatedErrors(errors);
|
|
133
|
+
return resolved;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Check a resolved path value against its declared filesystem expectations.
|
|
137
|
+
*
|
|
138
|
+
* @returns `undefined` when the path satisfies the checks, or a
|
|
139
|
+
* {@link ValidationError} with code `'CONSTRAINT_VIOLATED'` otherwise.
|
|
140
|
+
* @internal
|
|
141
|
+
*/
|
|
142
|
+
async function validatePathChecks(flagName, value, checks, stat) {
|
|
143
|
+
const found = await stat(value);
|
|
144
|
+
if (found === null) return new ValidationError(`Path '${value}' for flag --${flagName} does not exist`, {
|
|
145
|
+
code: "CONSTRAINT_VIOLATED",
|
|
146
|
+
details: {
|
|
147
|
+
flag: flagName,
|
|
148
|
+
value,
|
|
149
|
+
constraint: "mustExist"
|
|
150
|
+
},
|
|
151
|
+
suggest: `Provide an existing path for --${flagName}`
|
|
152
|
+
});
|
|
153
|
+
if (checks.type !== void 0 && found !== checks.type) return new ValidationError(`Path '${value}' for flag --${flagName} is a ${found}, expected a ${checks.type}`, {
|
|
154
|
+
code: "CONSTRAINT_VIOLATED",
|
|
155
|
+
details: {
|
|
156
|
+
flag: flagName,
|
|
157
|
+
value,
|
|
158
|
+
constraint: "pathType",
|
|
159
|
+
expected: checks.type
|
|
160
|
+
},
|
|
161
|
+
suggest: `Provide a ${checks.type} path for --${flagName}`
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
/** Maps each flag kind to the prompt kinds that produce compatible values. */
|
|
165
|
+
const COMPATIBLE_PROMPT_KINDS = {
|
|
166
|
+
boolean: ["confirm"],
|
|
167
|
+
string: ["input", "select"],
|
|
168
|
+
number: ["input"],
|
|
169
|
+
enum: ["select", "input"],
|
|
170
|
+
array: ["multiselect"],
|
|
171
|
+
custom: [
|
|
172
|
+
"input",
|
|
173
|
+
"select",
|
|
174
|
+
"confirm",
|
|
175
|
+
"multiselect"
|
|
176
|
+
],
|
|
177
|
+
count: [],
|
|
178
|
+
keyValue: []
|
|
179
|
+
};
|
|
180
|
+
/**
|
|
181
|
+
* Check whether a prompt kind is compatible with the flag's declared kind.
|
|
182
|
+
*
|
|
183
|
+
* @returns `undefined` when compatible, or a {@link ValidationError} with
|
|
184
|
+
* code `'CONSTRAINT_VIOLATED'` and an actionable `suggest` message when not.
|
|
185
|
+
* @internal
|
|
186
|
+
*/
|
|
187
|
+
function validatePromptFlagCompatibility(flagName, flagKind, promptKind) {
|
|
188
|
+
const allowed = COMPATIBLE_PROMPT_KINDS[flagKind];
|
|
189
|
+
if (allowed.includes(promptKind)) return void 0;
|
|
190
|
+
const first = allowed[0];
|
|
191
|
+
if (first === void 0) return new ValidationError(`Prompt kind '${promptKind}' is not compatible with ${flagKind} flag --${flagName}. ${flagKind} flags are not promptable`, {
|
|
192
|
+
code: "CONSTRAINT_VIOLATED",
|
|
193
|
+
details: {
|
|
194
|
+
flag: flagName,
|
|
195
|
+
flagKind,
|
|
196
|
+
promptKind,
|
|
197
|
+
allowed
|
|
198
|
+
},
|
|
199
|
+
suggest: `Remove the prompt config for --${flagName}`
|
|
200
|
+
});
|
|
201
|
+
return new ValidationError(`Prompt kind '${promptKind}' is not compatible with ${flagKind} flag --${flagName}. Use '${first}' instead`, {
|
|
202
|
+
code: "CONSTRAINT_VIOLATED",
|
|
203
|
+
details: {
|
|
204
|
+
flag: flagName,
|
|
205
|
+
flagKind,
|
|
206
|
+
promptKind,
|
|
207
|
+
allowed
|
|
208
|
+
},
|
|
209
|
+
suggest: `Change the prompt to { kind: '${first}' } for --${flagName}`
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Validate prompt/flag compatibility, run the prompt engine, and coerce the result.
|
|
214
|
+
*
|
|
215
|
+
* Returns early with a {@link ValidationError} if the prompt kind is
|
|
216
|
+
* incompatible with the flag kind (checked via {@link COMPATIBLE_PROMPT_KINDS}
|
|
217
|
+
* before the prompter is invoked).
|
|
218
|
+
* @internal
|
|
219
|
+
*/
|
|
220
|
+
async function resolvePromptValueWithConfig(flagName, schema, promptConfig, prompter) {
|
|
221
|
+
const mismatch = validatePromptFlagCompatibility(flagName, schema.kind, promptConfig.kind);
|
|
222
|
+
if (mismatch !== void 0) return {
|
|
223
|
+
ok: false,
|
|
224
|
+
error: mismatch
|
|
225
|
+
};
|
|
226
|
+
const resolvedConfig = resolvePromptConfig(promptConfig, schema.enumValues);
|
|
227
|
+
const result = await prompter.promptOne(resolvedConfig);
|
|
228
|
+
if (!result.answered) return {
|
|
229
|
+
ok: false,
|
|
230
|
+
error: void 0
|
|
231
|
+
};
|
|
232
|
+
if (promptConfig.kind === "input" && promptConfig.default === void 0 && typeof result.value === "string" && result.value.trim() === "") return {
|
|
233
|
+
ok: false,
|
|
234
|
+
error: void 0
|
|
235
|
+
};
|
|
236
|
+
return coerceValue(flagName, { kind: "prompt" }, result.value, schema);
|
|
237
|
+
}
|
|
238
|
+
/** Build a human-readable suggestion listing all available sources for a required flag. @internal */
|
|
239
|
+
function buildRequiredFlagSuggest(name, schema) {
|
|
240
|
+
const sources = [];
|
|
241
|
+
const takesValue = schema.kind !== "boolean" && schema.kind !== "count";
|
|
242
|
+
sources.push(`Provide --${name}${takesValue ? " <value>" : ""}`);
|
|
243
|
+
if (schema.envVar !== void 0) sources.push(`set ${schema.envVar}`);
|
|
244
|
+
if (schema.configPath !== void 0) sources.push(`add ${schema.configPath} to config`);
|
|
245
|
+
if (sources.length <= 1) return sources.join("");
|
|
246
|
+
const rest = sources.slice(0, -1);
|
|
247
|
+
const last = sources.slice(-1).join("");
|
|
248
|
+
return sources.length === 2 ? `${rest.join("")} or ${last}` : `${rest.join(", ")}, or ${last}`;
|
|
249
|
+
}
|
|
250
|
+
//#endregion
|
|
251
|
+
export { COMPATIBLE_PROMPT_KINDS, resolveFlags };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { ParseResult } from "../parse/index.mjs";
|
|
2
1
|
import { DeprecationWarning, ResolveOptions, ResolveResult } from "./contracts.mjs";
|
|
3
2
|
import { CommandSchema } from "../schema/command.mjs";
|
|
4
|
-
|
|
3
|
+
import "../schema/index.mjs";
|
|
4
|
+
import { ParseResult } from "../parse/index.mjs";
|
|
5
5
|
//#region src/core/resolve/index.d.ts
|
|
6
6
|
/**
|
|
7
7
|
* Resolve parsed values against a command schema.
|
|
@@ -1 +1,85 @@
|
|
|
1
|
-
import{isValidationError
|
|
1
|
+
import { isValidationError } from "../errors/index.mjs";
|
|
2
|
+
import { collectValidationErrors, isNonEmpty, throwAggregatedErrors } from "./errors.mjs";
|
|
3
|
+
import { resolveArgs } from "./args.mjs";
|
|
4
|
+
import { resolveFlags } from "./flags.mjs";
|
|
5
|
+
//#region src/core/resolve/index.ts
|
|
6
|
+
/**
|
|
7
|
+
* Resolution chain: CLI -> env -> config -> prompt -> default.
|
|
8
|
+
*
|
|
9
|
+
* v0.1 implemented: **CLI parsed value -> default value**.
|
|
10
|
+
* v0.2 adds: **env variable resolution** and **config object resolution**
|
|
11
|
+
* between CLI and default.
|
|
12
|
+
* v0.3 adds: **prompt resolution** between config and default.
|
|
13
|
+
*
|
|
14
|
+
* The resolver takes raw {@linkcode ParseResult} (from the parser) and a
|
|
15
|
+
* {@linkcode CommandSchema}, applies the resolution chain, and validates
|
|
16
|
+
* that all required flags/args are present. On success it returns a
|
|
17
|
+
* {@linkcode ResolveResult} with fully resolved values; on failure it throws
|
|
18
|
+
* {@linkcode ValidationError}.
|
|
19
|
+
*
|
|
20
|
+
* @module dreamcli/core/resolve
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Resolve parsed values against a command schema.
|
|
24
|
+
*
|
|
25
|
+
* Low-level API: most applications should rely on `cli().run()`, `.execute()`,
|
|
26
|
+
* or `runCommand()`, which already call {@linkcode resolve} at the right time.
|
|
27
|
+
* Reach for this function when testing precedence rules directly or building
|
|
28
|
+
* custom execution flows around {@linkcode CommandSchema}.
|
|
29
|
+
*
|
|
30
|
+
* Resolution order:
|
|
31
|
+
* 1. CLI parsed value (from {@linkcode ParseResult})
|
|
32
|
+
* 2. Env variable (from {@linkcode ResolveOptions.env}, if flag declares `envVar`)
|
|
33
|
+
* 3. Config value (from {@linkcode ResolveOptions.config}, if flag declares `configPath`)
|
|
34
|
+
* 4. Prompt (from {@linkcode ResolveOptions.prompter}, if flag declares `prompt`)
|
|
35
|
+
* 5. Default value (from schema)
|
|
36
|
+
*
|
|
37
|
+
* After resolution, validates that all required flags and args have
|
|
38
|
+
* a value. Collects **all** validation errors before throwing, so the
|
|
39
|
+
* user sees every missing field at once.
|
|
40
|
+
*
|
|
41
|
+
* @param schema - The command schema defining flags and args
|
|
42
|
+
* @param parsed - Raw parsed values from the parser
|
|
43
|
+
* @param options - External state for the resolution chain
|
|
44
|
+
* @returns Fully resolved flag and arg values
|
|
45
|
+
* @throws {@linkcode ValidationError} if any required flag or arg is missing,
|
|
46
|
+
* or if an env/config value fails coercion
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* const parsed = parse(deploy.schema, ['production']);
|
|
51
|
+
* const resolved = await resolve(deploy.schema, parsed, {
|
|
52
|
+
* env: { DEPLOY_REGION: 'eu' },
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
async function resolve(schema, parsed, options) {
|
|
57
|
+
const stdinData = options?.stdinData;
|
|
58
|
+
const env = options?.env ?? {};
|
|
59
|
+
const config = options?.config ?? {};
|
|
60
|
+
const prompter = options?.prompter;
|
|
61
|
+
const deprecations = [];
|
|
62
|
+
let flags = {};
|
|
63
|
+
let args = {};
|
|
64
|
+
const errors = [];
|
|
65
|
+
try {
|
|
66
|
+
flags = await resolveFlags(schema.flags, parsed.flags, env, config, prompter, schema.interactive, deprecations, options?.stat);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
if (!isValidationError(error)) throw error;
|
|
69
|
+
errors.push(...collectValidationErrors(error));
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
args = resolveArgs(schema.args, parsed.args, stdinData, env, deprecations);
|
|
73
|
+
} catch (error) {
|
|
74
|
+
if (!isValidationError(error)) throw error;
|
|
75
|
+
errors.push(...collectValidationErrors(error));
|
|
76
|
+
}
|
|
77
|
+
if (isNonEmpty(errors)) throwAggregatedErrors(errors);
|
|
78
|
+
return {
|
|
79
|
+
flags,
|
|
80
|
+
args,
|
|
81
|
+
deprecations
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
//#endregion
|
|
85
|
+
export { resolve };
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import { NumberConstraints } from "../schema/number-constraints.mjs";
|
|
2
2
|
import { ArgSchema } from "../schema/arg.mjs";
|
|
3
|
+
import { StringConstraints } from "../schema/string-constraints.mjs";
|
|
3
4
|
import { FlagSchema } from "../schema/flag.mjs";
|
|
4
|
-
|
|
5
|
+
import "../schema/index.mjs";
|
|
5
6
|
//#region src/core/resolve/property.d.ts
|
|
6
7
|
/** Flag/arg kinds that share a common coercion path. */
|
|
7
|
-
declare const SHARED_PROPERTY_MODEL_KINDS: readonly [
|
|
8
|
+
declare const SHARED_PROPERTY_MODEL_KINDS: readonly ["string", "number", "enum", "custom"];
|
|
8
9
|
/** Union of kind discriminants that participate in shared coercion. */
|
|
9
10
|
type SharedPropertyKind = (typeof SHARED_PROPERTY_MODEL_KINDS)[number];
|
|
10
11
|
/** Minimal coercion-relevant slice of a flag or arg schema, discriminated by kind. */
|
|
11
12
|
type SharedPropertySchema = {
|
|
12
13
|
readonly kind: 'string';
|
|
14
|
+
readonly stringConstraints: StringConstraints | undefined;
|
|
13
15
|
} | {
|
|
14
16
|
readonly kind: 'number';
|
|
15
17
|
readonly numberConstraints: NumberConstraints | undefined;
|
|
@@ -1 +1,53 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/core/resolve/property.ts
|
|
2
|
+
/** Extract the shared coercion slice from a flag schema, or `undefined` for boolean/array (handled separately). */
|
|
3
|
+
function toSharedFlagPropertySchema(schema) {
|
|
4
|
+
switch (schema.kind) {
|
|
5
|
+
case "string": return {
|
|
6
|
+
kind: "string",
|
|
7
|
+
stringConstraints: schema.stringConstraints
|
|
8
|
+
};
|
|
9
|
+
case "number": return {
|
|
10
|
+
kind: "number",
|
|
11
|
+
numberConstraints: schema.numberConstraints
|
|
12
|
+
};
|
|
13
|
+
case "enum": return {
|
|
14
|
+
kind: "enum",
|
|
15
|
+
enumValues: schema.enumValues
|
|
16
|
+
};
|
|
17
|
+
case "custom": return schema.parseFn === void 0 ? { kind: "custom" } : {
|
|
18
|
+
kind: "custom",
|
|
19
|
+
parseFn: schema.parseFn
|
|
20
|
+
};
|
|
21
|
+
case "boolean":
|
|
22
|
+
case "array":
|
|
23
|
+
case "count":
|
|
24
|
+
case "keyValue": return;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** Extract the shared coercion slice from an arg schema (all arg kinds are shared). */
|
|
28
|
+
function toSharedArgPropertySchema(schema) {
|
|
29
|
+
switch (schema.kind) {
|
|
30
|
+
case "string": return {
|
|
31
|
+
kind: "string",
|
|
32
|
+
stringConstraints: void 0
|
|
33
|
+
};
|
|
34
|
+
case "number": return {
|
|
35
|
+
kind: "number",
|
|
36
|
+
numberConstraints: schema.numberConstraints
|
|
37
|
+
};
|
|
38
|
+
case "enum": return {
|
|
39
|
+
kind: "enum",
|
|
40
|
+
enumValues: schema.enumValues
|
|
41
|
+
};
|
|
42
|
+
case "custom": {
|
|
43
|
+
if (schema.parseFn === void 0) return { kind: "custom" };
|
|
44
|
+
const parseFn = schema.parseFn;
|
|
45
|
+
return {
|
|
46
|
+
kind: "custom",
|
|
47
|
+
parseFn: (raw) => parseFn(typeof raw === "string" ? raw : String(raw))
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
export { toSharedArgPropertySchema, toSharedFlagPropertySchema };
|
|
@@ -87,34 +87,54 @@ interface ProgressHandle {
|
|
|
87
87
|
* without polluting stdout/stderr arrays.
|
|
88
88
|
*/
|
|
89
89
|
type ActivityEvent = {
|
|
90
|
-
/** Spinner created and started. */
|
|
90
|
+
/** Spinner created and started. */
|
|
91
|
+
readonly type: 'spinner:start';
|
|
92
|
+
/** Initial spinner text. */
|
|
91
93
|
readonly text: string;
|
|
92
94
|
} | {
|
|
93
|
-
/** Spinner text changed. */
|
|
95
|
+
/** Spinner text changed. */
|
|
96
|
+
readonly type: 'spinner:update';
|
|
97
|
+
/** Updated spinner text. */
|
|
94
98
|
readonly text: string;
|
|
95
99
|
} | {
|
|
96
|
-
/** Spinner completed successfully. */
|
|
100
|
+
/** Spinner completed successfully. */
|
|
101
|
+
readonly type: 'spinner:succeed';
|
|
102
|
+
/** Success message. */
|
|
97
103
|
readonly text: string;
|
|
98
104
|
} | {
|
|
99
|
-
/** Spinner stopped with an error. */
|
|
105
|
+
/** Spinner stopped with an error. */
|
|
106
|
+
readonly type: 'spinner:fail';
|
|
107
|
+
/** Failure message. */
|
|
100
108
|
readonly text: string;
|
|
101
109
|
} | {
|
|
102
|
-
/** Spinner stopped without a final status. */
|
|
110
|
+
/** Spinner stopped without a final status. */
|
|
111
|
+
readonly type: 'spinner:stop';
|
|
103
112
|
} | {
|
|
104
|
-
/** Progress bar created and started. */
|
|
105
|
-
readonly
|
|
113
|
+
/** Progress bar created and started. */
|
|
114
|
+
readonly type: 'progress:start';
|
|
115
|
+
/** Bar label. */
|
|
116
|
+
readonly label: string;
|
|
117
|
+
/** Known total, or `undefined` for indeterminate. */
|
|
106
118
|
readonly total: number | undefined;
|
|
107
119
|
} | {
|
|
108
|
-
/** Progress bar advanced. */
|
|
120
|
+
/** Progress bar advanced. */
|
|
121
|
+
readonly type: 'progress:increment';
|
|
122
|
+
/** Amount advanced. */
|
|
109
123
|
readonly delta: number;
|
|
110
124
|
} | {
|
|
111
|
-
/** Progress bar set to an absolute value. */
|
|
125
|
+
/** Progress bar set to an absolute value. */
|
|
126
|
+
readonly type: 'progress:update';
|
|
127
|
+
/** New absolute value. */
|
|
112
128
|
readonly value: number;
|
|
113
129
|
} | {
|
|
114
|
-
/** Progress bar completed. */
|
|
130
|
+
/** Progress bar completed. */
|
|
131
|
+
readonly type: 'progress:done';
|
|
132
|
+
/** Completion message, if any. */
|
|
115
133
|
readonly text: string | undefined;
|
|
116
134
|
} | {
|
|
117
|
-
/** Progress bar stopped with an error. */
|
|
135
|
+
/** Progress bar stopped with an error. */
|
|
136
|
+
readonly type: 'progress:fail';
|
|
137
|
+
/** Failure message, if any. */
|
|
118
138
|
readonly text: string | undefined;
|
|
119
139
|
};
|
|
120
140
|
/**
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { NumberConstraints } from "./number-constraints.mjs";
|
|
2
|
-
|
|
3
2
|
//#region src/core/schema/arg.d.ts
|
|
4
3
|
/** All arg presence states as a runtime array. */
|
|
5
|
-
declare const ARG_PRESENCES: readonly [
|
|
4
|
+
declare const ARG_PRESENCES: readonly ["required", "optional", "defaulted"];
|
|
6
5
|
/**
|
|
7
6
|
* Presence describes whether a positional arg is guaranteed to exist when the
|
|
8
7
|
* action handler runs:
|
|
@@ -60,9 +59,9 @@ type ResolvedArgValue<C extends ArgConfig> = C['variadic'] extends true ? C['val
|
|
|
60
59
|
/** Extract the resolved value type from an {@linkcode ArgBuilder}. */
|
|
61
60
|
type InferArg<B> = B extends ArgBuilder<infer C extends ArgConfig> ? ResolvedArgValue<C> : never;
|
|
62
61
|
/** Extract resolved value types from a record of builders. */
|
|
63
|
-
type InferArgs<T extends Record<string, ArgBuilder<ArgConfig>>> = { [K in keyof T]: InferArg<T[K]
|
|
62
|
+
type InferArgs<T extends Record<string, ArgBuilder<ArgConfig>>> = { [K in keyof T]: InferArg<T[K]>; };
|
|
64
63
|
/** All arg kind discriminators as a runtime array. */
|
|
65
|
-
declare const ARG_KINDS: readonly [
|
|
64
|
+
declare const ARG_KINDS: readonly ["string", "number", "enum", "custom"];
|
|
66
65
|
/** Discriminator for the kind of value an arg accepts. */
|
|
67
66
|
type ArgKind = (typeof ARG_KINDS)[number];
|
|
68
67
|
/** Custom parse function for `arg.custom()`. */
|