@kjanat/dreamcli 3.0.0-rc.1 → 3.0.0-rc.3
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 +37 -1
- package/dist/core/cli/dispatch.mjs +214 -1
- package/dist/core/cli/help-links.mjs +41 -1
- package/dist/core/cli/index.mjs +901 -1
- package/dist/core/cli/planner.mjs +300 -1
- package/dist/core/cli/plugin.mjs +41 -1
- package/dist/core/cli/propagate.mjs +57 -1
- package/dist/core/cli/root-help.mjs +208 -4
- package/dist/core/cli/root-surface.mjs +27 -1
- package/dist/core/cli/runtime-preflight.mjs +168 -1
- package/dist/core/completion/index.mjs +90 -1
- package/dist/core/completion/shells/bash.mjs +304 -3
- package/dist/core/completion/shells/fish.mjs +159 -2
- package/dist/core/completion/shells/powershell.mjs +353 -2
- package/dist/core/completion/shells/shared.mjs +154 -1
- package/dist/core/completion/shells/zsh.mjs +244 -2
- package/dist/core/config/index.mjs +246 -1
- package/dist/core/config/package-json.mjs +385 -3
- package/dist/core/errors/index.mjs +87 -1
- package/dist/core/execution/index.mjs +189 -1
- package/dist/core/help/ansi.mjs +122 -2
- package/dist/core/help/index.mjs +329 -7
- package/dist/core/json-schema/index.mjs +487 -9
- package/dist/core/json-schema/meta-descriptions.generated.mjs +93 -28
- package/dist/core/output/activity.mjs +452 -1
- package/dist/core/output/bind.mjs +56 -1
- 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 -0
- package/dist/core/output/index.mjs +442 -2
- package/dist/core/output/renderers.mjs +46 -1
- package/dist/core/output/writer.mjs +12 -1
- package/dist/core/parse/index.mjs +555 -1
- package/dist/core/prompt/index.d.mts +0 -1
- package/dist/core/prompt/index.mjs +348 -6
- package/dist/core/resolve/args.mjs +105 -1
- package/dist/core/resolve/coerce.mjs +279 -1
- package/dist/core/resolve/config.mjs +19 -1
- package/dist/core/resolve/errors.mjs +140 -2
- package/dist/core/resolve/flags.mjs +196 -1
- package/dist/core/resolve/index.mjs +85 -1
- package/dist/core/resolve/property.d.mts +0 -1
- package/dist/core/resolve/property.mjs +45 -1
- package/dist/core/schema/arg.mjs +463 -1
- package/dist/core/schema/command.d.mts +18 -0
- package/dist/core/schema/command.mjs +712 -1
- package/dist/core/schema/flag.mjs +453 -1
- package/dist/core/schema/middleware.mjs +41 -1
- package/dist/core/schema/number-constraints.mjs +60 -1
- package/dist/core/schema-dsl/runtime.mjs +271 -2
- package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
- package/dist/core/testkit/index.mjs +83 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +22 -1
- package/dist/runtime/adapter.mjs +82 -1
- package/dist/runtime/auto.mjs +46 -1
- package/dist/runtime/bun.mjs +28 -1
- package/dist/runtime/deno.mjs +217 -2
- package/dist/runtime/detect.mjs +45 -1
- package/dist/runtime/node.mjs +169 -2
- package/dist/runtime/paths.mjs +48 -1
- package/dist/runtime.mjs +7 -1
- package/dist/strings.mjs +22 -1
- package/dist/testkit.mjs +5 -1
- package/dist/version.d.mts +7 -3
- package/dist/version.mjs +7 -1
- package/package.json +21 -26
- 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,279 @@
|
|
|
1
|
-
import{describeNumberConstraintViolation
|
|
1
|
+
import { describeNumberConstraintViolation, validateNumberConstraints } from "../schema/number-constraints.mjs";
|
|
2
|
+
import { ValidationError } from "../errors/index.mjs";
|
|
3
|
+
import { toSharedArgPropertySchema, toSharedFlagPropertySchema } from "./property.mjs";
|
|
4
|
+
//#region src/core/resolve/coerce.ts
|
|
5
|
+
function sourceLabel(source) {
|
|
6
|
+
switch (source.kind) {
|
|
7
|
+
case "env": return `from env ${source.envVar}`;
|
|
8
|
+
case "config": return `from config ${source.configPath}`;
|
|
9
|
+
case "prompt": return "from prompt";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function sourceDetails(source) {
|
|
13
|
+
switch (source.kind) {
|
|
14
|
+
case "env": return { envVar: source.envVar };
|
|
15
|
+
case "config": return { configPath: source.configPath };
|
|
16
|
+
case "prompt": return { source: "prompt" };
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function coercionError(flagName, source, code, expected, raw, messageSuffix, suggest, extraDetails) {
|
|
20
|
+
return {
|
|
21
|
+
ok: false,
|
|
22
|
+
error: new ValidationError(`${messageSuffix} ${sourceLabel(source)} for flag --${flagName}`, {
|
|
23
|
+
code,
|
|
24
|
+
details: {
|
|
25
|
+
flag: flagName,
|
|
26
|
+
...sourceDetails(source),
|
|
27
|
+
value: raw,
|
|
28
|
+
expected,
|
|
29
|
+
...extraDetails
|
|
30
|
+
},
|
|
31
|
+
suggest
|
|
32
|
+
})
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Apply numeric constraints to an already-parsed number. Shares the single
|
|
37
|
+
* {@link validateNumberConstraints} check used by the parse path, so the two
|
|
38
|
+
* boundaries cannot drift. On violation, returns a `CONSTRAINT_VIOLATED`
|
|
39
|
+
* failure; the human-readable reason is also surfaced in `details.constraint`
|
|
40
|
+
* (the violation kind) and the message suffix.
|
|
41
|
+
*/
|
|
42
|
+
function finalizeNumber(flagName, source, raw, value, constraints) {
|
|
43
|
+
const violation = validateNumberConstraints(value, constraints);
|
|
44
|
+
if (violation === void 0) return {
|
|
45
|
+
ok: true,
|
|
46
|
+
value
|
|
47
|
+
};
|
|
48
|
+
const reason = describeNumberConstraintViolation(violation);
|
|
49
|
+
return {
|
|
50
|
+
ok: false,
|
|
51
|
+
error: new ValidationError(`Invalid number value '${String(raw)}' ${sourceLabel(source)} for flag --${flagName}: ${reason}`, {
|
|
52
|
+
code: "CONSTRAINT_VIOLATED",
|
|
53
|
+
details: {
|
|
54
|
+
flag: flagName,
|
|
55
|
+
...sourceDetails(source),
|
|
56
|
+
value: raw,
|
|
57
|
+
expected: "number",
|
|
58
|
+
constraint: violation.kind,
|
|
59
|
+
..."bound" in violation ? { bound: violation.bound } : {}
|
|
60
|
+
},
|
|
61
|
+
suggest: source.kind === "env" ? `Set ${source.envVar} to a valid number` : source.kind === "config" ? `Set ${source.configPath} to a valid number in your config` : `Enter a valid number for --${flagName}`
|
|
62
|
+
})
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/** Coerce a raw value from env/config/prompt into the type declared by a flag schema. */
|
|
66
|
+
function coerceValue(flagName, source, raw, schema) {
|
|
67
|
+
const sharedSchema = toSharedFlagPropertySchema(schema);
|
|
68
|
+
if (sharedSchema !== void 0) return coerceSharedPropertyValue(flagName, source, raw, sharedSchema);
|
|
69
|
+
switch (schema.kind) {
|
|
70
|
+
case "boolean":
|
|
71
|
+
if (typeof raw === "boolean") return {
|
|
72
|
+
ok: true,
|
|
73
|
+
value: raw
|
|
74
|
+
};
|
|
75
|
+
if (typeof raw === "string") {
|
|
76
|
+
const lower = raw.toLowerCase();
|
|
77
|
+
if (lower === "true" || lower === "1" || lower === "yes" || source.kind === "prompt" && lower === "y") return {
|
|
78
|
+
ok: true,
|
|
79
|
+
value: true
|
|
80
|
+
};
|
|
81
|
+
if (lower === "false" || lower === "0" || lower === "no" || lower === "" || source.kind === "prompt" && lower === "n") return {
|
|
82
|
+
ok: true,
|
|
83
|
+
value: false
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
return coercionError(flagName, source, "TYPE_MISMATCH", "boolean", raw, typeof raw === "string" ? `Invalid boolean value '${raw}'` : "Invalid boolean value", source.kind === "env" ? `Set ${source.envVar} to true/false, 1/0, or yes/no` : source.kind === "config" ? `Set ${source.configPath} to true or false in your config` : `Answer yes or no for --${flagName}`);
|
|
87
|
+
case "array":
|
|
88
|
+
if (Array.isArray(raw)) {
|
|
89
|
+
if (schema.elementSchema) {
|
|
90
|
+
const coerced = [];
|
|
91
|
+
for (const element of raw) {
|
|
92
|
+
const result = coerceValue(flagName, source, element, schema.elementSchema);
|
|
93
|
+
if (!result.ok) return result;
|
|
94
|
+
coerced.push(result.value);
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
ok: true,
|
|
98
|
+
value: coerced
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
ok: true,
|
|
103
|
+
value: raw
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
if (typeof raw === "string") {
|
|
107
|
+
if (raw === "") return {
|
|
108
|
+
ok: true,
|
|
109
|
+
value: []
|
|
110
|
+
};
|
|
111
|
+
const parts = raw.split(",");
|
|
112
|
+
if (schema.elementSchema) {
|
|
113
|
+
const coerced = [];
|
|
114
|
+
for (const part of parts) {
|
|
115
|
+
const result = coerceValue(flagName, source, source.kind === "prompt" ? part.trim() : part, schema.elementSchema);
|
|
116
|
+
if (!result.ok) return result;
|
|
117
|
+
coerced.push(result.value);
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
ok: true,
|
|
121
|
+
value: coerced
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
ok: true,
|
|
126
|
+
value: source.kind === "prompt" ? parts.map((part) => part.trim()) : parts
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
return coercionError(flagName, source, "TYPE_MISMATCH", "array", raw, "Invalid array value", source.kind === "env" ? `Set ${source.envVar} to comma-separated values` : source.kind === "config" ? `Set ${source.configPath} to an array in your config` : `Provide valid values for --${flagName}`);
|
|
130
|
+
}
|
|
131
|
+
throw new Error(`Unreachable flag coercion kind: ${schema.kind}`);
|
|
132
|
+
}
|
|
133
|
+
function coerceSharedPropertyValue(flagName, source, raw, schema) {
|
|
134
|
+
switch (schema.kind) {
|
|
135
|
+
case "string":
|
|
136
|
+
if (typeof raw === "string") return {
|
|
137
|
+
ok: true,
|
|
138
|
+
value: raw
|
|
139
|
+
};
|
|
140
|
+
if (source.kind === "prompt") return {
|
|
141
|
+
ok: true,
|
|
142
|
+
value: String(raw)
|
|
143
|
+
};
|
|
144
|
+
if (source.kind === "config" && (typeof raw === "number" || typeof raw === "boolean")) return {
|
|
145
|
+
ok: true,
|
|
146
|
+
value: String(raw)
|
|
147
|
+
};
|
|
148
|
+
return coercionError(flagName, source, "TYPE_MISMATCH", "string", raw, "Invalid string value", source.kind === "config" ? `Set ${source.configPath} to a string in your config` : `Enter a valid string for --${flagName}`);
|
|
149
|
+
case "number":
|
|
150
|
+
if (typeof raw === "number") {
|
|
151
|
+
if (Number.isNaN(raw)) return coercionError(flagName, source, "TYPE_MISMATCH", "number", raw, "Invalid number value NaN", source.kind === "env" ? `Set ${source.envVar} to a valid number` : source.kind === "config" ? `Set ${source.configPath} to a valid number in your config` : `Enter a valid number for --${flagName}`);
|
|
152
|
+
return finalizeNumber(flagName, source, raw, raw, schema.numberConstraints);
|
|
153
|
+
}
|
|
154
|
+
if (typeof raw === "string") {
|
|
155
|
+
const value = Number(raw);
|
|
156
|
+
if (!Number.isNaN(value)) return finalizeNumber(flagName, source, raw, value, schema.numberConstraints);
|
|
157
|
+
}
|
|
158
|
+
return coercionError(flagName, source, "TYPE_MISMATCH", "number", raw, typeof raw === "string" ? `Invalid number value '${raw}'` : "Invalid number value", source.kind === "env" ? `Set ${source.envVar} to a valid number` : source.kind === "config" ? `Set ${source.configPath} to a valid number in your config` : `Enter a valid number for --${flagName}`);
|
|
159
|
+
case "enum": {
|
|
160
|
+
const allowed = schema.enumValues ?? [];
|
|
161
|
+
if (typeof raw === "string" && allowed.includes(raw)) return {
|
|
162
|
+
ok: true,
|
|
163
|
+
value: raw
|
|
164
|
+
};
|
|
165
|
+
return {
|
|
166
|
+
ok: false,
|
|
167
|
+
error: new ValidationError(`Invalid value '${String(raw)}' ${sourceLabel(source)} for flag --${flagName}. Allowed: ${allowed.join(", ")}`, {
|
|
168
|
+
code: "INVALID_ENUM",
|
|
169
|
+
details: {
|
|
170
|
+
flag: flagName,
|
|
171
|
+
...sourceDetails(source),
|
|
172
|
+
value: raw,
|
|
173
|
+
allowed
|
|
174
|
+
},
|
|
175
|
+
suggest: source.kind === "env" ? `Set ${source.envVar} to one of: ${allowed.join(", ")}` : source.kind === "config" ? `Set ${source.configPath} to one of: ${allowed.join(", ")}` : `Select one of: ${allowed.join(", ")}`
|
|
176
|
+
})
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
case "custom":
|
|
180
|
+
if (schema.parseFn === void 0) return {
|
|
181
|
+
ok: true,
|
|
182
|
+
value: raw
|
|
183
|
+
};
|
|
184
|
+
try {
|
|
185
|
+
return {
|
|
186
|
+
ok: true,
|
|
187
|
+
value: schema.parseFn(raw)
|
|
188
|
+
};
|
|
189
|
+
} catch (error) {
|
|
190
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
191
|
+
return {
|
|
192
|
+
ok: false,
|
|
193
|
+
error: new ValidationError(`Failed to parse ${source.kind === "env" ? `env ${source.envVar}` : source.kind === "config" ? `config ${source.configPath}` : "prompt value"} for flag --${flagName}: ${message}`, {
|
|
194
|
+
code: "TYPE_MISMATCH",
|
|
195
|
+
details: {
|
|
196
|
+
flag: flagName,
|
|
197
|
+
...sourceDetails(source),
|
|
198
|
+
value: raw,
|
|
199
|
+
expected: "custom"
|
|
200
|
+
},
|
|
201
|
+
suggest: source.kind === "env" ? `Set ${source.envVar} to a valid value for --${flagName}` : source.kind === "config" ? `Set ${source.configPath} to a valid value for --${flagName} in your config` : `Enter a valid value for --${flagName}`
|
|
202
|
+
})
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
function argSourceLabel(source) {
|
|
208
|
+
return source.kind === "env" ? `from env ${source.envVar}` : "from stdin";
|
|
209
|
+
}
|
|
210
|
+
function argSourceDetails(source) {
|
|
211
|
+
return source.kind === "env" ? { envVar: source.envVar } : { source: "stdin" };
|
|
212
|
+
}
|
|
213
|
+
function buildArgCoercionSuggest(argName, source, expected) {
|
|
214
|
+
if (source.kind === "env") return expected === "number" ? `Set ${source.envVar} to a valid number` : `Set ${source.envVar} to a valid value for <${argName}>`;
|
|
215
|
+
return expected === "number" ? `Pipe a valid number to stdin for <${argName}>` : `Pipe a valid value to stdin for <${argName}>`;
|
|
216
|
+
}
|
|
217
|
+
function argSourceToCoerceSource(source) {
|
|
218
|
+
return source.kind === "env" ? {
|
|
219
|
+
kind: "env",
|
|
220
|
+
envVar: source.envVar
|
|
221
|
+
} : { kind: "prompt" };
|
|
222
|
+
}
|
|
223
|
+
function redactArgCoercionMessage(argName, source, schema, error) {
|
|
224
|
+
switch (schema.kind) {
|
|
225
|
+
case "number": {
|
|
226
|
+
const base = `Invalid number value '<redacted>' ${argSourceLabel(source)} for argument <${argName}>`;
|
|
227
|
+
if (error.code !== "CONSTRAINT_VIOLATED") return base;
|
|
228
|
+
const suffix = /: ([^:]+)$/.exec(error.message)?.[1];
|
|
229
|
+
return suffix !== void 0 ? `${base}: ${suffix}` : base;
|
|
230
|
+
}
|
|
231
|
+
case "enum": {
|
|
232
|
+
const allowed = Array.isArray(error.details?.allowed) ? error.details.allowed.filter((value) => typeof value === "string") : [];
|
|
233
|
+
return `Invalid value '<redacted>' ${argSourceLabel(source)} for argument <${argName}>. Allowed: ${allowed.join(", ")}`;
|
|
234
|
+
}
|
|
235
|
+
case "custom": {
|
|
236
|
+
const suffix = /: ([^:]+)$/.exec(error.message)?.[1];
|
|
237
|
+
return suffix !== void 0 ? `Invalid value '<redacted>' ${argSourceLabel(source)} for argument <${argName}>: ${suffix}` : `Invalid value '<redacted>' ${argSourceLabel(source)} for argument <${argName}>`;
|
|
238
|
+
}
|
|
239
|
+
case "string": return `Invalid value '<redacted>' ${argSourceLabel(source)} for argument <${argName}>`;
|
|
240
|
+
default: return schema.kind;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
function redactArgCoercionDetails(argName, source, schema, error) {
|
|
244
|
+
return {
|
|
245
|
+
arg: argName,
|
|
246
|
+
...argSourceDetails(source),
|
|
247
|
+
...schema.kind === "number" || schema.kind === "custom" ? { expected: schema.kind } : {},
|
|
248
|
+
...schema.kind === "number" && typeof error.details?.constraint === "string" ? { constraint: error.details.constraint } : {},
|
|
249
|
+
...schema.kind === "number" && typeof error.details?.bound === "number" ? { bound: error.details.bound } : {},
|
|
250
|
+
...schema.kind === "enum" && Array.isArray(error.details?.allowed) ? { allowed: error.details.allowed.filter((value) => typeof value === "string") } : {}
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
function redactArgCoercionSuggest(argName, source, schema, error) {
|
|
254
|
+
if (schema.kind === "number" || schema.kind === "custom") return buildArgCoercionSuggest(argName, source, schema.kind);
|
|
255
|
+
if (schema.kind === "enum") {
|
|
256
|
+
const allowedList = (Array.isArray(error.details?.allowed) ? error.details.allowed.filter((value) => typeof value === "string") : []).join(", ");
|
|
257
|
+
return source.kind === "env" ? `Set ${source.envVar} to one of: ${allowedList}` : `Provide one of: ${allowedList}`;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
/** Coerce a string value from stdin/env into the type declared by an arg schema, redacting raw values in error diagnostics. */
|
|
261
|
+
function coerceArgStringValue(argName, source, raw, schema) {
|
|
262
|
+
const coerced = coerceSharedPropertyValue(argName, argSourceToCoerceSource(source), raw, toSharedArgPropertySchema(schema));
|
|
263
|
+
if (coerced.ok) return coerced;
|
|
264
|
+
const suggest = redactArgCoercionSuggest(argName, source, schema, coerced.error);
|
|
265
|
+
const options = suggest === void 0 ? {
|
|
266
|
+
code: coerced.error.code,
|
|
267
|
+
details: redactArgCoercionDetails(argName, source, schema, coerced.error)
|
|
268
|
+
} : {
|
|
269
|
+
code: coerced.error.code,
|
|
270
|
+
details: redactArgCoercionDetails(argName, source, schema, coerced.error),
|
|
271
|
+
suggest
|
|
272
|
+
};
|
|
273
|
+
return {
|
|
274
|
+
ok: false,
|
|
275
|
+
error: new ValidationError(redactArgCoercionMessage(argName, source, schema, coerced.error), options)
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
//#endregion
|
|
279
|
+
export { coerceArgStringValue, coerceValue };
|
|
@@ -1 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/core/resolve/config.ts
|
|
2
|
+
/**
|
|
3
|
+
* Internal resolver config helpers.
|
|
4
|
+
*
|
|
5
|
+
* @module dreamcli/core/resolve/config
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
function resolveConfigPath(config, path) {
|
|
9
|
+
const segments = path.split(".");
|
|
10
|
+
let current = config;
|
|
11
|
+
for (const segment of segments) {
|
|
12
|
+
if (current === null || current === void 0 || typeof current !== "object") return;
|
|
13
|
+
if (!Object.hasOwn(current, segment)) return;
|
|
14
|
+
current = current[segment];
|
|
15
|
+
}
|
|
16
|
+
return current;
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
export { resolveConfigPath };
|
|
@@ -1,2 +1,140 @@
|
|
|
1
|
-
import{ValidationError
|
|
2
|
-
|
|
1
|
+
import { ValidationError } from "../errors/index.mjs";
|
|
2
|
+
//#region src/core/resolve/errors.ts
|
|
3
|
+
function isRecord(value) {
|
|
4
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
5
|
+
}
|
|
6
|
+
function readStringProperty(record, key) {
|
|
7
|
+
const value = Reflect.get(record, key);
|
|
8
|
+
return typeof value === "string" ? value : void 0;
|
|
9
|
+
}
|
|
10
|
+
function hasValueProperty(record, key) {
|
|
11
|
+
return Object.hasOwn(record, key);
|
|
12
|
+
}
|
|
13
|
+
function describeIssueSource(details) {
|
|
14
|
+
const explicitSource = readStringProperty(details, "source");
|
|
15
|
+
if (explicitSource === "stdin") return {
|
|
16
|
+
kind: "stdin",
|
|
17
|
+
label: "stdin"
|
|
18
|
+
};
|
|
19
|
+
if (explicitSource === "prompt") return {
|
|
20
|
+
kind: "prompt",
|
|
21
|
+
label: "prompt"
|
|
22
|
+
};
|
|
23
|
+
if (hasValueProperty(details, "value")) {
|
|
24
|
+
const envVar = readStringProperty(details, "envVar");
|
|
25
|
+
if (envVar !== void 0) return {
|
|
26
|
+
kind: "env",
|
|
27
|
+
label: `env ${envVar}`
|
|
28
|
+
};
|
|
29
|
+
const configPath = readStringProperty(details, "configPath");
|
|
30
|
+
if (configPath !== void 0) return {
|
|
31
|
+
kind: "config",
|
|
32
|
+
label: `config ${configPath}`
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function summarizeValidationError(error) {
|
|
37
|
+
const details = error.details;
|
|
38
|
+
if (!isRecord(details)) return {
|
|
39
|
+
code: error.code,
|
|
40
|
+
inputKind: "input",
|
|
41
|
+
label: "input",
|
|
42
|
+
message: error.message
|
|
43
|
+
};
|
|
44
|
+
const flag = readStringProperty(details, "flag");
|
|
45
|
+
if (flag !== void 0) {
|
|
46
|
+
const source = describeIssueSource(details);
|
|
47
|
+
return {
|
|
48
|
+
code: error.code,
|
|
49
|
+
inputKind: "flag",
|
|
50
|
+
name: flag,
|
|
51
|
+
label: `flag --${flag}`,
|
|
52
|
+
message: error.message,
|
|
53
|
+
...source !== void 0 ? {
|
|
54
|
+
sourceKind: source.kind,
|
|
55
|
+
sourceLabel: source.label
|
|
56
|
+
} : {}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
const arg = readStringProperty(details, "arg");
|
|
60
|
+
if (arg !== void 0) {
|
|
61
|
+
const source = describeIssueSource(details);
|
|
62
|
+
return {
|
|
63
|
+
code: error.code,
|
|
64
|
+
inputKind: "arg",
|
|
65
|
+
name: arg,
|
|
66
|
+
label: `argument <${arg}>`,
|
|
67
|
+
message: error.message,
|
|
68
|
+
...source !== void 0 ? {
|
|
69
|
+
sourceKind: source.kind,
|
|
70
|
+
sourceLabel: source.label
|
|
71
|
+
} : {}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
code: error.code,
|
|
76
|
+
inputKind: "input",
|
|
77
|
+
label: "input",
|
|
78
|
+
message: error.message
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function formatAggregateHeadline(issues) {
|
|
82
|
+
const flagCount = issues.filter((issue) => issue.inputKind === "flag").length;
|
|
83
|
+
const argCount = issues.filter((issue) => issue.inputKind === "arg").length;
|
|
84
|
+
const otherCount = issues.length - flagCount - argCount;
|
|
85
|
+
const counts = [];
|
|
86
|
+
if (flagCount > 0) counts.push(`${flagCount} flag${flagCount === 1 ? "" : "s"}`);
|
|
87
|
+
if (argCount > 0) counts.push(`${argCount} arg${argCount === 1 ? "" : "s"}`);
|
|
88
|
+
if (otherCount > 0) counts.push(`${otherCount} input${otherCount === 1 ? "" : "s"}`);
|
|
89
|
+
return counts.length === 0 ? `Multiple validation errors (${issues.length})` : `Multiple validation errors (${counts.join(", ")})`;
|
|
90
|
+
}
|
|
91
|
+
function formatAggregateLine(issue) {
|
|
92
|
+
const source = issue.sourceLabel !== void 0 ? ` [${issue.sourceLabel}]` : "";
|
|
93
|
+
return `${issue.label}${source}: ${issue.message}`;
|
|
94
|
+
}
|
|
95
|
+
/** Type-narrowing guard that asserts a readonly array has at least one element. */
|
|
96
|
+
function isNonEmpty(arr) {
|
|
97
|
+
return arr.length > 0;
|
|
98
|
+
}
|
|
99
|
+
function isValidationErrorJson(value) {
|
|
100
|
+
if (value === null || typeof value !== "object") return false;
|
|
101
|
+
if (!("code" in value) || !("message" in value) || !("exitCode" in value)) return false;
|
|
102
|
+
if (typeof value.code !== "string" || typeof value.message !== "string") return false;
|
|
103
|
+
if (typeof value.exitCode !== "number") return false;
|
|
104
|
+
if ("suggest" in value && value.suggest !== void 0 && typeof value.suggest !== "string") return false;
|
|
105
|
+
if ("details" in value && value.details !== void 0 && (value.details === null || typeof value.details !== "object")) return false;
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
/** Flatten an aggregate validation error's nested `details.errors` into individual errors, or return the original as a singleton. */
|
|
109
|
+
function collectValidationErrors(error) {
|
|
110
|
+
const nestedErrors = error.details?.errors;
|
|
111
|
+
if (!Array.isArray(nestedErrors)) return [error];
|
|
112
|
+
const flattened = [];
|
|
113
|
+
for (const entry of nestedErrors) {
|
|
114
|
+
if (!isValidationErrorJson(entry)) return [error];
|
|
115
|
+
flattened.push(new ValidationError(entry.message, {
|
|
116
|
+
code: entry.code,
|
|
117
|
+
exitCode: entry.exitCode,
|
|
118
|
+
...entry.suggest !== void 0 ? { suggest: entry.suggest } : {},
|
|
119
|
+
...entry.details !== void 0 ? { details: entry.details } : {}
|
|
120
|
+
}));
|
|
121
|
+
}
|
|
122
|
+
return flattened;
|
|
123
|
+
}
|
|
124
|
+
/** Throw a single error if one, or an aggregate with per-issue summaries if many. */
|
|
125
|
+
function throwAggregatedErrors(errors) {
|
|
126
|
+
if (errors.length === 1) throw errors[0];
|
|
127
|
+
const issues = errors.map(summarizeValidationError);
|
|
128
|
+
const lines = issues.map(formatAggregateLine);
|
|
129
|
+
throw new ValidationError(`${formatAggregateHeadline(issues)}:\n - ${lines.join("\n - ")}`, {
|
|
130
|
+
code: errors[0].code,
|
|
131
|
+
details: {
|
|
132
|
+
errors: errors.map((error) => error.toJSON()),
|
|
133
|
+
count: errors.length,
|
|
134
|
+
issues
|
|
135
|
+
},
|
|
136
|
+
suggest: "Fix the listed validation errors and retry"
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
//#endregion
|
|
140
|
+
export { collectValidationErrors, isNonEmpty, throwAggregatedErrors };
|
|
@@ -1 +1,196 @@
|
|
|
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) {
|
|
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.presence === "required") {
|
|
105
|
+
const details = {
|
|
106
|
+
flag: name,
|
|
107
|
+
kind: schema.kind
|
|
108
|
+
};
|
|
109
|
+
if (schema.envVar !== void 0) details.envVar = schema.envVar;
|
|
110
|
+
if (schema.configPath !== void 0) details.configPath = schema.configPath;
|
|
111
|
+
errors.push(new ValidationError(`Missing required flag --${name}`, {
|
|
112
|
+
code: "REQUIRED_FLAG",
|
|
113
|
+
details,
|
|
114
|
+
suggest: buildRequiredFlagSuggest(name, schema)
|
|
115
|
+
}));
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
resolved[name] = void 0;
|
|
119
|
+
}
|
|
120
|
+
if (isNonEmpty(errors)) throwAggregatedErrors(errors);
|
|
121
|
+
return resolved;
|
|
122
|
+
}
|
|
123
|
+
/** Maps each flag kind to the prompt kinds that produce compatible values. */
|
|
124
|
+
const COMPATIBLE_PROMPT_KINDS = {
|
|
125
|
+
boolean: ["confirm"],
|
|
126
|
+
string: ["input", "select"],
|
|
127
|
+
number: ["input"],
|
|
128
|
+
enum: ["select", "input"],
|
|
129
|
+
array: ["multiselect"],
|
|
130
|
+
custom: [
|
|
131
|
+
"input",
|
|
132
|
+
"select",
|
|
133
|
+
"confirm",
|
|
134
|
+
"multiselect"
|
|
135
|
+
]
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* Check whether a prompt kind is compatible with the flag's declared kind.
|
|
139
|
+
*
|
|
140
|
+
* @returns `undefined` when compatible, or a {@link ValidationError} with
|
|
141
|
+
* code `'CONSTRAINT_VIOLATED'` and an actionable `suggest` message when not.
|
|
142
|
+
* @internal
|
|
143
|
+
*/
|
|
144
|
+
function validatePromptFlagCompatibility(flagName, flagKind, promptKind) {
|
|
145
|
+
const allowed = COMPATIBLE_PROMPT_KINDS[flagKind];
|
|
146
|
+
if (allowed.includes(promptKind)) return void 0;
|
|
147
|
+
return new ValidationError(`Prompt kind '${promptKind}' is not compatible with ${flagKind} flag --${flagName}. Use '${allowed[0]}' instead`, {
|
|
148
|
+
code: "CONSTRAINT_VIOLATED",
|
|
149
|
+
details: {
|
|
150
|
+
flag: flagName,
|
|
151
|
+
flagKind,
|
|
152
|
+
promptKind,
|
|
153
|
+
allowed
|
|
154
|
+
},
|
|
155
|
+
suggest: `Change the prompt to { kind: '${allowed[0]}' } for --${flagName}`
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Validate prompt/flag compatibility, run the prompt engine, and coerce the result.
|
|
160
|
+
*
|
|
161
|
+
* Returns early with a {@link ValidationError} if the prompt kind is
|
|
162
|
+
* incompatible with the flag kind (checked via {@link COMPATIBLE_PROMPT_KINDS}
|
|
163
|
+
* before the prompter is invoked).
|
|
164
|
+
* @internal
|
|
165
|
+
*/
|
|
166
|
+
async function resolvePromptValueWithConfig(flagName, schema, promptConfig, prompter) {
|
|
167
|
+
const mismatch = validatePromptFlagCompatibility(flagName, schema.kind, promptConfig.kind);
|
|
168
|
+
if (mismatch !== void 0) return {
|
|
169
|
+
ok: false,
|
|
170
|
+
error: mismatch
|
|
171
|
+
};
|
|
172
|
+
const resolvedConfig = resolvePromptConfig(promptConfig, schema.enumValues);
|
|
173
|
+
const result = await prompter.promptOne(resolvedConfig);
|
|
174
|
+
if (!result.answered) return {
|
|
175
|
+
ok: false,
|
|
176
|
+
error: void 0
|
|
177
|
+
};
|
|
178
|
+
if (promptConfig.kind === "input" && promptConfig.default === void 0 && typeof result.value === "string" && result.value.trim() === "") return {
|
|
179
|
+
ok: false,
|
|
180
|
+
error: void 0
|
|
181
|
+
};
|
|
182
|
+
return coerceValue(flagName, { kind: "prompt" }, result.value, schema);
|
|
183
|
+
}
|
|
184
|
+
/** Build a human-readable suggestion listing all available sources for a required flag. @internal */
|
|
185
|
+
function buildRequiredFlagSuggest(name, schema) {
|
|
186
|
+
const sources = [];
|
|
187
|
+
sources.push(`Provide --${name}${schema.kind !== "boolean" ? " <value>" : ""}`);
|
|
188
|
+
if (schema.envVar !== void 0) sources.push(`set ${schema.envVar}`);
|
|
189
|
+
if (schema.configPath !== void 0) sources.push(`add ${schema.configPath} to config`);
|
|
190
|
+
if (sources.length <= 1) return sources.join("");
|
|
191
|
+
const rest = sources.slice(0, -1);
|
|
192
|
+
const last = sources.slice(-1).join("");
|
|
193
|
+
return sources.length === 2 ? `${rest.join("")} or ${last}` : `${rest.join(", ")}, or ${last}`;
|
|
194
|
+
}
|
|
195
|
+
//#endregion
|
|
196
|
+
export { resolveFlags };
|