@duyquangnvx/agent-cli 0.1.0 → 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 +12 -0
- package/README.md +11 -0
- package/dist/command-spec.d.ts +28 -0
- package/dist/command-spec.js +31 -0
- package/dist/command-spec.js.map +1 -0
- package/dist/confirm.d.ts +0 -1
- package/dist/confirm.js +4 -4
- package/dist/confirm.js.map +1 -1
- package/dist/define.d.ts +30 -6
- package/dist/define.js +2 -0
- package/dist/define.js.map +1 -1
- package/dist/execute.d.ts +12 -3
- package/dist/execute.js +10 -2
- package/dist/execute.js.map +1 -1
- package/dist/flags.d.ts +44 -6
- package/dist/flags.js +53 -16
- package/dist/flags.js.map +1 -1
- package/dist/global-install.d.ts +2 -0
- package/dist/global-install.js +5 -4
- package/dist/global-install.js.map +1 -1
- package/dist/help-json.d.ts +4 -4
- package/dist/help-json.js +26 -42
- package/dist/help-json.js.map +1 -1
- package/dist/help.d.ts +4 -5
- package/dist/help.js +15 -12
- package/dist/help.js.map +1 -1
- package/dist/index.d.ts +5 -4
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/input.d.ts +8 -8
- package/dist/input.js +9 -16
- package/dist/input.js.map +1 -1
- package/dist/list.d.ts +44 -0
- package/dist/list.js +126 -0
- package/dist/list.js.map +1 -0
- package/dist/output.d.ts +33 -0
- package/dist/output.js +65 -0
- package/dist/output.js.map +1 -0
- package/dist/parse-input.d.ts +18 -4
- package/dist/parse-input.js +82 -49
- package/dist/parse-input.js.map +1 -1
- package/dist/route.d.ts +25 -0
- package/dist/route.js +75 -0
- package/dist/route.js.map +1 -0
- package/dist/run-cli.d.ts +2 -6
- package/dist/run-cli.js +53 -179
- package/dist/run-cli.js.map +1 -1
- package/dist/suggest.d.ts +4 -0
- package/dist/suggest.js +19 -11
- package/dist/suggest.js.map +1 -1
- package/dist/tokenize.d.ts +20 -0
- package/dist/tokenize.js +79 -0
- package/dist/tokenize.js.map +1 -0
- package/dist/usage.d.ts +3 -11
- package/dist/usage.js +20 -55
- package/dist/usage.js.map +1 -1
- package/package.json +1 -4
- package/dist/fields.d.ts +0 -14
- package/dist/fields.js +0 -71
- package/dist/fields.js.map +0 -1
- package/dist/page.d.ts +0 -26
- package/dist/page.js +0 -49
- package/dist/page.js.map +0 -1
package/dist/help.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { isCommand } from "./define.js";
|
|
2
|
-
import { runtimeFlagsFor } from "./flags.js";
|
|
3
1
|
function table(rows) {
|
|
4
2
|
const width = Math.max(...rows.map(([left]) => left.length));
|
|
5
3
|
return rows.map(([left, right]) => right === "" ? ` ${left}` : ` ${left.padEnd(width)} ${right}`);
|
|
@@ -8,22 +6,24 @@ function section(title, rows, style) {
|
|
|
8
6
|
return rows.length === 0 ? [] : ["", style.bold(`${title}:`), ...table(rows)];
|
|
9
7
|
}
|
|
10
8
|
function summaryOf(node) {
|
|
11
|
-
return
|
|
9
|
+
return node.kind === "command"
|
|
10
|
+
? node.description
|
|
11
|
+
: `group: ${Object.keys(node.commands).join(", ")}`;
|
|
12
12
|
}
|
|
13
|
-
function commandRows(commands) {
|
|
13
|
+
function commandRows({ commands }) {
|
|
14
14
|
return Object.entries(commands).map(([key, node]) => [key, summaryOf(node)]);
|
|
15
15
|
}
|
|
16
16
|
function lines(...parts) {
|
|
17
17
|
return `${parts.join("\n")}\n`;
|
|
18
18
|
}
|
|
19
|
-
function listing(heading,
|
|
20
|
-
return lines(style.bold(heading), ...section("Commands", commandRows(
|
|
19
|
+
function listing(heading, group, style) {
|
|
20
|
+
return lines(style.bold(heading), ...section("Commands", commandRows(group), style), "", `Run \`${group.commandLine} <command> --help\` for its examples and input.`);
|
|
21
21
|
}
|
|
22
|
-
export function cliHelp(
|
|
23
|
-
return listing(`${
|
|
22
|
+
export function cliHelp(version, root, style) {
|
|
23
|
+
return listing(`${root.commandLine} ${version}`, root, style);
|
|
24
24
|
}
|
|
25
|
-
export function groupHelp(
|
|
26
|
-
return listing(commandLine,
|
|
25
|
+
export function groupHelp(group, style) {
|
|
26
|
+
return listing(group.commandLine, group, style);
|
|
27
27
|
}
|
|
28
28
|
const EFFECT_NOTES = {
|
|
29
29
|
read: "changes nothing",
|
|
@@ -55,7 +55,10 @@ function argRow(field) {
|
|
|
55
55
|
function optionRow(field) {
|
|
56
56
|
return fieldRow(field, defaultsOf(field));
|
|
57
57
|
}
|
|
58
|
-
|
|
59
|
-
return
|
|
58
|
+
function flagRow({ usage, description }) {
|
|
59
|
+
return [usage, description];
|
|
60
|
+
}
|
|
61
|
+
export function commandHelp(spec, style) {
|
|
62
|
+
return lines(style.bold(spec.commandLine), spec.description, ...section("Examples", spec.examples.map(({ command, note }) => [command, note]), style), ...section("Arguments", spec.args.map(argRow), style), ...section("Options", spec.options.map(optionRow), style), "", `${style.bold("Effect:")} ${spec.effect} (${EFFECT_NOTES[spec.effect]})`, ...section("Standard flags", spec.standardFlags.map(flagRow), style));
|
|
60
63
|
}
|
|
61
64
|
//# sourceMappingURL=help.js.map
|
package/dist/help.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"help.js","sourceRoot":"","sources":["../src/help.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"help.js","sourceRoot":"","sources":["../src/help.ts"],"names":[],"mappings":"AAQA,SAAS,KAAK,CAAC,IAAoB;IACjC,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAC7D,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,EAAE,CAChC,KAAK,KAAK,EAAE,CAAC,CAAC,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,MAAM,C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OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,KAAK,CAAC,EACzD,EAAE,EACF,GAAG,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,IAAI,CAAC,MAAM,KAAK,YAAY,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,EACxE,GAAG,OAAO,CAAC,gBAAgB,EAAE,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,KAAK,CAAC,CACrE,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
export { defineCli } from "./built-ins.js";
|
|
2
|
-
export type { Cli, Command, Commands, Context, DestructiveCommand, DoctorCheck, DoctorFinding, Env, Example, GlobalInstall, ReadCommand, WriteCommand, } from "./define.js";
|
|
2
|
+
export type { Cli, Command, Commands, Context, DestructiveCommand, DoctorCheck, DoctorFinding, Env, Example, GlobalInstall, ListCommand, ReadCommand, WriteCommand, } from "./define.js";
|
|
3
3
|
export { defineCommand } from "./define.js";
|
|
4
4
|
export type { CliErrorDetails, UsageErrorDetails } from "./errors.js";
|
|
5
5
|
export { CliError, debugVariable, UsageError } from "./errors.js";
|
|
6
|
-
export type { Page, PageInput } from "./
|
|
7
|
-
export {
|
|
8
|
-
export type {
|
|
6
|
+
export type { Page, PageInput } from "./list.js";
|
|
7
|
+
export { paginate } from "./list.js";
|
|
8
|
+
export type { RunResult } from "./output.js";
|
|
9
|
+
export type { RunOptions } from "./run-cli.js";
|
|
9
10
|
export { runCli } from "./run-cli.js";
|
|
10
11
|
export { runProcess } from "./run-process.js";
|
|
11
12
|
export type { Style } from "./style.js";
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { defineCli } from "./built-ins.js";
|
|
2
2
|
export { defineCommand } from "./define.js";
|
|
3
3
|
export { CliError, debugVariable, UsageError } from "./errors.js";
|
|
4
|
-
export {
|
|
4
|
+
export { paginate } from "./list.js";
|
|
5
5
|
export { runCli } from "./run-cli.js";
|
|
6
6
|
export { runProcess } from "./run-process.js";
|
|
7
7
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAgB3C,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAElE,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAGrC,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/input.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type {
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import type { InputSchema } from "./define.js";
|
|
3
3
|
export type ValueType = "string" | "number" | "boolean";
|
|
4
4
|
export interface InputField {
|
|
5
5
|
readonly key: string;
|
|
6
|
+
/** The name the caller sees: the key for an arg, the flag without `--` for an option. */
|
|
7
|
+
readonly name: string;
|
|
8
|
+
readonly schema: z.ZodType;
|
|
6
9
|
readonly type: ValueType;
|
|
7
10
|
readonly required: boolean;
|
|
8
11
|
readonly defaultValue: unknown;
|
|
@@ -13,10 +16,7 @@ export interface InputField {
|
|
|
13
16
|
/** How the caller passes it: the label plus a value placeholder such as `<value>` or `<a|b>`. */
|
|
14
17
|
readonly usage: string;
|
|
15
18
|
}
|
|
16
|
-
export interface InputFields {
|
|
17
|
-
readonly args: readonly InputField[];
|
|
18
|
-
readonly options: readonly InputField[];
|
|
19
|
-
}
|
|
20
19
|
export declare function kebab(key: string): string;
|
|
21
|
-
export declare function
|
|
22
|
-
export declare function
|
|
20
|
+
export declare function optionField(key: string, schema: z.ZodType): InputField;
|
|
21
|
+
export declare function argFields(schema: InputSchema | undefined): InputField[];
|
|
22
|
+
export declare function optionFields(schema: InputSchema | undefined): InputField[];
|
package/dist/input.js
CHANGED
|
@@ -38,33 +38,26 @@ function describe(schema) {
|
|
|
38
38
|
}
|
|
39
39
|
function argField(key, schema) {
|
|
40
40
|
const label = `<${key}>`;
|
|
41
|
-
return { key, ...describe(schema), label, usage: label };
|
|
41
|
+
return { key, name: key, schema, ...describe(schema), label, usage: label };
|
|
42
42
|
}
|
|
43
|
-
function optionField(key, schema) {
|
|
43
|
+
export function optionField(key, schema) {
|
|
44
44
|
const described = describe(schema);
|
|
45
|
+
const name = kebab(key);
|
|
45
46
|
const isBoolean = described.type === "boolean";
|
|
46
47
|
// A boolean that defaults to true can only be changed by turning it off.
|
|
47
|
-
const label = isBoolean && described.defaultValue === true ? `--no-${
|
|
48
|
+
const label = isBoolean && described.defaultValue === true ? `--no-${name}` : `--${name}`;
|
|
48
49
|
const usage = isBoolean ? label : `${label} <${described.choices?.join("|") ?? "value"}>`;
|
|
49
|
-
return { key, ...described, label, usage };
|
|
50
|
+
return { key, name, schema, ...described, label, usage };
|
|
50
51
|
}
|
|
51
52
|
function fieldsOf(schema, toField) {
|
|
52
53
|
return schema === undefined
|
|
53
54
|
? []
|
|
54
55
|
: Object.entries(schema.shape).map(([key, value]) => toField(key, value));
|
|
55
56
|
}
|
|
56
|
-
export function
|
|
57
|
-
return
|
|
58
|
-
args: fieldsOf(command.args, argField),
|
|
59
|
-
options: fieldsOf(command.options, optionField),
|
|
60
|
-
};
|
|
57
|
+
export function argFields(schema) {
|
|
58
|
+
return fieldsOf(schema, argField);
|
|
61
59
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
export function registerInput(sub, options) {
|
|
65
|
-
sub.allowUnknownOption().allowExcessArguments();
|
|
66
|
-
for (const field of options) {
|
|
67
|
-
sub.option(field.type === "boolean" ? field.label : `${field.label} [value]`);
|
|
68
|
-
}
|
|
60
|
+
export function optionFields(schema) {
|
|
61
|
+
return fieldsOf(schema, optionField);
|
|
69
62
|
}
|
|
70
63
|
//# sourceMappingURL=input.js.map
|
package/dist/input.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"input.js","sourceRoot":"","sources":["../src/input.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"input.js","sourceRoot":"","sources":["../src/input.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAyBzB,SAAS,SAAS,CAAC,MAAiB;IAClC,OAAO,CACL,MAAM,YAAY,CAAC,CAAC,WAAW;QAC/B,MAAM,YAAY,CAAC,CAAC,WAAW;QAC/B,MAAM,YAAY,CAAC,CAAC,UAAU;QAC9B,MAAM,YAAY,CAAC,CAAC,WAAW,CAChC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,KAAK,CAAC,GAAW;IAC/B,OAAO,GAAG,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,MAAM,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC;AACvE,CAAC;AAED,0EAA0E;AAC1E,SAAS,MAAM,CAAC,MAAiB;IAC/B,IAAI,MAAM,YAAY,CAAC,CAAC,SAAS,EAAE,CAAC;QAClC,OAAO,QAAQ,CAAC;IAClB,CAAC;IACD,OAAO,MAAM,YAAY,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC;AAC/D,CAAC;AAED,SAAS,QAAQ,CAAC,MAAiB;IACjC,IAAI,OAAO,GAAG,MAAM,CAAC;IACrB,IAAI,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;IACtC,IAAI,QAAQ,G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CAAC,KAAK,IAAI,EAAE,CAAC;IAC1F,MAAM,KAAK,GAAG,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,KAAK,SAAS,CAAC,OAAO,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,OAAO,GAAG,CAAC;IAC1F,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,SAAS,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;AAC3D,CAAC;AAED,SAAS,QAAQ,CACf,MAA+B,EAC/B,OAAuD;IAEvD,OAAO,MAAM,KAAK,SAAS;QACzB,CAAC,CAAC,EAAE;QACJ,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC;AAC9E,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,MAA+B;IACvD,OAAO,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;AACpC,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,MAA+B;IAC1D,OAAO,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;AACvC,CAAC"}
|
package/dist/list.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import type { NameOf, StandardFlag } from "./flags.js";
|
|
3
|
+
import { type InputField } from "./input.js";
|
|
4
|
+
import { type Invocation } from "./invocation.js";
|
|
5
|
+
declare const FIELDS_FLAG = "--fields";
|
|
6
|
+
export declare const PAGE_INPUT: z.ZodObject<{
|
|
7
|
+
limit: z.ZodDefault<z.ZodNumber>;
|
|
8
|
+
cursor: z.ZodOptional<z.ZodNumber>;
|
|
9
|
+
}, z.core.$strip>;
|
|
10
|
+
export type PageInput = z.output<typeof PAGE_INPUT>;
|
|
11
|
+
export type ListFlagName = keyof PageInput | NameOf<typeof FIELDS_FLAG>;
|
|
12
|
+
export type RowSchema = z.ZodObject;
|
|
13
|
+
export declare class Page<Item> {
|
|
14
|
+
readonly items: readonly Item[];
|
|
15
|
+
readonly nextCursor: number | undefined;
|
|
16
|
+
constructor(items: readonly Item[], nextCursor: number | undefined);
|
|
17
|
+
}
|
|
18
|
+
export declare function paginate<Item>(items: readonly Item[], { limit, cursor }: PageInput): Page<Item>;
|
|
19
|
+
/** What the Runtime adds to a List Command, derived from its row schema. */
|
|
20
|
+
export interface ListSpec {
|
|
21
|
+
readonly rowFields: readonly string[];
|
|
22
|
+
/** `--limit` and `--cursor`, read and validated as the Command's options are. */
|
|
23
|
+
readonly pageFields: readonly InputField[];
|
|
24
|
+
readonly standardFlags: readonly StandardFlag[];
|
|
25
|
+
/** The printed page, for help JSON. */
|
|
26
|
+
readonly output: z.ZodType;
|
|
27
|
+
}
|
|
28
|
+
/** The Standard flags every List Command gets, as the caller types them. */
|
|
29
|
+
export declare const LIST_FLAGS: readonly string[];
|
|
30
|
+
export declare function listSpec(rows: RowSchema): ListSpec;
|
|
31
|
+
/** The fields the caller asked for, in their order, or undefined when they gave no `--fields`. */
|
|
32
|
+
export declare function selectedFields(values: ReadonlyMap<string, string | true>, rowFields: readonly string[], commandLine: string): readonly string[] | undefined;
|
|
33
|
+
type Row = Readonly<Record<string, unknown>>;
|
|
34
|
+
export declare function projected(rows: readonly unknown[], fields: readonly string[]): Row[];
|
|
35
|
+
/** Selected rows as text: a header line of the field names, then one tab-separated line per row. */
|
|
36
|
+
export declare function renderRows(rows: readonly Row[], fields: readonly string[]): string;
|
|
37
|
+
export interface PrintedPage {
|
|
38
|
+
readonly items: readonly unknown[];
|
|
39
|
+
readonly next?: string;
|
|
40
|
+
}
|
|
41
|
+
export declare function printedPage(page: Page<unknown>, invocation: Invocation): PrintedPage;
|
|
42
|
+
/** The line that ends a rendered page, so the caller always knows whether and how to get more. */
|
|
43
|
+
export declare function pageFooter({ items, next }: PrintedPage): string;
|
|
44
|
+
export {};
|
package/dist/list.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { UsageError } from "./errors.js";
|
|
3
|
+
import { optionField, optionFields } from "./input.js";
|
|
4
|
+
import { commandLineWith } from "./invocation.js";
|
|
5
|
+
const DEFAULT_PAGE_LIMIT = 20;
|
|
6
|
+
const FIELDS_FLAG = "--fields";
|
|
7
|
+
const SEPARATOR = ",";
|
|
8
|
+
const COLUMN_SEPARATOR = "\t";
|
|
9
|
+
const UNSAFE_IN_COLUMN = /[\t\n\r]/;
|
|
10
|
+
export const PAGE_INPUT = z.object({
|
|
11
|
+
limit: z.number().int().positive().default(DEFAULT_PAGE_LIMIT),
|
|
12
|
+
cursor: z.number().int().nonnegative().optional(),
|
|
13
|
+
});
|
|
14
|
+
export class Page {
|
|
15
|
+
items;
|
|
16
|
+
nextCursor;
|
|
17
|
+
constructor(items, nextCursor) {
|
|
18
|
+
this.items = items;
|
|
19
|
+
this.nextCursor = nextCursor;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export function paginate(items, { limit, cursor = 0 }) {
|
|
23
|
+
const end = cursor + limit;
|
|
24
|
+
return new Page(items.slice(cursor, end), end < items.length ? end : undefined);
|
|
25
|
+
}
|
|
26
|
+
const PAGE_FLAG_DESCRIPTIONS = {
|
|
27
|
+
limit: `how many rows to show, a positive whole number (default: ${DEFAULT_PAGE_LIMIT})`,
|
|
28
|
+
cursor: "where to start, as given by the previous page's next",
|
|
29
|
+
};
|
|
30
|
+
function pageFlag(key) {
|
|
31
|
+
const field = optionField(key, PAGE_INPUT.shape[key]);
|
|
32
|
+
return {
|
|
33
|
+
flag: field.label,
|
|
34
|
+
usage: field.usage,
|
|
35
|
+
description: PAGE_FLAG_DESCRIPTIONS[key],
|
|
36
|
+
takes: "optional value",
|
|
37
|
+
schema: field.schema,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function fieldsFlag(rowFields) {
|
|
41
|
+
return {
|
|
42
|
+
flag: FIELDS_FLAG,
|
|
43
|
+
usage: `${FIELDS_FLAG} <a,b>`,
|
|
44
|
+
description: `print only these comma-separated fields of each row (any of: ${rowFields.join(", ")})`,
|
|
45
|
+
takes: "optional value",
|
|
46
|
+
schema: z.string(),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
const PAGE_FIELDS = optionFields(PAGE_INPUT);
|
|
50
|
+
const PAGE_FLAGS = {
|
|
51
|
+
limit: pageFlag("limit"),
|
|
52
|
+
cursor: pageFlag("cursor"),
|
|
53
|
+
};
|
|
54
|
+
/** The Standard flags every List Command gets, as the caller types them. */
|
|
55
|
+
export const LIST_FLAGS = [
|
|
56
|
+
...Object.values(PAGE_FLAGS).map(({ flag }) => flag),
|
|
57
|
+
FIELDS_FLAG,
|
|
58
|
+
];
|
|
59
|
+
export function listSpec(rows) {
|
|
60
|
+
const rowFields = Object.keys(rows.shape);
|
|
61
|
+
return {
|
|
62
|
+
rowFields,
|
|
63
|
+
pageFields: PAGE_FIELDS,
|
|
64
|
+
standardFlags: [...Object.values(PAGE_FLAGS), fieldsFlag(rowFields)],
|
|
65
|
+
output: z.object({ items: z.array(rows), next: z.string().optional() }),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function quoted(names) {
|
|
69
|
+
return names.map((name) => `'${name}'`).join(", ");
|
|
70
|
+
}
|
|
71
|
+
/** The fields the caller asked for, in their order, or undefined when they gave no `--fields`. */
|
|
72
|
+
export function selectedFields(values, rowFields, commandLine) {
|
|
73
|
+
const raw = values.get(FIELDS_FLAG);
|
|
74
|
+
if (raw === undefined) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
const hint = `pass ${FIELDS_FLAG} with any of: ${rowFields.join(", ")}`;
|
|
78
|
+
const names = typeof raw === "string" ? raw.split(SEPARATOR).filter((name) => name !== "") : [];
|
|
79
|
+
if (names.length === 0) {
|
|
80
|
+
throw new UsageError({
|
|
81
|
+
code: "INVALID_INPUT",
|
|
82
|
+
message: `${FIELDS_FLAG}: missing value for \`${commandLine}\``,
|
|
83
|
+
hint,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
const unknown = names.filter((name) => !rowFields.includes(name));
|
|
87
|
+
if (unknown.length > 0) {
|
|
88
|
+
throw new UsageError({
|
|
89
|
+
code: "UNKNOWN_FIELD",
|
|
90
|
+
message: `unknown field ${quoted(unknown)} for \`${commandLine}\``,
|
|
91
|
+
hint,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
return names;
|
|
95
|
+
}
|
|
96
|
+
function fieldOf(row, field) {
|
|
97
|
+
return typeof row === "object" && row !== null ? row[field] : undefined;
|
|
98
|
+
}
|
|
99
|
+
export function projected(rows, fields) {
|
|
100
|
+
return rows.map((row) => Object.fromEntries(fields.map((field) => [field, fieldOf(row, field)])));
|
|
101
|
+
}
|
|
102
|
+
function cell(value) {
|
|
103
|
+
if (value === undefined || value === null) {
|
|
104
|
+
return "";
|
|
105
|
+
}
|
|
106
|
+
return typeof value === "string" && !UNSAFE_IN_COLUMN.test(value) ? value : JSON.stringify(value);
|
|
107
|
+
}
|
|
108
|
+
/** Selected rows as text: a header line of the field names, then one tab-separated line per row. */
|
|
109
|
+
export function renderRows(rows, fields) {
|
|
110
|
+
const lines = [fields, ...rows.map((row) => fields.map((field) => cell(row[field])))];
|
|
111
|
+
return `${lines.map((line) => line.join(COLUMN_SEPARATOR)).join("\n")}\n`;
|
|
112
|
+
}
|
|
113
|
+
export function printedPage(page, invocation) {
|
|
114
|
+
return page.nextCursor === undefined
|
|
115
|
+
? { items: page.items }
|
|
116
|
+
: {
|
|
117
|
+
items: page.items,
|
|
118
|
+
next: commandLineWith(invocation, PAGE_FLAGS.cursor.flag, String(page.nextCursor)),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/** The line that ends a rendered page, so the caller always knows whether and how to get more. */
|
|
122
|
+
export function pageFooter({ items, next }) {
|
|
123
|
+
const more = next === undefined ? "no more pages" : `next page: \`${next}\``;
|
|
124
|
+
return `${items.length} shown; ${more}\n`;
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=list.js.map
|
package/dist/list.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"list.js","sourceRoot":"","sources":["../src/list.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AACzB,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAmB,WAAW,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AACxE,OAAO,EAAE,eAAe,EAAmB,MAAM,iBAAiB,CAAC;AAEnE,MAAM,kBAAkB,GAAG,EAAE,CAAC;AAC9B,MAAM,WAAW,GAAG,UAAU,CAAC;AAC/B,MAAM,SAAS,GAAG,GAAG,CAAC;AACtB,MAAM,gBAAgB,GAAG,IAAI,CAAC;AAC9B,MAAM,gBAAgB,GAAG,UAAU,CAAC;AAEpC,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,CAAC,MAAM,CAAC;IACjC,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,kBAAkB,CAAC;IAC9D,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,WAAW,EAAE,CAAC,QAAQ,EAAE;CAClD,CAAC,CAAC;AAQH,MAAM,OAAO,IAAI;IACN,KAAK,CAAkB;IACvB,UAAU,CAAqB;IAExC,YAAY,KAAsB,EAAE,UAA8B;QAChE,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;CACF;AAED,MAAM,UAAU,QAAQ,CACtB,KAAsB,EACtB,EAAE,KAAK,EAAE,MAAM,GAAG,CAAC,EAAa;IAEhC,MAAM,G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.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { SpecNode } from "./command-spec.js";
|
|
2
|
+
import type { Cli, Env } from "./define.js";
|
|
3
|
+
import type { Outcome } from "./execute.js";
|
|
4
|
+
import type { Invocation } from "./invocation.js";
|
|
5
|
+
import type { Style } from "./style.js";
|
|
6
|
+
export interface RunResult {
|
|
7
|
+
readonly stdout: string;
|
|
8
|
+
readonly stderr: string;
|
|
9
|
+
readonly exitCode: number;
|
|
10
|
+
}
|
|
11
|
+
export interface OutputFormat {
|
|
12
|
+
readonly cliName: string;
|
|
13
|
+
readonly env: Env;
|
|
14
|
+
readonly style: Style;
|
|
15
|
+
readonly invocation: Invocation;
|
|
16
|
+
readonly json: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface DataFormat extends OutputFormat {
|
|
19
|
+
readonly fields: readonly string[] | undefined;
|
|
20
|
+
}
|
|
21
|
+
/** How a Command's run ended; a thrown error carries the Command's render for any data it holds. */
|
|
22
|
+
export type Settled = {
|
|
23
|
+
readonly ok: true;
|
|
24
|
+
readonly outcome: Outcome;
|
|
25
|
+
} | {
|
|
26
|
+
readonly ok: false;
|
|
27
|
+
readonly error: unknown;
|
|
28
|
+
readonly render: (data: unknown, style: Style) => string;
|
|
29
|
+
};
|
|
30
|
+
export declare function errorResult(error: unknown, format: OutputFormat): RunResult;
|
|
31
|
+
export declare function commandResult(settled: Settled, format: DataFormat): RunResult;
|
|
32
|
+
export declare function helpResult(node: SpecNode, root: SpecNode, cli: Cli, { style, json }: OutputFormat): RunResult;
|
|
33
|
+
export declare function versionResult({ version, build }: Cli): RunResult;
|
package/dist/output.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { FailureWithData, formatRuntimeError, UsageError } from "./errors.js";
|
|
2
|
+
import { cliHelp, commandHelp, groupHelp } from "./help.js";
|
|
3
|
+
import { cliHelpJson, commandHelpJson, groupHelpJson } from "./help-json.js";
|
|
4
|
+
import { Page, pageFooter, printedPage, projected, renderRows } from "./list.js";
|
|
5
|
+
const SUCCESS_EXIT_CODE = 0;
|
|
6
|
+
const FAILURE_EXIT_CODE = 1;
|
|
7
|
+
const USAGE_EXIT_CODE = 2;
|
|
8
|
+
function withTrailingNewline(text) {
|
|
9
|
+
return text === "" || text.endsWith("\n") ? text : `${text}\n`;
|
|
10
|
+
}
|
|
11
|
+
function printed(value, render, { json, invocation, style, fields }) {
|
|
12
|
+
if (!(value instanceof Page)) {
|
|
13
|
+
return json ? `${JSON.stringify(value)}\n` : withTrailingNewline(render(style));
|
|
14
|
+
}
|
|
15
|
+
const page = printedPage(value, invocation);
|
|
16
|
+
if (fields === undefined) {
|
|
17
|
+
return json
|
|
18
|
+
? `${JSON.stringify(page)}\n`
|
|
19
|
+
: `${withTrailingNewline(render(style))}${pageFooter(page)}`;
|
|
20
|
+
}
|
|
21
|
+
const rows = projected(page.items, fields);
|
|
22
|
+
// The author's render may read any field, so selected rows are rendered by the Runtime.
|
|
23
|
+
return json
|
|
24
|
+
? `${JSON.stringify({ ...page, items: rows })}\n`
|
|
25
|
+
: `${renderRows(rows, fields)}${pageFooter(page)}`;
|
|
26
|
+
}
|
|
27
|
+
export function errorResult(error, format) {
|
|
28
|
+
return {
|
|
29
|
+
stdout: "",
|
|
30
|
+
stderr: formatRuntimeError(error, format),
|
|
31
|
+
exitCode: error instanceof UsageError ? USAGE_EXIT_CODE : FAILURE_EXIT_CODE,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export function commandResult(settled, format) {
|
|
35
|
+
if (settled.ok) {
|
|
36
|
+
const { value, render } = settled.outcome;
|
|
37
|
+
return { stdout: printed(value, render, format), stderr: "", exitCode: SUCCESS_EXIT_CODE };
|
|
38
|
+
}
|
|
39
|
+
const { error, render } = settled;
|
|
40
|
+
const result = errorResult(error, format);
|
|
41
|
+
if (!(error instanceof FailureWithData)) {
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
const stdout = printed(error.data, (style) => render(error.data, style), format);
|
|
45
|
+
return { ...result, stdout };
|
|
46
|
+
}
|
|
47
|
+
// The root's help names the CLI and its version; a single Command is its CLI's root.
|
|
48
|
+
function helpOf(node, root, cli, style, json) {
|
|
49
|
+
if (json) {
|
|
50
|
+
if (node === root)
|
|
51
|
+
return cliHelpJson(cli.version, node);
|
|
52
|
+
return node.kind === "command" ? commandHelpJson(node) : groupHelpJson(node);
|
|
53
|
+
}
|
|
54
|
+
if (node.kind === "command")
|
|
55
|
+
return commandHelp(node, style);
|
|
56
|
+
return node === root ? cliHelp(cli.version, node, style) : groupHelp(node, style);
|
|
57
|
+
}
|
|
58
|
+
export function helpResult(node, root, cli, { style, json }) {
|
|
59
|
+
return { stdout: helpOf(node, root, cli, style, json), stderr: "", exitCode: SUCCESS_EXIT_CODE };
|
|
60
|
+
}
|
|
61
|
+
export function versionResult({ version, build }) {
|
|
62
|
+
const line = build === undefined ? version : `${version} (${build.sha}${build.dirty ? "+dirty" : ""})`;
|
|
63
|
+
return { stdout: `${line}\n`, stderr: "", exitCode: SUCCESS_EXIT_CODE };
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=output.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"output.js","sourceRoot":"","sources":["../src/output.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9E,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC5D,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAE7E,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AASjF,MAAM,iBAAiB,GAAG,CAAC,CAAC;AAC5B,MAAM,iBAAiB,GAAG,CAAC,CAAC;AAC5B,MAAM,eAAe,GAAG,CAAC,CA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,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;IAC/E,CAAC;IACD,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS;QAAE,OAAO,WAAW,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAC7D,OAAO,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AACpF,CAAC;AAED,MAAM,UAAU,UAAU,CACxB,IAAc,EACd,IAAc,EACd,GAAQ,EACR,EAAE,KAAK,EAAE,IAAI,EAAgB;IAE7B,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,EAAE,iBAAiB,EAAE,CAAC;AACnG,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,EAAE,OAAO,EAAE,KAAK,EAAO;IACnD,MAAM,IAAI,GACR,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,KAAK,KAAK,CAAC,GAAG,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC;IAC5F,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,EAAE,iBAAiB,EAAE,CAAC;AAC1E,CAAC"}
|
package/dist/parse-input.d.ts
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
|
-
import type { Command as Commander } from "commander";
|
|
2
|
-
import type { Command } from "./define.js";
|
|
3
|
-
import type { InputFields } from "./input.js";
|
|
4
1
|
import { type Invocation } from "./invocation.js";
|
|
5
|
-
|
|
2
|
+
import { type PageInput } from "./list.js";
|
|
3
|
+
import type { CommandRoute } from "./route.js";
|
|
4
|
+
/** The Standard flag values of one run of a Command. */
|
|
5
|
+
export interface StandardValues {
|
|
6
|
+
readonly json: boolean;
|
|
7
|
+
readonly dryRun: boolean;
|
|
8
|
+
readonly confirm: string | undefined;
|
|
9
|
+
/** The page a List Command was asked for; the default page for any other Command. */
|
|
10
|
+
readonly page: PageInput;
|
|
11
|
+
/** The row fields to print, or undefined when the caller gave no `--fields`. */
|
|
12
|
+
readonly fields: readonly string[] | undefined;
|
|
13
|
+
}
|
|
14
|
+
export interface ParsedCommand {
|
|
15
|
+
readonly input: Record<string, unknown>;
|
|
16
|
+
readonly flags: StandardValues;
|
|
17
|
+
}
|
|
18
|
+
/** The validated input and Standard flag values of the routed Command, or one usage error. */
|
|
19
|
+
export declare function parseCommand({ spec, words, json }: CommandRoute, invocation: Invocation): ParsedCommand;
|