@reliverse/rempts 1.7.29 → 1.7.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +92 -29
- package/bin/{components/animate/animate.js → libs/animate/animate-mod.js} +1 -4
- package/bin/{components → libs}/anykey/anykey-mod.js +2 -2
- package/bin/libs/confirm/confirm-alias.d.ts +1 -0
- package/bin/libs/confirm/confirm-alias.js +2 -0
- package/bin/{components/confirm/confirm-prompt.js → libs/confirm/confirm-mod.js} +1 -1
- package/bin/{components → libs}/editor/editor-mod.js +8 -8
- package/bin/{components → libs}/figures/figures-mod.d.ts +2 -3
- package/bin/{components → libs}/figures/figures-mod.js +1 -2
- package/bin/libs/input/input-alias.d.ts +4 -0
- package/bin/libs/input/input-alias.js +4 -0
- package/bin/{components/input/input-prompt.js → libs/input/input-mod.js} +2 -2
- package/bin/libs/intro/intro-alias.d.ts +2 -0
- package/bin/libs/intro/intro-alias.js +3 -0
- package/bin/{components/intro/intro-start.js → libs/intro/intro-mod.js} +3 -3
- package/bin/libs/launcher/launcher-alias.d.ts +2 -0
- package/bin/libs/launcher/launcher-alias.js +2 -0
- package/bin/{components → libs}/launcher/launcher-mod.d.ts +37 -5
- package/bin/{components → libs}/launcher/launcher-mod.js +326 -205
- package/bin/{components → libs}/launcher/launcher-types.d.ts +1 -1
- package/bin/libs/launcher/trpc-orpc-support/completions.d.ts +4 -0
- package/bin/libs/launcher/trpc-orpc-support/completions.js +45 -0
- package/bin/libs/launcher/trpc-orpc-support/errors.d.ts +11 -0
- package/bin/libs/launcher/trpc-orpc-support/errors.js +10 -0
- package/bin/libs/launcher/trpc-orpc-support/index.d.ts +34 -0
- package/bin/libs/launcher/trpc-orpc-support/index.js +641 -0
- package/bin/libs/launcher/trpc-orpc-support/json-schema.d.ts +17 -0
- package/bin/libs/launcher/trpc-orpc-support/json-schema.js +168 -0
- package/bin/libs/launcher/trpc-orpc-support/json.d.ts +44 -0
- package/bin/libs/launcher/trpc-orpc-support/json.js +41 -0
- package/bin/libs/launcher/trpc-orpc-support/logging.d.ts +11 -0
- package/bin/libs/launcher/trpc-orpc-support/logging.js +26 -0
- package/bin/libs/launcher/trpc-orpc-support/parse-procedure.d.ts +2 -0
- package/bin/libs/launcher/trpc-orpc-support/parse-procedure.js +486 -0
- package/bin/libs/launcher/trpc-orpc-support/prompts.d.ts +18 -0
- package/bin/libs/launcher/trpc-orpc-support/prompts.js +534 -0
- package/bin/libs/launcher/trpc-orpc-support/standard-schema/contract.d.ts +53 -0
- package/bin/libs/launcher/trpc-orpc-support/standard-schema/contract.js +0 -0
- package/bin/libs/launcher/trpc-orpc-support/standard-schema/errors.d.ts +9 -0
- package/bin/libs/launcher/trpc-orpc-support/standard-schema/errors.js +47 -0
- package/bin/libs/launcher/trpc-orpc-support/standard-schema/utils.d.ts +3 -0
- package/bin/libs/launcher/trpc-orpc-support/standard-schema/utils.js +6 -0
- package/bin/libs/launcher/trpc-orpc-support/trpc-compat.d.ts +71 -0
- package/bin/libs/launcher/trpc-orpc-support/trpc-compat.js +11 -0
- package/bin/libs/launcher/trpc-orpc-support/types.d.ts +276 -0
- package/bin/libs/launcher/trpc-orpc-support/types.js +0 -0
- package/bin/libs/launcher/trpc-orpc-support/util.d.ts +9 -0
- package/bin/libs/launcher/trpc-orpc-support/util.js +9 -0
- package/bin/{components → libs}/msg-fmt/logger.js +1 -1
- package/bin/libs/multiselect/multiselect-alias.d.ts +1 -0
- package/bin/libs/multiselect/multiselect-alias.js +2 -0
- package/bin/{components/select → libs/multiselect}/multiselect-prompt.js +1 -1
- package/bin/{components → libs}/number/number-mod.js +1 -4
- package/bin/libs/outro/outro-alias.d.ts +2 -0
- package/bin/libs/outro/outro-alias.js +3 -0
- package/bin/{components/outro/outro-end.js → libs/outro/outro-mod.js} +2 -2
- package/bin/libs/select/aliases-alias.d.ts +1 -0
- package/bin/libs/select/aliases-alias.js +2 -0
- package/bin/{components → libs}/select/select-prompt.js +2 -2
- package/bin/{components → libs}/select/toggle-prompt.js +1 -1
- package/bin/libs/spinner/spinner-alias.d.ts +1 -0
- package/bin/libs/spinner/spinner-alias.js +2 -0
- package/bin/{components → libs}/spinner/spinner-mod.js +3 -1
- package/bin/{components → libs}/task/task-spin.js +1 -1
- package/bin/{utils → libs/utils}/colorize.d.ts +1 -1
- package/bin/{utils → libs/utils}/prevent.d.ts +1 -1
- package/bin/{utils → libs/utils}/prevent.js +2 -2
- package/bin/{utils → libs/utils}/prompt-end.d.ts +1 -1
- package/bin/{utils → libs/utils}/prompt-end.js +2 -2
- package/bin/{utils → libs/utils}/stream-text.d.ts +1 -1
- package/bin/{utils → libs/utils}/stream-text.js +2 -2
- package/bin/mod.d.ts +66 -41
- package/bin/mod.js +102 -66
- package/package.json +16 -2
- package/bin/components/aliases/aliases-mod.d.ts +0 -11
- package/bin/components/aliases/aliases-mod.js +0 -16
- /package/bin/{components/animate/animate.d.ts → libs/animate/animate-mod.d.ts} +0 -0
- /package/bin/{components → libs}/anykey/anykey-mod.d.ts +0 -0
- /package/bin/{components → libs}/cancel/cancel.d.ts +0 -0
- /package/bin/{components → libs}/cancel/cancel.js +0 -0
- /package/bin/{components/confirm/confirm-prompt.d.ts → libs/confirm/confirm-mod.d.ts} +0 -0
- /package/bin/{components → libs}/date/date.d.ts +0 -0
- /package/bin/{components → libs}/date/date.js +0 -0
- /package/bin/{components → libs}/editor/editor-mod.d.ts +0 -0
- /package/bin/{components/input/input-prompt.d.ts → libs/input/input-mod.d.ts} +0 -0
- /package/bin/{components/intro/intro-start.d.ts → libs/intro/intro-mod.d.ts} +0 -0
- /package/bin/{components → libs}/launcher/launcher-types.js +0 -0
- /package/bin/{components → libs}/launcher/run-command.d.ts +0 -0
- /package/bin/{components → libs}/launcher/run-command.js +0 -0
- /package/bin/{components/log/log.d.ts → libs/log/log-alias.d.ts} +0 -0
- /package/bin/{components/log/log.js → libs/log/log-alias.js} +0 -0
- /package/bin/{components → libs}/msg-fmt/colors.d.ts +0 -0
- /package/bin/{components → libs}/msg-fmt/colors.js +0 -0
- /package/bin/{components → libs}/msg-fmt/logger.d.ts +0 -0
- /package/bin/{components → libs}/msg-fmt/mapping.d.ts +0 -0
- /package/bin/{components → libs}/msg-fmt/mapping.js +0 -0
- /package/bin/{components → libs}/msg-fmt/messages.d.ts +0 -0
- /package/bin/{components → libs}/msg-fmt/messages.js +0 -0
- /package/bin/{components → libs}/msg-fmt/terminal.d.ts +0 -0
- /package/bin/{components → libs}/msg-fmt/terminal.js +0 -0
- /package/bin/{components → libs}/msg-fmt/variants.d.ts +0 -0
- /package/bin/{components → libs}/msg-fmt/variants.js +0 -0
- /package/bin/{components/select → libs/multiselect}/multiselect-prompt.d.ts +0 -0
- /package/bin/{components → libs}/next-steps/next-steps.d.ts +0 -0
- /package/bin/{components → libs}/next-steps/next-steps.js +0 -0
- /package/bin/{components → libs}/number/number-mod.d.ts +0 -0
- /package/bin/{components/outro/outro-end.d.ts → libs/outro/outro-mod.d.ts} +0 -0
- /package/bin/{components → libs}/results/results.d.ts +0 -0
- /package/bin/{components → libs}/results/results.js +0 -0
- /package/bin/{components → libs}/select/nummultiselect-prompt.d.ts +0 -0
- /package/bin/{components → libs}/select/nummultiselect-prompt.js +0 -0
- /package/bin/{components → libs}/select/numselect-prompt.d.ts +0 -0
- /package/bin/{components → libs}/select/numselect-prompt.js +0 -0
- /package/bin/{components → libs}/select/select-prompt.d.ts +0 -0
- /package/bin/{components → libs}/select/toggle-prompt.d.ts +0 -0
- /package/bin/{components → libs}/spinner/spinner-mod.d.ts +0 -0
- /package/bin/{components → libs}/task/progress.d.ts +0 -0
- /package/bin/{components → libs}/task/progress.js +0 -0
- /package/bin/{components → libs}/task/task-spin.d.ts +0 -0
- /package/bin/{utils → libs/utils}/colorize.js +0 -0
- /package/bin/{utils → libs/utils}/errors.d.ts +0 -0
- /package/bin/{utils → libs/utils}/errors.js +0 -0
- /package/bin/{utils → libs/utils}/system.d.ts +0 -0
- /package/bin/{utils → libs/utils}/system.js +0 -0
- /package/bin/{utils → libs/utils}/validate.d.ts +0 -0
- /package/bin/{utils → libs/utils}/validate.js +0 -0
- /package/bin/{components/ascii-art/ascii-art.d.ts → libs/visual/visual-mod.d.ts} +0 -0
- /package/bin/{components/ascii-art/ascii-art.js → libs/visual/visual-mod.js} +0 -0
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
import { inspect } from "node:util";
|
|
2
|
+
import zodToJsonSchema from "zod-to-json-schema";
|
|
3
|
+
import * as zod4 from "zod/v4/core";
|
|
4
|
+
import { CliValidationError } from "./errors.js";
|
|
5
|
+
import { getSchemaTypes } from "./json-schema.js";
|
|
6
|
+
function toJsonSchema(input2, dependencies2) {
|
|
7
|
+
try {
|
|
8
|
+
const jsonSchemaConverters = getJsonSchemaConverters(dependencies2);
|
|
9
|
+
const vendor = getVendor(input2);
|
|
10
|
+
if (vendor && vendor in jsonSchemaConverters) {
|
|
11
|
+
const converter = jsonSchemaConverters[vendor];
|
|
12
|
+
const converted = converter(input2);
|
|
13
|
+
return { success: true, value: converted };
|
|
14
|
+
}
|
|
15
|
+
return { success: false, error: "Schema not convertible to JSON schema" };
|
|
16
|
+
} catch (e) {
|
|
17
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
18
|
+
return {
|
|
19
|
+
success: false,
|
|
20
|
+
error: `Failed to convert input to JSON Schema: ${message}`
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function looksLikeJsonSchema(value) {
|
|
25
|
+
return typeof value === "object" && value !== null && ("type" in value && (typeof value.type === "string" || Array.isArray(value.type)) || "const" in value || "oneOf" in value || "anyOf" in value);
|
|
26
|
+
}
|
|
27
|
+
export function parseProcedureInputs(inputs, dependencies2) {
|
|
28
|
+
if (inputs.length === 0) {
|
|
29
|
+
return {
|
|
30
|
+
success: true,
|
|
31
|
+
value: {
|
|
32
|
+
positionalParameters: [],
|
|
33
|
+
optionsJsonSchema: {},
|
|
34
|
+
getPojoInput: () => ({})
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
const allJsonSchemaable = inputs.every((input2) => looksJsonSchemaable(input2));
|
|
39
|
+
if (!allJsonSchemaable) {
|
|
40
|
+
return {
|
|
41
|
+
success: false,
|
|
42
|
+
error: `Invalid input type ${inputs.map((s) => s?.constructor.name).join(", ")}, only inputs that can be converted to JSON Schema are supported`
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
if (inputs.length > 1) {
|
|
46
|
+
return parseMultiInputs(inputs, dependencies2);
|
|
47
|
+
}
|
|
48
|
+
const mergedSchemaResult = toJsonSchema(inputs[0], dependencies2);
|
|
49
|
+
if (!mergedSchemaResult.success) {
|
|
50
|
+
return {
|
|
51
|
+
success: false,
|
|
52
|
+
error: mergedSchemaResult.error
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
const mergedSchema = mergedSchemaResult.value;
|
|
56
|
+
return handleMergedSchema(mergedSchema);
|
|
57
|
+
}
|
|
58
|
+
function handleMergedSchema(mergedSchema) {
|
|
59
|
+
if (mergedSchema.additionalProperties) {
|
|
60
|
+
return {
|
|
61
|
+
success: false,
|
|
62
|
+
error: "Inputs with additional properties are not currently supported"
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
if (mergedSchema.type === "string") {
|
|
66
|
+
return {
|
|
67
|
+
success: true,
|
|
68
|
+
value: {
|
|
69
|
+
positionalParameters: [
|
|
70
|
+
{
|
|
71
|
+
type: "string",
|
|
72
|
+
array: false,
|
|
73
|
+
description: mergedSchema.description || "",
|
|
74
|
+
name: mergedSchema.title || "string",
|
|
75
|
+
required: !isOptional(mergedSchema)
|
|
76
|
+
}
|
|
77
|
+
],
|
|
78
|
+
optionsJsonSchema: {},
|
|
79
|
+
getPojoInput: (argv) => argv.positionalValues[0]
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
if (acceptedPrimitiveTypes(mergedSchema).length > 0) {
|
|
84
|
+
return parsePrimitiveInput(mergedSchema);
|
|
85
|
+
}
|
|
86
|
+
if (isTuple(mergedSchema)) {
|
|
87
|
+
return parseTupleInput(mergedSchema);
|
|
88
|
+
}
|
|
89
|
+
if (mergedSchema.type === "array") {
|
|
90
|
+
return parseArrayInput(
|
|
91
|
+
mergedSchema
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
if (mergedSchema.anyOf) {
|
|
95
|
+
const allObjects = mergedSchema.anyOf.every(
|
|
96
|
+
(sub) => acceptsObject(toRoughJsonSchema7(sub))
|
|
97
|
+
);
|
|
98
|
+
if (allObjects) {
|
|
99
|
+
return {
|
|
100
|
+
success: true,
|
|
101
|
+
value: {
|
|
102
|
+
positionalParameters: [],
|
|
103
|
+
optionsJsonSchema: mergedSchema,
|
|
104
|
+
getPojoInput: (argv) => argv.options
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
if (mergedSchema.anyOf.length === 2 && JSON.stringify(mergedSchema.anyOf[0]) === '{"not":{}}') {
|
|
109
|
+
return handleMergedSchema(mergedSchema.anyOf[1]);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (mergedSchema.type !== "object") {
|
|
113
|
+
return {
|
|
114
|
+
success: false,
|
|
115
|
+
error: `Invalid input type ${inspect(mergedSchema, { depth: 2, breakLength: Number.POSITIVE_INFINITY })}, expected object or tuple.`
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
success: true,
|
|
120
|
+
value: {
|
|
121
|
+
positionalParameters: [],
|
|
122
|
+
optionsJsonSchema: mergedSchema,
|
|
123
|
+
getPojoInput: (argv) => argv.options
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function isOptional(schema) {
|
|
128
|
+
if (schema && typeof schema === "object" && "optional" in schema)
|
|
129
|
+
return schema.optional === true;
|
|
130
|
+
const anyOf = schemaDefPropValue(schema, "anyOf");
|
|
131
|
+
if (anyOf?.length === 2 && JSON.stringify(anyOf[0]) === '{"not":{}}')
|
|
132
|
+
return true;
|
|
133
|
+
if (anyOf?.some((sub) => isOptional(sub))) return true;
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
function parsePrimitiveInput(schema) {
|
|
137
|
+
const typeName = acceptedPrimitiveTypes(schema).join(" | ");
|
|
138
|
+
const name = (schema.title || schema.description || /\W/.test(typeName) ? "value" : typeName).replaceAll(/\s+/g, "_");
|
|
139
|
+
return {
|
|
140
|
+
success: true,
|
|
141
|
+
value: {
|
|
142
|
+
positionalParameters: [
|
|
143
|
+
{
|
|
144
|
+
name,
|
|
145
|
+
array: false,
|
|
146
|
+
description: schema.description || "",
|
|
147
|
+
required: !isOptional(schema),
|
|
148
|
+
type: typeName
|
|
149
|
+
}
|
|
150
|
+
],
|
|
151
|
+
optionsJsonSchema: {},
|
|
152
|
+
getPojoInput: (argv) => convertPositional(schema, argv.positionalValues[0])
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
const schemaDefPropValue = (schema, prop) => {
|
|
157
|
+
if (schema && typeof schema === "object" && prop in schema)
|
|
158
|
+
return schema[prop];
|
|
159
|
+
return void 0;
|
|
160
|
+
};
|
|
161
|
+
const primitiveCandidateTypes = [
|
|
162
|
+
"string",
|
|
163
|
+
"number",
|
|
164
|
+
"boolean",
|
|
165
|
+
"integer"
|
|
166
|
+
];
|
|
167
|
+
function acceptedPrimitiveTypes(schema) {
|
|
168
|
+
let constVals = [
|
|
169
|
+
toRoughJsonSchema7(schema).const,
|
|
170
|
+
toRoughJsonSchema7(schema).enum
|
|
171
|
+
].flat().filter(Boolean).map((s) => typeof s);
|
|
172
|
+
if (constVals.length === 0) constVals = void 0;
|
|
173
|
+
const typeList = constVals || schemaDefPropValue(schema, "type") || schemaDefPropValue(schema, "oneOf")?.flatMap(
|
|
174
|
+
(s) => acceptedPrimitiveTypes(s)
|
|
175
|
+
) || schemaDefPropValue(schema, "anyOf")?.flatMap(
|
|
176
|
+
(s) => acceptedPrimitiveTypes(s)
|
|
177
|
+
);
|
|
178
|
+
const acceptedJsonSchemaTypes = new Set([typeList].flat().filter(Boolean));
|
|
179
|
+
return primitiveCandidateTypes.filter((c) => acceptedJsonSchemaTypes.has(c));
|
|
180
|
+
}
|
|
181
|
+
function parseMultiInputs(inputs, dependencies2) {
|
|
182
|
+
const parsedIndividually = inputs.map(
|
|
183
|
+
(input2) => parseProcedureInputs([input2], dependencies2)
|
|
184
|
+
);
|
|
185
|
+
const failures = parsedIndividually.flatMap(
|
|
186
|
+
(p) => p.success ? [] : [p.error]
|
|
187
|
+
);
|
|
188
|
+
if (failures.length > 0) {
|
|
189
|
+
return { success: false, error: failures.join("\n") };
|
|
190
|
+
}
|
|
191
|
+
const allObjects = parsedIndividually.every(
|
|
192
|
+
(p) => p.success && p.value.positionalParameters.length === 0
|
|
193
|
+
);
|
|
194
|
+
if (!allObjects) {
|
|
195
|
+
return {
|
|
196
|
+
success: false,
|
|
197
|
+
error: `Can't use positional parameters with multi-input type.`
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
return {
|
|
201
|
+
success: true,
|
|
202
|
+
value: {
|
|
203
|
+
positionalParameters: [],
|
|
204
|
+
optionsJsonSchema: {
|
|
205
|
+
allOf: parsedIndividually.map((p) => {
|
|
206
|
+
const successful = p;
|
|
207
|
+
const optionsSchema = successful.value.optionsJsonSchema;
|
|
208
|
+
if ("additionalProperties" in optionsSchema && optionsSchema.additionalProperties === false) {
|
|
209
|
+
const { additionalProperties, ...rest } = optionsSchema;
|
|
210
|
+
return rest;
|
|
211
|
+
}
|
|
212
|
+
return optionsSchema;
|
|
213
|
+
})
|
|
214
|
+
},
|
|
215
|
+
getPojoInput: (argv) => argv.options
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
function isNullable(schema) {
|
|
220
|
+
if (Array.isArray(schema.type) && schema.type.includes("null")) return true;
|
|
221
|
+
if (schema.type === "null") return true;
|
|
222
|
+
if ((schema.anyOf || schema.oneOf)?.some(
|
|
223
|
+
(sub) => isNullable(toRoughJsonSchema7(sub))
|
|
224
|
+
))
|
|
225
|
+
return true;
|
|
226
|
+
if (schema.const === null) return true;
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
const tupleItemsSchemas = (schema) => {
|
|
230
|
+
if (!schema || typeof schema !== "object") return void 0;
|
|
231
|
+
if (Array.isArray(schema.items)) return schema.items;
|
|
232
|
+
if ("prefixItems" in schema && Array.isArray(schema.prefixItems))
|
|
233
|
+
return schema.prefixItems;
|
|
234
|
+
return void 0;
|
|
235
|
+
};
|
|
236
|
+
function isTuple(schema) {
|
|
237
|
+
return Array.isArray(tupleItemsSchemas(schema));
|
|
238
|
+
}
|
|
239
|
+
function parseArrayInput(array) {
|
|
240
|
+
if (looksLikeJsonSchema(array.items) && isNullable(array.items)) {
|
|
241
|
+
return {
|
|
242
|
+
success: false,
|
|
243
|
+
error: `Invalid input type Array<${getSchemaTypes(array.items).join(" | ")}>. Nullable arrays are not supported.`
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
return {
|
|
247
|
+
success: true,
|
|
248
|
+
value: {
|
|
249
|
+
positionalParameters: [
|
|
250
|
+
{
|
|
251
|
+
name: parameterName(array, 1),
|
|
252
|
+
array: true,
|
|
253
|
+
description: array.description || "",
|
|
254
|
+
required: !isOptional(array),
|
|
255
|
+
type: "string"
|
|
256
|
+
}
|
|
257
|
+
],
|
|
258
|
+
optionsJsonSchema: {},
|
|
259
|
+
getPojoInput: (argv) => argv.positionalValues.at(-1).map(
|
|
260
|
+
(s) => convertPositional(array.items, s)
|
|
261
|
+
)
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
function parseTupleInput(tuple) {
|
|
266
|
+
const items = tupleItemsSchemas(tuple);
|
|
267
|
+
if (!Array.isArray(items))
|
|
268
|
+
throw new Error(".items is not an array, is this really a tuple?");
|
|
269
|
+
const flagsSchemaIndex = items.findIndex((item) => {
|
|
270
|
+
if (acceptedPrimitiveTypes(item).length > 0) {
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
if (looksLikeArray(item) && acceptedPrimitiveTypes(item.items).length > 0) {
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
return true;
|
|
277
|
+
});
|
|
278
|
+
const types = `[${items.map((s) => schemaDefPropValue(s, "type")).join(", ")}]`;
|
|
279
|
+
if (flagsSchemaIndex > -1 && flagsSchemaIndex !== items.length - 1) {
|
|
280
|
+
return {
|
|
281
|
+
success: false,
|
|
282
|
+
error: `Invalid input type ${types}. Positional parameters must be strings, numbers or booleans.`
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
const flagsSchema = flagsSchemaIndex === -1 ? null : items[flagsSchemaIndex];
|
|
286
|
+
if (flagsSchema && !acceptsObject(flagsSchema)) {
|
|
287
|
+
return {
|
|
288
|
+
success: false,
|
|
289
|
+
error: `Invalid input type ${types}. The last type must accept object inputs.`
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
const positionalSchemas = flagsSchemaIndex === -1 ? items : items.slice(0, flagsSchemaIndex);
|
|
293
|
+
return {
|
|
294
|
+
success: true,
|
|
295
|
+
value: {
|
|
296
|
+
positionalParameters: positionalSchemas.map((schema, i) => ({
|
|
297
|
+
name: parameterName(schema, i + 1),
|
|
298
|
+
array: looksLikeArray(schema),
|
|
299
|
+
description: schemaDefPropValue(schema, "description") || "",
|
|
300
|
+
required: !isOptional(schema),
|
|
301
|
+
type: getSchemaTypes(toRoughJsonSchema7(schema)).join(" | ")
|
|
302
|
+
})),
|
|
303
|
+
optionsJsonSchema: flagsSchema && typeof flagsSchema === "object" ? flagsSchema : {},
|
|
304
|
+
getPojoInput: (commandArgs) => {
|
|
305
|
+
const inputs = commandArgs.positionalValues.map((v2, i) => {
|
|
306
|
+
const correspondingSchema = positionalSchemas[i];
|
|
307
|
+
if (!correspondingSchema) {
|
|
308
|
+
throw new CliValidationError(`No schema found for position ${i}`);
|
|
309
|
+
}
|
|
310
|
+
if (looksLikeArray(correspondingSchema)) {
|
|
311
|
+
if (!Array.isArray(v2)) {
|
|
312
|
+
throw new CliValidationError(
|
|
313
|
+
`Expected array at position ${i}, got ${typeof v2}`
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
return v2.map((s) => {
|
|
317
|
+
if (!correspondingSchema.items || Array.isArray(correspondingSchema.items))
|
|
318
|
+
return s;
|
|
319
|
+
return convertPositional(correspondingSchema.items, s);
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
if (typeof v2 !== "string" && v2 !== void 0) {
|
|
323
|
+
throw new CliValidationError(
|
|
324
|
+
`Expected string at position ${i}, got ${typeof v2}`
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
return convertPositional(correspondingSchema, v2);
|
|
328
|
+
});
|
|
329
|
+
if (flagsSchema) {
|
|
330
|
+
inputs.push(commandArgs.options);
|
|
331
|
+
}
|
|
332
|
+
return inputs;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
const convertPositional = (schema, value) => {
|
|
338
|
+
let preprocessed;
|
|
339
|
+
const acceptedTypes = new Set(acceptedPrimitiveTypes(schema));
|
|
340
|
+
if (acceptedTypes.has("string")) {
|
|
341
|
+
preprocessed = value;
|
|
342
|
+
}
|
|
343
|
+
if (acceptedTypes.has("boolean")) {
|
|
344
|
+
if (value === "true") preprocessed = true;
|
|
345
|
+
else if (value === "false") preprocessed = false;
|
|
346
|
+
}
|
|
347
|
+
if (acceptedTypes.has("number")) {
|
|
348
|
+
const number = Number(value);
|
|
349
|
+
if (!Number.isNaN(number)) {
|
|
350
|
+
preprocessed = number;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
if (acceptedTypes.has("integer")) {
|
|
354
|
+
const num = Number(value);
|
|
355
|
+
if (Number.isInteger(num)) {
|
|
356
|
+
preprocessed = num;
|
|
357
|
+
} else if (!Number.isNaN(num) && acceptedTypes === void 0) {
|
|
358
|
+
preprocessed = value;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
if (preprocessed === void 0) {
|
|
362
|
+
return value;
|
|
363
|
+
}
|
|
364
|
+
return preprocessed;
|
|
365
|
+
};
|
|
366
|
+
const looksLikeArray = (schema) => {
|
|
367
|
+
return schemaDefPropValue(schema, "type") === "array";
|
|
368
|
+
};
|
|
369
|
+
const toRoughJsonSchema7 = (schema) => {
|
|
370
|
+
if (!schema || typeof schema !== "object") {
|
|
371
|
+
return {};
|
|
372
|
+
}
|
|
373
|
+
return schema;
|
|
374
|
+
};
|
|
375
|
+
const parameterName = (s, position) => {
|
|
376
|
+
if (looksLikeArray(s)) {
|
|
377
|
+
const items = toRoughJsonSchema7(s).items;
|
|
378
|
+
const elementName = parameterName(
|
|
379
|
+
!items || Array.isArray(items) ? {} : items,
|
|
380
|
+
position
|
|
381
|
+
);
|
|
382
|
+
return `[${elementName.slice(1, -1)}...]`;
|
|
383
|
+
}
|
|
384
|
+
let name = schemaDefPropValue(s, "title") || schemaDefPropValue(s, "description") || `parameter_${position}`;
|
|
385
|
+
name = name.replaceAll(/\W+/g, " ").trim();
|
|
386
|
+
return isOptional(s) ? `[${name}]` : `<${name}>`;
|
|
387
|
+
};
|
|
388
|
+
const acceptsObject = (schema) => {
|
|
389
|
+
return (schema.type === "object" || schema.anyOf?.some((sub) => acceptsObject(toRoughJsonSchema7(sub)))) ?? false;
|
|
390
|
+
};
|
|
391
|
+
const getJsonSchemaConverters = (dependencies) => {
|
|
392
|
+
return {
|
|
393
|
+
zod: (input2) => {
|
|
394
|
+
if (input2._zod?.version?.major === 4) {
|
|
395
|
+
return zod4.toJSONSchema(input2, {
|
|
396
|
+
// todo[zod@>=4.0.0] remove the line if https://github.com/colinhacks/zod/issues/4167 is resolved, or this comment if it's closed
|
|
397
|
+
io: "input",
|
|
398
|
+
// todo[zod@>=4.0.0] remove the override if https://github.com/colinhacks/zod/issues/4164 is resolved, or this comment if it's closed
|
|
399
|
+
unrepresentable: "any",
|
|
400
|
+
// todo[zod@>=4.0.0] remove the override if https://github.com/colinhacks/zod/issues/4164 is resolved, or this comment if it's closed
|
|
401
|
+
override: (ctx) => {
|
|
402
|
+
if (ctx.zodSchema?.constructor?.name === "ZodOptional") {
|
|
403
|
+
ctx.jsonSchema.optional = true;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
return zodToJsonSchema(input2);
|
|
409
|
+
},
|
|
410
|
+
arktype: (input2) => {
|
|
411
|
+
const type = prepareArktypeType(input2);
|
|
412
|
+
return type.toJsonSchema({
|
|
413
|
+
fallback: (ctx) => {
|
|
414
|
+
if (ctx.code === "unit" && ctx.unit === void 0)
|
|
415
|
+
return { ...ctx.base, optional: true };
|
|
416
|
+
return ctx.base;
|
|
417
|
+
}
|
|
418
|
+
});
|
|
419
|
+
},
|
|
420
|
+
valibot: (input) => {
|
|
421
|
+
let valibotToJsonSchemaLib = dependencies["@valibot/to-json-schema"];
|
|
422
|
+
if (!valibotToJsonSchemaLib) {
|
|
423
|
+
try {
|
|
424
|
+
valibotToJsonSchemaLib = eval(`require('@valibot/to-json-schema')`);
|
|
425
|
+
} catch (e) {
|
|
426
|
+
throw new Error(
|
|
427
|
+
"@valibot/to-json-schema could not be found - try installing it and re-running",
|
|
428
|
+
{ cause: e }
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
const valibotToJsonSchema = valibotToJsonSchemaLib?.toJsonSchema;
|
|
433
|
+
if (!valibotToJsonSchema) {
|
|
434
|
+
throw new Error(
|
|
435
|
+
`no 'toJsonSchema' function found in @valibot/to-json-schema - check you are using a supported version`
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
let v;
|
|
439
|
+
try {
|
|
440
|
+
v = eval(`require('valibot')`);
|
|
441
|
+
} catch {
|
|
442
|
+
return valibotToJsonSchema(input);
|
|
443
|
+
}
|
|
444
|
+
const parent = valibotToJsonSchema(
|
|
445
|
+
v.object({ child: input }),
|
|
446
|
+
{
|
|
447
|
+
errorMode: "ignore"
|
|
448
|
+
}
|
|
449
|
+
);
|
|
450
|
+
const child = parent.properties.child;
|
|
451
|
+
return parent.required?.length === 0 ? Object.assign(child, { optional: true }) : child;
|
|
452
|
+
},
|
|
453
|
+
effect: (input) => {
|
|
454
|
+
const effect = dependencies.effect || eval(`require('effect')`);
|
|
455
|
+
if (!effect) {
|
|
456
|
+
throw new Error(
|
|
457
|
+
"effect dependency could not be found - try installing it and re-running"
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
if (!effect.Schema.isSchema(input)) {
|
|
461
|
+
const message = "input was not an effect schema - please use effect version 3.14.2 or higher. See https://github.com/mmkal/trpc-cli/pull/63";
|
|
462
|
+
throw new Error(message);
|
|
463
|
+
}
|
|
464
|
+
return effect.JSONSchema.make(input);
|
|
465
|
+
}
|
|
466
|
+
};
|
|
467
|
+
};
|
|
468
|
+
function getVendor(schema) {
|
|
469
|
+
return schema?.["~standard"]?.vendor ?? null;
|
|
470
|
+
}
|
|
471
|
+
const jsonSchemaVendorNames = new Set(Object.keys(getJsonSchemaConverters({})));
|
|
472
|
+
function looksJsonSchemaable(value) {
|
|
473
|
+
const vendor = getVendor(value);
|
|
474
|
+
return !!vendor && jsonSchemaVendorNames.has(vendor);
|
|
475
|
+
}
|
|
476
|
+
function prepareArktypeType(type) {
|
|
477
|
+
let innerType = type;
|
|
478
|
+
while (innerType) {
|
|
479
|
+
if (innerType?.in && innerType.in !== innerType) {
|
|
480
|
+
innerType = innerType.in;
|
|
481
|
+
} else {
|
|
482
|
+
break;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
return innerType;
|
|
486
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Argument, Command, Option } from "commander";
|
|
2
|
+
import type { CommanderProgramLike, Promptable } from "./types";
|
|
3
|
+
interface Shadowed<T> {
|
|
4
|
+
original: T;
|
|
5
|
+
shadow: T;
|
|
6
|
+
}
|
|
7
|
+
type WithValue<T> = Shadowed<T> & {
|
|
8
|
+
value: string | undefined;
|
|
9
|
+
specified: boolean;
|
|
10
|
+
};
|
|
11
|
+
interface Analysis {
|
|
12
|
+
command: Shadowed<Command>;
|
|
13
|
+
arguments: WithValue<Argument>[];
|
|
14
|
+
options: WithValue<Option>[];
|
|
15
|
+
}
|
|
16
|
+
export declare const createShadowCommand: (command: Command, onAnalyze: (params: Analysis) => void | Promise<void>) => Command;
|
|
17
|
+
export declare const promptify: (program: CommanderProgramLike, prompts: Promptable) => CommanderProgramLike;
|
|
18
|
+
export {};
|