@kjanat/dreamcli 2.5.0 → 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 (160) hide show
  1. package/CHANGELOG.md +153 -1
  2. package/README.md +9 -2
  3. package/dist/core/cli/dispatch.mjs +214 -1
  4. package/dist/core/cli/help-links.mjs +41 -1
  5. package/dist/core/cli/index.d.mts +109 -16
  6. package/dist/core/cli/index.mjs +901 -1
  7. package/dist/core/cli/planner.d.mts +27 -5
  8. package/dist/core/cli/planner.mjs +300 -1
  9. package/dist/core/cli/plugin.mjs +41 -1
  10. package/dist/core/cli/propagate.mjs +57 -1
  11. package/dist/core/cli/root-help.d.mts +16 -2
  12. package/dist/core/cli/root-help.mjs +208 -6
  13. package/dist/core/cli/root-surface.d.mts +18 -3
  14. package/dist/core/cli/root-surface.mjs +27 -1
  15. package/dist/core/cli/runtime-preflight.d.mts +16 -1
  16. package/dist/core/cli/runtime-preflight.mjs +168 -1
  17. package/dist/core/completion/index.d.mts +32 -1
  18. package/dist/core/completion/index.mjs +90 -1
  19. package/dist/core/completion/shells/bash.mjs +304 -3
  20. package/dist/core/completion/shells/fish.mjs +159 -2
  21. package/dist/core/completion/shells/powershell.mjs +353 -2
  22. package/dist/core/completion/shells/shared.d.mts +10 -0
  23. package/dist/core/completion/shells/shared.mjs +154 -1
  24. package/dist/core/completion/shells/zsh.mjs +244 -2
  25. package/dist/core/config/index.mjs +246 -1
  26. package/dist/core/config/package-json.mjs +385 -3
  27. package/dist/core/errors/index.mjs +87 -1
  28. package/dist/core/execution/index.mjs +189 -1
  29. package/dist/core/help/ansi.mjs +122 -2
  30. package/dist/core/help/index.d.mts +45 -1
  31. package/dist/core/help/index.mjs +329 -7
  32. package/dist/core/json-schema/index.mjs +487 -9
  33. package/dist/core/json-schema/meta-descriptions.generated.d.mts +38 -38
  34. package/dist/core/json-schema/meta-descriptions.generated.mjs +93 -28
  35. package/dist/core/output/activity.mjs +452 -1
  36. package/dist/core/output/bind.mjs +56 -1
  37. package/dist/core/output/contracts.mjs +57 -1
  38. package/dist/core/output/display-value.mjs +32 -1
  39. package/dist/core/output/index.d.mts +21 -0
  40. package/dist/core/output/index.mjs +442 -2
  41. package/dist/core/output/renderers.mjs +46 -1
  42. package/dist/core/output/writer.mjs +12 -1
  43. package/dist/core/parse/index.d.mts +15 -1
  44. package/dist/core/parse/index.mjs +555 -1
  45. package/dist/core/prompt/index.d.mts +11 -4
  46. package/dist/core/prompt/index.mjs +348 -6
  47. package/dist/core/resolve/args.mjs +105 -1
  48. package/dist/core/resolve/coerce.mjs +279 -1
  49. package/dist/core/resolve/config.mjs +19 -1
  50. package/dist/core/resolve/errors.mjs +140 -2
  51. package/dist/core/resolve/flags.mjs +196 -1
  52. package/dist/core/resolve/index.mjs +85 -1
  53. package/dist/core/resolve/property.d.mts +2 -1
  54. package/dist/core/resolve/property.mjs +45 -1
  55. package/dist/core/schema/arg.d.mts +90 -10
  56. package/dist/core/schema/arg.mjs +463 -1
  57. package/dist/core/schema/command.d.mts +21 -1
  58. package/dist/core/schema/command.mjs +712 -1
  59. package/dist/core/schema/flag.d.mts +92 -2
  60. package/dist/core/schema/flag.mjs +453 -1
  61. package/dist/core/schema/index.d.mts +2 -1
  62. package/dist/core/schema/middleware.mjs +41 -1
  63. package/dist/core/schema/number-constraints.d.mts +96 -0
  64. package/dist/core/schema/number-constraints.mjs +60 -0
  65. package/dist/core/schema/prompt.d.mts +15 -0
  66. package/dist/core/schema/run.d.mts +4 -4
  67. package/dist/core/schema-dsl/runtime.mjs +271 -2
  68. package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
  69. package/dist/core/testkit/index.d.mts +9 -5
  70. package/dist/core/testkit/index.mjs +83 -1
  71. package/dist/index.d.mts +3 -1
  72. package/dist/index.mjs +22 -1
  73. package/dist/runtime/adapter.mjs +82 -1
  74. package/dist/runtime/auto.mjs +46 -1
  75. package/dist/runtime/bun.mjs +28 -1
  76. package/dist/runtime/deno.mjs +217 -2
  77. package/dist/runtime/detect.mjs +45 -1
  78. package/dist/runtime/node.mjs +169 -2
  79. package/dist/runtime/paths.mjs +48 -1
  80. package/dist/runtime.mjs +7 -1
  81. package/dist/strings.mjs +22 -1
  82. package/dist/testkit.mjs +5 -1
  83. package/dist/version.mjs +7 -1
  84. package/examples/basic.ts +5 -1
  85. package/examples/gh/package.json +2 -7
  86. package/examples/pwsh-demo/package.json +1 -7
  87. package/examples/pwsh-demo/src/main.test.ts +1 -4
  88. package/examples/pwsh-demo/src/main.ts +14 -4
  89. package/examples/transport-launcher.ts +85 -0
  90. package/package.json +23 -41
  91. package/dist/runtime/support.d.mts +0 -42
  92. package/dist/runtime/support.mjs +0 -1
  93. package/src/core/cli/dispatch.ts +0 -349
  94. package/src/core/cli/help-links.ts +0 -82
  95. package/src/core/cli/index.ts +0 -1413
  96. package/src/core/cli/planner.ts +0 -487
  97. package/src/core/cli/plugin.ts +0 -117
  98. package/src/core/cli/propagate.ts +0 -97
  99. package/src/core/cli/root-help.ts +0 -252
  100. package/src/core/cli/root-surface.ts +0 -74
  101. package/src/core/cli/runtime-preflight.ts +0 -438
  102. package/src/core/completion/index.ts +0 -88
  103. package/src/core/completion/shells/bash.ts +0 -459
  104. package/src/core/completion/shells/fish.ts +0 -259
  105. package/src/core/completion/shells/powershell.ts +0 -439
  106. package/src/core/completion/shells/shared.ts +0 -283
  107. package/src/core/completion/shells/zsh.ts +0 -381
  108. package/src/core/config/index.ts +0 -417
  109. package/src/core/config/package-json.ts +0 -560
  110. package/src/core/errors/index.ts +0 -193
  111. package/src/core/execution/index.ts +0 -268
  112. package/src/core/help/ansi.ts +0 -144
  113. package/src/core/help/index.ts +0 -516
  114. package/src/core/json-schema/index.ts +0 -891
  115. package/src/core/json-schema/meta-descriptions.generated.ts +0 -185
  116. package/src/core/output/activity.ts +0 -575
  117. package/src/core/output/bind.ts +0 -59
  118. package/src/core/output/contracts.ts +0 -194
  119. package/src/core/output/display-value.ts +0 -48
  120. package/src/core/output/index.ts +0 -704
  121. package/src/core/output/renderers.ts +0 -125
  122. package/src/core/output/writer.ts +0 -46
  123. package/src/core/parse/index.ts +0 -681
  124. package/src/core/prompt/index.ts +0 -583
  125. package/src/core/resolve/args.ts +0 -144
  126. package/src/core/resolve/coerce.ts +0 -432
  127. package/src/core/resolve/config.ts +0 -26
  128. package/src/core/resolve/contracts.ts +0 -135
  129. package/src/core/resolve/errors.ts +0 -227
  130. package/src/core/resolve/flags.ts +0 -255
  131. package/src/core/resolve/index.ts +0 -108
  132. package/src/core/resolve/property.ts +0 -99
  133. package/src/core/schema/activity.ts +0 -240
  134. package/src/core/schema/arg.ts +0 -719
  135. package/src/core/schema/command.ts +0 -1498
  136. package/src/core/schema/flag.ts +0 -801
  137. package/src/core/schema/index.ts +0 -91
  138. package/src/core/schema/middleware.ts +0 -171
  139. package/src/core/schema/prompt.ts +0 -156
  140. package/src/core/schema/run.ts +0 -198
  141. package/src/core/schema-dsl/index.ts +0 -109
  142. package/src/core/schema-dsl/parse.ts +0 -262
  143. package/src/core/schema-dsl/runtime.ts +0 -609
  144. package/src/core/schema-dsl/to-json-schema.ts +0 -102
  145. package/src/core/testkit/index.ts +0 -123
  146. package/src/index.ts +0 -179
  147. package/src/runtime/adapter.ts +0 -303
  148. package/src/runtime/auto.ts +0 -110
  149. package/src/runtime/bun.ts +0 -55
  150. package/src/runtime/deno.ts +0 -369
  151. package/src/runtime/detect.ts +0 -105
  152. package/src/runtime/index.ts +0 -21
  153. package/src/runtime/node-builtins.d.ts +0 -27
  154. package/src/runtime/node.ts +0 -300
  155. package/src/runtime/paths.ts +0 -64
  156. package/src/runtime/support.ts +0 -135
  157. package/src/runtime.ts +0 -31
  158. package/src/strings.ts +0 -34
  159. package/src/testkit.ts +0 -25
  160. 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 };