@kjanat/dreamcli 2.5.0 → 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 (202) hide show
  1. package/CHANGELOG.md +344 -1
  2. package/README.md +9 -2
  3. package/dist/core/cli/dispatch.d.mts +6 -5
  4. package/dist/core/cli/dispatch.mjs +205 -1
  5. package/dist/core/cli/help-links.d.mts +0 -1
  6. package/dist/core/cli/help-links.mjs +41 -1
  7. package/dist/core/cli/index.d.mts +185 -19
  8. package/dist/core/cli/index.mjs +945 -1
  9. package/dist/core/cli/planner.d.mts +32 -6
  10. package/dist/core/cli/planner.mjs +300 -1
  11. package/dist/core/cli/plugin.d.mts +1 -1
  12. package/dist/core/cli/plugin.mjs +41 -1
  13. package/dist/core/cli/propagate.d.mts +1 -1
  14. package/dist/core/cli/propagate.mjs +57 -1
  15. package/dist/core/cli/root-help.d.mts +18 -3
  16. package/dist/core/cli/root-help.mjs +226 -6
  17. package/dist/core/cli/root-surface.d.mts +30 -4
  18. package/dist/core/cli/root-surface.mjs +28 -1
  19. package/dist/core/cli/runtime-preflight.d.mts +30 -3
  20. package/dist/core/cli/runtime-preflight.mjs +169 -1
  21. package/dist/core/completion/index.d.mts +32 -2
  22. package/dist/core/completion/index.mjs +90 -1
  23. package/dist/core/completion/shells/bash.d.mts +0 -1
  24. package/dist/core/completion/shells/bash.mjs +308 -3
  25. package/dist/core/completion/shells/fish.d.mts +0 -1
  26. package/dist/core/completion/shells/fish.mjs +162 -2
  27. package/dist/core/completion/shells/powershell.d.mts +0 -1
  28. package/dist/core/completion/shells/powershell.mjs +358 -2
  29. package/dist/core/completion/shells/shared.d.mts +23 -2
  30. package/dist/core/completion/shells/shared.mjs +176 -1
  31. package/dist/core/completion/shells/zsh.d.mts +0 -1
  32. package/dist/core/completion/shells/zsh.mjs +251 -2
  33. package/dist/core/config/index.d.mts +2 -3
  34. package/dist/core/config/index.mjs +246 -1
  35. package/dist/core/config/package-json.d.mts +27 -4
  36. package/dist/core/config/package-json.mjs +386 -3
  37. package/dist/core/errors/index.d.mts +3 -3
  38. package/dist/core/errors/index.mjs +87 -1
  39. package/dist/core/execution/index.d.mts +0 -1
  40. package/dist/core/execution/index.mjs +201 -1
  41. package/dist/core/help/ansi.d.mts +11 -5
  42. package/dist/core/help/ansi.mjs +121 -2
  43. package/dist/core/help/index.d.mts +65 -2
  44. package/dist/core/help/index.mjs +341 -7
  45. package/dist/core/help/theme.d.mts +68 -0
  46. package/dist/core/help/theme.mjs +46 -0
  47. package/dist/core/json-schema/index.d.mts +15 -2
  48. package/dist/core/json-schema/index.mjs +536 -12
  49. package/dist/core/json-schema/meta-descriptions.generated.d.mts +22 -5
  50. package/dist/core/json-schema/meta-descriptions.generated.mjs +102 -28
  51. package/dist/core/output/activity.d.mts +0 -1
  52. package/dist/core/output/activity.mjs +452 -1
  53. package/dist/core/output/bind.mjs +56 -1
  54. package/dist/core/output/contracts.d.mts +4 -5
  55. package/dist/core/output/contracts.mjs +57 -1
  56. package/dist/core/output/display-value.mjs +32 -1
  57. package/dist/core/output/index.d.mts +21 -1
  58. package/dist/core/output/index.mjs +442 -2
  59. package/dist/core/output/renderers.d.mts +0 -1
  60. package/dist/core/output/renderers.mjs +46 -1
  61. package/dist/core/output/writer.mjs +12 -1
  62. package/dist/core/parse/index.d.mts +54 -7
  63. package/dist/core/parse/index.mjs +727 -1
  64. package/dist/core/prompt/index.d.mts +12 -4
  65. package/dist/core/prompt/index.mjs +348 -6
  66. package/dist/core/resolve/args.d.mts +1 -1
  67. package/dist/core/resolve/args.mjs +105 -1
  68. package/dist/core/resolve/coerce.d.mts +1 -1
  69. package/dist/core/resolve/coerce.mjs +336 -1
  70. package/dist/core/resolve/config.mjs +19 -1
  71. package/dist/core/resolve/contracts.d.mts +9 -4
  72. package/dist/core/resolve/errors.d.mts +0 -1
  73. package/dist/core/resolve/errors.mjs +140 -2
  74. package/dist/core/resolve/flags.d.mts +7 -2
  75. package/dist/core/resolve/flags.mjs +251 -1
  76. package/dist/core/resolve/index.d.mts +2 -2
  77. package/dist/core/resolve/index.mjs +85 -1
  78. package/dist/core/resolve/property.d.mts +6 -2
  79. package/dist/core/resolve/property.mjs +53 -1
  80. package/dist/core/schema/activity.d.mts +31 -11
  81. package/dist/core/schema/arg.d.mts +92 -13
  82. package/dist/core/schema/arg.mjs +463 -1
  83. package/dist/core/schema/command.d.mts +29 -7
  84. package/dist/core/schema/command.mjs +726 -1
  85. package/dist/core/schema/flag.d.mts +577 -21
  86. package/dist/core/schema/flag.mjs +705 -1
  87. package/dist/core/schema/index.d.mts +5 -2
  88. package/dist/core/schema/middleware.d.mts +4 -3
  89. package/dist/core/schema/middleware.mjs +41 -1
  90. package/dist/core/schema/number-constraints.d.mts +96 -0
  91. package/dist/core/schema/number-constraints.mjs +60 -0
  92. package/dist/core/schema/prompt.d.mts +21 -3
  93. package/dist/core/schema/run.d.mts +24 -5
  94. package/dist/core/schema/string-constraints.d.mts +97 -0
  95. package/dist/core/schema/string-constraints.mjs +69 -0
  96. package/dist/core/schema/value-parsers.d.mts +78 -0
  97. package/dist/core/schema/value-parsers.mjs +142 -0
  98. package/dist/core/schema-dsl/runtime.mjs +271 -2
  99. package/dist/core/schema-dsl/to-json-schema.d.mts +0 -1
  100. package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
  101. package/dist/core/testkit/index.d.mts +9 -36
  102. package/dist/core/testkit/index.mjs +83 -1
  103. package/dist/index.d.mts +12 -6
  104. package/dist/index.mjs +22 -1
  105. package/dist/runtime/adapter.d.mts +38 -2
  106. package/dist/runtime/adapter.mjs +87 -1
  107. package/dist/runtime/auto.d.mts +2 -3
  108. package/dist/runtime/auto.mjs +45 -1
  109. package/dist/runtime/deno.d.mts +24 -5
  110. package/dist/runtime/deno.mjs +262 -2
  111. package/dist/runtime/detect.mjs +45 -1
  112. package/dist/runtime/index.d.mts +3 -4
  113. package/dist/runtime/node.d.mts +8 -2
  114. package/dist/runtime/node.mjs +220 -2
  115. package/dist/runtime/paths.mjs +48 -1
  116. package/dist/runtime.d.mts +4 -4
  117. package/dist/runtime.mjs +6 -1
  118. package/dist/strings.mjs +22 -1
  119. package/dist/testkit.d.mts +1 -0
  120. package/dist/testkit.mjs +5 -1
  121. package/dist/version.d.mts +7 -3
  122. package/dist/version.mjs +7 -1
  123. package/dreamcli.schema.json +36 -7
  124. package/examples/basic.ts +5 -1
  125. package/examples/gh/package.json +2 -7
  126. package/examples/pwsh-demo/package.json +1 -7
  127. package/examples/pwsh-demo/src/main.test.ts +1 -4
  128. package/examples/pwsh-demo/src/main.ts +14 -4
  129. package/examples/transport-launcher.ts +85 -0
  130. package/package.json +43 -49
  131. package/dist/runtime/bun.d.mts +0 -28
  132. package/dist/runtime/bun.mjs +0 -1
  133. package/dist/runtime/support.d.mts +0 -42
  134. package/dist/runtime/support.mjs +0 -1
  135. package/src/core/cli/dispatch.ts +0 -349
  136. package/src/core/cli/help-links.ts +0 -82
  137. package/src/core/cli/index.ts +0 -1413
  138. package/src/core/cli/planner.ts +0 -487
  139. package/src/core/cli/plugin.ts +0 -117
  140. package/src/core/cli/propagate.ts +0 -97
  141. package/src/core/cli/root-help.ts +0 -252
  142. package/src/core/cli/root-surface.ts +0 -74
  143. package/src/core/cli/runtime-preflight.ts +0 -438
  144. package/src/core/completion/index.ts +0 -88
  145. package/src/core/completion/shells/bash.ts +0 -459
  146. package/src/core/completion/shells/fish.ts +0 -259
  147. package/src/core/completion/shells/powershell.ts +0 -439
  148. package/src/core/completion/shells/shared.ts +0 -283
  149. package/src/core/completion/shells/zsh.ts +0 -381
  150. package/src/core/config/index.ts +0 -417
  151. package/src/core/config/package-json.ts +0 -560
  152. package/src/core/errors/index.ts +0 -193
  153. package/src/core/execution/index.ts +0 -268
  154. package/src/core/help/ansi.ts +0 -144
  155. package/src/core/help/index.ts +0 -516
  156. package/src/core/json-schema/index.ts +0 -891
  157. package/src/core/json-schema/meta-descriptions.generated.ts +0 -185
  158. package/src/core/output/activity.ts +0 -575
  159. package/src/core/output/bind.ts +0 -59
  160. package/src/core/output/contracts.ts +0 -194
  161. package/src/core/output/display-value.ts +0 -48
  162. package/src/core/output/index.ts +0 -704
  163. package/src/core/output/renderers.ts +0 -125
  164. package/src/core/output/writer.ts +0 -46
  165. package/src/core/parse/index.ts +0 -681
  166. package/src/core/prompt/index.ts +0 -583
  167. package/src/core/resolve/args.ts +0 -144
  168. package/src/core/resolve/coerce.ts +0 -432
  169. package/src/core/resolve/config.ts +0 -26
  170. package/src/core/resolve/contracts.ts +0 -135
  171. package/src/core/resolve/errors.ts +0 -227
  172. package/src/core/resolve/flags.ts +0 -255
  173. package/src/core/resolve/index.ts +0 -108
  174. package/src/core/resolve/property.ts +0 -99
  175. package/src/core/schema/activity.ts +0 -240
  176. package/src/core/schema/arg.ts +0 -719
  177. package/src/core/schema/command.ts +0 -1498
  178. package/src/core/schema/flag.ts +0 -801
  179. package/src/core/schema/index.ts +0 -91
  180. package/src/core/schema/middleware.ts +0 -171
  181. package/src/core/schema/prompt.ts +0 -156
  182. package/src/core/schema/run.ts +0 -198
  183. package/src/core/schema-dsl/index.ts +0 -109
  184. package/src/core/schema-dsl/parse.ts +0 -262
  185. package/src/core/schema-dsl/runtime.ts +0 -609
  186. package/src/core/schema-dsl/to-json-schema.ts +0 -102
  187. package/src/core/testkit/index.ts +0 -123
  188. package/src/index.ts +0 -179
  189. package/src/runtime/adapter.ts +0 -303
  190. package/src/runtime/auto.ts +0 -110
  191. package/src/runtime/bun.ts +0 -55
  192. package/src/runtime/deno.ts +0 -369
  193. package/src/runtime/detect.ts +0 -105
  194. package/src/runtime/index.ts +0 -21
  195. package/src/runtime/node-builtins.d.ts +0 -27
  196. package/src/runtime/node.ts +0 -300
  197. package/src/runtime/paths.ts +0 -64
  198. package/src/runtime/support.ts +0 -135
  199. package/src/runtime.ts +0 -31
  200. package/src/strings.ts +0 -34
  201. package/src/testkit.ts +0 -25
  202. 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,432 +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 } from '#internals/core/schema/index.ts';
11
- import type { ArgDiagnosticSource, FlagDiagnosticSource } from './contracts.ts';
12
- import type { SharedPropertySchema } from './property.ts';
13
- import { toSharedArgPropertySchema, toSharedFlagPropertySchema } from './property.ts';
14
-
15
- type CoerceSource = FlagDiagnosticSource;
16
-
17
- /** Discriminated result of a value coercion attempt — success with the coerced value, or failure with a structured validation error. */
18
- type CoerceResult =
19
- | { readonly ok: true; readonly value: unknown }
20
- | { readonly ok: false; readonly error: ValidationError };
21
-
22
- function sourceLabel(source: CoerceSource): string {
23
- switch (source.kind) {
24
- case 'env':
25
- return `from env ${source.envVar}`;
26
- case 'config':
27
- return `from config ${source.configPath}`;
28
- case 'prompt':
29
- return 'from prompt';
30
- }
31
- }
32
-
33
- function sourceDetails(source: CoerceSource): Record<string, unknown> {
34
- switch (source.kind) {
35
- case 'env':
36
- return { envVar: source.envVar };
37
- case 'config':
38
- return { configPath: source.configPath };
39
- case 'prompt':
40
- return { source: 'prompt' };
41
- }
42
- }
43
-
44
- function coercionError(
45
- flagName: string,
46
- source: CoerceSource,
47
- code: ValidationErrorCode,
48
- expected: string,
49
- raw: unknown,
50
- messageSuffix: string,
51
- suggest: string,
52
- extraDetails?: Record<string, unknown>,
53
- ): CoerceResult {
54
- return {
55
- ok: false,
56
- error: new ValidationError(`${messageSuffix} ${sourceLabel(source)} for flag --${flagName}`, {
57
- code,
58
- details: { flag: flagName, ...sourceDetails(source), value: raw, expected, ...extraDetails },
59
- suggest,
60
- }),
61
- };
62
- }
63
-
64
- /** Coerce a raw value from env/config/prompt into the type declared by a flag schema. */
65
- function coerceValue(
66
- flagName: string,
67
- source: CoerceSource,
68
- raw: unknown,
69
- schema: FlagSchema,
70
- ): CoerceResult {
71
- const sharedSchema = toSharedFlagPropertySchema(schema);
72
- if (sharedSchema !== undefined) {
73
- return coerceSharedPropertyValue(flagName, source, raw, sharedSchema);
74
- }
75
-
76
- switch (schema.kind) {
77
- case 'boolean': {
78
- if (typeof raw === 'boolean') return { ok: true, value: raw };
79
- if (typeof raw === 'string') {
80
- const lower = raw.toLowerCase();
81
- const truthy =
82
- lower === 'true' ||
83
- lower === '1' ||
84
- lower === 'yes' ||
85
- (source.kind === 'prompt' && lower === 'y');
86
- if (truthy) return { ok: true, value: true };
87
- const falsy =
88
- lower === 'false' ||
89
- lower === '0' ||
90
- lower === 'no' ||
91
- lower === '' ||
92
- (source.kind === 'prompt' && lower === 'n');
93
- if (falsy) return { ok: true, value: false };
94
- }
95
- return coercionError(
96
- flagName,
97
- source,
98
- 'TYPE_MISMATCH',
99
- 'boolean',
100
- raw,
101
- typeof raw === 'string' ? `Invalid boolean value '${raw}'` : 'Invalid boolean value',
102
- source.kind === 'env'
103
- ? `Set ${source.envVar} to true/false, 1/0, or yes/no`
104
- : source.kind === 'config'
105
- ? `Set ${source.configPath} to true or false in your config`
106
- : `Answer yes or no for --${flagName}`,
107
- );
108
- }
109
-
110
- case 'array': {
111
- if (Array.isArray(raw)) {
112
- if (schema.elementSchema) {
113
- const coerced: unknown[] = [];
114
- for (const element of raw) {
115
- const result = coerceValue(flagName, source, element, schema.elementSchema);
116
- if (!result.ok) return result;
117
- coerced.push(result.value);
118
- }
119
- return { ok: true, value: coerced };
120
- }
121
- return { ok: true, value: raw };
122
- }
123
- if (typeof raw === 'string') {
124
- if (raw === '') return { ok: true, value: [] };
125
- const parts = raw.split(',');
126
- if (schema.elementSchema) {
127
- const coerced: unknown[] = [];
128
- for (const part of parts) {
129
- const element = source.kind === 'prompt' ? part.trim() : part;
130
- const result = coerceValue(flagName, source, element, schema.elementSchema);
131
- if (!result.ok) return result;
132
- coerced.push(result.value);
133
- }
134
- return { ok: true, value: coerced };
135
- }
136
- return {
137
- ok: true,
138
- value: source.kind === 'prompt' ? parts.map((part) => part.trim()) : parts,
139
- };
140
- }
141
- return coercionError(
142
- flagName,
143
- source,
144
- 'TYPE_MISMATCH',
145
- 'array',
146
- raw,
147
- 'Invalid array value',
148
- source.kind === 'env'
149
- ? `Set ${source.envVar} to comma-separated values`
150
- : source.kind === 'config'
151
- ? `Set ${source.configPath} to an array in your config`
152
- : `Provide valid values for --${flagName}`,
153
- );
154
- }
155
- }
156
-
157
- throw new Error(`Unreachable flag coercion kind: ${schema.kind}`);
158
- }
159
-
160
- function coerceSharedPropertyValue(
161
- flagName: string,
162
- source: CoerceSource,
163
- raw: unknown,
164
- schema: SharedPropertySchema,
165
- ): CoerceResult {
166
- switch (schema.kind) {
167
- case 'string': {
168
- if (typeof raw === 'string') return { ok: true, value: raw };
169
- if (source.kind === 'prompt') return { ok: true, value: String(raw) };
170
- if (source.kind === 'config' && (typeof raw === 'number' || typeof raw === 'boolean')) {
171
- return { ok: true, value: String(raw) };
172
- }
173
- return coercionError(
174
- flagName,
175
- source,
176
- 'TYPE_MISMATCH',
177
- 'string',
178
- raw,
179
- 'Invalid string value',
180
- source.kind === 'config'
181
- ? `Set ${source.configPath} to a string in your config`
182
- : `Enter a valid string for --${flagName}`,
183
- );
184
- }
185
-
186
- case 'number': {
187
- if (typeof raw === 'number') {
188
- if (Number.isNaN(raw)) {
189
- return coercionError(
190
- flagName,
191
- source,
192
- 'TYPE_MISMATCH',
193
- 'number',
194
- raw,
195
- 'Invalid number value NaN',
196
- source.kind === 'env'
197
- ? `Set ${source.envVar} to a valid number`
198
- : source.kind === 'config'
199
- ? `Set ${source.configPath} to a valid number in your config`
200
- : `Enter a valid number for --${flagName}`,
201
- );
202
- }
203
- return { ok: true, value: raw };
204
- }
205
- if (typeof raw === 'string') {
206
- const value = Number(raw);
207
- if (!Number.isNaN(value)) return { ok: true, value };
208
- }
209
- return coercionError(
210
- flagName,
211
- source,
212
- 'TYPE_MISMATCH',
213
- 'number',
214
- raw,
215
- typeof raw === 'string' ? `Invalid number value '${raw}'` : 'Invalid number value',
216
- source.kind === 'env'
217
- ? `Set ${source.envVar} to a valid number`
218
- : source.kind === 'config'
219
- ? `Set ${source.configPath} to a valid number in your config`
220
- : `Enter a valid number for --${flagName}`,
221
- );
222
- }
223
-
224
- case 'enum': {
225
- const allowed = schema.enumValues ?? [];
226
- if (typeof raw === 'string' && allowed.includes(raw)) {
227
- return { ok: true, value: raw };
228
- }
229
- return {
230
- ok: false,
231
- error: new ValidationError(
232
- `Invalid value '${String(raw)}' ${sourceLabel(source)} for flag --${flagName}. Allowed: ${allowed.join(', ')}`,
233
- {
234
- code: 'INVALID_ENUM',
235
- details: { flag: flagName, ...sourceDetails(source), value: raw, allowed },
236
- suggest:
237
- source.kind === 'env'
238
- ? `Set ${source.envVar} to one of: ${allowed.join(', ')}`
239
- : source.kind === 'config'
240
- ? `Set ${source.configPath} to one of: ${allowed.join(', ')}`
241
- : `Select one of: ${allowed.join(', ')}`,
242
- },
243
- ),
244
- };
245
- }
246
-
247
- case 'custom': {
248
- if (schema.parseFn === undefined) {
249
- return { ok: true, value: raw };
250
- }
251
-
252
- try {
253
- return { ok: true, value: schema.parseFn(raw) };
254
- } catch (error) {
255
- const message = error instanceof Error ? error.message : String(error);
256
- const sourceRef =
257
- source.kind === 'env'
258
- ? `env ${source.envVar}`
259
- : source.kind === 'config'
260
- ? `config ${source.configPath}`
261
- : 'prompt value';
262
- return {
263
- ok: false,
264
- error: new ValidationError(
265
- `Failed to parse ${sourceRef} for flag --${flagName}: ${message}`,
266
- {
267
- code: 'TYPE_MISMATCH',
268
- details: {
269
- flag: flagName,
270
- ...sourceDetails(source),
271
- value: raw,
272
- expected: 'custom',
273
- },
274
- suggest:
275
- source.kind === 'env'
276
- ? `Set ${source.envVar} to a valid value for --${flagName}`
277
- : source.kind === 'config'
278
- ? `Set ${source.configPath} to a valid value for --${flagName} in your config`
279
- : `Enter a valid value for --${flagName}`,
280
- },
281
- ),
282
- };
283
- }
284
- }
285
- }
286
- }
287
-
288
- type ArgStringSource = ArgDiagnosticSource;
289
-
290
- function argSourceLabel(source: ArgStringSource): string {
291
- return source.kind === 'env' ? `from env ${source.envVar}` : 'from stdin';
292
- }
293
-
294
- function argSourceDetails(source: ArgStringSource): Record<string, unknown> {
295
- return source.kind === 'env' ? { envVar: source.envVar } : { source: 'stdin' };
296
- }
297
-
298
- function buildArgCoercionSuggest(
299
- argName: string,
300
- source: ArgStringSource,
301
- expected: 'number' | 'custom',
302
- ): string {
303
- if (source.kind === 'env') {
304
- return expected === 'number'
305
- ? `Set ${source.envVar} to a valid number`
306
- : `Set ${source.envVar} to a valid value for <${argName}>`;
307
- }
308
-
309
- return expected === 'number'
310
- ? `Pipe a valid number to stdin for <${argName}>`
311
- : `Pipe a valid value to stdin for <${argName}>`;
312
- }
313
-
314
- function argSourceToCoerceSource(source: ArgStringSource): CoerceSource {
315
- return source.kind === 'env' ? { kind: 'env', envVar: source.envVar } : { kind: 'prompt' };
316
- }
317
-
318
- function redactArgCoercionMessage(
319
- argName: string,
320
- source: ArgStringSource,
321
- schema: ArgSchema,
322
- error: ValidationError,
323
- ): string {
324
- switch (schema.kind) {
325
- case 'number':
326
- return `Invalid number value '<redacted>' ${argSourceLabel(source)} for argument <${argName}>`;
327
- case 'enum': {
328
- const allowed = Array.isArray(error.details?.allowed)
329
- ? error.details.allowed.filter((value): value is string => typeof value === 'string')
330
- : [];
331
- return `Invalid value '<redacted>' ${argSourceLabel(source)} for argument <${argName}>. Allowed: ${allowed.join(', ')}`;
332
- }
333
- case 'custom': {
334
- const match = /: ([^:]+)$/.exec(error.message);
335
- const suffix = match?.[1];
336
- return suffix !== undefined
337
- ? `Invalid value '<redacted>' ${argSourceLabel(source)} for argument <${argName}>: ${suffix}`
338
- : `Invalid value '<redacted>' ${argSourceLabel(source)} for argument <${argName}>`;
339
- }
340
- case 'string':
341
- return `Invalid value '<redacted>' ${argSourceLabel(source)} for argument <${argName}>`;
342
- default: {
343
- const exhaustive: never = schema.kind;
344
- return exhaustive;
345
- }
346
- }
347
- }
348
-
349
- function redactArgCoercionDetails(
350
- argName: string,
351
- source: ArgStringSource,
352
- schema: ArgSchema,
353
- error: ValidationError,
354
- ): Readonly<Record<string, unknown>> {
355
- return {
356
- arg: argName,
357
- ...argSourceDetails(source),
358
- ...(schema.kind === 'number' || schema.kind === 'custom' ? { expected: schema.kind } : {}),
359
- ...(schema.kind === 'enum' && Array.isArray(error.details?.allowed)
360
- ? {
361
- allowed: error.details.allowed.filter(
362
- (value): value is string => typeof value === 'string',
363
- ),
364
- }
365
- : {}),
366
- };
367
- }
368
-
369
- function redactArgCoercionSuggest(
370
- argName: string,
371
- source: ArgStringSource,
372
- schema: ArgSchema,
373
- error: ValidationError,
374
- ): string | undefined {
375
- if (schema.kind === 'number' || schema.kind === 'custom') {
376
- return buildArgCoercionSuggest(argName, source, schema.kind);
377
- }
378
-
379
- if (schema.kind === 'enum') {
380
- const allowed = Array.isArray(error.details?.allowed)
381
- ? error.details.allowed.filter((value): value is string => typeof value === 'string')
382
- : [];
383
- const allowedList = allowed.join(', ');
384
- return source.kind === 'env'
385
- ? `Set ${source.envVar} to one of: ${allowedList}`
386
- : `Provide one of: ${allowedList}`;
387
- }
388
-
389
- return undefined;
390
- }
391
-
392
- /** Coerce a string value from stdin/env into the type declared by an arg schema, redacting raw values in error diagnostics. */
393
- function coerceArgStringValue(
394
- argName: string,
395
- source: ArgStringSource,
396
- raw: string,
397
- schema: ArgSchema,
398
- ): CoerceResult {
399
- const coerced = coerceSharedPropertyValue(
400
- argName,
401
- argSourceToCoerceSource(source),
402
- raw,
403
- toSharedArgPropertySchema(schema),
404
- );
405
- if (coerced.ok) {
406
- return coerced;
407
- }
408
-
409
- const suggest = redactArgCoercionSuggest(argName, source, schema, coerced.error);
410
- const options =
411
- suggest === undefined
412
- ? {
413
- code: coerced.error.code,
414
- details: redactArgCoercionDetails(argName, source, schema, coerced.error),
415
- }
416
- : {
417
- code: coerced.error.code,
418
- details: redactArgCoercionDetails(argName, source, schema, coerced.error),
419
- suggest,
420
- };
421
-
422
- return {
423
- ok: false,
424
- error: new ValidationError(
425
- redactArgCoercionMessage(argName, source, schema, coerced.error),
426
- options,
427
- ),
428
- };
429
- }
430
-
431
- export type { CoerceResult };
432
- 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 };