@procedures/mark 0.0.0-stage → 0.1.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 +5 -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 +530 -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 +66 -0
- package/dist/ecosystem.d.ts.map +1 -0
- package/dist/ecosystem.js +211 -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 +20885 -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 +596 -0
- package/src/client-mode.ts +90 -0
- package/src/ecosystem.ts +258 -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/src/args.ts
ADDED
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Command Line Splitting
|
|
3
|
+
*
|
|
4
|
+
* Splits the argv of `mark` into the global flags, the command path, and the input of the
|
|
5
|
+
* procedure. The rules (deep dive CLI-7, CLI-9, CLI-10, CLI-11):
|
|
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 negative number is a value, not a flag. `--` ends the flags.
|
|
15
|
+
* - The positional fields take the arguments in order. A last array field takes the rest.
|
|
16
|
+
* An argument that no field takes is an error.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { findGlobalFlag, type CliGlobalFlag } from "@procedures/lib/cli-flags";
|
|
20
|
+
import {
|
|
21
|
+
convertValue,
|
|
22
|
+
fieldsOf,
|
|
23
|
+
mergeValue,
|
|
24
|
+
toCamel,
|
|
25
|
+
toKebab,
|
|
26
|
+
type CLIMeta,
|
|
27
|
+
type CommandLike,
|
|
28
|
+
type FieldType,
|
|
29
|
+
} from "./parse.js";
|
|
30
|
+
|
|
31
|
+
/** The long names of the global flags */
|
|
32
|
+
export type GlobalName =
|
|
33
|
+
| "help"
|
|
34
|
+
| "version"
|
|
35
|
+
| "verbose"
|
|
36
|
+
| "format"
|
|
37
|
+
| "local"
|
|
38
|
+
| "json"
|
|
39
|
+
| "no-input"
|
|
40
|
+
| "interactive"
|
|
41
|
+
| "server"
|
|
42
|
+
| "port"
|
|
43
|
+
| "host"
|
|
44
|
+
| "transport";
|
|
45
|
+
|
|
46
|
+
/** The global flags that the command line gives: true for a flag, text for a flag with a value */
|
|
47
|
+
export type Globals = Partial<Record<GlobalName, string | boolean>>;
|
|
48
|
+
|
|
49
|
+
export interface ParsedCommandLine<T extends CommandLike = CommandLike> {
|
|
50
|
+
/** The command path */
|
|
51
|
+
path: string[];
|
|
52
|
+
/** The procedure at the path, if there is one */
|
|
53
|
+
procedure: T | undefined;
|
|
54
|
+
/** The procedure input, converted by the field types (not validated yet) */
|
|
55
|
+
input: Record<string, unknown>;
|
|
56
|
+
/** The global flags */
|
|
57
|
+
globals: Globals;
|
|
58
|
+
/** The arguments that no positional field took */
|
|
59
|
+
extraArguments: string[];
|
|
60
|
+
/** The problems, one message each */
|
|
61
|
+
errors: string[];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const NEGATIVE_NUMBER = /^-(\d+\.?\d*|\.\d+)([eE][-+]?\d+)?$/;
|
|
65
|
+
const ANY: FieldType = { kind: "any" };
|
|
66
|
+
|
|
67
|
+
/** A token that is a flag (not a value, not a negative number, not "-" alone) */
|
|
68
|
+
function isFlag(token: string): boolean {
|
|
69
|
+
return token.startsWith("-") && token !== "-" && !NEGATIVE_NUMBER.test(token);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Find a procedure matching the given path
|
|
74
|
+
*/
|
|
75
|
+
export function findProcedure<T extends CommandLike>(procedures: readonly T[], path: readonly string[]): T | undefined {
|
|
76
|
+
return procedures.find((p) => p.path.length === path.length && p.path.every((seg, i) => seg === path[i]));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Find procedures that are children of the given path
|
|
81
|
+
*/
|
|
82
|
+
export function findChildren<T extends CommandLike>(procedures: readonly T[], path: readonly string[]): T[] {
|
|
83
|
+
return procedures.filter((p) => p.path.length > path.length && path.every((seg, i) => seg === p.path[i]));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface FlagSpec {
|
|
87
|
+
field: string;
|
|
88
|
+
type: FieldType;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
interface ProcedureFlags {
|
|
92
|
+
long: Map<string, FlagSpec>;
|
|
93
|
+
short: Map<string, FlagSpec>;
|
|
94
|
+
/** The field types by field name */
|
|
95
|
+
fields: Map<string, FieldType>;
|
|
96
|
+
/** True when the schema has readable fields: then an unknown flag is an error */
|
|
97
|
+
typed: boolean;
|
|
98
|
+
meta: CLIMeta;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The flags a procedure declares: each field of its input schema (camelCase and kebab-case
|
|
103
|
+
* names) and the short letters of its metadata.
|
|
104
|
+
*/
|
|
105
|
+
export function procedureFlags(procedure: CommandLike | undefined): ProcedureFlags {
|
|
106
|
+
const meta = (procedure?.metadata ?? {}) as CLIMeta;
|
|
107
|
+
const schemaFields = procedure ? fieldsOf(procedure) : [];
|
|
108
|
+
const fields = new Map(schemaFields.map((field) => [field.name, field.fieldType]));
|
|
109
|
+
const long = new Map<string, FlagSpec>();
|
|
110
|
+
const short = new Map<string, FlagSpec>();
|
|
111
|
+
|
|
112
|
+
const addLong = (field: string): void => {
|
|
113
|
+
const spec = { field, type: fields.get(field) ?? ANY };
|
|
114
|
+
long.set(field, spec);
|
|
115
|
+
long.set(toKebab(field), spec);
|
|
116
|
+
};
|
|
117
|
+
for (const field of fields.keys()) {
|
|
118
|
+
addLong(field);
|
|
119
|
+
}
|
|
120
|
+
for (const [field, letter] of Object.entries(meta.shorts ?? {})) {
|
|
121
|
+
if (!long.has(field)) {
|
|
122
|
+
addLong(field);
|
|
123
|
+
}
|
|
124
|
+
short.set(letter, { field, type: fields.get(field) ?? ANY });
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return { long, short, fields, typed: schemaFields.length > 0, meta };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The state of one pass over the tokens */
|
|
131
|
+
class TokenReader {
|
|
132
|
+
index = 0;
|
|
133
|
+
constructor(readonly tokens: readonly string[]) {}
|
|
134
|
+
|
|
135
|
+
get current(): string | undefined {
|
|
136
|
+
return this.tokens[this.index];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** The value of a flag: the inline value, or the next token when it is not a flag */
|
|
140
|
+
takeValue(inline: string | undefined): string | undefined {
|
|
141
|
+
if (inline !== undefined) {
|
|
142
|
+
return inline;
|
|
143
|
+
}
|
|
144
|
+
const next = this.tokens[this.index + 1];
|
|
145
|
+
if (next !== undefined && !isFlag(next)) {
|
|
146
|
+
this.index++;
|
|
147
|
+
return next;
|
|
148
|
+
}
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** The value of a boolean flag: the inline value, or a next token that is "true" or "false" */
|
|
153
|
+
takeBoolean(inline: string | undefined): string | undefined {
|
|
154
|
+
if (inline !== undefined) {
|
|
155
|
+
return inline;
|
|
156
|
+
}
|
|
157
|
+
const next = this.tokens[this.index + 1];
|
|
158
|
+
if (next === "true" || next === "false") {
|
|
159
|
+
this.index++;
|
|
160
|
+
return next;
|
|
161
|
+
}
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Split "--name=value" (or "-n=value") into the name and the inline value */
|
|
167
|
+
function splitInline(body: string): { name: string; inline: string | undefined } {
|
|
168
|
+
const eq = body.indexOf("=");
|
|
169
|
+
return eq === -1 ? { name: body, inline: undefined } : { name: body.slice(0, eq), inline: body.slice(eq + 1) };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Read one global flag. Returns an error message, or undefined. */
|
|
173
|
+
function readGlobal(
|
|
174
|
+
flag: CliGlobalFlag,
|
|
175
|
+
label: string,
|
|
176
|
+
inline: string | undefined,
|
|
177
|
+
reader: TokenReader,
|
|
178
|
+
globals: Globals
|
|
179
|
+
): string | undefined {
|
|
180
|
+
const name = flag.long as GlobalName;
|
|
181
|
+
if (flag.takesValue) {
|
|
182
|
+
const value = reader.takeValue(inline);
|
|
183
|
+
if (value === undefined) {
|
|
184
|
+
return `${label} needs a value`;
|
|
185
|
+
}
|
|
186
|
+
globals[name] = value;
|
|
187
|
+
return undefined;
|
|
188
|
+
}
|
|
189
|
+
if (inline !== undefined && inline !== "true" && inline !== "false") {
|
|
190
|
+
return `${label} needs true or false, not "${inline}"`;
|
|
191
|
+
}
|
|
192
|
+
globals[name] = inline !== "false";
|
|
193
|
+
return undefined;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Read the global flags before the command path. This needs no procedures: `mark` reads them
|
|
198
|
+
* before it loads the procedures (`--version`, `--server`, `-i`).
|
|
199
|
+
*/
|
|
200
|
+
export function parseLeadingGlobals(argv: readonly string[]): { globals: Globals; rest: string[]; errors: string[] } {
|
|
201
|
+
const globals: Globals = {};
|
|
202
|
+
const errors: string[] = [];
|
|
203
|
+
const reader = new TokenReader(argv);
|
|
204
|
+
|
|
205
|
+
for (; reader.index < argv.length; reader.index++) {
|
|
206
|
+
const token = reader.current!;
|
|
207
|
+
if (!isFlag(token) || token === "--") {
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
const isLong = token.startsWith("--");
|
|
211
|
+
const { name, inline } = splitInline(token.slice(isLong ? 2 : 1));
|
|
212
|
+
const flag = isLong || name.length === 1 ? findGlobalFlag(name, !isLong) : undefined;
|
|
213
|
+
if (!flag) {
|
|
214
|
+
errors.push(`Unknown option before the command: ${token}`);
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
const error = readGlobal(flag, isLong ? `--${name}` : `-${name}`, inline, reader, globals);
|
|
218
|
+
if (error) {
|
|
219
|
+
errors.push(error);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const rest = argv.slice(reader.index);
|
|
224
|
+
if (rest[0] === "--") {
|
|
225
|
+
rest.shift();
|
|
226
|
+
}
|
|
227
|
+
return { globals, rest, errors };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* The command path at the start of the tokens: the longest run of words that names a procedure
|
|
232
|
+
* or a group of procedures. The first word is part of the path even when it names nothing (the
|
|
233
|
+
* command is then unknown).
|
|
234
|
+
*/
|
|
235
|
+
function readPath(tokens: readonly string[], procedures: readonly CommandLike[]): string[] {
|
|
236
|
+
const path: string[] = [];
|
|
237
|
+
for (const token of tokens) {
|
|
238
|
+
if (isFlag(token) || token === "--") {
|
|
239
|
+
break;
|
|
240
|
+
}
|
|
241
|
+
const testPath = [...path, token];
|
|
242
|
+
if (findProcedure(procedures, testPath)) {
|
|
243
|
+
path.push(token);
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
if (findChildren(procedures, testPath).length > 0 || path.length === 0) {
|
|
247
|
+
path.push(token);
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
return path;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Split a whole command line: global flags, the command path, and the procedure input
|
|
257
|
+
*/
|
|
258
|
+
export function parseCommandLine<T extends CommandLike>(argv: readonly string[], procedures: readonly T[]): ParsedCommandLine<T> {
|
|
259
|
+
const leading = parseLeadingGlobals(argv);
|
|
260
|
+
const globals = leading.globals;
|
|
261
|
+
const errors = [...leading.errors];
|
|
262
|
+
const path = readPath(leading.rest, procedures);
|
|
263
|
+
const procedure = findProcedure(procedures, path);
|
|
264
|
+
const flags = procedureFlags(procedure);
|
|
265
|
+
const input: Record<string, unknown> = {};
|
|
266
|
+
const fromFlag = new Set<string>();
|
|
267
|
+
const positionals: string[] = [];
|
|
268
|
+
|
|
269
|
+
const setField = (spec: FlagSpec, text: string, label: string): void => {
|
|
270
|
+
const converted = convertValue(text, spec.type, label);
|
|
271
|
+
if (!converted.ok) {
|
|
272
|
+
errors.push(converted.error);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
input[spec.field] = mergeValue(input[spec.field], converted.value, spec.type);
|
|
276
|
+
fromFlag.add(spec.field);
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
/** Read one procedure flag. Returns false when the procedure does not declare it. */
|
|
280
|
+
const readProcedureFlag = (
|
|
281
|
+
spec: FlagSpec | undefined,
|
|
282
|
+
label: string,
|
|
283
|
+
inline: string | undefined,
|
|
284
|
+
reader: TokenReader
|
|
285
|
+
): boolean => {
|
|
286
|
+
if (!spec) {
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
if (spec.type.kind === "boolean") {
|
|
290
|
+
setField(spec, reader.takeBoolean(inline) ?? "true", label);
|
|
291
|
+
return true;
|
|
292
|
+
}
|
|
293
|
+
const value = reader.takeValue(inline);
|
|
294
|
+
if (value === undefined) {
|
|
295
|
+
errors.push(`${label} needs a value`);
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
setField(spec, value, label);
|
|
299
|
+
return true;
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
/** A flag that neither the procedure nor the globals have */
|
|
303
|
+
const readOther = (name: string, label: string, inline: string | undefined, reader: TokenReader): void => {
|
|
304
|
+
if (procedure && !flags.typed) {
|
|
305
|
+
// The schema has no readable fields: pass the flag through, as JSON or text
|
|
306
|
+
const value = reader.takeValue(inline);
|
|
307
|
+
setField({ field: toCamel(name), type: ANY }, value ?? "true", label);
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
const where = path.length > 0 ? ` for mark ${path.join(" ")}` : "";
|
|
311
|
+
errors.push(`Unknown option${where}: ${label}`);
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
const reader = new TokenReader(leading.rest);
|
|
315
|
+
reader.index = path.length;
|
|
316
|
+
let onlyPositionals = false;
|
|
317
|
+
|
|
318
|
+
for (; reader.index < leading.rest.length; reader.index++) {
|
|
319
|
+
const token = reader.current!;
|
|
320
|
+
|
|
321
|
+
if (onlyPositionals || !isFlag(token)) {
|
|
322
|
+
positionals.push(token);
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
if (token === "--") {
|
|
326
|
+
onlyPositionals = true;
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if (token.startsWith("--")) {
|
|
331
|
+
const { name, inline } = splitInline(token.slice(2));
|
|
332
|
+
const label = `--${name}`;
|
|
333
|
+
if (readProcedureFlag(flags.long.get(name), label, inline, reader)) {
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
// --no-<flag> sets a boolean field to false
|
|
337
|
+
const negated = name.startsWith("no-") ? flags.long.get(name.slice(3)) : undefined;
|
|
338
|
+
if (negated?.type.kind === "boolean") {
|
|
339
|
+
if (inline !== undefined) {
|
|
340
|
+
errors.push(`${label} takes no value`);
|
|
341
|
+
} else {
|
|
342
|
+
setField(negated, "false", label);
|
|
343
|
+
}
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
const global = findGlobalFlag(name, false);
|
|
347
|
+
if (global) {
|
|
348
|
+
const error = readGlobal(global, label, inline, reader, globals);
|
|
349
|
+
if (error) errors.push(error);
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
readOther(name, label, inline, reader);
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// Short flags: "-x", "-x=value", "-xvalue" (a flag with a value), or "-abc" (boolean flags)
|
|
357
|
+
const { name: body, inline } = splitInline(token.slice(1));
|
|
358
|
+
for (let k = 0; k < body.length; k++) {
|
|
359
|
+
const letter = body[k]!;
|
|
360
|
+
const label = `-${letter}`;
|
|
361
|
+
const last = k === body.length - 1;
|
|
362
|
+
const attached = last ? inline : body.slice(k + 1);
|
|
363
|
+
const spec = flags.short.get(letter);
|
|
364
|
+
const global = spec ? undefined : findGlobalFlag(letter, true);
|
|
365
|
+
const takesValue = spec ? spec.type.kind !== "boolean" : global?.takesValue === true;
|
|
366
|
+
|
|
367
|
+
if (!spec && !global) {
|
|
368
|
+
if (body.length === 1) {
|
|
369
|
+
readOther(letter, label, inline, reader);
|
|
370
|
+
} else {
|
|
371
|
+
errors.push(`Unknown option ${label} in ${token}`);
|
|
372
|
+
}
|
|
373
|
+
break;
|
|
374
|
+
}
|
|
375
|
+
if (takesValue || last) {
|
|
376
|
+
// The rest of the token (or the next token) is the value of this flag
|
|
377
|
+
if (spec) {
|
|
378
|
+
readProcedureFlag(spec, label, attached === "" ? undefined : attached, reader);
|
|
379
|
+
} else {
|
|
380
|
+
const error = readGlobal(global!, label, attached === "" ? undefined : attached, reader, globals);
|
|
381
|
+
if (error) errors.push(error);
|
|
382
|
+
}
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
if (spec) {
|
|
386
|
+
setField(spec, "true", label);
|
|
387
|
+
} else {
|
|
388
|
+
globals[global!.long as GlobalName] = true;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// Positional fields take the arguments in order; a last array field takes the rest
|
|
394
|
+
const positionalFields = flags.meta.args ?? [];
|
|
395
|
+
let next = 0;
|
|
396
|
+
if (procedure) {
|
|
397
|
+
positionalFields.forEach((field, k) => {
|
|
398
|
+
if (next >= positionals.length) {
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
const type = flags.fields.get(field) ?? ANY;
|
|
402
|
+
const many = type.kind === "array" && k === positionalFields.length - 1;
|
|
403
|
+
const texts = many ? positionals.slice(next) : [positionals[next]!];
|
|
404
|
+
next += texts.length;
|
|
405
|
+
if (fromFlag.has(field)) {
|
|
406
|
+
errors.push(`${field} is given twice: as an argument and as --${toKebab(field)}`);
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
let value: unknown;
|
|
410
|
+
if (many && texts.length > 1) {
|
|
411
|
+
const items: unknown[] = [];
|
|
412
|
+
for (const text of texts) {
|
|
413
|
+
const converted = convertValue(text, type.item ?? ANY, field);
|
|
414
|
+
if (!converted.ok) {
|
|
415
|
+
errors.push(converted.error);
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
items.push(converted.value);
|
|
419
|
+
}
|
|
420
|
+
value = items;
|
|
421
|
+
} else {
|
|
422
|
+
const converted = convertValue(texts[0]!, type, field);
|
|
423
|
+
if (!converted.ok) {
|
|
424
|
+
errors.push(converted.error);
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
value = converted.value;
|
|
428
|
+
}
|
|
429
|
+
input[field] = value;
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
const extraArguments = positionals.slice(next);
|
|
433
|
+
if (procedure && extraArguments.length > 0) {
|
|
434
|
+
errors.push(
|
|
435
|
+
`Unexpected argument${extraArguments.length > 1 ? "s" : ""} for mark ${path.join(" ")}: ${extraArguments.join(" ")}`
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
return { path, procedure, input, globals, extraArguments, errors };
|
|
440
|
+
}
|
package/src/ask.ts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Questions for missing arguments.
|
|
3
|
+
*
|
|
4
|
+
* When a person runs mark in a terminal and a required argument is missing, mark asks for it:
|
|
5
|
+
* the question shows the name, the type and the description of the field. mark does not ask
|
|
6
|
+
* when stdin or stdout is not a terminal, when the environment variable CI is set, with
|
|
7
|
+
* `--no-input`, or when the procedure sets `interactive: false` in its CLI metadata. Then a
|
|
8
|
+
* missing argument is an error of the input, as before.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { convertValue, type CLIMeta, type SchemaFieldInfo } from "./parse.js";
|
|
12
|
+
|
|
13
|
+
/** The answer to one question: the text of the person, or `cancelled` */
|
|
14
|
+
export type Answer = { value: unknown } | { cancelled: true };
|
|
15
|
+
|
|
16
|
+
/** A function that asks one question. Tests give a fake. */
|
|
17
|
+
export type Asker = (field: SchemaFieldInfo, message: string) => Promise<Answer>;
|
|
18
|
+
|
|
19
|
+
/** The terminal state that decides if mark can ask */
|
|
20
|
+
export interface Terminal {
|
|
21
|
+
stdinIsTTY: boolean;
|
|
22
|
+
stdoutIsTTY: boolean;
|
|
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
|
+
};
|
package/src/bin.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* The `mark` executable.
|
|
4
|
+
*
|
|
5
|
+
* Importing `@procedures/mark` gives the functions and runs nothing (deep dive CLI-17).
|
|
6
|
+
* This file runs the CLI. `node dist/cli.js` also runs it, for the documented commands.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { main } from "./cli.js";
|
|
10
|
+
|
|
11
|
+
await main(process.argv.slice(2));
|