@procedures/mark 0.0.0-stage → 0.2.0
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 +36 -0
- package/LICENSE +21 -0
- package/README.md +799 -2
- package/dist/.tsbuildinfo +1 -0
- package/dist/args.d.ts +77 -0
- package/dist/args.d.ts.map +1 -0
- package/dist/args.js +351 -0
- package/dist/args.js.map +1 -0
- package/dist/ask.d.ts +48 -0
- package/dist/ask.d.ts.map +1 -0
- package/dist/ask.js +91 -0
- package/dist/ask.js.map +1 -0
- package/dist/bin.d.ts +9 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +10 -0
- package/dist/bin.js.map +1 -0
- package/dist/cli.d.ts +61 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +535 -0
- package/dist/cli.js.map +1 -0
- package/dist/client-mode.d.ts +21 -0
- package/dist/client-mode.d.ts.map +1 -0
- package/dist/client-mode.js +75 -0
- package/dist/client-mode.js.map +1 -0
- package/dist/ecosystem.d.ts +67 -0
- package/dist/ecosystem.d.ts.map +1 -0
- package/dist/ecosystem.js +212 -0
- package/dist/ecosystem.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/format.d.ts +30 -0
- package/dist/format.d.ts.map +1 -0
- package/dist/format.js +125 -0
- package/dist/format.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/lockfile.d.ts +26 -0
- package/dist/lockfile.d.ts.map +1 -0
- package/dist/lockfile.js +68 -0
- package/dist/lockfile.js.map +1 -0
- package/dist/manifest-build.d.ts +7 -0
- package/dist/manifest-build.d.ts.map +1 -0
- package/dist/manifest-build.js +11 -0
- package/dist/manifest-build.js.map +1 -0
- package/dist/manifest.d.ts +57 -0
- package/dist/manifest.d.ts.map +1 -0
- package/dist/manifest.js +140 -0
- package/dist/manifest.js.map +1 -0
- package/dist/manifest.json +20960 -0
- package/dist/parse.d.ts +143 -0
- package/dist/parse.d.ts.map +1 -0
- package/dist/parse.js +509 -0
- package/dist/parse.js.map +1 -0
- package/dist/print.d.ts +42 -0
- package/dist/print.d.ts.map +1 -0
- package/dist/print.js +80 -0
- package/dist/print.js.map +1 -0
- package/dist/prompts.d.ts +58 -0
- package/dist/prompts.d.ts.map +1 -0
- package/dist/prompts.js +124 -0
- package/dist/prompts.js.map +1 -0
- package/dist/repl.d.ts +34 -0
- package/dist/repl.d.ts.map +1 -0
- package/dist/repl.js +101 -0
- package/dist/repl.js.map +1 -0
- package/dist/server-mode.d.ts +37 -0
- package/dist/server-mode.d.ts.map +1 -0
- package/dist/server-mode.js +167 -0
- package/dist/server-mode.js.map +1 -0
- package/package.json +97 -3
- package/src/args.ts +440 -0
- package/src/ask.ts +119 -0
- package/src/bin.ts +11 -0
- package/src/cli.ts +600 -0
- package/src/client-mode.ts +90 -0
- package/src/ecosystem.ts +260 -0
- package/src/failure.ts +47 -0
- package/src/format.ts +177 -0
- package/src/index.ts +31 -0
- package/src/lockfile.ts +85 -0
- package/src/manifest-build.ts +12 -0
- package/src/manifest.ts +162 -0
- package/src/parse.ts +670 -0
- package/src/print.ts +108 -0
- package/src/prompts.ts +181 -0
- package/src/repl.ts +121 -0
- package/src/server-mode.ts +205 -0
package/dist/parse.d.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema-based CLI Argument Parser
|
|
3
|
+
*
|
|
4
|
+
* Reads the field types of a procedure's input schema, converts CLI text to those types, and
|
|
5
|
+
* makes the help text. Validation is delegated to the procedure's schema.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* CLI metadata that can be attached to procedures via .meta()
|
|
9
|
+
*/
|
|
10
|
+
export interface CLIMeta {
|
|
11
|
+
/** Description for help text */
|
|
12
|
+
description?: string;
|
|
13
|
+
/** Field names that are positional args (in order). An array field that is last takes the rest. */
|
|
14
|
+
args?: string[];
|
|
15
|
+
/** Short flag mappings: { fieldName: "f" } */
|
|
16
|
+
shorts?: Record<string, string>;
|
|
17
|
+
/** Output format hint */
|
|
18
|
+
output?: "text" | "json" | "table" | "streaming";
|
|
19
|
+
/** Whether to prompt for missing required fields */
|
|
20
|
+
interactive?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The kinds of field the CLI can convert to. "any" is a field with no usable type: its value is
|
|
24
|
+
* JSON when it parses as JSON, and text otherwise.
|
|
25
|
+
*/
|
|
26
|
+
export type FieldKind = "string" | "number" | "bigint" | "boolean" | "enum" | "literal" | "date" | "array" | "record" | "object" | "union" | "any";
|
|
27
|
+
/**
|
|
28
|
+
* The type of a schema field, as the CLI sees it
|
|
29
|
+
*/
|
|
30
|
+
export interface FieldType {
|
|
31
|
+
kind: FieldKind;
|
|
32
|
+
/** The item type of an array */
|
|
33
|
+
item?: FieldType;
|
|
34
|
+
/** The value type of a record */
|
|
35
|
+
value?: FieldType;
|
|
36
|
+
/** The member types of a union */
|
|
37
|
+
options?: FieldType[];
|
|
38
|
+
/** The values of an enum */
|
|
39
|
+
values?: readonly string[];
|
|
40
|
+
/** The value of a literal */
|
|
41
|
+
literal?: unknown;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Extracted schema field info for help generation
|
|
45
|
+
*/
|
|
46
|
+
export interface SchemaFieldInfo {
|
|
47
|
+
name: string;
|
|
48
|
+
/** The display name of the type, for the help text */
|
|
49
|
+
type: string;
|
|
50
|
+
/** The type the CLI converts values to */
|
|
51
|
+
fieldType: FieldType;
|
|
52
|
+
required: boolean;
|
|
53
|
+
description: string | undefined;
|
|
54
|
+
defaultValue: unknown;
|
|
55
|
+
enumValues: readonly string[] | undefined;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The CLI type of a schema: a procedure schema (a Standard Schema such as zod 4) or its JSON
|
|
59
|
+
* Schema. The type comes from the JSON Schema, so every schema library works. (Before, the
|
|
60
|
+
* parser read the private fields of zod 3.)
|
|
61
|
+
*/
|
|
62
|
+
export declare function fieldTypeOf(schema: unknown): FieldType;
|
|
63
|
+
/**
|
|
64
|
+
* Extract field information from a schema: a procedure schema or its JSON Schema. A schema that
|
|
65
|
+
* is not an object (or that has no JSON Schema) gives no fields: the CLI then passes its flags
|
|
66
|
+
* through.
|
|
67
|
+
*/
|
|
68
|
+
export declare function extractSchemaFields(schema: unknown): SchemaFieldInfo[];
|
|
69
|
+
/** The result of a conversion: the value, or a message for the user */
|
|
70
|
+
export type Converted = {
|
|
71
|
+
ok: true;
|
|
72
|
+
value: unknown;
|
|
73
|
+
} | {
|
|
74
|
+
ok: false;
|
|
75
|
+
error: string;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Convert the CLI text of one value to a field type
|
|
79
|
+
*/
|
|
80
|
+
export declare function convertValue(text: string, type: FieldType, label: string): Converted;
|
|
81
|
+
/**
|
|
82
|
+
* Add a converted value to a field that already has one. Arrays collect and records merge,
|
|
83
|
+
* so a repeated flag adds to the field. Other fields take the last value.
|
|
84
|
+
*/
|
|
85
|
+
export declare function mergeValue(previous: unknown, next: unknown, type: FieldType): unknown;
|
|
86
|
+
/**
|
|
87
|
+
* Gluegun parameters shape
|
|
88
|
+
*/
|
|
89
|
+
export interface Parameters {
|
|
90
|
+
first?: string;
|
|
91
|
+
second?: string;
|
|
92
|
+
third?: string;
|
|
93
|
+
array?: string[];
|
|
94
|
+
options?: Record<string, unknown>;
|
|
95
|
+
raw?: string[];
|
|
96
|
+
string?: string;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Convert camelCase to kebab-case
|
|
100
|
+
*/
|
|
101
|
+
export declare function toKebab(str: string): string;
|
|
102
|
+
/**
|
|
103
|
+
* Convert kebab-case to camelCase
|
|
104
|
+
*/
|
|
105
|
+
export declare function toCamel(str: string): string;
|
|
106
|
+
/**
|
|
107
|
+
* Parse CLI parameters into procedure input based on metadata. With the schema, each value is
|
|
108
|
+
* converted by the type of its field: a string field keeps its text. Without the schema, a value
|
|
109
|
+
* is JSON when it parses as JSON, and text otherwise.
|
|
110
|
+
*/
|
|
111
|
+
export declare function parseFromSchema(params: Parameters, meta: CLIMeta, schema?: unknown): Record<string, unknown>;
|
|
112
|
+
/**
|
|
113
|
+
* A command as mark sees it: a procedure of the registry (with its input schema), or a command
|
|
114
|
+
* of the manifest (with the fields of its input). See manifest.ts.
|
|
115
|
+
*/
|
|
116
|
+
export interface CommandLike {
|
|
117
|
+
path: readonly string[];
|
|
118
|
+
metadata?: unknown;
|
|
119
|
+
/** The input schema of a procedure of the registry */
|
|
120
|
+
input?: unknown;
|
|
121
|
+
/** The output schema of a procedure of the registry */
|
|
122
|
+
output?: unknown;
|
|
123
|
+
/** The fields of the input of a command of the manifest */
|
|
124
|
+
fields?: readonly SchemaFieldInfo[];
|
|
125
|
+
/** The fields of the result of a command of the manifest */
|
|
126
|
+
resultFields?: readonly SchemaFieldInfo[];
|
|
127
|
+
}
|
|
128
|
+
/** The fields of the input of a command: from the manifest, or read from the schema */
|
|
129
|
+
export declare function fieldsOf(command: CommandLike): SchemaFieldInfo[];
|
|
130
|
+
/**
|
|
131
|
+
* The fields of the result of a procedure, from its output schema: only for a result that is an
|
|
132
|
+
* object (a union, a stream item or a pass-through schema gives none).
|
|
133
|
+
*/
|
|
134
|
+
export declare function resultFieldsOfSchema(output: unknown): SchemaFieldInfo[];
|
|
135
|
+
/** The fields of the result of a command: from the manifest, or read from the output schema */
|
|
136
|
+
export declare function resultFieldsOf(command: CommandLike): SchemaFieldInfo[];
|
|
137
|
+
/**
|
|
138
|
+
* Generate help text for a procedure based on its metadata and schema. `schema` is the input
|
|
139
|
+
* schema, or the fields of the input (a command of the manifest). `result` gives the fields of
|
|
140
|
+
* the result, which the help lists after the options.
|
|
141
|
+
*/
|
|
142
|
+
export declare function generateHelp(path: string[], meta: CLIMeta, schema?: unknown, result?: readonly SchemaFieldInfo[]): string;
|
|
143
|
+
//# sourceMappingURL=parse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"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"}
|
package/dist/parse.js
ADDED
|
@@ -0,0 +1,509 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema-based CLI Argument Parser
|
|
3
|
+
*
|
|
4
|
+
* Reads the field types of a procedure's input schema, converts CLI text to those types, and
|
|
5
|
+
* makes the help text. Validation is delegated to the procedure's schema.
|
|
6
|
+
*/
|
|
7
|
+
import { CLI_GLOBAL_FLAGS } from "@procedures/lib/cli-flags";
|
|
8
|
+
import { jsonSchemaOf } from "@procedures/client";
|
|
9
|
+
/** True for the JSON Schema of `null` (a nullable field is `anyOf: [T, null]`). */
|
|
10
|
+
function isNull(node) {
|
|
11
|
+
return node.type === "null" || (node.const === null && node.type === undefined);
|
|
12
|
+
}
|
|
13
|
+
/** The members of a union, without `null` (a nullable value is the type itself on the CLI). */
|
|
14
|
+
function unionMembers(node) {
|
|
15
|
+
const members = node.anyOf ?? node.oneOf;
|
|
16
|
+
return members?.filter((member) => !isNull(member));
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The CLI type of a JSON Schema node.
|
|
20
|
+
*/
|
|
21
|
+
function fieldTypeOfJson(node) {
|
|
22
|
+
if (!node || typeof node !== "object") {
|
|
23
|
+
return { kind: "any" };
|
|
24
|
+
}
|
|
25
|
+
const members = unionMembers(node);
|
|
26
|
+
if (members) {
|
|
27
|
+
if (members.length === 1)
|
|
28
|
+
return fieldTypeOfJson(members[0]);
|
|
29
|
+
// A union of string constants is an enum on the CLI
|
|
30
|
+
if (members.length > 1 && members.every((member) => typeof member.const === "string")) {
|
|
31
|
+
return { kind: "enum", values: members.map((member) => member.const) };
|
|
32
|
+
}
|
|
33
|
+
return { kind: "union", options: members.map((member) => fieldTypeOfJson(member)) };
|
|
34
|
+
}
|
|
35
|
+
if (node.allOf?.length === 1) {
|
|
36
|
+
return fieldTypeOfJson(node.allOf[0]);
|
|
37
|
+
}
|
|
38
|
+
if (Array.isArray(node.enum)) {
|
|
39
|
+
return node.enum.every((value) => typeof value === "string")
|
|
40
|
+
? { kind: "enum", values: node.enum }
|
|
41
|
+
: { kind: "any" };
|
|
42
|
+
}
|
|
43
|
+
if ("const" in node && node.const !== undefined) {
|
|
44
|
+
return { kind: "literal", literal: node.const };
|
|
45
|
+
}
|
|
46
|
+
const types = Array.isArray(node.type) ? node.type.filter((type) => type !== "null") : node.type ? [node.type] : [];
|
|
47
|
+
if (types.length > 1) {
|
|
48
|
+
return { kind: "union", options: types.map((type) => fieldTypeOfJson({ ...node, type })) };
|
|
49
|
+
}
|
|
50
|
+
switch (types[0]) {
|
|
51
|
+
case "string":
|
|
52
|
+
return node.format === "date-time" || node.format === "date" ? { kind: "date" } : { kind: "string" };
|
|
53
|
+
case "number":
|
|
54
|
+
case "integer":
|
|
55
|
+
return { kind: "number" };
|
|
56
|
+
case "boolean":
|
|
57
|
+
return { kind: "boolean" };
|
|
58
|
+
case "array":
|
|
59
|
+
return { kind: "array", item: fieldTypeOfJson(Array.isArray(node.items) ? undefined : node.items) };
|
|
60
|
+
case "object": {
|
|
61
|
+
const extra = node.additionalProperties;
|
|
62
|
+
const hasFields = node.properties && Object.keys(node.properties).length > 0;
|
|
63
|
+
if (!hasFields && extra && typeof extra === "object") {
|
|
64
|
+
return { kind: "record", value: fieldTypeOfJson(extra) };
|
|
65
|
+
}
|
|
66
|
+
return { kind: "object" };
|
|
67
|
+
}
|
|
68
|
+
default:
|
|
69
|
+
return { kind: "any" };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The JSON Schema of a procedure schema (or a JSON Schema itself, as the manifest stores it).
|
|
74
|
+
*/
|
|
75
|
+
function asJsonSchema(schema) {
|
|
76
|
+
if (!schema || typeof schema !== "object") {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
// A JSON Schema already (from the manifest): it has no validator
|
|
80
|
+
const props = schema;
|
|
81
|
+
if (!("~standard" in props) && typeof props["safeParse"] !== "function" && typeof props["parse"] !== "function") {
|
|
82
|
+
return schema;
|
|
83
|
+
}
|
|
84
|
+
return jsonSchemaOf(schema);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The CLI type of a schema: a procedure schema (a Standard Schema such as zod 4) or its JSON
|
|
88
|
+
* Schema. The type comes from the JSON Schema, so every schema library works. (Before, the
|
|
89
|
+
* parser read the private fields of zod 3.)
|
|
90
|
+
*/
|
|
91
|
+
export function fieldTypeOf(schema) {
|
|
92
|
+
return fieldTypeOfJson(asJsonSchema(schema));
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The display name of a type, for the help text
|
|
96
|
+
*/
|
|
97
|
+
function typeName(type) {
|
|
98
|
+
switch (type.kind) {
|
|
99
|
+
case "array":
|
|
100
|
+
return `${typeName(type.item ?? { kind: "any" })}[]`;
|
|
101
|
+
case "record":
|
|
102
|
+
return "key=value";
|
|
103
|
+
case "object":
|
|
104
|
+
case "any":
|
|
105
|
+
return "json";
|
|
106
|
+
case "union":
|
|
107
|
+
return (type.options ?? []).map(typeName).join("|");
|
|
108
|
+
case "literal":
|
|
109
|
+
return JSON.stringify(type.literal);
|
|
110
|
+
default:
|
|
111
|
+
return type.kind;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Extract field information from a schema: a procedure schema or its JSON Schema. A schema that
|
|
116
|
+
* is not an object (or that has no JSON Schema) gives no fields: the CLI then passes its flags
|
|
117
|
+
* through.
|
|
118
|
+
*/
|
|
119
|
+
export function extractSchemaFields(schema) {
|
|
120
|
+
const json = asJsonSchema(schema);
|
|
121
|
+
const object = json && !json.properties ? unionMembers(json)?.find((member) => member.properties) : json;
|
|
122
|
+
if (!object?.properties) {
|
|
123
|
+
return [];
|
|
124
|
+
}
|
|
125
|
+
const required = new Set(object.required ?? []);
|
|
126
|
+
const fields = [];
|
|
127
|
+
for (const [name, field] of Object.entries(object.properties)) {
|
|
128
|
+
if (!field || typeof field !== "object") {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const fieldType = fieldTypeOfJson(field);
|
|
132
|
+
const description = field.description ?? unionMembers(field)?.find((member) => member.description)?.description;
|
|
133
|
+
fields.push({
|
|
134
|
+
name,
|
|
135
|
+
type: fieldType.kind === "enum" ? "enum" : typeName(fieldType),
|
|
136
|
+
fieldType,
|
|
137
|
+
required: required.has(name) && field.default === undefined,
|
|
138
|
+
description,
|
|
139
|
+
defaultValue: field.default,
|
|
140
|
+
enumValues: fieldType.kind === "enum" ? fieldType.values : undefined,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
return fields;
|
|
144
|
+
}
|
|
145
|
+
const NUMBER_TEXT = /^[-+]?(\d+\.?\d*|\.\d+)([eE][-+]?\d+)?$|^0[xX][0-9a-fA-F]+$/;
|
|
146
|
+
const SAFE_INTEGER_LIMIT = BigInt(Number.MAX_SAFE_INTEGER);
|
|
147
|
+
/**
|
|
148
|
+
* Text as a JSON value, when it is JSON. A whole number that a JavaScript number cannot hold
|
|
149
|
+
* exactly stays text. Anything that is not JSON stays text.
|
|
150
|
+
*/
|
|
151
|
+
function jsonOrText(text) {
|
|
152
|
+
const trimmed = text.trim();
|
|
153
|
+
if (/^-?\d+$/.test(trimmed)) {
|
|
154
|
+
const big = BigInt(trimmed);
|
|
155
|
+
if (big > SAFE_INTEGER_LIMIT || big < -SAFE_INTEGER_LIMIT) {
|
|
156
|
+
return text;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
try {
|
|
160
|
+
return JSON.parse(trimmed);
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
return text;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Convert the CLI text of one value to a field type
|
|
168
|
+
*/
|
|
169
|
+
export function convertValue(text, type, label) {
|
|
170
|
+
switch (type.kind) {
|
|
171
|
+
case "string":
|
|
172
|
+
case "date":
|
|
173
|
+
return { ok: true, value: text };
|
|
174
|
+
case "enum":
|
|
175
|
+
return { ok: true, value: text };
|
|
176
|
+
case "number": {
|
|
177
|
+
if (!NUMBER_TEXT.test(text.trim())) {
|
|
178
|
+
return { ok: false, error: `${label} needs a number, not "${text}"` };
|
|
179
|
+
}
|
|
180
|
+
return { ok: true, value: Number(text) };
|
|
181
|
+
}
|
|
182
|
+
case "bigint":
|
|
183
|
+
try {
|
|
184
|
+
return { ok: true, value: BigInt(text) };
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
return { ok: false, error: `${label} needs a whole number, not "${text}"` };
|
|
188
|
+
}
|
|
189
|
+
case "boolean":
|
|
190
|
+
if (text === "true")
|
|
191
|
+
return { ok: true, value: true };
|
|
192
|
+
if (text === "false")
|
|
193
|
+
return { ok: true, value: false };
|
|
194
|
+
return { ok: false, error: `${label} needs true or false, not "${text}"` };
|
|
195
|
+
case "literal": {
|
|
196
|
+
const literal = type.literal;
|
|
197
|
+
if (typeof literal === "number")
|
|
198
|
+
return convertValue(text, { kind: "number" }, label);
|
|
199
|
+
if (typeof literal === "boolean")
|
|
200
|
+
return convertValue(text, { kind: "boolean" }, label);
|
|
201
|
+
return { ok: true, value: text };
|
|
202
|
+
}
|
|
203
|
+
case "array": {
|
|
204
|
+
const item = type.item ?? { kind: "any" };
|
|
205
|
+
if (text.trimStart().startsWith("[")) {
|
|
206
|
+
const parsed = jsonOrText(text);
|
|
207
|
+
if (Array.isArray(parsed)) {
|
|
208
|
+
return { ok: true, value: parsed };
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
const converted = convertValue(text, item, label);
|
|
212
|
+
return converted.ok ? { ok: true, value: [converted.value] } : converted;
|
|
213
|
+
}
|
|
214
|
+
case "record": {
|
|
215
|
+
if (text.trimStart().startsWith("{")) {
|
|
216
|
+
const parsed = jsonOrText(text);
|
|
217
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
218
|
+
return { ok: true, value: parsed };
|
|
219
|
+
}
|
|
220
|
+
return { ok: false, error: `${label} needs key=value or a JSON object` };
|
|
221
|
+
}
|
|
222
|
+
const eq = text.indexOf("=");
|
|
223
|
+
if (eq <= 0) {
|
|
224
|
+
return { ok: false, error: `${label} needs key=value, not "${text}"` };
|
|
225
|
+
}
|
|
226
|
+
const converted = convertValue(text.slice(eq + 1), type.value ?? { kind: "any" }, label);
|
|
227
|
+
return converted.ok ? { ok: true, value: { [text.slice(0, eq)]: converted.value } } : converted;
|
|
228
|
+
}
|
|
229
|
+
case "object": {
|
|
230
|
+
const parsed = jsonOrText(text);
|
|
231
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
232
|
+
return { ok: true, value: parsed };
|
|
233
|
+
}
|
|
234
|
+
return { ok: false, error: `${label} needs a JSON object, not "${text}"` };
|
|
235
|
+
}
|
|
236
|
+
case "union": {
|
|
237
|
+
const options = type.options ?? [];
|
|
238
|
+
// Zod tries the members in order: a member that takes text takes the text as it is
|
|
239
|
+
for (const option of options) {
|
|
240
|
+
if (option.kind === "string" || option.kind === "date")
|
|
241
|
+
return { ok: true, value: text };
|
|
242
|
+
if (option.kind === "enum" && option.values?.includes(text))
|
|
243
|
+
return { ok: true, value: text };
|
|
244
|
+
if (option.kind === "literal" && String(option.literal) === text) {
|
|
245
|
+
return { ok: true, value: option.literal };
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
for (const option of options) {
|
|
249
|
+
if (option.kind === "enum" || option.kind === "literal")
|
|
250
|
+
continue;
|
|
251
|
+
const converted = convertValue(text, option, label);
|
|
252
|
+
if (converted.ok)
|
|
253
|
+
return converted;
|
|
254
|
+
}
|
|
255
|
+
return { ok: true, value: jsonOrText(text) };
|
|
256
|
+
}
|
|
257
|
+
case "any":
|
|
258
|
+
default:
|
|
259
|
+
return { ok: true, value: jsonOrText(text) };
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Add a converted value to a field that already has one. Arrays collect and records merge,
|
|
264
|
+
* so a repeated flag adds to the field. Other fields take the last value.
|
|
265
|
+
*/
|
|
266
|
+
export function mergeValue(previous, next, type) {
|
|
267
|
+
if (previous === undefined) {
|
|
268
|
+
return next;
|
|
269
|
+
}
|
|
270
|
+
if (type.kind === "array" && Array.isArray(previous) && Array.isArray(next)) {
|
|
271
|
+
return [...previous, ...next];
|
|
272
|
+
}
|
|
273
|
+
if (type.kind === "record" &&
|
|
274
|
+
previous && typeof previous === "object" && !Array.isArray(previous) &&
|
|
275
|
+
next && typeof next === "object" && !Array.isArray(next)) {
|
|
276
|
+
return { ...previous, ...next };
|
|
277
|
+
}
|
|
278
|
+
return next;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Convert camelCase to kebab-case
|
|
282
|
+
*/
|
|
283
|
+
export function toKebab(str) {
|
|
284
|
+
return str.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Convert kebab-case to camelCase
|
|
288
|
+
*/
|
|
289
|
+
export function toCamel(str) {
|
|
290
|
+
return str.replace(/-([a-zA-Z0-9])/g, (_, c) => c.toUpperCase());
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Convert one parameter by the type of its field. A value that is not text (true for a flag
|
|
294
|
+
* with no value) is not changed.
|
|
295
|
+
*/
|
|
296
|
+
function convertParameter(value, type, label) {
|
|
297
|
+
if (typeof value !== "string") {
|
|
298
|
+
return value;
|
|
299
|
+
}
|
|
300
|
+
const converted = convertValue(value, type, label);
|
|
301
|
+
if (!converted.ok) {
|
|
302
|
+
throw new Error(converted.error);
|
|
303
|
+
}
|
|
304
|
+
return converted.value;
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Parse CLI parameters into procedure input based on metadata. With the schema, each value is
|
|
308
|
+
* converted by the type of its field: a string field keeps its text. Without the schema, a value
|
|
309
|
+
* is JSON when it parses as JSON, and text otherwise.
|
|
310
|
+
*/
|
|
311
|
+
export function parseFromSchema(params, meta, schema) {
|
|
312
|
+
const positionalArgs = meta.args ?? [];
|
|
313
|
+
const shorts = meta.shorts ?? {};
|
|
314
|
+
const fields = new Map(extractSchemaFields(schema).map((field) => [field.name, field.fieldType]));
|
|
315
|
+
const typeOfField = (field) => fields.get(field) ?? { kind: "any" };
|
|
316
|
+
const input = {};
|
|
317
|
+
// 1. Parse positional args
|
|
318
|
+
const positionalValues = params.array ?? [];
|
|
319
|
+
positionalArgs.forEach((field, i) => {
|
|
320
|
+
if (positionalValues[i] !== undefined) {
|
|
321
|
+
input[field] = convertParameter(positionalValues[i], typeOfField(field), field);
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
// 2. Parse options from CLI flags
|
|
325
|
+
const options = params.options ?? {};
|
|
326
|
+
// Build reverse lookup for short flags
|
|
327
|
+
const shortToField = {};
|
|
328
|
+
for (const [field, short] of Object.entries(shorts)) {
|
|
329
|
+
shortToField[short] = field;
|
|
330
|
+
}
|
|
331
|
+
for (const [key, value] of Object.entries(options)) {
|
|
332
|
+
// Skip if it's a positional arg already set
|
|
333
|
+
if (positionalArgs.includes(key) && input[key] !== undefined) {
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
const field = shortToField[key] ?? (key.includes("-") ? toCamel(key) : key);
|
|
337
|
+
input[field] = convertParameter(value, typeOfField(field), `--${toKebab(field)}`);
|
|
338
|
+
}
|
|
339
|
+
return input;
|
|
340
|
+
}
|
|
341
|
+
// =============================================================================
|
|
342
|
+
// Help
|
|
343
|
+
// =============================================================================
|
|
344
|
+
/**
|
|
345
|
+
* How to give a field on the command line, for the help text
|
|
346
|
+
*/
|
|
347
|
+
function valueHint(field) {
|
|
348
|
+
const type = field.fieldType;
|
|
349
|
+
switch (type.kind) {
|
|
350
|
+
case "boolean":
|
|
351
|
+
return "";
|
|
352
|
+
case "array":
|
|
353
|
+
return ` <${typeName(type.item ?? { kind: "any" })}>... (repeat the flag, or give a JSON array)`;
|
|
354
|
+
case "record":
|
|
355
|
+
return " <key=value>... (repeat the flag, or give a JSON object)";
|
|
356
|
+
case "object":
|
|
357
|
+
case "any":
|
|
358
|
+
return " <json>";
|
|
359
|
+
case "enum":
|
|
360
|
+
return ` <${(field.enumValues ?? []).join("|")}>`;
|
|
361
|
+
default:
|
|
362
|
+
return ` <${typeName(type)}>`;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
/** The fields of the input of a command: from the manifest, or read from the schema */
|
|
366
|
+
export function fieldsOf(command) {
|
|
367
|
+
return command.fields ? [...command.fields] : extractSchemaFields(command.input);
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* The fields of the result of a procedure, from its output schema: only for a result that is an
|
|
371
|
+
* object (a union, a stream item or a pass-through schema gives none).
|
|
372
|
+
*/
|
|
373
|
+
export function resultFieldsOfSchema(output) {
|
|
374
|
+
const json = output ? jsonSchemaOf(output, { io: "output" }) : undefined;
|
|
375
|
+
if (!json)
|
|
376
|
+
return [];
|
|
377
|
+
if (json.type === "object")
|
|
378
|
+
return extractSchemaFields(json);
|
|
379
|
+
// A number, a string, a boolean or an array: the help shows the one value (`mark client add`)
|
|
380
|
+
if (typeof json.type === "string" && ["number", "integer", "string", "boolean", "array"].includes(json.type)) {
|
|
381
|
+
const fieldType = fieldTypeOfJson(json);
|
|
382
|
+
return [
|
|
383
|
+
{
|
|
384
|
+
name: "value",
|
|
385
|
+
type: typeName(fieldType),
|
|
386
|
+
fieldType,
|
|
387
|
+
required: true,
|
|
388
|
+
description: json.description,
|
|
389
|
+
defaultValue: undefined,
|
|
390
|
+
enumValues: undefined,
|
|
391
|
+
},
|
|
392
|
+
];
|
|
393
|
+
}
|
|
394
|
+
return [];
|
|
395
|
+
}
|
|
396
|
+
/** The fields of the result of a command: from the manifest, or read from the output schema */
|
|
397
|
+
export function resultFieldsOf(command) {
|
|
398
|
+
return command.resultFields ? [...command.resultFields] : resultFieldsOfSchema(command.output);
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Generate help text for a procedure based on its metadata and schema. `schema` is the input
|
|
402
|
+
* schema, or the fields of the input (a command of the manifest). `result` gives the fields of
|
|
403
|
+
* the result, which the help lists after the options.
|
|
404
|
+
*/
|
|
405
|
+
export function generateHelp(path, meta, schema, result = []) {
|
|
406
|
+
const positionalArgs = meta.args ?? [];
|
|
407
|
+
const shorts = meta.shorts ?? {};
|
|
408
|
+
const lines = [];
|
|
409
|
+
// Extract schema fields for enhanced help
|
|
410
|
+
const schemaFields = Array.isArray(schema)
|
|
411
|
+
? schema
|
|
412
|
+
: schema
|
|
413
|
+
? extractSchemaFields(schema)
|
|
414
|
+
: [];
|
|
415
|
+
const schemaFieldMap = new Map(schemaFields.map((f) => [f.name, f]));
|
|
416
|
+
// Command signature: the last positional array takes the rest of the arguments
|
|
417
|
+
const cmdName = `mark ${path.join(" ")}`;
|
|
418
|
+
const posStr = positionalArgs
|
|
419
|
+
.map((a, i) => {
|
|
420
|
+
const many = i === positionalArgs.length - 1 && schemaFieldMap.get(a)?.fieldType.kind === "array";
|
|
421
|
+
return many ? `<${a}...>` : `<${a}>`;
|
|
422
|
+
})
|
|
423
|
+
.join(" ");
|
|
424
|
+
lines.push(`Usage: ${cmdName}${posStr ? " " + posStr : ""} [options]`);
|
|
425
|
+
lines.push("");
|
|
426
|
+
if (meta.description) {
|
|
427
|
+
lines.push(meta.description);
|
|
428
|
+
lines.push("");
|
|
429
|
+
}
|
|
430
|
+
// Positional args with schema info
|
|
431
|
+
if (positionalArgs.length > 0) {
|
|
432
|
+
lines.push("Arguments:");
|
|
433
|
+
for (const arg of positionalArgs) {
|
|
434
|
+
const fieldInfo = schemaFieldMap.get(arg);
|
|
435
|
+
const typeStr = fieldInfo ? ` (${fieldInfo.type})` : "";
|
|
436
|
+
const reqStr = fieldInfo?.required ? " [required]" : "";
|
|
437
|
+
const descStr = fieldInfo?.description ? ` ${fieldInfo.description}` : "";
|
|
438
|
+
lines.push(` ${arg}${typeStr}${reqStr}`);
|
|
439
|
+
if (descStr) {
|
|
440
|
+
lines.push(` ${descStr}`);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
lines.push("");
|
|
444
|
+
}
|
|
445
|
+
// Options: each schema field that is not positional, with its short flag if it has one
|
|
446
|
+
const optionLines = [];
|
|
447
|
+
const positionalSet = new Set(positionalArgs);
|
|
448
|
+
const listed = new Set();
|
|
449
|
+
const optionFields = [
|
|
450
|
+
...Object.keys(shorts).map((name) => schemaFieldMap.get(name) ?? fieldWithoutSchema(name)),
|
|
451
|
+
...schemaFields.filter((field) => !(field.name in shorts) && !positionalSet.has(field.name)),
|
|
452
|
+
];
|
|
453
|
+
for (const field of optionFields) {
|
|
454
|
+
if (listed.has(field.name)) {
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
listed.add(field.name);
|
|
458
|
+
const short = shorts[field.name];
|
|
459
|
+
const kebab = toKebab(field.name);
|
|
460
|
+
const flag = short ? ` -${short}, --${kebab}` : ` --${kebab}`;
|
|
461
|
+
const negation = field.fieldType.kind === "boolean" ? ` (or --no-${kebab})` : "";
|
|
462
|
+
const reqStr = field.required ? " [required]" : "";
|
|
463
|
+
const defaultStr = field.defaultValue !== undefined ? ` (default: ${JSON.stringify(field.defaultValue)})` : "";
|
|
464
|
+
optionLines.push(`${flag}${valueHint(field)}${negation}${reqStr}${defaultStr}`);
|
|
465
|
+
if (field.description) {
|
|
466
|
+
optionLines.push(` ${field.description}`);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
if (optionLines.length > 0) {
|
|
470
|
+
lines.push("Options:");
|
|
471
|
+
lines.push(...optionLines);
|
|
472
|
+
}
|
|
473
|
+
// The fields of the result (roadmap 2.1: the output schemas describe them)
|
|
474
|
+
if (result.length > 0) {
|
|
475
|
+
lines.push("");
|
|
476
|
+
lines.push("Result:");
|
|
477
|
+
for (const field of result) {
|
|
478
|
+
const optional = field.required ? "" : ", optional";
|
|
479
|
+
lines.push(` ${field.name} (${field.type}${optional})`);
|
|
480
|
+
if (field.description) {
|
|
481
|
+
lines.push(` ${field.description}`);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
// Standard options
|
|
486
|
+
lines.push("");
|
|
487
|
+
lines.push("Global Options (before the command, or after it when the command has no flag of that name):");
|
|
488
|
+
for (const flag of CLI_GLOBAL_FLAGS) {
|
|
489
|
+
if (flag.mode || flag.serverOption || flag.long === "version") {
|
|
490
|
+
continue;
|
|
491
|
+
}
|
|
492
|
+
const name = flag.short ? `-${flag.short}, --${flag.long}` : ` --${flag.long}`;
|
|
493
|
+
lines.push(` ${name.padEnd(18)} ${flag.description}`);
|
|
494
|
+
}
|
|
495
|
+
return lines.join("\n");
|
|
496
|
+
}
|
|
497
|
+
/** A field that only the metadata names (the schema has no readable shape) */
|
|
498
|
+
function fieldWithoutSchema(name) {
|
|
499
|
+
return {
|
|
500
|
+
name,
|
|
501
|
+
type: "json",
|
|
502
|
+
fieldType: { kind: "any" },
|
|
503
|
+
required: false,
|
|
504
|
+
description: undefined,
|
|
505
|
+
defaultValue: undefined,
|
|
506
|
+
enumValues: undefined,
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
//# sourceMappingURL=parse.js.map
|