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

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 (194) hide show
  1. package/CHANGELOG.md +217 -1
  2. package/dist/core/cli/dispatch.d.mts +6 -5
  3. package/dist/core/cli/dispatch.mjs +205 -1
  4. package/dist/core/cli/help-links.d.mts +0 -1
  5. package/dist/core/cli/help-links.mjs +41 -1
  6. package/dist/core/cli/index.d.mts +81 -8
  7. package/dist/core/cli/index.mjs +945 -1
  8. package/dist/core/cli/planner.d.mts +5 -1
  9. package/dist/core/cli/planner.mjs +300 -1
  10. package/dist/core/cli/plugin.d.mts +1 -1
  11. package/dist/core/cli/plugin.mjs +41 -1
  12. package/dist/core/cli/propagate.d.mts +1 -1
  13. package/dist/core/cli/propagate.mjs +57 -1
  14. package/dist/core/cli/root-help.d.mts +2 -1
  15. package/dist/core/cli/root-help.mjs +226 -4
  16. package/dist/core/cli/root-surface.d.mts +12 -1
  17. package/dist/core/cli/root-surface.mjs +28 -1
  18. package/dist/core/cli/runtime-preflight.d.mts +14 -2
  19. package/dist/core/cli/runtime-preflight.mjs +169 -1
  20. package/dist/core/completion/index.d.mts +0 -1
  21. package/dist/core/completion/index.mjs +90 -1
  22. package/dist/core/completion/shells/bash.d.mts +0 -1
  23. package/dist/core/completion/shells/bash.mjs +308 -3
  24. package/dist/core/completion/shells/fish.d.mts +0 -1
  25. package/dist/core/completion/shells/fish.mjs +162 -2
  26. package/dist/core/completion/shells/powershell.d.mts +0 -1
  27. package/dist/core/completion/shells/powershell.mjs +358 -2
  28. package/dist/core/completion/shells/shared.d.mts +13 -2
  29. package/dist/core/completion/shells/shared.mjs +176 -1
  30. package/dist/core/completion/shells/zsh.d.mts +0 -1
  31. package/dist/core/completion/shells/zsh.mjs +251 -2
  32. package/dist/core/config/index.d.mts +2 -3
  33. package/dist/core/config/index.mjs +246 -1
  34. package/dist/core/config/package-json.d.mts +27 -4
  35. package/dist/core/config/package-json.mjs +386 -3
  36. package/dist/core/errors/index.d.mts +3 -3
  37. package/dist/core/errors/index.mjs +87 -1
  38. package/dist/core/execution/index.d.mts +0 -1
  39. package/dist/core/execution/index.mjs +201 -1
  40. package/dist/core/help/ansi.d.mts +11 -5
  41. package/dist/core/help/ansi.mjs +121 -2
  42. package/dist/core/help/index.d.mts +22 -3
  43. package/dist/core/help/index.mjs +341 -7
  44. package/dist/core/help/theme.d.mts +68 -0
  45. package/dist/core/help/theme.mjs +46 -0
  46. package/dist/core/json-schema/index.d.mts +15 -2
  47. package/dist/core/json-schema/index.mjs +536 -12
  48. package/dist/core/json-schema/meta-descriptions.generated.d.mts +55 -38
  49. package/dist/core/json-schema/meta-descriptions.generated.mjs +102 -28
  50. package/dist/core/output/activity.d.mts +0 -1
  51. package/dist/core/output/activity.mjs +452 -1
  52. package/dist/core/output/bind.mjs +56 -1
  53. package/dist/core/output/contracts.d.mts +4 -5
  54. package/dist/core/output/contracts.mjs +57 -1
  55. package/dist/core/output/display-value.mjs +32 -1
  56. package/dist/core/output/index.d.mts +21 -1
  57. package/dist/core/output/index.mjs +442 -2
  58. package/dist/core/output/renderers.d.mts +0 -1
  59. package/dist/core/output/renderers.mjs +46 -1
  60. package/dist/core/output/writer.mjs +12 -1
  61. package/dist/core/parse/index.d.mts +40 -7
  62. package/dist/core/parse/index.mjs +727 -1
  63. package/dist/core/prompt/index.d.mts +1 -1
  64. package/dist/core/prompt/index.mjs +348 -6
  65. package/dist/core/resolve/args.d.mts +1 -1
  66. package/dist/core/resolve/args.mjs +105 -1
  67. package/dist/core/resolve/coerce.d.mts +1 -1
  68. package/dist/core/resolve/coerce.mjs +336 -1
  69. package/dist/core/resolve/config.mjs +19 -1
  70. package/dist/core/resolve/contracts.d.mts +9 -4
  71. package/dist/core/resolve/errors.d.mts +0 -1
  72. package/dist/core/resolve/errors.mjs +140 -2
  73. package/dist/core/resolve/flags.d.mts +7 -2
  74. package/dist/core/resolve/flags.mjs +251 -1
  75. package/dist/core/resolve/index.d.mts +2 -2
  76. package/dist/core/resolve/index.mjs +85 -1
  77. package/dist/core/resolve/property.d.mts +4 -2
  78. package/dist/core/resolve/property.mjs +53 -1
  79. package/dist/core/schema/activity.d.mts +31 -11
  80. package/dist/core/schema/arg.d.mts +3 -4
  81. package/dist/core/schema/arg.mjs +463 -1
  82. package/dist/core/schema/command.d.mts +26 -6
  83. package/dist/core/schema/command.mjs +726 -1
  84. package/dist/core/schema/flag.d.mts +485 -19
  85. package/dist/core/schema/flag.mjs +705 -1
  86. package/dist/core/schema/index.d.mts +4 -2
  87. package/dist/core/schema/middleware.d.mts +4 -3
  88. package/dist/core/schema/middleware.mjs +41 -1
  89. package/dist/core/schema/number-constraints.mjs +60 -1
  90. package/dist/core/schema/prompt.d.mts +6 -3
  91. package/dist/core/schema/run.d.mts +20 -1
  92. package/dist/core/schema/string-constraints.d.mts +97 -0
  93. package/dist/core/schema/string-constraints.mjs +69 -0
  94. package/dist/core/schema/value-parsers.d.mts +78 -0
  95. package/dist/core/schema/value-parsers.mjs +142 -0
  96. package/dist/core/schema-dsl/runtime.mjs +271 -2
  97. package/dist/core/schema-dsl/to-json-schema.d.mts +0 -1
  98. package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
  99. package/dist/core/testkit/index.d.mts +0 -31
  100. package/dist/core/testkit/index.mjs +83 -1
  101. package/dist/index.d.mts +11 -6
  102. package/dist/index.mjs +22 -1
  103. package/dist/runtime/adapter.d.mts +38 -2
  104. package/dist/runtime/adapter.mjs +87 -1
  105. package/dist/runtime/auto.d.mts +2 -3
  106. package/dist/runtime/auto.mjs +45 -1
  107. package/dist/runtime/deno.d.mts +24 -5
  108. package/dist/runtime/deno.mjs +262 -2
  109. package/dist/runtime/detect.mjs +45 -1
  110. package/dist/runtime/index.d.mts +3 -4
  111. package/dist/runtime/node.d.mts +8 -2
  112. package/dist/runtime/node.mjs +220 -2
  113. package/dist/runtime/paths.mjs +48 -1
  114. package/dist/runtime.d.mts +4 -4
  115. package/dist/runtime.mjs +6 -1
  116. package/dist/strings.mjs +22 -1
  117. package/dist/testkit.d.mts +1 -0
  118. package/dist/testkit.mjs +5 -1
  119. package/dist/version.d.mts +7 -3
  120. package/dist/version.mjs +7 -1
  121. package/dreamcli.schema.json +36 -7
  122. package/package.json +45 -42
  123. package/dist/runtime/bun.d.mts +0 -28
  124. package/dist/runtime/bun.mjs +0 -1
  125. package/src/core/cli/dispatch.ts +0 -355
  126. package/src/core/cli/help-links.ts +0 -82
  127. package/src/core/cli/index.ts +0 -1614
  128. package/src/core/cli/planner.ts +0 -656
  129. package/src/core/cli/plugin.ts +0 -117
  130. package/src/core/cli/propagate.ts +0 -97
  131. package/src/core/cli/root-help.ts +0 -342
  132. package/src/core/cli/root-surface.ts +0 -87
  133. package/src/core/cli/runtime-preflight.ts +0 -456
  134. package/src/core/completion/index.ts +0 -138
  135. package/src/core/completion/shells/bash.ts +0 -459
  136. package/src/core/completion/shells/fish.ts +0 -259
  137. package/src/core/completion/shells/powershell.ts +0 -439
  138. package/src/core/completion/shells/shared.ts +0 -310
  139. package/src/core/completion/shells/zsh.ts +0 -381
  140. package/src/core/config/index.ts +0 -417
  141. package/src/core/config/package-json.ts +0 -560
  142. package/src/core/errors/index.ts +0 -193
  143. package/src/core/execution/index.ts +0 -265
  144. package/src/core/help/ansi.ts +0 -144
  145. package/src/core/help/index.ts +0 -563
  146. package/src/core/json-schema/index.ts +0 -910
  147. package/src/core/json-schema/meta-descriptions.generated.ts +0 -202
  148. package/src/core/output/activity.ts +0 -575
  149. package/src/core/output/bind.ts +0 -59
  150. package/src/core/output/contracts.ts +0 -194
  151. package/src/core/output/display-value.ts +0 -48
  152. package/src/core/output/index.ts +0 -704
  153. package/src/core/output/renderers.ts +0 -125
  154. package/src/core/output/writer.ts +0 -46
  155. package/src/core/parse/index.ts +0 -742
  156. package/src/core/prompt/index.ts +0 -623
  157. package/src/core/resolve/args.ts +0 -144
  158. package/src/core/resolve/coerce.ts +0 -500
  159. package/src/core/resolve/config.ts +0 -26
  160. package/src/core/resolve/contracts.ts +0 -135
  161. package/src/core/resolve/errors.ts +0 -227
  162. package/src/core/resolve/flags.ts +0 -269
  163. package/src/core/resolve/index.ts +0 -108
  164. package/src/core/resolve/property.ts +0 -99
  165. package/src/core/schema/activity.ts +0 -240
  166. package/src/core/schema/arg.ts +0 -841
  167. package/src/core/schema/command.ts +0 -1500
  168. package/src/core/schema/flag.ts +0 -917
  169. package/src/core/schema/index.ts +0 -96
  170. package/src/core/schema/middleware.ts +0 -171
  171. package/src/core/schema/number-constraints.ts +0 -153
  172. package/src/core/schema/prompt.ts +0 -171
  173. package/src/core/schema/run.ts +0 -198
  174. package/src/core/schema-dsl/index.ts +0 -109
  175. package/src/core/schema-dsl/parse.ts +0 -262
  176. package/src/core/schema-dsl/runtime.ts +0 -609
  177. package/src/core/schema-dsl/to-json-schema.ts +0 -102
  178. package/src/core/testkit/index.ts +0 -147
  179. package/src/index.ts +0 -181
  180. package/src/runtime/adapter.ts +0 -303
  181. package/src/runtime/auto.ts +0 -86
  182. package/src/runtime/bun.ts +0 -48
  183. package/src/runtime/deno.ts +0 -367
  184. package/src/runtime/detect.ts +0 -105
  185. package/src/runtime/index.ts +0 -21
  186. package/src/runtime/node-builtins.d.ts +0 -27
  187. package/src/runtime/node.ts +0 -289
  188. package/src/runtime/paths.ts +0 -64
  189. package/src/runtime/support.ts +0 -77
  190. package/src/runtime.ts +0 -31
  191. package/src/schema.ts +0 -16
  192. package/src/strings.ts +0 -34
  193. package/src/testkit.ts +0 -25
  194. 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 };