@procedures/cli-kit 0.0.0-bootstrap.0 → 0.35.1
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/CHANGELOG.md +20 -0
- package/LICENSE +21 -0
- package/README.md +61 -1
- package/dist/.tsbuildinfo +1 -0
- package/dist/args.d.ts +109 -0
- package/dist/args.d.ts.map +1 -0
- package/dist/args.js +399 -0
- package/dist/args.js.map +1 -0
- package/dist/ask.d.ts +49 -0
- package/dist/ask.d.ts.map +1 -0
- package/dist/ask.js +94 -0
- package/dist/ask.js.map +1 -0
- package/dist/cli.d.ts +117 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +253 -0
- package/dist/cli.js.map +1 -0
- package/dist/failure.d.ts +16 -0
- package/dist/failure.d.ts.map +1 -0
- package/dist/failure.js +47 -0
- package/dist/failure.js.map +1 -0
- package/dist/fields.d.ts +125 -0
- package/dist/fields.d.ts.map +1 -0
- package/dist/fields.js +339 -0
- package/dist/fields.js.map +1 -0
- package/dist/format.d.ts +35 -0
- package/dist/format.d.ts.map +1 -0
- package/dist/format.js +145 -0
- package/dist/format.js.map +1 -0
- package/dist/help.d.ts +42 -0
- package/dist/help.d.ts.map +1 -0
- package/dist/help.js +188 -0
- package/dist/help.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/package.json +60 -5
- package/src/args.ts +511 -0
- package/src/ask.ts +119 -0
- package/src/cli.ts +360 -0
- package/src/failure.ts +47 -0
- package/src/fields.ts +454 -0
- package/src/format.ts +196 -0
- package/src/help.ts +241 -0
- package/src/index.ts +71 -0
package/src/fields.ts
ADDED
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The fields of a procedure input, as a command line sees them.
|
|
3
|
+
*
|
|
4
|
+
* This module reads the field types of an input schema from its JSON Schema, converts the text of
|
|
5
|
+
* the command line to those types, and merges repeated values. The schema validates the result.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { jsonSchemaOf, type ProcedureSchema } from "@procedures/client";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* CLI metadata that can be attached to procedures via .meta()
|
|
12
|
+
*/
|
|
13
|
+
export interface CLIMeta {
|
|
14
|
+
/** Description for help text */
|
|
15
|
+
description?: string;
|
|
16
|
+
/** Field names that are positional args (in order). An array field that is last takes the rest. */
|
|
17
|
+
args?: string[];
|
|
18
|
+
/** Short flag mappings: { fieldName: "f" } */
|
|
19
|
+
shorts?: Record<string, string>;
|
|
20
|
+
/** Output format hint */
|
|
21
|
+
output?: "text" | "json" | "table" | "streaming";
|
|
22
|
+
/** Whether to prompt for missing required fields */
|
|
23
|
+
interactive?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The kinds of field the CLI can convert to. "any" is a field with no usable type: its value is
|
|
28
|
+
* JSON when it parses as JSON, and text otherwise.
|
|
29
|
+
*/
|
|
30
|
+
export type FieldKind =
|
|
31
|
+
| "string"
|
|
32
|
+
| "number"
|
|
33
|
+
| "bigint"
|
|
34
|
+
| "boolean"
|
|
35
|
+
| "enum"
|
|
36
|
+
| "literal"
|
|
37
|
+
| "date"
|
|
38
|
+
| "array"
|
|
39
|
+
| "record"
|
|
40
|
+
| "object"
|
|
41
|
+
| "union"
|
|
42
|
+
| "any";
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The type of a schema field, as the CLI sees it
|
|
46
|
+
*/
|
|
47
|
+
export interface FieldType {
|
|
48
|
+
kind: FieldKind;
|
|
49
|
+
/** The item type of an array */
|
|
50
|
+
item?: FieldType;
|
|
51
|
+
/** The value type of a record */
|
|
52
|
+
value?: FieldType;
|
|
53
|
+
/** The member types of a union */
|
|
54
|
+
options?: FieldType[];
|
|
55
|
+
/** The values of an enum */
|
|
56
|
+
values?: readonly string[];
|
|
57
|
+
/** The value of a literal */
|
|
58
|
+
literal?: unknown;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Extracted schema field info for help generation
|
|
63
|
+
*/
|
|
64
|
+
export interface SchemaFieldInfo {
|
|
65
|
+
name: string;
|
|
66
|
+
/** The display name of the type, for the help text */
|
|
67
|
+
type: string;
|
|
68
|
+
/** The type the CLI converts values to */
|
|
69
|
+
fieldType: FieldType;
|
|
70
|
+
required: boolean;
|
|
71
|
+
description: string | undefined;
|
|
72
|
+
defaultValue: unknown;
|
|
73
|
+
enumValues: readonly string[] | undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* A JSON Schema node, as `jsonSchemaOf()` of the core gives it (draft-07).
|
|
78
|
+
*/
|
|
79
|
+
interface JsonNode {
|
|
80
|
+
type?: string | string[];
|
|
81
|
+
description?: string;
|
|
82
|
+
default?: unknown;
|
|
83
|
+
enum?: unknown[];
|
|
84
|
+
const?: unknown;
|
|
85
|
+
format?: string;
|
|
86
|
+
items?: JsonNode | JsonNode[];
|
|
87
|
+
properties?: Record<string, JsonNode>;
|
|
88
|
+
required?: string[];
|
|
89
|
+
additionalProperties?: boolean | JsonNode;
|
|
90
|
+
anyOf?: JsonNode[];
|
|
91
|
+
oneOf?: JsonNode[];
|
|
92
|
+
allOf?: JsonNode[];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** True for the JSON Schema of `null` (a nullable field is `anyOf: [T, null]`). */
|
|
96
|
+
function isNull(node: JsonNode): boolean {
|
|
97
|
+
return node.type === "null" || (node.const === null && node.type === undefined);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The members of a union, without `null` (a nullable value is the type itself on the CLI). */
|
|
101
|
+
function unionMembers(node: JsonNode): JsonNode[] | undefined {
|
|
102
|
+
const members = node.anyOf ?? node.oneOf;
|
|
103
|
+
return members?.filter((member) => !isNull(member));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The CLI type of a JSON Schema node.
|
|
108
|
+
*/
|
|
109
|
+
function fieldTypeOfJson(node: JsonNode | undefined): FieldType {
|
|
110
|
+
if (!node || typeof node !== "object") {
|
|
111
|
+
return { kind: "any" };
|
|
112
|
+
}
|
|
113
|
+
const members = unionMembers(node);
|
|
114
|
+
if (members) {
|
|
115
|
+
if (members.length === 1) return fieldTypeOfJson(members[0]);
|
|
116
|
+
// A union of string constants is an enum on the CLI
|
|
117
|
+
if (members.length > 1 && members.every((member) => typeof member.const === "string")) {
|
|
118
|
+
return { kind: "enum", values: members.map((member) => member.const as string) };
|
|
119
|
+
}
|
|
120
|
+
return { kind: "union", options: members.map((member) => fieldTypeOfJson(member)) };
|
|
121
|
+
}
|
|
122
|
+
if (node.allOf?.length === 1) {
|
|
123
|
+
return fieldTypeOfJson(node.allOf[0]);
|
|
124
|
+
}
|
|
125
|
+
if (Array.isArray(node.enum)) {
|
|
126
|
+
return node.enum.every((value) => typeof value === "string")
|
|
127
|
+
? { kind: "enum", values: node.enum as string[] }
|
|
128
|
+
: { kind: "any" };
|
|
129
|
+
}
|
|
130
|
+
if ("const" in node && node.const !== undefined) {
|
|
131
|
+
return { kind: "literal", literal: node.const };
|
|
132
|
+
}
|
|
133
|
+
const types = Array.isArray(node.type) ? node.type.filter((type) => type !== "null") : node.type ? [node.type] : [];
|
|
134
|
+
if (types.length > 1) {
|
|
135
|
+
return { kind: "union", options: types.map((type) => fieldTypeOfJson({ ...node, type })) };
|
|
136
|
+
}
|
|
137
|
+
switch (types[0]) {
|
|
138
|
+
case "string":
|
|
139
|
+
return node.format === "date-time" || node.format === "date" ? { kind: "date" } : { kind: "string" };
|
|
140
|
+
case "number":
|
|
141
|
+
case "integer":
|
|
142
|
+
return { kind: "number" };
|
|
143
|
+
case "boolean":
|
|
144
|
+
return { kind: "boolean" };
|
|
145
|
+
case "array":
|
|
146
|
+
return { kind: "array", item: fieldTypeOfJson(Array.isArray(node.items) ? undefined : node.items) };
|
|
147
|
+
case "object": {
|
|
148
|
+
const extra = node.additionalProperties;
|
|
149
|
+
const hasFields = node.properties && Object.keys(node.properties).length > 0;
|
|
150
|
+
if (!hasFields && extra && typeof extra === "object") {
|
|
151
|
+
return { kind: "record", value: fieldTypeOfJson(extra) };
|
|
152
|
+
}
|
|
153
|
+
return { kind: "object" };
|
|
154
|
+
}
|
|
155
|
+
default:
|
|
156
|
+
return { kind: "any" };
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* The JSON Schema of a procedure schema (or a JSON Schema itself, as the manifest stores it).
|
|
162
|
+
*/
|
|
163
|
+
function asJsonSchema(schema: unknown): JsonNode | undefined {
|
|
164
|
+
if (!schema || typeof schema !== "object") {
|
|
165
|
+
return undefined;
|
|
166
|
+
}
|
|
167
|
+
// A JSON Schema already (from the manifest): it has no validator
|
|
168
|
+
const props = schema as Record<string, unknown>;
|
|
169
|
+
if (!("~standard" in props) && typeof props["safeParse"] !== "function" && typeof props["parse"] !== "function") {
|
|
170
|
+
return schema as JsonNode;
|
|
171
|
+
}
|
|
172
|
+
return jsonSchemaOf(schema as ProcedureSchema) as JsonNode | undefined;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* The CLI type of a schema: a procedure schema (a Standard Schema such as zod 4) or its JSON
|
|
177
|
+
* Schema. The type comes from the JSON Schema, so every schema library works. (Before, the
|
|
178
|
+
* parser read the private fields of zod 3.)
|
|
179
|
+
*/
|
|
180
|
+
export function fieldTypeOf(schema: unknown): FieldType {
|
|
181
|
+
return fieldTypeOfJson(asJsonSchema(schema));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* The display name of a type, for the help text
|
|
186
|
+
*/
|
|
187
|
+
export function typeName(type: FieldType): string {
|
|
188
|
+
switch (type.kind) {
|
|
189
|
+
case "array":
|
|
190
|
+
return `${typeName(type.item ?? { kind: "any" })}[]`;
|
|
191
|
+
case "record":
|
|
192
|
+
return "key=value";
|
|
193
|
+
case "object":
|
|
194
|
+
case "any":
|
|
195
|
+
return "json";
|
|
196
|
+
case "union":
|
|
197
|
+
return (type.options ?? []).map(typeName).join("|");
|
|
198
|
+
case "literal":
|
|
199
|
+
return JSON.stringify(type.literal);
|
|
200
|
+
default:
|
|
201
|
+
return type.kind;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Extract field information from a schema: a procedure schema or its JSON Schema. A schema that
|
|
207
|
+
* is not an object (or that has no JSON Schema) gives no fields: the CLI then passes its flags
|
|
208
|
+
* through.
|
|
209
|
+
*/
|
|
210
|
+
export function extractSchemaFields(schema: unknown): SchemaFieldInfo[] {
|
|
211
|
+
const json = asJsonSchema(schema);
|
|
212
|
+
const object = json && !json.properties ? unionMembers(json)?.find((member) => member.properties) : json;
|
|
213
|
+
if (!object?.properties) {
|
|
214
|
+
return [];
|
|
215
|
+
}
|
|
216
|
+
const required = new Set(object.required ?? []);
|
|
217
|
+
const fields: SchemaFieldInfo[] = [];
|
|
218
|
+
for (const [name, field] of Object.entries(object.properties)) {
|
|
219
|
+
if (!field || typeof field !== "object") {
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
const fieldType = fieldTypeOfJson(field);
|
|
223
|
+
const description = field.description ?? unionMembers(field)?.find((member) => member.description)?.description;
|
|
224
|
+
fields.push({
|
|
225
|
+
name,
|
|
226
|
+
type: fieldType.kind === "enum" ? "enum" : typeName(fieldType),
|
|
227
|
+
fieldType,
|
|
228
|
+
required: required.has(name) && field.default === undefined,
|
|
229
|
+
description,
|
|
230
|
+
defaultValue: field.default,
|
|
231
|
+
enumValues: fieldType.kind === "enum" ? fieldType.values : undefined,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
return fields;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// =============================================================================
|
|
238
|
+
// Conversion of CLI text to the field types
|
|
239
|
+
// =============================================================================
|
|
240
|
+
|
|
241
|
+
/** The result of a conversion: the value, or a message for the user */
|
|
242
|
+
export type Converted = { ok: true; value: unknown } | { ok: false; error: string };
|
|
243
|
+
|
|
244
|
+
// The digits before the point and the digits after it are two groups, so each text has one match
|
|
245
|
+
const NUMBER_TEXT = /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+)?$|^0[xX][0-9a-fA-F]+$/;
|
|
246
|
+
const SAFE_INTEGER_LIMIT = BigInt(Number.MAX_SAFE_INTEGER);
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Text as a JSON value, when it is JSON. A whole number that a JavaScript number cannot hold
|
|
250
|
+
* exactly stays text. Anything that is not JSON stays text.
|
|
251
|
+
*/
|
|
252
|
+
function jsonOrText(text: string): unknown {
|
|
253
|
+
const trimmed = text.trim();
|
|
254
|
+
if (/^-?\d+$/.test(trimmed)) {
|
|
255
|
+
const big = BigInt(trimmed);
|
|
256
|
+
if (big > SAFE_INTEGER_LIMIT || big < -SAFE_INTEGER_LIMIT) {
|
|
257
|
+
return text;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
try {
|
|
261
|
+
return JSON.parse(trimmed);
|
|
262
|
+
} catch {
|
|
263
|
+
return text;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Convert the CLI text of one value to a field type
|
|
269
|
+
*/
|
|
270
|
+
export function convertValue(text: string, type: FieldType, label: string): Converted {
|
|
271
|
+
switch (type.kind) {
|
|
272
|
+
case "string":
|
|
273
|
+
case "date":
|
|
274
|
+
return { ok: true, value: text };
|
|
275
|
+
case "enum":
|
|
276
|
+
return { ok: true, value: text };
|
|
277
|
+
case "number": {
|
|
278
|
+
if (!NUMBER_TEXT.test(text.trim())) {
|
|
279
|
+
return { ok: false, error: `${label} needs a number, not "${text}"` };
|
|
280
|
+
}
|
|
281
|
+
return { ok: true, value: Number(text) };
|
|
282
|
+
}
|
|
283
|
+
case "bigint":
|
|
284
|
+
try {
|
|
285
|
+
return { ok: true, value: BigInt(text) };
|
|
286
|
+
} catch {
|
|
287
|
+
return { ok: false, error: `${label} needs a whole number, not "${text}"` };
|
|
288
|
+
}
|
|
289
|
+
case "boolean":
|
|
290
|
+
if (text === "true") return { ok: true, value: true };
|
|
291
|
+
if (text === "false") return { ok: true, value: false };
|
|
292
|
+
return { ok: false, error: `${label} needs true or false, not "${text}"` };
|
|
293
|
+
case "literal": {
|
|
294
|
+
const literal = type.literal;
|
|
295
|
+
if (typeof literal === "number") return convertValue(text, { kind: "number" }, label);
|
|
296
|
+
if (typeof literal === "boolean") return convertValue(text, { kind: "boolean" }, label);
|
|
297
|
+
return { ok: true, value: text };
|
|
298
|
+
}
|
|
299
|
+
case "array": {
|
|
300
|
+
const item = type.item ?? { kind: "any" };
|
|
301
|
+
if (text.trimStart().startsWith("[")) {
|
|
302
|
+
const parsed = jsonOrText(text);
|
|
303
|
+
if (Array.isArray(parsed)) {
|
|
304
|
+
return { ok: true, value: parsed };
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
const converted = convertValue(text, item, label);
|
|
308
|
+
return converted.ok ? { ok: true, value: [converted.value] } : converted;
|
|
309
|
+
}
|
|
310
|
+
case "record": {
|
|
311
|
+
if (text.trimStart().startsWith("{")) {
|
|
312
|
+
const parsed = jsonOrText(text);
|
|
313
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
314
|
+
return { ok: true, value: parsed };
|
|
315
|
+
}
|
|
316
|
+
return { ok: false, error: `${label} needs key=value or a JSON object` };
|
|
317
|
+
}
|
|
318
|
+
const eq = text.indexOf("=");
|
|
319
|
+
if (eq <= 0) {
|
|
320
|
+
return { ok: false, error: `${label} needs key=value, not "${text}"` };
|
|
321
|
+
}
|
|
322
|
+
const converted = convertValue(text.slice(eq + 1), type.value ?? { kind: "any" }, label);
|
|
323
|
+
return converted.ok ? { ok: true, value: { [text.slice(0, eq)]: converted.value } } : converted;
|
|
324
|
+
}
|
|
325
|
+
case "object": {
|
|
326
|
+
const parsed = jsonOrText(text);
|
|
327
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
328
|
+
return { ok: true, value: parsed };
|
|
329
|
+
}
|
|
330
|
+
return { ok: false, error: `${label} needs a JSON object, not "${text}"` };
|
|
331
|
+
}
|
|
332
|
+
case "union": {
|
|
333
|
+
const options = type.options ?? [];
|
|
334
|
+
// Zod tries the members in order: a member that takes text takes the text as it is
|
|
335
|
+
for (const option of options) {
|
|
336
|
+
if (option.kind === "string" || option.kind === "date") return { ok: true, value: text };
|
|
337
|
+
if (option.kind === "enum" && option.values?.includes(text)) return { ok: true, value: text };
|
|
338
|
+
if (option.kind === "literal" && String(option.literal) === text) {
|
|
339
|
+
return { ok: true, value: option.literal };
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
for (const option of options) {
|
|
343
|
+
if (option.kind === "enum" || option.kind === "literal") continue;
|
|
344
|
+
const converted = convertValue(text, option, label);
|
|
345
|
+
if (converted.ok) return converted;
|
|
346
|
+
}
|
|
347
|
+
return { ok: true, value: jsonOrText(text) };
|
|
348
|
+
}
|
|
349
|
+
case "any":
|
|
350
|
+
default:
|
|
351
|
+
return { ok: true, value: jsonOrText(text) };
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Add a converted value to a field that already has one. Arrays collect and records merge,
|
|
357
|
+
* so a repeated flag adds to the field. Other fields take the last value.
|
|
358
|
+
*/
|
|
359
|
+
export function mergeValue(previous: unknown, next: unknown, type: FieldType): unknown {
|
|
360
|
+
if (previous === undefined) {
|
|
361
|
+
return next;
|
|
362
|
+
}
|
|
363
|
+
if (type.kind === "array" && Array.isArray(previous) && Array.isArray(next)) {
|
|
364
|
+
return [...previous, ...next];
|
|
365
|
+
}
|
|
366
|
+
if (
|
|
367
|
+
type.kind === "record" &&
|
|
368
|
+
previous && typeof previous === "object" && !Array.isArray(previous) &&
|
|
369
|
+
next && typeof next === "object" && !Array.isArray(next)
|
|
370
|
+
) {
|
|
371
|
+
return { ...previous, ...next };
|
|
372
|
+
}
|
|
373
|
+
return next;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Convert camelCase to kebab-case
|
|
378
|
+
*/
|
|
379
|
+
export function toKebab(str: string): string {
|
|
380
|
+
return str.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Convert kebab-case to camelCase
|
|
385
|
+
*/
|
|
386
|
+
export function toCamel(str: string): string {
|
|
387
|
+
return str.replace(/-([a-zA-Z0-9])/g, (_, c: string) => c.toUpperCase());
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* A command as the command line sees it: a procedure of a registry (with its input schema), or a
|
|
392
|
+
* description of a procedure (with the fields of its input), for example the manifest of `mark`.
|
|
393
|
+
*/
|
|
394
|
+
export interface CommandLike {
|
|
395
|
+
path: readonly string[];
|
|
396
|
+
metadata?: unknown;
|
|
397
|
+
/** The input schema of a procedure of the registry */
|
|
398
|
+
input?: unknown;
|
|
399
|
+
/** The output schema of a procedure of the registry */
|
|
400
|
+
output?: unknown;
|
|
401
|
+
/** The fields of the input of a description */
|
|
402
|
+
fields?: readonly SchemaFieldInfo[];
|
|
403
|
+
/** The fields of the result of a description */
|
|
404
|
+
resultFields?: readonly SchemaFieldInfo[];
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** This function gives the fields of the input of a command: from its description, or read from its schema. */
|
|
408
|
+
export function fieldsOf(command: CommandLike): SchemaFieldInfo[] {
|
|
409
|
+
return command.fields ? [...command.fields] : extractSchemaFields(command.input);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* The fields of the result of a procedure, from its output schema: only for a result that is an
|
|
414
|
+
* object (a union, a stream item or a pass-through schema gives none).
|
|
415
|
+
*/
|
|
416
|
+
export function resultFieldsOfSchema(output: unknown): SchemaFieldInfo[] {
|
|
417
|
+
const json = output ? (jsonSchemaOf(output as ProcedureSchema, { io: "output" }) as JsonNode | undefined) : undefined;
|
|
418
|
+
if (!json) return [];
|
|
419
|
+
if (json.type === "object") return extractSchemaFields(json);
|
|
420
|
+
// A number, a string, a boolean or an array: the help shows the one value
|
|
421
|
+
if (typeof json.type === "string" && ["number", "integer", "string", "boolean", "array"].includes(json.type)) {
|
|
422
|
+
const fieldType = fieldTypeOfJson(json);
|
|
423
|
+
return [
|
|
424
|
+
{
|
|
425
|
+
name: "value",
|
|
426
|
+
type: typeName(fieldType),
|
|
427
|
+
fieldType,
|
|
428
|
+
required: true,
|
|
429
|
+
description: json.description,
|
|
430
|
+
defaultValue: undefined,
|
|
431
|
+
enumValues: undefined,
|
|
432
|
+
},
|
|
433
|
+
];
|
|
434
|
+
}
|
|
435
|
+
return [];
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/** This function gives the fields of the result of a command: from its description, or read from its output schema. */
|
|
439
|
+
export function resultFieldsOf(command: CommandLike): SchemaFieldInfo[] {
|
|
440
|
+
return command.resultFields ? [...command.resultFields] : resultFieldsOfSchema(command.output);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/** A field that only the metadata names (the schema has no readable shape) */
|
|
444
|
+
export function fieldWithoutSchema(name: string): SchemaFieldInfo {
|
|
445
|
+
return {
|
|
446
|
+
name,
|
|
447
|
+
type: "json",
|
|
448
|
+
fieldType: { kind: "any" },
|
|
449
|
+
required: false,
|
|
450
|
+
description: undefined,
|
|
451
|
+
defaultValue: undefined,
|
|
452
|
+
enumValues: undefined,
|
|
453
|
+
};
|
|
454
|
+
}
|
package/src/format.ts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Output Formatting
|
|
3
|
+
*
|
|
4
|
+
* Formats procedure results for CLI output based on metadata hints.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export type OutputFormat = "text" | "json" | "table" | "streaming";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Print interface (used by formatOutput)
|
|
11
|
+
*/
|
|
12
|
+
export interface Print {
|
|
13
|
+
info: (message: string) => void;
|
|
14
|
+
error: (message: string) => void;
|
|
15
|
+
success: (message: string) => void;
|
|
16
|
+
warning: (message: string) => void;
|
|
17
|
+
table: (data: string[][], options?: { format?: string }) => void;
|
|
18
|
+
spin: (message: string) => Spinner;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface Spinner {
|
|
22
|
+
stop: () => void;
|
|
23
|
+
succeed: (message?: string) => void;
|
|
24
|
+
fail: (message?: string) => void;
|
|
25
|
+
text: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Format and print the result based on output format hint
|
|
30
|
+
*/
|
|
31
|
+
export function formatOutput(
|
|
32
|
+
print: Print,
|
|
33
|
+
result: unknown,
|
|
34
|
+
format: OutputFormat = "text"
|
|
35
|
+
): void {
|
|
36
|
+
if (result === undefined || result === null) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
switch (format) {
|
|
41
|
+
case "json":
|
|
42
|
+
print.info(JSON.stringify(result, null, 2));
|
|
43
|
+
break;
|
|
44
|
+
|
|
45
|
+
case "table":
|
|
46
|
+
formatTable(print, result);
|
|
47
|
+
break;
|
|
48
|
+
|
|
49
|
+
case "text":
|
|
50
|
+
case "streaming":
|
|
51
|
+
default:
|
|
52
|
+
formatText(print, result);
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Format as text - handles common result shapes
|
|
59
|
+
*/
|
|
60
|
+
function formatText(print: Print, result: unknown): void {
|
|
61
|
+
// String output
|
|
62
|
+
if (typeof result === "string") {
|
|
63
|
+
print.info(result);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Result with output field (common pattern)
|
|
68
|
+
if (isObjectWithOutput(result)) {
|
|
69
|
+
print.info(result.output);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Result with message field
|
|
74
|
+
if (isObjectWithMessage(result)) {
|
|
75
|
+
if (result.success === false) {
|
|
76
|
+
print.error(result.message);
|
|
77
|
+
} else {
|
|
78
|
+
print.success(result.message);
|
|
79
|
+
}
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Result with error field
|
|
84
|
+
if (isObjectWithError(result)) {
|
|
85
|
+
print.error(result.error);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Array of items
|
|
90
|
+
if (Array.isArray(result)) {
|
|
91
|
+
for (const item of result) {
|
|
92
|
+
formatText(print, item);
|
|
93
|
+
}
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Fallback to JSON
|
|
98
|
+
print.info(JSON.stringify(result, null, 2));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Format as table - handles arrays of objects
|
|
103
|
+
*/
|
|
104
|
+
function formatTable(print: Print, result: unknown): void {
|
|
105
|
+
// Result with rows/headers
|
|
106
|
+
if (isTableResult(result)) {
|
|
107
|
+
const headers = result.headers ?? Object.keys(result.rows[0] ?? {});
|
|
108
|
+
const data: string[][] = [headers];
|
|
109
|
+
|
|
110
|
+
for (const row of result.rows) {
|
|
111
|
+
if (Array.isArray(row)) {
|
|
112
|
+
data.push(row.map(String));
|
|
113
|
+
} else if (typeof row === "object" && row !== null) {
|
|
114
|
+
data.push(headers.map((h) => String((row as Record<string, unknown>)[h] ?? "")));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
print.table(data);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Plain array of objects
|
|
123
|
+
if (Array.isArray(result) && result.length > 0 && typeof result[0] === "object") {
|
|
124
|
+
const headers = Object.keys(result[0] as object);
|
|
125
|
+
const data: string[][] = [headers];
|
|
126
|
+
|
|
127
|
+
for (const row of result) {
|
|
128
|
+
if (typeof row === "object" && row !== null) {
|
|
129
|
+
data.push(headers.map((h) => String((row as Record<string, unknown>)[h] ?? "")));
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
print.table(data);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Fallback to text
|
|
138
|
+
formatText(print, result);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Type guards
|
|
142
|
+
|
|
143
|
+
function isObjectWithOutput(val: unknown): val is { output: string } {
|
|
144
|
+
return (
|
|
145
|
+
typeof val === "object" &&
|
|
146
|
+
val !== null &&
|
|
147
|
+
"output" in val &&
|
|
148
|
+
typeof (val as { output: unknown }).output === "string"
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function isObjectWithMessage(val: unknown): val is { message: string; success?: boolean } {
|
|
153
|
+
return (
|
|
154
|
+
typeof val === "object" &&
|
|
155
|
+
val !== null &&
|
|
156
|
+
"message" in val &&
|
|
157
|
+
typeof (val as { message: unknown }).message === "string"
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function isObjectWithError(val: unknown): val is { error: string } {
|
|
162
|
+
return (
|
|
163
|
+
typeof val === "object" &&
|
|
164
|
+
val !== null &&
|
|
165
|
+
"error" in val &&
|
|
166
|
+
typeof (val as { error: unknown }).error === "string"
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function isTableResult(val: unknown): val is { rows: unknown[]; headers?: string[] } {
|
|
171
|
+
return (
|
|
172
|
+
typeof val === "object" &&
|
|
173
|
+
val !== null &&
|
|
174
|
+
"rows" in val &&
|
|
175
|
+
Array.isArray((val as { rows: unknown }).rows)
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* This function gives a `Print` that writes plain text to the console: messages to stdout,
|
|
181
|
+
* errors to stderr, a table as columns. Its spinner shows nothing.
|
|
182
|
+
*/
|
|
183
|
+
export function plainPrint(): Print {
|
|
184
|
+
return {
|
|
185
|
+
info: (message) => console.log(message),
|
|
186
|
+
error: (message) => console.error(message),
|
|
187
|
+
success: (message) => console.log(message),
|
|
188
|
+
warning: (message) => console.error(message),
|
|
189
|
+
table: (data) => {
|
|
190
|
+
const widths: number[] = [];
|
|
191
|
+
for (const row of data) row.forEach((cell, i) => (widths[i] = Math.max(widths[i] ?? 0, cell.length)));
|
|
192
|
+
for (const row of data) console.log(row.map((cell, i) => cell.padEnd(widths[i] ?? 0)).join(" ").trimEnd());
|
|
193
|
+
},
|
|
194
|
+
spin: (message) => ({ stop: () => {}, succeed: () => {}, fail: () => {}, text: message }),
|
|
195
|
+
};
|
|
196
|
+
}
|