@kjanat/dreamcli 3.0.0-rc.1 → 3.0.0-rc.2

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.
Files changed (136) hide show
  1. package/CHANGELOG.md +26 -1
  2. package/dist/core/cli/dispatch.mjs +214 -1
  3. package/dist/core/cli/help-links.mjs +41 -1
  4. package/dist/core/cli/index.mjs +901 -1
  5. package/dist/core/cli/planner.mjs +300 -1
  6. package/dist/core/cli/plugin.mjs +41 -1
  7. package/dist/core/cli/propagate.mjs +57 -1
  8. package/dist/core/cli/root-help.mjs +208 -4
  9. package/dist/core/cli/root-surface.mjs +27 -1
  10. package/dist/core/cli/runtime-preflight.mjs +168 -1
  11. package/dist/core/completion/index.mjs +90 -1
  12. package/dist/core/completion/shells/bash.mjs +304 -3
  13. package/dist/core/completion/shells/fish.mjs +159 -2
  14. package/dist/core/completion/shells/powershell.mjs +353 -2
  15. package/dist/core/completion/shells/shared.mjs +154 -1
  16. package/dist/core/completion/shells/zsh.mjs +244 -2
  17. package/dist/core/config/index.mjs +246 -1
  18. package/dist/core/config/package-json.mjs +385 -3
  19. package/dist/core/errors/index.mjs +87 -1
  20. package/dist/core/execution/index.mjs +189 -1
  21. package/dist/core/help/ansi.mjs +122 -2
  22. package/dist/core/help/index.mjs +329 -7
  23. package/dist/core/json-schema/index.mjs +487 -9
  24. package/dist/core/json-schema/meta-descriptions.generated.mjs +93 -28
  25. package/dist/core/output/activity.mjs +452 -1
  26. package/dist/core/output/bind.mjs +56 -1
  27. package/dist/core/output/contracts.mjs +57 -1
  28. package/dist/core/output/display-value.mjs +32 -1
  29. package/dist/core/output/index.d.mts +21 -0
  30. package/dist/core/output/index.mjs +442 -2
  31. package/dist/core/output/renderers.mjs +46 -1
  32. package/dist/core/output/writer.mjs +12 -1
  33. package/dist/core/parse/index.mjs +555 -1
  34. package/dist/core/prompt/index.d.mts +0 -1
  35. package/dist/core/prompt/index.mjs +348 -6
  36. package/dist/core/resolve/args.mjs +105 -1
  37. package/dist/core/resolve/coerce.mjs +279 -1
  38. package/dist/core/resolve/config.mjs +19 -1
  39. package/dist/core/resolve/errors.mjs +140 -2
  40. package/dist/core/resolve/flags.mjs +196 -1
  41. package/dist/core/resolve/index.mjs +85 -1
  42. package/dist/core/resolve/property.d.mts +0 -1
  43. package/dist/core/resolve/property.mjs +45 -1
  44. package/dist/core/schema/arg.mjs +463 -1
  45. package/dist/core/schema/command.d.mts +18 -0
  46. package/dist/core/schema/command.mjs +712 -1
  47. package/dist/core/schema/flag.mjs +453 -1
  48. package/dist/core/schema/middleware.mjs +41 -1
  49. package/dist/core/schema/number-constraints.mjs +60 -1
  50. package/dist/core/schema-dsl/runtime.mjs +271 -2
  51. package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
  52. package/dist/core/testkit/index.mjs +83 -1
  53. package/dist/index.d.mts +2 -1
  54. package/dist/index.mjs +22 -1
  55. package/dist/runtime/adapter.mjs +82 -1
  56. package/dist/runtime/auto.mjs +46 -1
  57. package/dist/runtime/bun.mjs +28 -1
  58. package/dist/runtime/deno.mjs +217 -2
  59. package/dist/runtime/detect.mjs +45 -1
  60. package/dist/runtime/node.mjs +169 -2
  61. package/dist/runtime/paths.mjs +48 -1
  62. package/dist/runtime.mjs +7 -1
  63. package/dist/strings.mjs +22 -1
  64. package/dist/testkit.mjs +5 -1
  65. package/dist/version.mjs +7 -1
  66. package/package.json +17 -26
  67. package/src/core/cli/dispatch.ts +0 -355
  68. package/src/core/cli/help-links.ts +0 -82
  69. package/src/core/cli/index.ts +0 -1614
  70. package/src/core/cli/planner.ts +0 -656
  71. package/src/core/cli/plugin.ts +0 -117
  72. package/src/core/cli/propagate.ts +0 -97
  73. package/src/core/cli/root-help.ts +0 -342
  74. package/src/core/cli/root-surface.ts +0 -87
  75. package/src/core/cli/runtime-preflight.ts +0 -456
  76. package/src/core/completion/index.ts +0 -138
  77. package/src/core/completion/shells/bash.ts +0 -459
  78. package/src/core/completion/shells/fish.ts +0 -259
  79. package/src/core/completion/shells/powershell.ts +0 -439
  80. package/src/core/completion/shells/shared.ts +0 -310
  81. package/src/core/completion/shells/zsh.ts +0 -381
  82. package/src/core/config/index.ts +0 -417
  83. package/src/core/config/package-json.ts +0 -560
  84. package/src/core/errors/index.ts +0 -193
  85. package/src/core/execution/index.ts +0 -265
  86. package/src/core/help/ansi.ts +0 -144
  87. package/src/core/help/index.ts +0 -563
  88. package/src/core/json-schema/index.ts +0 -910
  89. package/src/core/json-schema/meta-descriptions.generated.ts +0 -202
  90. package/src/core/output/activity.ts +0 -575
  91. package/src/core/output/bind.ts +0 -59
  92. package/src/core/output/contracts.ts +0 -194
  93. package/src/core/output/display-value.ts +0 -48
  94. package/src/core/output/index.ts +0 -704
  95. package/src/core/output/renderers.ts +0 -125
  96. package/src/core/output/writer.ts +0 -46
  97. package/src/core/parse/index.ts +0 -742
  98. package/src/core/prompt/index.ts +0 -623
  99. package/src/core/resolve/args.ts +0 -144
  100. package/src/core/resolve/coerce.ts +0 -500
  101. package/src/core/resolve/config.ts +0 -26
  102. package/src/core/resolve/contracts.ts +0 -135
  103. package/src/core/resolve/errors.ts +0 -227
  104. package/src/core/resolve/flags.ts +0 -269
  105. package/src/core/resolve/index.ts +0 -108
  106. package/src/core/resolve/property.ts +0 -99
  107. package/src/core/schema/activity.ts +0 -240
  108. package/src/core/schema/arg.ts +0 -841
  109. package/src/core/schema/command.ts +0 -1500
  110. package/src/core/schema/flag.ts +0 -917
  111. package/src/core/schema/index.ts +0 -96
  112. package/src/core/schema/middleware.ts +0 -171
  113. package/src/core/schema/number-constraints.ts +0 -153
  114. package/src/core/schema/prompt.ts +0 -171
  115. package/src/core/schema/run.ts +0 -198
  116. package/src/core/schema-dsl/index.ts +0 -109
  117. package/src/core/schema-dsl/parse.ts +0 -262
  118. package/src/core/schema-dsl/runtime.ts +0 -609
  119. package/src/core/schema-dsl/to-json-schema.ts +0 -102
  120. package/src/core/testkit/index.ts +0 -147
  121. package/src/index.ts +0 -181
  122. package/src/runtime/adapter.ts +0 -303
  123. package/src/runtime/auto.ts +0 -86
  124. package/src/runtime/bun.ts +0 -48
  125. package/src/runtime/deno.ts +0 -367
  126. package/src/runtime/detect.ts +0 -105
  127. package/src/runtime/index.ts +0 -21
  128. package/src/runtime/node-builtins.d.ts +0 -27
  129. package/src/runtime/node.ts +0 -289
  130. package/src/runtime/paths.ts +0 -64
  131. package/src/runtime/support.ts +0 -77
  132. package/src/runtime.ts +0 -31
  133. package/src/schema.ts +0 -16
  134. package/src/strings.ts +0 -34
  135. package/src/testkit.ts +0 -25
  136. package/src/version.ts +0 -20
@@ -1,144 +0,0 @@
1
- /**
2
- * Internal arg resolution helpers.
3
- *
4
- * @module dreamcli/core/resolve/args
5
- * @internal
6
- */
7
-
8
- import { ValidationError } from '#internals/core/errors/index.ts';
9
- import type { ArgSchema, CommandArgEntry } from '#internals/core/schema/index.ts';
10
- import { coerceArgStringValue } from './coerce.ts';
11
- import type { DeprecationWarning } from './contracts.ts';
12
- import { isNonEmpty, throwAggregatedErrors } from './errors.ts';
13
-
14
- /** Walk every declared arg through the resolution chain (cli -> stdin -> env -> default), collecting deprecations and throwing aggregated errors. */
15
- function resolveArgs(
16
- argEntries: readonly CommandArgEntry[],
17
- parsedArgs: Readonly<Record<string, unknown>>,
18
- stdinData: string | null | undefined,
19
- env: Readonly<Record<string, string | undefined>>,
20
- deprecations: DeprecationWarning[],
21
- ): Readonly<Record<string, unknown>> {
22
- const resolved: Record<string, unknown> = {};
23
- const errors: ValidationError[] = [];
24
-
25
- for (const entry of argEntries) {
26
- const { name, schema } = entry;
27
- const hasParsedValue = Object.hasOwn(parsedArgs, name);
28
- const parsedValue = parsedArgs[name];
29
- const usesCliValue =
30
- hasParsedValue &&
31
- parsedValue !== undefined &&
32
- (!schema.stdinMode || parsedValue !== '-') &&
33
- !(schema.variadic && Array.isArray(parsedValue) && parsedValue.length === 0);
34
-
35
- if (usesCliValue) {
36
- if (schema.deprecated !== undefined) {
37
- deprecations.push({ kind: 'arg', name, message: schema.deprecated });
38
- }
39
- resolved[name] = parsedValue;
40
- continue;
41
- }
42
-
43
- if (schema.stdinMode && (!hasParsedValue || parsedValue === undefined || parsedValue === '-')) {
44
- if (stdinData !== undefined && stdinData !== null) {
45
- const coerced = coerceArgStringValue(name, { kind: 'stdin' }, stdinData, schema);
46
- if (coerced.ok) {
47
- if (schema.deprecated !== undefined) {
48
- deprecations.push({ kind: 'arg', name, message: schema.deprecated });
49
- }
50
- resolved[name] = coerced.value;
51
- continue;
52
- }
53
- errors.push(coerced.error);
54
- continue;
55
- }
56
- }
57
-
58
- if (schema.envVar !== undefined) {
59
- const envValue = env[schema.envVar];
60
- if (envValue !== undefined) {
61
- const coerced = coerceArgStringValue(
62
- name,
63
- { kind: 'env', envVar: schema.envVar },
64
- envValue,
65
- schema,
66
- );
67
- if (coerced.ok) {
68
- if (schema.deprecated !== undefined) {
69
- deprecations.push({ kind: 'arg', name, message: schema.deprecated });
70
- }
71
- resolved[name] = coerced.value;
72
- continue;
73
- }
74
- errors.push(coerced.error);
75
- continue;
76
- }
77
- }
78
-
79
- if (schema.defaultValue !== undefined) {
80
- resolved[name] = schema.defaultValue;
81
- continue;
82
- }
83
-
84
- if (schema.variadic) {
85
- if (schema.presence === 'required') {
86
- errors.push(
87
- new ValidationError(`Missing required argument <${name}>`, {
88
- code: 'REQUIRED_ARG',
89
- details: { arg: name, variadic: true },
90
- suggest: buildRequiredArgSuggest(name, schema, true),
91
- }),
92
- );
93
- continue;
94
- }
95
- resolved[name] = [];
96
- continue;
97
- }
98
-
99
- if (schema.presence === 'required') {
100
- errors.push(
101
- new ValidationError(`Missing required argument <${name}>`, {
102
- code: 'REQUIRED_ARG',
103
- details: { arg: name },
104
- suggest: buildRequiredArgSuggest(name, schema),
105
- }),
106
- );
107
- continue;
108
- }
109
-
110
- resolved[name] = undefined;
111
- }
112
-
113
- if (isNonEmpty(errors)) {
114
- throwAggregatedErrors(errors);
115
- }
116
-
117
- return resolved;
118
- }
119
-
120
- function buildRequiredArgSuggest(name: string, schema: ArgSchema, variadic?: boolean): string {
121
- const sources: [string, ...string[]] = [
122
- variadic ? `Provide at least one value for <${name}>` : `Provide a value for <${name}>`,
123
- ];
124
-
125
- if (schema.stdinMode) {
126
- sources.push(`pipe a value to stdin or pass '-'`);
127
- }
128
-
129
- if (schema.envVar !== undefined) {
130
- sources.push(`set ${schema.envVar}`);
131
- }
132
-
133
- if (sources.length === 1) {
134
- return sources[0];
135
- }
136
-
137
- if (sources.length === 2) {
138
- return `${sources[0]} or ${sources[1]}`;
139
- }
140
-
141
- return `${sources.slice(0, -1).join(', ')}, or ${sources[sources.length - 1]}`;
142
- }
143
-
144
- export { resolveArgs };
@@ -1,500 +0,0 @@
1
- /**
2
- * Internal resolver coercion helpers.
3
- *
4
- * @module dreamcli/core/resolve/coerce
5
- * @internal
6
- */
7
-
8
- import type { ValidationErrorCode } from '#internals/core/errors/index.ts';
9
- import { ValidationError } from '#internals/core/errors/index.ts';
10
- import type { ArgSchema, FlagSchema, NumberConstraints } from '#internals/core/schema/index.ts';
11
- import {
12
- describeNumberConstraintViolation,
13
- validateNumberConstraints,
14
- } from '#internals/core/schema/index.ts';
15
- import type { ArgDiagnosticSource, FlagDiagnosticSource } from './contracts.ts';
16
- import type { SharedPropertySchema } from './property.ts';
17
- import { toSharedArgPropertySchema, toSharedFlagPropertySchema } from './property.ts';
18
-
19
- type CoerceSource = FlagDiagnosticSource;
20
-
21
- /** Discriminated result of a value coercion attempt — success with the coerced value, or failure with a structured validation error. */
22
- type CoerceResult =
23
- | { readonly ok: true; readonly value: unknown }
24
- | { readonly ok: false; readonly error: ValidationError };
25
-
26
- function sourceLabel(source: CoerceSource): string {
27
- switch (source.kind) {
28
- case 'env':
29
- return `from env ${source.envVar}`;
30
- case 'config':
31
- return `from config ${source.configPath}`;
32
- case 'prompt':
33
- return 'from prompt';
34
- }
35
- }
36
-
37
- function sourceDetails(source: CoerceSource): Record<string, unknown> {
38
- switch (source.kind) {
39
- case 'env':
40
- return { envVar: source.envVar };
41
- case 'config':
42
- return { configPath: source.configPath };
43
- case 'prompt':
44
- return { source: 'prompt' };
45
- }
46
- }
47
-
48
- function coercionError(
49
- flagName: string,
50
- source: CoerceSource,
51
- code: ValidationErrorCode,
52
- expected: string,
53
- raw: unknown,
54
- messageSuffix: string,
55
- suggest: string,
56
- extraDetails?: Record<string, unknown>,
57
- ): CoerceResult {
58
- return {
59
- ok: false,
60
- error: new ValidationError(`${messageSuffix} ${sourceLabel(source)} for flag --${flagName}`, {
61
- code,
62
- details: { flag: flagName, ...sourceDetails(source), value: raw, expected, ...extraDetails },
63
- suggest,
64
- }),
65
- };
66
- }
67
-
68
- /**
69
- * Apply numeric constraints to an already-parsed number. Shares the single
70
- * {@link validateNumberConstraints} check used by the parse path, so the two
71
- * boundaries cannot drift. On violation, returns a `CONSTRAINT_VIOLATED`
72
- * failure; the human-readable reason is also surfaced in `details.constraint`
73
- * (the violation kind) and the message suffix.
74
- */
75
- function finalizeNumber(
76
- flagName: string,
77
- source: CoerceSource,
78
- raw: unknown,
79
- value: number,
80
- constraints: NumberConstraints | undefined,
81
- ): CoerceResult {
82
- const violation = validateNumberConstraints(value, constraints);
83
- if (violation === undefined) {
84
- return { ok: true, value };
85
- }
86
-
87
- const reason = describeNumberConstraintViolation(violation);
88
- return {
89
- ok: false,
90
- error: new ValidationError(
91
- `Invalid number value '${String(raw)}' ${sourceLabel(source)} for flag --${flagName}: ${reason}`,
92
- {
93
- code: 'CONSTRAINT_VIOLATED',
94
- details: {
95
- flag: flagName,
96
- ...sourceDetails(source),
97
- value: raw,
98
- expected: 'number',
99
- constraint: violation.kind,
100
- ...('bound' in violation ? { bound: violation.bound } : {}),
101
- },
102
- suggest:
103
- source.kind === 'env'
104
- ? `Set ${source.envVar} to a valid number`
105
- : source.kind === 'config'
106
- ? `Set ${source.configPath} to a valid number in your config`
107
- : `Enter a valid number for --${flagName}`,
108
- },
109
- ),
110
- };
111
- }
112
-
113
- /** Coerce a raw value from env/config/prompt into the type declared by a flag schema. */
114
- function coerceValue(
115
- flagName: string,
116
- source: CoerceSource,
117
- raw: unknown,
118
- schema: FlagSchema,
119
- ): CoerceResult {
120
- const sharedSchema = toSharedFlagPropertySchema(schema);
121
- if (sharedSchema !== undefined) {
122
- return coerceSharedPropertyValue(flagName, source, raw, sharedSchema);
123
- }
124
-
125
- switch (schema.kind) {
126
- case 'boolean': {
127
- if (typeof raw === 'boolean') return { ok: true, value: raw };
128
- if (typeof raw === 'string') {
129
- const lower = raw.toLowerCase();
130
- const truthy =
131
- lower === 'true' ||
132
- lower === '1' ||
133
- lower === 'yes' ||
134
- (source.kind === 'prompt' && lower === 'y');
135
- if (truthy) return { ok: true, value: true };
136
- const falsy =
137
- lower === 'false' ||
138
- lower === '0' ||
139
- lower === 'no' ||
140
- lower === '' ||
141
- (source.kind === 'prompt' && lower === 'n');
142
- if (falsy) return { ok: true, value: false };
143
- }
144
- return coercionError(
145
- flagName,
146
- source,
147
- 'TYPE_MISMATCH',
148
- 'boolean',
149
- raw,
150
- typeof raw === 'string' ? `Invalid boolean value '${raw}'` : 'Invalid boolean value',
151
- source.kind === 'env'
152
- ? `Set ${source.envVar} to true/false, 1/0, or yes/no`
153
- : source.kind === 'config'
154
- ? `Set ${source.configPath} to true or false in your config`
155
- : `Answer yes or no for --${flagName}`,
156
- );
157
- }
158
-
159
- case 'array': {
160
- if (Array.isArray(raw)) {
161
- if (schema.elementSchema) {
162
- const coerced: unknown[] = [];
163
- for (const element of raw) {
164
- const result = coerceValue(flagName, source, element, schema.elementSchema);
165
- if (!result.ok) return result;
166
- coerced.push(result.value);
167
- }
168
- return { ok: true, value: coerced };
169
- }
170
- return { ok: true, value: raw };
171
- }
172
- if (typeof raw === 'string') {
173
- if (raw === '') return { ok: true, value: [] };
174
- const parts = raw.split(',');
175
- if (schema.elementSchema) {
176
- const coerced: unknown[] = [];
177
- for (const part of parts) {
178
- const element = source.kind === 'prompt' ? part.trim() : part;
179
- const result = coerceValue(flagName, source, element, schema.elementSchema);
180
- if (!result.ok) return result;
181
- coerced.push(result.value);
182
- }
183
- return { ok: true, value: coerced };
184
- }
185
- return {
186
- ok: true,
187
- value: source.kind === 'prompt' ? parts.map((part) => part.trim()) : parts,
188
- };
189
- }
190
- return coercionError(
191
- flagName,
192
- source,
193
- 'TYPE_MISMATCH',
194
- 'array',
195
- raw,
196
- 'Invalid array value',
197
- source.kind === 'env'
198
- ? `Set ${source.envVar} to comma-separated values`
199
- : source.kind === 'config'
200
- ? `Set ${source.configPath} to an array in your config`
201
- : `Provide valid values for --${flagName}`,
202
- );
203
- }
204
- }
205
-
206
- throw new Error(`Unreachable flag coercion kind: ${schema.kind}`);
207
- }
208
-
209
- function coerceSharedPropertyValue(
210
- flagName: string,
211
- source: CoerceSource,
212
- raw: unknown,
213
- schema: SharedPropertySchema,
214
- ): CoerceResult {
215
- switch (schema.kind) {
216
- case 'string': {
217
- if (typeof raw === 'string') return { ok: true, value: raw };
218
- if (source.kind === 'prompt') return { ok: true, value: String(raw) };
219
- if (source.kind === 'config' && (typeof raw === 'number' || typeof raw === 'boolean')) {
220
- return { ok: true, value: String(raw) };
221
- }
222
- return coercionError(
223
- flagName,
224
- source,
225
- 'TYPE_MISMATCH',
226
- 'string',
227
- raw,
228
- 'Invalid string value',
229
- source.kind === 'config'
230
- ? `Set ${source.configPath} to a string in your config`
231
- : `Enter a valid string for --${flagName}`,
232
- );
233
- }
234
-
235
- case 'number': {
236
- if (typeof raw === 'number') {
237
- if (Number.isNaN(raw)) {
238
- return coercionError(
239
- flagName,
240
- source,
241
- 'TYPE_MISMATCH',
242
- 'number',
243
- raw,
244
- 'Invalid number value NaN',
245
- source.kind === 'env'
246
- ? `Set ${source.envVar} to a valid number`
247
- : source.kind === 'config'
248
- ? `Set ${source.configPath} to a valid number in your config`
249
- : `Enter a valid number for --${flagName}`,
250
- );
251
- }
252
- return finalizeNumber(flagName, source, raw, raw, schema.numberConstraints);
253
- }
254
- if (typeof raw === 'string') {
255
- const value = Number(raw);
256
- if (!Number.isNaN(value)) {
257
- return finalizeNumber(flagName, source, raw, value, schema.numberConstraints);
258
- }
259
- }
260
- return coercionError(
261
- flagName,
262
- source,
263
- 'TYPE_MISMATCH',
264
- 'number',
265
- raw,
266
- typeof raw === 'string' ? `Invalid number value '${raw}'` : 'Invalid number value',
267
- source.kind === 'env'
268
- ? `Set ${source.envVar} to a valid number`
269
- : source.kind === 'config'
270
- ? `Set ${source.configPath} to a valid number in your config`
271
- : `Enter a valid number for --${flagName}`,
272
- );
273
- }
274
-
275
- case 'enum': {
276
- const allowed = schema.enumValues ?? [];
277
- if (typeof raw === 'string' && allowed.includes(raw)) {
278
- return { ok: true, value: raw };
279
- }
280
- return {
281
- ok: false,
282
- error: new ValidationError(
283
- `Invalid value '${String(raw)}' ${sourceLabel(source)} for flag --${flagName}. Allowed: ${allowed.join(', ')}`,
284
- {
285
- code: 'INVALID_ENUM',
286
- details: { flag: flagName, ...sourceDetails(source), value: raw, allowed },
287
- suggest:
288
- source.kind === 'env'
289
- ? `Set ${source.envVar} to one of: ${allowed.join(', ')}`
290
- : source.kind === 'config'
291
- ? `Set ${source.configPath} to one of: ${allowed.join(', ')}`
292
- : `Select one of: ${allowed.join(', ')}`,
293
- },
294
- ),
295
- };
296
- }
297
-
298
- case 'custom': {
299
- if (schema.parseFn === undefined) {
300
- return { ok: true, value: raw };
301
- }
302
-
303
- try {
304
- return { ok: true, value: schema.parseFn(raw) };
305
- } catch (error) {
306
- const message = error instanceof Error ? error.message : String(error);
307
- const sourceRef =
308
- source.kind === 'env'
309
- ? `env ${source.envVar}`
310
- : source.kind === 'config'
311
- ? `config ${source.configPath}`
312
- : 'prompt value';
313
- return {
314
- ok: false,
315
- error: new ValidationError(
316
- `Failed to parse ${sourceRef} for flag --${flagName}: ${message}`,
317
- {
318
- code: 'TYPE_MISMATCH',
319
- details: {
320
- flag: flagName,
321
- ...sourceDetails(source),
322
- value: raw,
323
- expected: 'custom',
324
- },
325
- suggest:
326
- source.kind === 'env'
327
- ? `Set ${source.envVar} to a valid value for --${flagName}`
328
- : source.kind === 'config'
329
- ? `Set ${source.configPath} to a valid value for --${flagName} in your config`
330
- : `Enter a valid value for --${flagName}`,
331
- },
332
- ),
333
- };
334
- }
335
- }
336
- }
337
- }
338
-
339
- type ArgStringSource = ArgDiagnosticSource;
340
-
341
- function argSourceLabel(source: ArgStringSource): string {
342
- return source.kind === 'env' ? `from env ${source.envVar}` : 'from stdin';
343
- }
344
-
345
- function argSourceDetails(source: ArgStringSource): Record<string, unknown> {
346
- return source.kind === 'env' ? { envVar: source.envVar } : { source: 'stdin' };
347
- }
348
-
349
- function buildArgCoercionSuggest(
350
- argName: string,
351
- source: ArgStringSource,
352
- expected: 'number' | 'custom',
353
- ): string {
354
- if (source.kind === 'env') {
355
- return expected === 'number'
356
- ? `Set ${source.envVar} to a valid number`
357
- : `Set ${source.envVar} to a valid value for <${argName}>`;
358
- }
359
-
360
- return expected === 'number'
361
- ? `Pipe a valid number to stdin for <${argName}>`
362
- : `Pipe a valid value to stdin for <${argName}>`;
363
- }
364
-
365
- function argSourceToCoerceSource(source: ArgStringSource): CoerceSource {
366
- return source.kind === 'env' ? { kind: 'env', envVar: source.envVar } : { kind: 'prompt' };
367
- }
368
-
369
- function redactArgCoercionMessage(
370
- argName: string,
371
- source: ArgStringSource,
372
- schema: ArgSchema,
373
- error: ValidationError,
374
- ): string {
375
- switch (schema.kind) {
376
- case 'number': {
377
- const base = `Invalid number value '<redacted>' ${argSourceLabel(source)} for argument <${argName}>`;
378
- // Only a constraint violation carries a schema-derived reason suffix
379
- // (e.g. `: must be >= 0`), which is safe to keep. Any other failure
380
- // (e.g. TYPE_MISMATCH) embeds the raw value in `error.message`, so
381
- // extracting a trailing suffix there could leak user input.
382
- if (error.code !== 'CONSTRAINT_VIOLATED') {
383
- return base;
384
- }
385
- const match = /: ([^:]+)$/.exec(error.message);
386
- const suffix = match?.[1];
387
- return suffix !== undefined ? `${base}: ${suffix}` : base;
388
- }
389
- case 'enum': {
390
- const allowed = Array.isArray(error.details?.allowed)
391
- ? error.details.allowed.filter((value): value is string => typeof value === 'string')
392
- : [];
393
- return `Invalid value '<redacted>' ${argSourceLabel(source)} for argument <${argName}>. Allowed: ${allowed.join(', ')}`;
394
- }
395
- case 'custom': {
396
- const match = /: ([^:]+)$/.exec(error.message);
397
- const suffix = match?.[1];
398
- return suffix !== undefined
399
- ? `Invalid value '<redacted>' ${argSourceLabel(source)} for argument <${argName}>: ${suffix}`
400
- : `Invalid value '<redacted>' ${argSourceLabel(source)} for argument <${argName}>`;
401
- }
402
- case 'string':
403
- return `Invalid value '<redacted>' ${argSourceLabel(source)} for argument <${argName}>`;
404
- default: {
405
- const exhaustive: never = schema.kind;
406
- return exhaustive;
407
- }
408
- }
409
- }
410
-
411
- function redactArgCoercionDetails(
412
- argName: string,
413
- source: ArgStringSource,
414
- schema: ArgSchema,
415
- error: ValidationError,
416
- ): Readonly<Record<string, unknown>> {
417
- return {
418
- arg: argName,
419
- ...argSourceDetails(source),
420
- ...(schema.kind === 'number' || schema.kind === 'custom' ? { expected: schema.kind } : {}),
421
- ...(schema.kind === 'number' && typeof error.details?.constraint === 'string'
422
- ? { constraint: error.details.constraint }
423
- : {}),
424
- ...(schema.kind === 'number' && typeof error.details?.bound === 'number'
425
- ? { bound: error.details.bound }
426
- : {}),
427
- ...(schema.kind === 'enum' && Array.isArray(error.details?.allowed)
428
- ? {
429
- allowed: error.details.allowed.filter(
430
- (value): value is string => typeof value === 'string',
431
- ),
432
- }
433
- : {}),
434
- };
435
- }
436
-
437
- function redactArgCoercionSuggest(
438
- argName: string,
439
- source: ArgStringSource,
440
- schema: ArgSchema,
441
- error: ValidationError,
442
- ): string | undefined {
443
- if (schema.kind === 'number' || schema.kind === 'custom') {
444
- return buildArgCoercionSuggest(argName, source, schema.kind);
445
- }
446
-
447
- if (schema.kind === 'enum') {
448
- const allowed = Array.isArray(error.details?.allowed)
449
- ? error.details.allowed.filter((value): value is string => typeof value === 'string')
450
- : [];
451
- const allowedList = allowed.join(', ');
452
- return source.kind === 'env'
453
- ? `Set ${source.envVar} to one of: ${allowedList}`
454
- : `Provide one of: ${allowedList}`;
455
- }
456
-
457
- return undefined;
458
- }
459
-
460
- /** Coerce a string value from stdin/env into the type declared by an arg schema, redacting raw values in error diagnostics. */
461
- function coerceArgStringValue(
462
- argName: string,
463
- source: ArgStringSource,
464
- raw: string,
465
- schema: ArgSchema,
466
- ): CoerceResult {
467
- const coerced = coerceSharedPropertyValue(
468
- argName,
469
- argSourceToCoerceSource(source),
470
- raw,
471
- toSharedArgPropertySchema(schema),
472
- );
473
- if (coerced.ok) {
474
- return coerced;
475
- }
476
-
477
- const suggest = redactArgCoercionSuggest(argName, source, schema, coerced.error);
478
- const options =
479
- suggest === undefined
480
- ? {
481
- code: coerced.error.code,
482
- details: redactArgCoercionDetails(argName, source, schema, coerced.error),
483
- }
484
- : {
485
- code: coerced.error.code,
486
- details: redactArgCoercionDetails(argName, source, schema, coerced.error),
487
- suggest,
488
- };
489
-
490
- return {
491
- ok: false,
492
- error: new ValidationError(
493
- redactArgCoercionMessage(argName, source, schema, coerced.error),
494
- options,
495
- ),
496
- };
497
- }
498
-
499
- export type { CoerceResult };
500
- export { coerceArgStringValue, coerceValue };
@@ -1,26 +0,0 @@
1
- /**
2
- * Internal resolver config helpers.
3
- *
4
- * @module dreamcli/core/resolve/config
5
- * @internal
6
- */
7
-
8
- function resolveConfigPath(config: Readonly<Record<string, unknown>>, path: string): unknown {
9
- const segments = path.split('.');
10
- let current: unknown = config;
11
-
12
- for (const segment of segments) {
13
- if (current === null || current === undefined || typeof current !== 'object') {
14
- return undefined;
15
- }
16
- if (!Object.hasOwn(current, segment)) {
17
- return undefined;
18
- }
19
- // Safe cast: guarded as a non-null object and verified to have `segment` above.
20
- current = (current as Record<string, unknown>)[segment];
21
- }
22
-
23
- return current;
24
- }
25
-
26
- export { resolveConfigPath };