-
Notifications
You must be signed in to change notification settings - Fork 20
/
usage.ts
131 lines (117 loc) Β· 4.01 KB
/
usage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import consola from "consola";
import { colors } from "consola/utils";
import { formatLineColumns, resolveValue } from "./_utils";
import type { ArgsDef, CommandDef } from "./types";
import { resolveArgs } from "./args";
export async function showUsage<T extends ArgsDef = ArgsDef>(
cmd: CommandDef<T>,
parent?: CommandDef<T>,
) {
try {
consola.log((await renderUsage(cmd, parent)) + "\n");
} catch (error) {
consola.error(error);
}
}
export async function renderUsage<T extends ArgsDef = ArgsDef>(
cmd: CommandDef<T>,
parent?: CommandDef<T>,
) {
const cmdMeta = await resolveValue(cmd.meta || {});
const cmdArgs = resolveArgs(await resolveValue(cmd.args || {}));
const parentMeta = await resolveValue(parent?.meta || {});
const commandName =
`${parentMeta.name ? `${parentMeta.name} ` : ""}` +
(cmdMeta.name || process.argv[1]);
const argLines: string[][] = [];
const posLines: string[][] = [];
const commandsLines: string[][] = [];
const usageLine = [];
for (const arg of cmdArgs) {
if (arg.type === "positional") {
const name = arg.name.toUpperCase();
const isRequired = arg.required !== false && arg.default === undefined;
// (isRequired ? " (required)" : " (optional)"
const defaultHint = arg.default ? `="${arg.default}"` : "";
posLines.push([
"`" + name + defaultHint + "`",
arg.description || "",
arg.valueHint ? `<${arg.valueHint}>` : "",
]);
usageLine.push(isRequired ? `<${name}>` : `[${name}]`);
} else {
const isRequired = arg.required === true && arg.default === undefined;
const argStr =
(arg.type === "boolean" && arg.default === true
? [
...(arg.alias || []).map((a) => `--no-${a}`),
`--no-${arg.name}`,
].join(", ")
: [...(arg.alias || []).map((a) => `-${a}`), `--${arg.name}`].join(
", ",
)) +
(arg.type === "string" && (arg.valueHint || arg.default)
? `=${
arg.valueHint ? `<${arg.valueHint}>` : `"${arg.default || ""}"`
}`
: "") +
(arg.type === "enum" && arg.options
? `=<${arg.options.join("|")}>`
: "");
argLines.push([
"`" + argStr + (isRequired ? " (required)" : "") + "`",
arg.description || "",
]);
if (isRequired) {
usageLine.push(argStr);
}
}
}
if (cmd.subCommands) {
const commandNames: string[] = [];
const subCommands = await resolveValue(cmd.subCommands);
for (const [name, sub] of Object.entries(subCommands)) {
const subCmd = await resolveValue(sub);
const meta = await resolveValue(subCmd?.meta);
commandsLines.push([`\`${name}\``, meta?.description || ""]);
commandNames.push(name);
}
usageLine.push(commandNames.join("|"));
}
const usageLines: (string | undefined)[] = [];
const version = cmdMeta.version || parentMeta.version;
usageLines.push(
colors.gray(
`${cmdMeta.description} (${
commandName + (version ? ` v${version}` : "")
})`,
),
"",
);
const hasOptions = argLines.length > 0 || posLines.length > 0;
usageLines.push(
`${colors.underline(colors.bold("USAGE"))} \`${commandName}${
hasOptions ? " [OPTIONS]" : ""
} ${usageLine.join(" ")}\``,
"",
);
if (posLines.length > 0) {
usageLines.push(colors.underline(colors.bold("ARGUMENTS")), "");
usageLines.push(formatLineColumns(posLines, " "));
usageLines.push("");
}
if (argLines.length > 0) {
usageLines.push(colors.underline(colors.bold("OPTIONS")), "");
usageLines.push(formatLineColumns(argLines, " "));
usageLines.push("");
}
if (commandsLines.length > 0) {
usageLines.push(colors.underline(colors.bold("COMMANDS")), "");
usageLines.push(formatLineColumns(commandsLines, " "));
usageLines.push(
"",
`Use \`${commandName} <command> --help\` for more information about a command.`,
);
}
return usageLines.filter((l) => typeof l === "string").join("\n");
}