@procedures/mark 0.33.0 → 0.35.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 +108 -0
- package/README.md +2 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/args.d.ts +5 -53
- package/dist/args.d.ts.map +1 -1
- package/dist/args.js +9 -341
- package/dist/args.js.map +1 -1
- package/dist/ask.d.ts +3 -38
- package/dist/ask.d.ts.map +1 -1
- package/dist/ask.js +3 -81
- package/dist/ask.js.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +18 -64
- package/dist/cli.js.map +1 -1
- package/dist/client-mode.js +3 -3
- package/dist/client-mode.js.map +1 -1
- package/dist/failure.d.ts +3 -2
- package/dist/failure.d.ts.map +1 -1
- package/dist/failure.js +3 -33
- package/dist/failure.js.map +1 -1
- package/dist/format.d.ts +3 -25
- package/dist/format.d.ts.map +1 -1
- package/dist/format.js +3 -120
- package/dist/format.js.map +1 -1
- package/dist/manifest.d.ts.map +1 -1
- package/dist/manifest.js +3 -9
- package/dist/manifest.js.map +1 -1
- package/dist/manifest.json +23 -21
- package/dist/parse.d.ts +4 -114
- package/dist/parse.d.ts.map +1 -1
- package/dist/parse.js +7 -445
- package/dist/parse.js.map +1 -1
- package/package.json +50 -50
- package/src/args.ts +14 -403
- package/src/ask.ts +14 -109
- package/src/cli.ts +16 -70
- package/src/client-mode.ts +3 -3
- package/src/failure.ts +3 -33
- package/src/format.ts +3 -172
- package/src/manifest.ts +3 -8
- package/src/parse.ts +33 -570
- package/dist/prompts.d.ts +0 -58
- package/dist/prompts.d.ts.map +0 -1
- package/dist/prompts.js +0 -124
- package/dist/prompts.js.map +0 -1
- package/src/prompts.ts +0 -181
package/src/args.ts
CHANGED
|
@@ -2,33 +2,19 @@
|
|
|
2
2
|
* Command Line Splitting
|
|
3
3
|
*
|
|
4
4
|
* Splits the argv of `mark` into the global flags, the command path, and the input of the
|
|
5
|
-
* procedure. The rules
|
|
6
|
-
*
|
|
7
|
-
* - A flag before the command path is a global flag.
|
|
8
|
-
* - After the path, a flag is the procedure's when the procedure declares it (its field name, the
|
|
9
|
-
* kebab-case name, or its short letter). Otherwise it is a global flag. Otherwise it is an error.
|
|
10
|
-
* So `mark docker compose down -v` gives `-v` to the procedure, not to `--version`.
|
|
11
|
-
* - Each value is converted by the type of its field: a string field keeps its text.
|
|
12
|
-
* - A repeated array flag collects its values. A repeated record flag (`--env A=1 --env B=2`) merges.
|
|
13
|
-
* - A boolean flag takes `true`/`false` after it, or `--flag=false`, or `--no-flag`.
|
|
14
|
-
* - A repeated global flag with a value keeps the last value. A repeatable one (`--events`)
|
|
15
|
-
* collects its values, separated by commas.
|
|
16
|
-
* - A negative number is a value, not a flag. `--` ends the flags.
|
|
17
|
-
* - The positional fields take the arguments in order. A last array field takes the rest.
|
|
18
|
-
* An argument that no field takes is an error.
|
|
5
|
+
* procedure. The rules are in `@procedures/cli-kit` (roadmap O61): this module gives them the
|
|
6
|
+
* name `mark` and the global flags of mark (`@procedures/lib/cli-flags`).
|
|
19
7
|
*/
|
|
20
8
|
|
|
21
|
-
import {
|
|
9
|
+
import { CLI_GLOBAL_FLAGS } from "@procedures/lib/cli-flags";
|
|
22
10
|
import {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
mergeValue,
|
|
26
|
-
toCamel,
|
|
27
|
-
toKebab,
|
|
28
|
-
type CLIMeta,
|
|
11
|
+
parseCommandLine as kitParseCommandLine,
|
|
12
|
+
parseLeadingGlobals as kitParseLeadingGlobals,
|
|
29
13
|
type CommandLike,
|
|
30
|
-
type
|
|
31
|
-
} from "
|
|
14
|
+
type ParsedCommandLine as KitParsedCommandLine,
|
|
15
|
+
} from "@procedures/cli-kit";
|
|
16
|
+
|
|
17
|
+
export { findChildren, findProcedure, procedureFlags } from "@procedures/cli-kit";
|
|
32
18
|
|
|
33
19
|
/** The long names of the global flags */
|
|
34
20
|
export type GlobalName =
|
|
@@ -51,400 +37,25 @@ export type GlobalName =
|
|
|
51
37
|
/** The global flags that the command line gives: true for a flag, text for a flag with a value */
|
|
52
38
|
export type Globals = Partial<Record<GlobalName, string | boolean>>;
|
|
53
39
|
|
|
54
|
-
export interface ParsedCommandLine<T extends CommandLike = CommandLike> {
|
|
55
|
-
/** The command path */
|
|
56
|
-
path: string[];
|
|
57
|
-
/** The procedure at the path, if there is one */
|
|
58
|
-
procedure: T | undefined;
|
|
59
|
-
/** The procedure input, converted by the field types (not validated yet) */
|
|
60
|
-
input: Record<string, unknown>;
|
|
40
|
+
export interface ParsedCommandLine<T extends CommandLike = CommandLike> extends Omit<KitParsedCommandLine<T>, "globals"> {
|
|
61
41
|
/** The global flags */
|
|
62
42
|
globals: Globals;
|
|
63
|
-
/** The arguments that no positional field took */
|
|
64
|
-
extraArguments: string[];
|
|
65
|
-
/** The problems, one message each */
|
|
66
|
-
errors: string[];
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// The digits before the point and the digits after it are two groups, so each token has one match
|
|
70
|
-
const NEGATIVE_NUMBER = /^-(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+)?$/;
|
|
71
|
-
const ANY: FieldType = { kind: "any" };
|
|
72
|
-
|
|
73
|
-
/** A token that is a flag (not a value, not a negative number, not "-" alone) */
|
|
74
|
-
function isFlag(token: string): boolean {
|
|
75
|
-
return token.startsWith("-") && token !== "-" && !NEGATIVE_NUMBER.test(token);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Find a procedure matching the given path
|
|
80
|
-
*/
|
|
81
|
-
export function findProcedure<T extends CommandLike>(procedures: readonly T[], path: readonly string[]): T | undefined {
|
|
82
|
-
return procedures.find((p) => p.path.length === path.length && p.path.every((seg, i) => seg === path[i]));
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Find procedures that are children of the given path
|
|
87
|
-
*/
|
|
88
|
-
export function findChildren<T extends CommandLike>(procedures: readonly T[], path: readonly string[]): T[] {
|
|
89
|
-
return procedures.filter((p) => p.path.length > path.length && path.every((seg, i) => seg === p.path[i]));
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
interface FlagSpec {
|
|
93
|
-
field: string;
|
|
94
|
-
type: FieldType;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
interface ProcedureFlags {
|
|
98
|
-
long: Map<string, FlagSpec>;
|
|
99
|
-
short: Map<string, FlagSpec>;
|
|
100
|
-
/** The field types by field name */
|
|
101
|
-
fields: Map<string, FieldType>;
|
|
102
|
-
/** True when the schema has readable fields: then an unknown flag is an error */
|
|
103
|
-
typed: boolean;
|
|
104
|
-
meta: CLIMeta;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* The flags a procedure declares: each field of its input schema (camelCase and kebab-case
|
|
109
|
-
* names) and the short letters of its metadata.
|
|
110
|
-
*/
|
|
111
|
-
export function procedureFlags(procedure: CommandLike | undefined): ProcedureFlags {
|
|
112
|
-
const meta = (procedure?.metadata ?? {}) as CLIMeta;
|
|
113
|
-
const schemaFields = procedure ? fieldsOf(procedure) : [];
|
|
114
|
-
const fields = new Map(schemaFields.map((field) => [field.name, field.fieldType]));
|
|
115
|
-
const long = new Map<string, FlagSpec>();
|
|
116
|
-
const short = new Map<string, FlagSpec>();
|
|
117
|
-
|
|
118
|
-
const addLong = (field: string): void => {
|
|
119
|
-
const spec = { field, type: fields.get(field) ?? ANY };
|
|
120
|
-
long.set(field, spec);
|
|
121
|
-
long.set(toKebab(field), spec);
|
|
122
|
-
};
|
|
123
|
-
for (const field of fields.keys()) {
|
|
124
|
-
addLong(field);
|
|
125
|
-
}
|
|
126
|
-
for (const [field, letter] of Object.entries(meta.shorts ?? {})) {
|
|
127
|
-
if (!long.has(field)) {
|
|
128
|
-
addLong(field);
|
|
129
|
-
}
|
|
130
|
-
short.set(letter, { field, type: fields.get(field) ?? ANY });
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return { long, short, fields, typed: schemaFields.length > 0, meta };
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/** The state of one pass over the tokens */
|
|
137
|
-
class TokenReader {
|
|
138
|
-
index = 0;
|
|
139
|
-
constructor(readonly tokens: readonly string[]) {}
|
|
140
|
-
|
|
141
|
-
get current(): string | undefined {
|
|
142
|
-
return this.tokens[this.index];
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/** The value of a flag: the inline value, or the next token when it is not a flag */
|
|
146
|
-
takeValue(inline: string | undefined): string | undefined {
|
|
147
|
-
if (inline !== undefined) {
|
|
148
|
-
return inline;
|
|
149
|
-
}
|
|
150
|
-
const next = this.tokens[this.index + 1];
|
|
151
|
-
if (next !== undefined && !isFlag(next)) {
|
|
152
|
-
this.index++;
|
|
153
|
-
return next;
|
|
154
|
-
}
|
|
155
|
-
return undefined;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/** The value of a boolean flag: the inline value, or a next token that is "true" or "false" */
|
|
159
|
-
takeBoolean(inline: string | undefined): string | undefined {
|
|
160
|
-
if (inline !== undefined) {
|
|
161
|
-
return inline;
|
|
162
|
-
}
|
|
163
|
-
const next = this.tokens[this.index + 1];
|
|
164
|
-
if (next === "true" || next === "false") {
|
|
165
|
-
this.index++;
|
|
166
|
-
return next;
|
|
167
|
-
}
|
|
168
|
-
return undefined;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/** Split "--name=value" (or "-n=value") into the name and the inline value */
|
|
173
|
-
function splitInline(body: string): { name: string; inline: string | undefined } {
|
|
174
|
-
const eq = body.indexOf("=");
|
|
175
|
-
return eq === -1 ? { name: body, inline: undefined } : { name: body.slice(0, eq), inline: body.slice(eq + 1) };
|
|
176
43
|
}
|
|
177
44
|
|
|
178
|
-
/**
|
|
179
|
-
|
|
180
|
-
flag: CliGlobalFlag,
|
|
181
|
-
label: string,
|
|
182
|
-
inline: string | undefined,
|
|
183
|
-
reader: TokenReader,
|
|
184
|
-
globals: Globals
|
|
185
|
-
): string | undefined {
|
|
186
|
-
const name = flag.long as GlobalName;
|
|
187
|
-
if (flag.takesValue) {
|
|
188
|
-
const value = reader.takeValue(inline);
|
|
189
|
-
if (value === undefined) {
|
|
190
|
-
return `${label} needs a value`;
|
|
191
|
-
}
|
|
192
|
-
// A repeatable flag (--events) collects its values, separated by commas
|
|
193
|
-
const previous = globals[name];
|
|
194
|
-
globals[name] = flag.repeatable && typeof previous === "string" ? `${previous},${value}` : value;
|
|
195
|
-
return undefined;
|
|
196
|
-
}
|
|
197
|
-
if (inline !== undefined && inline !== "true" && inline !== "false") {
|
|
198
|
-
return `${label} needs true or false, not "${inline}"`;
|
|
199
|
-
}
|
|
200
|
-
globals[name] = inline !== "false";
|
|
201
|
-
return undefined;
|
|
202
|
-
}
|
|
45
|
+
/** The options of the parse of mark: its name and its global flags. A value `@<file>` stays text. */
|
|
46
|
+
const MARK = { name: "mark", globals: CLI_GLOBAL_FLAGS } as const;
|
|
203
47
|
|
|
204
48
|
/**
|
|
205
49
|
* Read the global flags before the command path. This needs no procedures: `mark` reads them
|
|
206
50
|
* before it loads the procedures (`--version`, `--server`, `-i`).
|
|
207
51
|
*/
|
|
208
52
|
export function parseLeadingGlobals(argv: readonly string[]): { globals: Globals; rest: string[]; errors: string[] } {
|
|
209
|
-
|
|
210
|
-
const errors: string[] = [];
|
|
211
|
-
const reader = new TokenReader(argv);
|
|
212
|
-
|
|
213
|
-
for (; reader.index < argv.length; reader.index++) {
|
|
214
|
-
const token = reader.current!;
|
|
215
|
-
if (!isFlag(token) || token === "--") {
|
|
216
|
-
break;
|
|
217
|
-
}
|
|
218
|
-
const isLong = token.startsWith("--");
|
|
219
|
-
const { name, inline } = splitInline(token.slice(isLong ? 2 : 1));
|
|
220
|
-
const flag = isLong || name.length === 1 ? findGlobalFlag(name, !isLong) : undefined;
|
|
221
|
-
if (!flag) {
|
|
222
|
-
errors.push(`Unknown option before the command: ${token}`);
|
|
223
|
-
continue;
|
|
224
|
-
}
|
|
225
|
-
const error = readGlobal(flag, isLong ? `--${name}` : `-${name}`, inline, reader, globals);
|
|
226
|
-
if (error) {
|
|
227
|
-
errors.push(error);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
const rest = argv.slice(reader.index);
|
|
232
|
-
if (rest[0] === "--") {
|
|
233
|
-
rest.shift();
|
|
234
|
-
}
|
|
235
|
-
return { globals, rest, errors };
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* The command path at the start of the tokens: the longest run of words that names a procedure
|
|
240
|
-
* or a group of procedures. The first word is part of the path even when it names nothing (the
|
|
241
|
-
* command is then unknown).
|
|
242
|
-
*/
|
|
243
|
-
function readPath(tokens: readonly string[], procedures: readonly CommandLike[]): string[] {
|
|
244
|
-
const path: string[] = [];
|
|
245
|
-
for (const token of tokens) {
|
|
246
|
-
if (isFlag(token) || token === "--") {
|
|
247
|
-
break;
|
|
248
|
-
}
|
|
249
|
-
const testPath = [...path, token];
|
|
250
|
-
if (findProcedure(procedures, testPath)) {
|
|
251
|
-
path.push(token);
|
|
252
|
-
break;
|
|
253
|
-
}
|
|
254
|
-
if (findChildren(procedures, testPath).length > 0 || path.length === 0) {
|
|
255
|
-
path.push(token);
|
|
256
|
-
continue;
|
|
257
|
-
}
|
|
258
|
-
break;
|
|
259
|
-
}
|
|
260
|
-
return path;
|
|
53
|
+
return kitParseLeadingGlobals(argv, MARK) as { globals: Globals; rest: string[]; errors: string[] };
|
|
261
54
|
}
|
|
262
55
|
|
|
263
56
|
/**
|
|
264
57
|
* Split a whole command line: global flags, the command path, and the procedure input
|
|
265
58
|
*/
|
|
266
59
|
export function parseCommandLine<T extends CommandLike>(argv: readonly string[], procedures: readonly T[]): ParsedCommandLine<T> {
|
|
267
|
-
|
|
268
|
-
const globals = leading.globals;
|
|
269
|
-
const errors = [...leading.errors];
|
|
270
|
-
const path = readPath(leading.rest, procedures);
|
|
271
|
-
const procedure = findProcedure(procedures, path);
|
|
272
|
-
const flags = procedureFlags(procedure);
|
|
273
|
-
const input: Record<string, unknown> = {};
|
|
274
|
-
const fromFlag = new Set<string>();
|
|
275
|
-
const positionals: string[] = [];
|
|
276
|
-
|
|
277
|
-
const setField = (spec: FlagSpec, text: string, label: string): void => {
|
|
278
|
-
const converted = convertValue(text, spec.type, label);
|
|
279
|
-
if (!converted.ok) {
|
|
280
|
-
errors.push(converted.error);
|
|
281
|
-
return;
|
|
282
|
-
}
|
|
283
|
-
input[spec.field] = mergeValue(input[spec.field], converted.value, spec.type);
|
|
284
|
-
fromFlag.add(spec.field);
|
|
285
|
-
};
|
|
286
|
-
|
|
287
|
-
/** Read one procedure flag. Returns false when the procedure does not declare it. */
|
|
288
|
-
const readProcedureFlag = (
|
|
289
|
-
spec: FlagSpec | undefined,
|
|
290
|
-
label: string,
|
|
291
|
-
inline: string | undefined,
|
|
292
|
-
reader: TokenReader
|
|
293
|
-
): boolean => {
|
|
294
|
-
if (!spec) {
|
|
295
|
-
return false;
|
|
296
|
-
}
|
|
297
|
-
if (spec.type.kind === "boolean") {
|
|
298
|
-
setField(spec, reader.takeBoolean(inline) ?? "true", label);
|
|
299
|
-
return true;
|
|
300
|
-
}
|
|
301
|
-
const value = reader.takeValue(inline);
|
|
302
|
-
if (value === undefined) {
|
|
303
|
-
errors.push(`${label} needs a value`);
|
|
304
|
-
return true;
|
|
305
|
-
}
|
|
306
|
-
setField(spec, value, label);
|
|
307
|
-
return true;
|
|
308
|
-
};
|
|
309
|
-
|
|
310
|
-
/** A flag that neither the procedure nor the globals have */
|
|
311
|
-
const readOther = (name: string, label: string, inline: string | undefined, reader: TokenReader): void => {
|
|
312
|
-
if (procedure && !flags.typed) {
|
|
313
|
-
// The schema has no readable fields: pass the flag through, as JSON or text
|
|
314
|
-
const value = reader.takeValue(inline);
|
|
315
|
-
setField({ field: toCamel(name), type: ANY }, value ?? "true", label);
|
|
316
|
-
return;
|
|
317
|
-
}
|
|
318
|
-
const where = path.length > 0 ? ` for mark ${path.join(" ")}` : "";
|
|
319
|
-
errors.push(`Unknown option${where}: ${label}`);
|
|
320
|
-
};
|
|
321
|
-
|
|
322
|
-
const reader = new TokenReader(leading.rest);
|
|
323
|
-
reader.index = path.length;
|
|
324
|
-
let onlyPositionals = false;
|
|
325
|
-
|
|
326
|
-
for (; reader.index < leading.rest.length; reader.index++) {
|
|
327
|
-
const token = reader.current!;
|
|
328
|
-
|
|
329
|
-
if (onlyPositionals || !isFlag(token)) {
|
|
330
|
-
positionals.push(token);
|
|
331
|
-
continue;
|
|
332
|
-
}
|
|
333
|
-
if (token === "--") {
|
|
334
|
-
onlyPositionals = true;
|
|
335
|
-
continue;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
if (token.startsWith("--")) {
|
|
339
|
-
const { name, inline } = splitInline(token.slice(2));
|
|
340
|
-
const label = `--${name}`;
|
|
341
|
-
if (readProcedureFlag(flags.long.get(name), label, inline, reader)) {
|
|
342
|
-
continue;
|
|
343
|
-
}
|
|
344
|
-
// --no-<flag> sets a boolean field to false
|
|
345
|
-
const negated = name.startsWith("no-") ? flags.long.get(name.slice(3)) : undefined;
|
|
346
|
-
if (negated?.type.kind === "boolean") {
|
|
347
|
-
if (inline !== undefined) {
|
|
348
|
-
errors.push(`${label} takes no value`);
|
|
349
|
-
} else {
|
|
350
|
-
setField(negated, "false", label);
|
|
351
|
-
}
|
|
352
|
-
continue;
|
|
353
|
-
}
|
|
354
|
-
const global = findGlobalFlag(name, false);
|
|
355
|
-
if (global) {
|
|
356
|
-
const error = readGlobal(global, label, inline, reader, globals);
|
|
357
|
-
if (error) errors.push(error);
|
|
358
|
-
continue;
|
|
359
|
-
}
|
|
360
|
-
readOther(name, label, inline, reader);
|
|
361
|
-
continue;
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
// Short flags: "-x", "-x=value", "-xvalue" (a flag with a value), or "-abc" (boolean flags)
|
|
365
|
-
const { name: body, inline } = splitInline(token.slice(1));
|
|
366
|
-
for (let k = 0; k < body.length; k++) {
|
|
367
|
-
const letter = body[k]!;
|
|
368
|
-
const label = `-${letter}`;
|
|
369
|
-
const last = k === body.length - 1;
|
|
370
|
-
const attached = last ? inline : body.slice(k + 1);
|
|
371
|
-
const spec = flags.short.get(letter);
|
|
372
|
-
const global = spec ? undefined : findGlobalFlag(letter, true);
|
|
373
|
-
const takesValue = spec ? spec.type.kind !== "boolean" : global?.takesValue === true;
|
|
374
|
-
|
|
375
|
-
if (!spec && !global) {
|
|
376
|
-
if (body.length === 1) {
|
|
377
|
-
readOther(letter, label, inline, reader);
|
|
378
|
-
} else {
|
|
379
|
-
errors.push(`Unknown option ${label} in ${token}`);
|
|
380
|
-
}
|
|
381
|
-
break;
|
|
382
|
-
}
|
|
383
|
-
if (takesValue || last) {
|
|
384
|
-
// The rest of the token (or the next token) is the value of this flag
|
|
385
|
-
if (spec) {
|
|
386
|
-
readProcedureFlag(spec, label, attached === "" ? undefined : attached, reader);
|
|
387
|
-
} else {
|
|
388
|
-
const error = readGlobal(global!, label, attached === "" ? undefined : attached, reader, globals);
|
|
389
|
-
if (error) errors.push(error);
|
|
390
|
-
}
|
|
391
|
-
break;
|
|
392
|
-
}
|
|
393
|
-
if (spec) {
|
|
394
|
-
setField(spec, "true", label);
|
|
395
|
-
} else {
|
|
396
|
-
globals[global!.long as GlobalName] = true;
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
// Positional fields take the arguments in order; a last array field takes the rest
|
|
402
|
-
const positionalFields = flags.meta.args ?? [];
|
|
403
|
-
let next = 0;
|
|
404
|
-
if (procedure) {
|
|
405
|
-
positionalFields.forEach((field, k) => {
|
|
406
|
-
if (next >= positionals.length) {
|
|
407
|
-
return;
|
|
408
|
-
}
|
|
409
|
-
const type = flags.fields.get(field) ?? ANY;
|
|
410
|
-
const many = type.kind === "array" && k === positionalFields.length - 1;
|
|
411
|
-
const texts = many ? positionals.slice(next) : [positionals[next]!];
|
|
412
|
-
next += texts.length;
|
|
413
|
-
if (fromFlag.has(field)) {
|
|
414
|
-
errors.push(`${field} is given twice: as an argument and as --${toKebab(field)}`);
|
|
415
|
-
return;
|
|
416
|
-
}
|
|
417
|
-
let value: unknown;
|
|
418
|
-
// Each argument of a last array field is one item, also when there is only one. Before, one
|
|
419
|
-
// argument was converted as the whole array, so `[]` gave no item and `["a","b"]` two.
|
|
420
|
-
if (many) {
|
|
421
|
-
const items: unknown[] = [];
|
|
422
|
-
for (const text of texts) {
|
|
423
|
-
const converted = convertValue(text, type.item ?? ANY, field);
|
|
424
|
-
if (!converted.ok) {
|
|
425
|
-
errors.push(converted.error);
|
|
426
|
-
return;
|
|
427
|
-
}
|
|
428
|
-
items.push(converted.value);
|
|
429
|
-
}
|
|
430
|
-
value = items;
|
|
431
|
-
} else {
|
|
432
|
-
const converted = convertValue(texts[0]!, type, field);
|
|
433
|
-
if (!converted.ok) {
|
|
434
|
-
errors.push(converted.error);
|
|
435
|
-
return;
|
|
436
|
-
}
|
|
437
|
-
value = converted.value;
|
|
438
|
-
}
|
|
439
|
-
input[field] = value;
|
|
440
|
-
});
|
|
441
|
-
}
|
|
442
|
-
const extraArguments = positionals.slice(next);
|
|
443
|
-
if (procedure && extraArguments.length > 0) {
|
|
444
|
-
errors.push(
|
|
445
|
-
`Unexpected argument${extraArguments.length > 1 ? "s" : ""} for mark ${path.join(" ")}: ${extraArguments.join(" ")}`
|
|
446
|
-
);
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
return { path, procedure, input, globals, extraArguments, errors };
|
|
60
|
+
return kitParseCommandLine(argv, procedures, MARK) as ParsedCommandLine<T>;
|
|
450
61
|
}
|
package/src/ask.ts
CHANGED
|
@@ -6,114 +6,19 @@
|
|
|
6
6
|
* when stdin or stdout is not a terminal, when the environment variable CI is set, with
|
|
7
7
|
* `--no-input`, or when the procedure sets `interactive: false` in its CLI metadata. Then a
|
|
8
8
|
* missing argument is an error of the input, as before.
|
|
9
|
+
*
|
|
10
|
+
* The code is in `@procedures/cli-kit` (roadmap O61).
|
|
9
11
|
*/
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
env: Record<string, string | undefined>;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function processTerminal(): Terminal {
|
|
27
|
-
return {
|
|
28
|
-
stdinIsTTY: process.stdin.isTTY === true,
|
|
29
|
-
stdoutIsTTY: process.stdout.isTTY === true,
|
|
30
|
-
env: process.env,
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** True when mark can ask for a missing argument */
|
|
35
|
-
export function canAsk(noInput: boolean, meta: CLIMeta, terminal: Terminal): boolean {
|
|
36
|
-
if (noInput || meta.interactive === false) {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
const ci = terminal.env["CI"];
|
|
40
|
-
if (ci !== undefined && ci !== "" && ci !== "false" && ci !== "0") {
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
return terminal.stdinIsTTY && terminal.stdoutIsTTY;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** The required fields that the input does not give (a field with a default is not missing) */
|
|
47
|
-
export function missingFields(fields: readonly SchemaFieldInfo[], input: Record<string, unknown>): SchemaFieldInfo[] {
|
|
48
|
-
return fields.filter((field) => field.required && field.defaultValue === undefined && input[field.name] === undefined);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/** The question of a field: its name, its type and its description */
|
|
52
|
-
export function questionOf(field: SchemaFieldInfo): string {
|
|
53
|
-
const many = field.fieldType.kind === "array" ? " (separate the values with spaces)" : "";
|
|
54
|
-
const description = field.description ? `: ${field.description}` : "";
|
|
55
|
-
return `${field.name} (${field.type})${description}${many}`;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** The result of the questions: the input with the answers, or the reason to stop */
|
|
59
|
-
export type Asked =
|
|
60
|
-
| { ok: true; input: Record<string, unknown> }
|
|
61
|
-
| { ok: false; cancelled: boolean; error: string };
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Ask for each missing field, in order, and convert each answer by the type of its field. An
|
|
65
|
-
* array field takes the words of the answer.
|
|
66
|
-
*/
|
|
67
|
-
export async function askMissing(
|
|
68
|
-
missing: readonly SchemaFieldInfo[],
|
|
69
|
-
input: Record<string, unknown>,
|
|
70
|
-
ask: Asker
|
|
71
|
-
): Promise<Asked> {
|
|
72
|
-
const result = { ...input };
|
|
73
|
-
for (const field of missing) {
|
|
74
|
-
const answer = await ask(field, questionOf(field));
|
|
75
|
-
if ("cancelled" in answer) {
|
|
76
|
-
return { ok: false, cancelled: true, error: "Cancelled" };
|
|
77
|
-
}
|
|
78
|
-
if (typeof answer.value !== "string") {
|
|
79
|
-
result[field.name] = answer.value;
|
|
80
|
-
continue;
|
|
81
|
-
}
|
|
82
|
-
const kind = field.fieldType.kind;
|
|
83
|
-
if (kind === "array") {
|
|
84
|
-
const words = answer.value.trim().split(/\s+/).filter((word) => word.length > 0);
|
|
85
|
-
const item = field.fieldType.item ?? { kind: "any" as const };
|
|
86
|
-
const values: unknown[] = [];
|
|
87
|
-
for (const word of words) {
|
|
88
|
-
const converted = convertValue(word, item, field.name);
|
|
89
|
-
if (!converted.ok) return { ok: false, cancelled: false, error: converted.error };
|
|
90
|
-
values.push(converted.value);
|
|
91
|
-
}
|
|
92
|
-
result[field.name] = values;
|
|
93
|
-
continue;
|
|
94
|
-
}
|
|
95
|
-
const converted = convertValue(answer.value, field.fieldType, field.name);
|
|
96
|
-
if (!converted.ok) {
|
|
97
|
-
return { ok: false, cancelled: false, error: converted.error };
|
|
98
|
-
}
|
|
99
|
-
result[field.name] = converted.value;
|
|
100
|
-
}
|
|
101
|
-
return { ok: true, input: result };
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/** The asker of the terminal: a yes/no question for a boolean, a choice for an enum, text otherwise */
|
|
105
|
-
export const terminalAsker: Asker = async (field, message) => {
|
|
106
|
-
const { prompts } = await import("./prompts.js");
|
|
107
|
-
let value: unknown;
|
|
108
|
-
if (field.fieldType.kind === "boolean") {
|
|
109
|
-
value = await prompts.confirm(message);
|
|
110
|
-
} else if (field.fieldType.kind === "enum" && field.enumValues) {
|
|
111
|
-
value = await prompts.select(
|
|
112
|
-
message,
|
|
113
|
-
field.enumValues.map((option) => ({ value: option, label: option }))
|
|
114
|
-
);
|
|
115
|
-
} else {
|
|
116
|
-
value = await prompts.text(message);
|
|
117
|
-
}
|
|
118
|
-
return prompts.isCancel(value) ? { cancelled: true } : { value };
|
|
119
|
-
};
|
|
13
|
+
export {
|
|
14
|
+
askMissing,
|
|
15
|
+
canAsk,
|
|
16
|
+
missingFields,
|
|
17
|
+
processTerminal,
|
|
18
|
+
questionOf,
|
|
19
|
+
terminalAsker,
|
|
20
|
+
type Answer,
|
|
21
|
+
type Asked,
|
|
22
|
+
type Asker,
|
|
23
|
+
type Terminal,
|
|
24
|
+
} from "@procedures/cli-kit";
|
package/src/cli.ts
CHANGED
|
@@ -22,6 +22,7 @@ import { connectCliServer } from "./client-mode.js";
|
|
|
22
22
|
import type { ColdCommand, CommandRun } from "./command.js";
|
|
23
23
|
import { startRepl } from "./repl.js";
|
|
24
24
|
import { findChildren, findProcedure, parseCommandLine, parseLeadingGlobals } from "./args.js";
|
|
25
|
+
import { groupHelp, rootHelp, validationMessage } from "@procedures/cli-kit";
|
|
25
26
|
import { failureOf } from "./failure.js";
|
|
26
27
|
|
|
27
28
|
/** The version of this package. This file is `dist/cli.js` (or `src/cli.ts` in the tests): the package.json is one folder up. */
|
|
@@ -50,19 +51,6 @@ function fail(message: string, hint?: string): void {
|
|
|
50
51
|
process.exitCode = 1;
|
|
51
52
|
}
|
|
52
53
|
|
|
53
|
-
/**
|
|
54
|
-
* The message of a validation error: one line for each issue of a Zod error
|
|
55
|
-
*/
|
|
56
|
-
function validationMessage(error: unknown): string {
|
|
57
|
-
const issues = (error as { issues?: Array<{ path?: unknown[]; message?: string }> }).issues;
|
|
58
|
-
if (Array.isArray(issues) && issues.length > 0) {
|
|
59
|
-
return issues
|
|
60
|
-
.map((issue) => `${(issue.path ?? []).join(".") || "input"}: ${issue.message ?? "invalid"}`)
|
|
61
|
-
.join("\n ");
|
|
62
|
-
}
|
|
63
|
-
return error instanceof Error ? error.message : String(error);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
54
|
/**
|
|
67
55
|
* Show help for a path (either a command or a group). Returns false for an unknown path.
|
|
68
56
|
*/
|
|
@@ -77,33 +65,9 @@ function showHelp(procedures: readonly CommandLike[], path: string[]): boolean {
|
|
|
77
65
|
}
|
|
78
66
|
|
|
79
67
|
// Show help for group
|
|
80
|
-
const
|
|
81
|
-
if (
|
|
82
|
-
const
|
|
83
|
-
print.info(`${groupName} commands:\n`);
|
|
84
|
-
|
|
85
|
-
// Direct children only
|
|
86
|
-
const directChildren = children.filter((p) => p.path.length === path.length + 1);
|
|
87
|
-
for (const child of directChildren) {
|
|
88
|
-
const meta = (child.metadata ?? {}) as CLIMeta;
|
|
89
|
-
const cmdName = child.path[child.path.length - 1] ?? "";
|
|
90
|
-
const desc = meta.description ?? "";
|
|
91
|
-
print.info(` mark ${groupName} ${cmdName} ${desc}`);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// Child groups
|
|
95
|
-
const childGroups = new Set<string>();
|
|
96
|
-
for (const child of children) {
|
|
97
|
-
if (child.path.length > path.length + 1) {
|
|
98
|
-
const groupSeg = child.path[path.length];
|
|
99
|
-
if (groupSeg) childGroups.add(groupSeg);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
for (const group of childGroups) {
|
|
103
|
-
print.info(` mark ${groupName} ${group} ${group} commands`);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
print.info(`\nRun 'mark ${groupName} <command> --help' for more info.`);
|
|
68
|
+
const group = groupHelp("mark", procedures, path);
|
|
69
|
+
if (group) {
|
|
70
|
+
for (const line of group) print.info(line);
|
|
107
71
|
return true;
|
|
108
72
|
}
|
|
109
73
|
|
|
@@ -116,36 +80,18 @@ function showHelp(procedures: readonly CommandLike[], path: string[]): boolean {
|
|
|
116
80
|
* Show root help
|
|
117
81
|
*/
|
|
118
82
|
function showRootHelp(procedures: readonly CommandLike[]): void {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
for (const [group, procs] of groups) {
|
|
133
|
-
print.info(` ${group}`);
|
|
134
|
-
for (const proc of procs) {
|
|
135
|
-
const meta = (proc.metadata ?? {}) as CLIMeta;
|
|
136
|
-
const cmdPath = proc.path.join(" ");
|
|
137
|
-
const desc = meta.description ?? "";
|
|
138
|
-
print.info(` mark ${cmdPath} ${desc}`);
|
|
139
|
-
}
|
|
140
|
-
print.info("");
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
print.info("Run 'mark <command> --help' for more information.");
|
|
144
|
-
print.info("Hosts: 'mark serve [--procedures pkgs] [--port N] [--expose patterns] [--events topics]' (the procedure server),");
|
|
145
|
-
print.info(" 'mark mcp' (the dev-tools MCP server over stdio).");
|
|
146
|
-
print.info("Modes: 'mark --server [--port N] [--host H] [--transport http|websocket|both] [--events topics] [--probe-port N]' and 'mark -i' (the REPL).");
|
|
147
|
-
print.info("Root: 'mark --fs-root <folder> <command>' keeps each path of each procedure in the folder, also in each mode and host.");
|
|
148
|
-
print.info(" It refuses the procedures that it cannot confine: git, pnpm, docker run, a program (FORBIDDEN, outside-root).");
|
|
83
|
+
const lines = rootHelp(procedures, {
|
|
84
|
+
name: "mark",
|
|
85
|
+
title: `mark v${VERSION} - Development workflow automation`,
|
|
86
|
+
footer: [
|
|
87
|
+
"Hosts: 'mark serve [--procedures pkgs] [--port N] [--expose patterns] [--events topics]' (the procedure server),",
|
|
88
|
+
" 'mark mcp' (the dev-tools MCP server over stdio).",
|
|
89
|
+
"Modes: 'mark --server [--port N] [--host H] [--transport http|websocket|both] [--events topics] [--probe-port N]' and 'mark -i' (the REPL).",
|
|
90
|
+
"Root: 'mark --fs-root <folder> <command>' keeps each path of each procedure in the folder, also in each mode and host.",
|
|
91
|
+
" It refuses the procedures that it cannot confine: git, pnpm, docker run, a program (FORBIDDEN, outside-root).",
|
|
92
|
+
],
|
|
93
|
+
});
|
|
94
|
+
for (const line of lines) print.info(line);
|
|
149
95
|
}
|
|
150
96
|
|
|
151
97
|
/** The global flags that only `--server` takes. */
|