@zinn-dev/cli 0.40.0 → 0.41.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/README.md +166 -38
- package/index.js +24 -13
- package/package.json +7 -2
- package/skills/zinn/SKILL.md +63 -0
- package/src/actor.d.ts +2 -1
- package/src/actor.js +78 -11
- package/src/capabilities-schema.d.ts +163 -0
- package/src/capabilities-schema.js +124 -0
- package/src/capabilities.d.ts +21 -0
- package/src/capabilities.js +22 -0
- package/src/database.d.ts +1 -1
- package/src/database.js +4 -4
- package/src/errors.d.ts +158 -0
- package/src/errors.js +224 -0
- package/src/input.d.ts +116 -0
- package/src/input.js +119 -0
- package/src/lib.d.ts +42 -7
- package/src/lib.js +213 -17
- package/src/presentation/activity-presentation.js +16 -2
- package/src/presentation/column-presentation.d.ts +3 -5
- package/src/presentation/info-presentation.d.ts +4 -0
- package/src/presentation/info-presentation.js +96 -0
- package/src/presentation/task-presentation.d.ts +2 -10
- package/src/project.d.ts +6 -0
- package/src/project.js +23 -10
- package/src/result-schemas.d.ts +987 -0
- package/src/result-schemas.js +220 -0
- package/src/results.d.ts +23 -0
- package/src/results.js +17 -0
- package/src/routes/activity-router.d.ts +71 -0
- package/src/routes/activity-router.js +59 -11
- package/src/routes/backup-router.d.ts +17 -0
- package/src/routes/backup-router.js +15 -3
- package/src/routes/capabilities-router.d.ts +21 -0
- package/src/routes/capabilities-router.js +28 -0
- package/src/routes/comment-router.d.ts +183 -4
- package/src/routes/comment-router.js +136 -29
- package/src/routes/info-router.d.ts +21 -0
- package/src/routes/info-router.js +29 -0
- package/src/routes/label-router.d.ts +165 -0
- package/src/routes/label-router.js +111 -29
- package/src/routes/migrate-router.d.ts +17 -0
- package/src/routes/migrate-router.js +17 -5
- package/src/routes/project-router.d.ts +349 -0
- package/src/routes/project-router.js +278 -92
- package/src/routes/reindex-router.d.ts +17 -0
- package/src/routes/reindex-router.js +15 -3
- package/src/routes/relation-router.d.ts +108 -12
- package/src/routes/relation-router.js +74 -14
- package/src/routes/restore-router.d.ts +29 -0
- package/src/routes/restore-router.js +30 -13
- package/src/routes/router.d.ts +1 -1
- package/src/routes/router.js +44 -25
- package/src/routes/routes.d.ts +1565 -0
- package/src/routes/routes.js +5 -1
- package/src/routes/task-router.d.ts +861 -0
- package/src/routes/task-router.js +419 -163
- package/src/text-edit.d.ts +20 -2
- package/src/text-edit.js +22 -10
- package/src/types.d.ts +14 -0
package/src/errors.js
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { CoreError, SqliteError } from "@zinn-dev/core";
|
|
3
|
+
export const errorCodeSchema = z.enum([
|
|
4
|
+
"invalid_input",
|
|
5
|
+
"not_found",
|
|
6
|
+
"conflict",
|
|
7
|
+
"storage",
|
|
8
|
+
"internal",
|
|
9
|
+
]);
|
|
10
|
+
export const errorDetailsSchema = z.strictObject({
|
|
11
|
+
argument: z.string().optional(),
|
|
12
|
+
field: z.array(z.union([z.string(), z.number()])).optional(),
|
|
13
|
+
entity: z
|
|
14
|
+
.enum([
|
|
15
|
+
"project",
|
|
16
|
+
"task",
|
|
17
|
+
"comment",
|
|
18
|
+
"label",
|
|
19
|
+
"column",
|
|
20
|
+
"relation",
|
|
21
|
+
"activity",
|
|
22
|
+
"database",
|
|
23
|
+
"backup",
|
|
24
|
+
])
|
|
25
|
+
.optional(),
|
|
26
|
+
key: z.string().optional(),
|
|
27
|
+
source: z.enum(["explicit", "defaultProject"]).optional(),
|
|
28
|
+
settingsPath: z.string().optional(),
|
|
29
|
+
reason: z
|
|
30
|
+
.enum([
|
|
31
|
+
"confirmation_required",
|
|
32
|
+
"invalid_json",
|
|
33
|
+
"invalid_settings",
|
|
34
|
+
"invalid_storage",
|
|
35
|
+
"already_exists",
|
|
36
|
+
"archived",
|
|
37
|
+
"parent_exists",
|
|
38
|
+
"cycle",
|
|
39
|
+
"different_project",
|
|
40
|
+
"different_column",
|
|
41
|
+
"ambiguous_reference",
|
|
42
|
+
"text_match_count",
|
|
43
|
+
"no_columns",
|
|
44
|
+
"unmarked_database",
|
|
45
|
+
"newer_database",
|
|
46
|
+
"older_database",
|
|
47
|
+
"invalid_backup",
|
|
48
|
+
"busy",
|
|
49
|
+
"locked",
|
|
50
|
+
"permission_denied",
|
|
51
|
+
"read_only",
|
|
52
|
+
"disk_full",
|
|
53
|
+
"corrupt",
|
|
54
|
+
"not_database",
|
|
55
|
+
"missing_index",
|
|
56
|
+
"io",
|
|
57
|
+
])
|
|
58
|
+
.optional(),
|
|
59
|
+
matchCount: z.number().int().nonnegative().optional(),
|
|
60
|
+
candidateIds: z.array(z.string()).optional(),
|
|
61
|
+
path: z.string().optional(),
|
|
62
|
+
databasePath: z.string().optional(),
|
|
63
|
+
backupDirectory: z.string().optional(),
|
|
64
|
+
backupPath: z.string().optional(),
|
|
65
|
+
schemaVersion: z.number().int().nonnegative().optional(),
|
|
66
|
+
supportedSchemaVersion: z.number().int().nonnegative().optional(),
|
|
67
|
+
cause: z
|
|
68
|
+
.strictObject({
|
|
69
|
+
code: z.string().optional(),
|
|
70
|
+
errcode: z.number().int().optional(),
|
|
71
|
+
message: z.string(),
|
|
72
|
+
})
|
|
73
|
+
.optional(),
|
|
74
|
+
});
|
|
75
|
+
export const errorDocumentSchema = z.strictObject({
|
|
76
|
+
error: z.strictObject({
|
|
77
|
+
code: errorCodeSchema,
|
|
78
|
+
message: z.string(),
|
|
79
|
+
help: z.string().optional(),
|
|
80
|
+
details: errorDetailsSchema.optional(),
|
|
81
|
+
diagnostics: z.array(z.string()).optional(),
|
|
82
|
+
}),
|
|
83
|
+
});
|
|
84
|
+
/** A failure detected by the CLI, with only facts intended for its public output. */
|
|
85
|
+
export class CliError extends Error {
|
|
86
|
+
code;
|
|
87
|
+
details;
|
|
88
|
+
help;
|
|
89
|
+
constructor(message, code = "invalid_input", details, help) {
|
|
90
|
+
super(message);
|
|
91
|
+
this.code = code;
|
|
92
|
+
this.details = details;
|
|
93
|
+
this.help = help;
|
|
94
|
+
this.name = "CliError";
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const coreCodes = {
|
|
98
|
+
input: "invalid_input",
|
|
99
|
+
missing: "not_found",
|
|
100
|
+
conflict: "conflict",
|
|
101
|
+
storage: "storage",
|
|
102
|
+
};
|
|
103
|
+
const osReasons = {
|
|
104
|
+
EACCES: "permission_denied",
|
|
105
|
+
EPERM: "permission_denied",
|
|
106
|
+
EROFS: "read_only",
|
|
107
|
+
ENOSPC: "disk_full",
|
|
108
|
+
EDQUOT: "disk_full",
|
|
109
|
+
EBUSY: "busy",
|
|
110
|
+
EIO: "io",
|
|
111
|
+
ENOENT: "io",
|
|
112
|
+
ENOTDIR: "io",
|
|
113
|
+
EISDIR: "io",
|
|
114
|
+
EMFILE: "io",
|
|
115
|
+
ENFILE: "io",
|
|
116
|
+
EFBIG: "io",
|
|
117
|
+
ENXIO: "io",
|
|
118
|
+
ENODEV: "io",
|
|
119
|
+
EEXIST: "io",
|
|
120
|
+
};
|
|
121
|
+
const sqliteReasons = {
|
|
122
|
+
SQLITE_BUSY: "busy",
|
|
123
|
+
SQLITE_LOCKED: "locked",
|
|
124
|
+
SQLITE_PERM: "permission_denied",
|
|
125
|
+
SQLITE_AUTH: "permission_denied",
|
|
126
|
+
SQLITE_READONLY: "read_only",
|
|
127
|
+
SQLITE_FULL: "disk_full",
|
|
128
|
+
SQLITE_CORRUPT: "corrupt",
|
|
129
|
+
SQLITE_NOTADB: "not_database",
|
|
130
|
+
SQLITE_IOERR: "io",
|
|
131
|
+
SQLITE_CANTOPEN: "io",
|
|
132
|
+
SQLITE_NOLFS: "io",
|
|
133
|
+
};
|
|
134
|
+
/** Selects public facts explicitly
|
|
135
|
+
* Error properties and submitted objects do not go to output
|
|
136
|
+
*/
|
|
137
|
+
function publicDetails(facts) {
|
|
138
|
+
const details = {
|
|
139
|
+
argument: facts.argument,
|
|
140
|
+
field: facts.field,
|
|
141
|
+
entity: facts.entity,
|
|
142
|
+
key: facts.key,
|
|
143
|
+
source: facts.source,
|
|
144
|
+
settingsPath: facts.settingsPath,
|
|
145
|
+
reason: facts.reason,
|
|
146
|
+
matchCount: facts.matchCount,
|
|
147
|
+
candidateIds: facts.candidateIds,
|
|
148
|
+
path: facts.path,
|
|
149
|
+
databasePath: facts.databasePath,
|
|
150
|
+
backupDirectory: facts.backupDirectory,
|
|
151
|
+
backupPath: facts.backupPath,
|
|
152
|
+
schemaVersion: facts.schemaVersion,
|
|
153
|
+
supportedSchemaVersion: facts.supportedSchemaVersion,
|
|
154
|
+
};
|
|
155
|
+
return Object.fromEntries(Object.entries(details).filter(([, value]) => value !== undefined));
|
|
156
|
+
}
|
|
157
|
+
function nativeFailure(error) {
|
|
158
|
+
const code = "code" in error && typeof error.code === "string" ? error.code : undefined;
|
|
159
|
+
if (code === undefined) {
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
const reason = error instanceof SqliteError
|
|
163
|
+
? Object.entries(sqliteReasons).find(([primary]) => code === primary || code.startsWith(`${primary}_`))?.[1]
|
|
164
|
+
: osReasons[code];
|
|
165
|
+
if (reason === undefined) {
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
reason,
|
|
170
|
+
...("path" in error && typeof error.path === "string" ? { path: error.path } : {}),
|
|
171
|
+
cause: {
|
|
172
|
+
code,
|
|
173
|
+
...(error instanceof SqliteError ? { errcode: error.errcode } : {}),
|
|
174
|
+
message: error.message,
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
/** Finds causes of initialization wrappers without serializing their error chains. */
|
|
179
|
+
export function classifyFailure(error) {
|
|
180
|
+
const visited = new Set();
|
|
181
|
+
let current = error;
|
|
182
|
+
let classified;
|
|
183
|
+
while (current instanceof Error && !visited.has(current)) {
|
|
184
|
+
visited.add(current);
|
|
185
|
+
if (current instanceof CliError) {
|
|
186
|
+
return { code: current.code, details: current.details, help: current.help };
|
|
187
|
+
}
|
|
188
|
+
if (current instanceof CoreError && classified === undefined) {
|
|
189
|
+
const details = publicDetails(current.facts);
|
|
190
|
+
if (current.kind === "missing" && current.facts.key !== undefined) {
|
|
191
|
+
details.source = "explicit";
|
|
192
|
+
}
|
|
193
|
+
classified = { code: coreCodes[current.kind], details };
|
|
194
|
+
}
|
|
195
|
+
if (current instanceof z.ZodError && classified === undefined) {
|
|
196
|
+
classified = {
|
|
197
|
+
code: "invalid_input",
|
|
198
|
+
details: {
|
|
199
|
+
field: current.issues[0]?.path.map((part) => typeof part === "symbol" ? String(part) : part),
|
|
200
|
+
},
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
const native = nativeFailure(current);
|
|
204
|
+
if (classified?.code === "storage" && current instanceof SqliteError) {
|
|
205
|
+
classified.details = {
|
|
206
|
+
...native,
|
|
207
|
+
...classified.details,
|
|
208
|
+
cause: { code: current.code, errcode: current.errcode, message: current.message },
|
|
209
|
+
};
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
if (native !== undefined) {
|
|
213
|
+
if (classified === undefined) {
|
|
214
|
+
return { code: "storage", details: native };
|
|
215
|
+
}
|
|
216
|
+
if (classified.code === "storage") {
|
|
217
|
+
classified.details = { ...native, ...classified.details, cause: native.cause };
|
|
218
|
+
}
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
current = current.cause;
|
|
222
|
+
}
|
|
223
|
+
return classified ?? { code: "internal" };
|
|
224
|
+
}
|
package/src/input.d.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { ParseArgsOptionsConfig } from "node:util";
|
|
2
|
+
export type InputValue = {
|
|
3
|
+
kind: string;
|
|
4
|
+
name?: string;
|
|
5
|
+
choices?: readonly string[];
|
|
6
|
+
caseInsensitive?: boolean;
|
|
7
|
+
pattern?: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
};
|
|
10
|
+
export type InputOption = ParseArgsOptionsConfig[string] & {
|
|
11
|
+
required?: boolean;
|
|
12
|
+
value?: InputValue;
|
|
13
|
+
description?: string;
|
|
14
|
+
};
|
|
15
|
+
export type InputOptions = Record<string, InputOption>;
|
|
16
|
+
export type PositionalInput = {
|
|
17
|
+
name: string;
|
|
18
|
+
min: 0 | 1;
|
|
19
|
+
max: 1 | null;
|
|
20
|
+
value: InputValue;
|
|
21
|
+
};
|
|
22
|
+
export type InputForm = {
|
|
23
|
+
id: string;
|
|
24
|
+
positionals: readonly PositionalInput[];
|
|
25
|
+
};
|
|
26
|
+
export type InputReference = `option:${string}` | `positional:${string}`;
|
|
27
|
+
export type InputConstraint = {
|
|
28
|
+
kind: "exclusive";
|
|
29
|
+
members: readonly InputReference[];
|
|
30
|
+
} | {
|
|
31
|
+
kind: "requires";
|
|
32
|
+
input: InputReference;
|
|
33
|
+
requires: InputReference;
|
|
34
|
+
} | {
|
|
35
|
+
kind: "atLeastOne";
|
|
36
|
+
members: readonly InputReference[];
|
|
37
|
+
};
|
|
38
|
+
export type CommandInput = {
|
|
39
|
+
options: InputOptions;
|
|
40
|
+
forms: readonly InputForm[];
|
|
41
|
+
constraints: readonly InputConstraint[];
|
|
42
|
+
notes?: readonly string[];
|
|
43
|
+
};
|
|
44
|
+
export declare const taskKeyPositional: {
|
|
45
|
+
readonly name: "task-key";
|
|
46
|
+
readonly min: 1;
|
|
47
|
+
readonly max: 1;
|
|
48
|
+
readonly value: {
|
|
49
|
+
readonly kind: "task-key";
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
export declare const noInput: {
|
|
53
|
+
readonly options: {};
|
|
54
|
+
readonly forms: readonly [{
|
|
55
|
+
readonly id: "default";
|
|
56
|
+
readonly positionals: readonly [];
|
|
57
|
+
}];
|
|
58
|
+
readonly constraints: readonly [];
|
|
59
|
+
};
|
|
60
|
+
export declare const archiveConstraint: {
|
|
61
|
+
readonly kind: "exclusive";
|
|
62
|
+
readonly members: readonly ["option:archived", "option:all"];
|
|
63
|
+
};
|
|
64
|
+
export declare const routerOptionNames: {
|
|
65
|
+
readonly json: "json";
|
|
66
|
+
readonly legacyRead: "allow-legacy-read";
|
|
67
|
+
};
|
|
68
|
+
export declare const routerOptions: {
|
|
69
|
+
readonly json: {
|
|
70
|
+
readonly type: "boolean";
|
|
71
|
+
};
|
|
72
|
+
readonly "allow-legacy-read": {
|
|
73
|
+
readonly type: "boolean";
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
export declare const helpSpellings: readonly string[];
|
|
77
|
+
export declare const versionSpellings: readonly string[];
|
|
78
|
+
export declare const invocation: {
|
|
79
|
+
readonly routerOptions: {
|
|
80
|
+
readonly json: {
|
|
81
|
+
readonly type: "boolean";
|
|
82
|
+
};
|
|
83
|
+
readonly "allow-legacy-read": {
|
|
84
|
+
readonly type: "boolean";
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
readonly routerOptionPlacement: "exact-tokens-anywhere-before-separator";
|
|
88
|
+
readonly optionSeparator: "--";
|
|
89
|
+
readonly literalOptionValue: "equals";
|
|
90
|
+
readonly singularOptions: "reject-repetition";
|
|
91
|
+
readonly jsonFailure: "one-stderr-document-with-buffered-diagnostics";
|
|
92
|
+
readonly jsonSuccessDiagnostics: "text-on-stderr-at-completion";
|
|
93
|
+
readonly canonicalArgv: readonly ["command-path", "options-with-equals-values", "--json", "separator-if-positionals", "positionals"];
|
|
94
|
+
readonly version: {
|
|
95
|
+
readonly spellings: readonly string[];
|
|
96
|
+
readonly placement: "sole-argument";
|
|
97
|
+
readonly stdout: "text";
|
|
98
|
+
readonly exitCode: 0;
|
|
99
|
+
};
|
|
100
|
+
readonly help: {
|
|
101
|
+
readonly spellings: readonly string[];
|
|
102
|
+
readonly stdout: "text";
|
|
103
|
+
readonly exitCode: 0;
|
|
104
|
+
readonly precedence: "root-first-token-otherwise-router-prefix-before-help";
|
|
105
|
+
readonly scansRouterOptionsFirst: true;
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
/** Formats the same positional facts used by validation */
|
|
109
|
+
export declare function positionalWords(form: InputForm): string[];
|
|
110
|
+
/** Validates structure without tripping on empty strings */
|
|
111
|
+
export declare function violatesConstraint(constraint: InputConstraint, inputs: Partial<Record<InputReference, unknown>>): boolean;
|
|
112
|
+
export declare function getOptionInputs(values: Record<string, unknown>): Partial<Record<InputReference, unknown>>;
|
|
113
|
+
/** Generates syntax without the explanatory help stuff */
|
|
114
|
+
export declare function generateInputUsage(input: CommandInput): string[];
|
|
115
|
+
export declare const usageLabel = "Usage: ";
|
|
116
|
+
export declare function generateCommandUsage(path: string, input: CommandInput): string;
|
package/src/input.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
export const taskKeyPositional = {
|
|
2
|
+
name: "task-key",
|
|
3
|
+
min: 1,
|
|
4
|
+
max: 1,
|
|
5
|
+
value: { kind: "task-key" },
|
|
6
|
+
};
|
|
7
|
+
export const noInput = {
|
|
8
|
+
options: {},
|
|
9
|
+
forms: [{ id: "default", positionals: [] }],
|
|
10
|
+
constraints: [],
|
|
11
|
+
};
|
|
12
|
+
export const archiveConstraint = {
|
|
13
|
+
kind: "exclusive",
|
|
14
|
+
members: ["option:archived", "option:all"],
|
|
15
|
+
};
|
|
16
|
+
export const routerOptionNames = { json: "json", legacyRead: "allow-legacy-read" };
|
|
17
|
+
export const routerOptions = {
|
|
18
|
+
[routerOptionNames.json]: { type: "boolean" },
|
|
19
|
+
[routerOptionNames.legacyRead]: { type: "boolean" },
|
|
20
|
+
};
|
|
21
|
+
export const helpSpellings = ["-h", "--help"];
|
|
22
|
+
export const versionSpellings = ["-v", "--version"];
|
|
23
|
+
export const invocation = {
|
|
24
|
+
routerOptions,
|
|
25
|
+
routerOptionPlacement: "exact-tokens-anywhere-before-separator",
|
|
26
|
+
optionSeparator: "--",
|
|
27
|
+
literalOptionValue: "equals",
|
|
28
|
+
singularOptions: "reject-repetition",
|
|
29
|
+
jsonFailure: "one-stderr-document-with-buffered-diagnostics",
|
|
30
|
+
jsonSuccessDiagnostics: "text-on-stderr-at-completion",
|
|
31
|
+
canonicalArgv: [
|
|
32
|
+
"command-path",
|
|
33
|
+
"options-with-equals-values",
|
|
34
|
+
"--json",
|
|
35
|
+
"separator-if-positionals",
|
|
36
|
+
"positionals",
|
|
37
|
+
],
|
|
38
|
+
version: { spellings: versionSpellings, placement: "sole-argument", stdout: "text", exitCode: 0 },
|
|
39
|
+
help: {
|
|
40
|
+
spellings: helpSpellings,
|
|
41
|
+
stdout: "text",
|
|
42
|
+
exitCode: 0,
|
|
43
|
+
precedence: "root-first-token-otherwise-router-prefix-before-help",
|
|
44
|
+
scansRouterOptionsFirst: true,
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
/** Formats the same positional facts used by validation */
|
|
48
|
+
export function positionalWords(form) {
|
|
49
|
+
return form.positionals.map(({ name, min, max, value }) => {
|
|
50
|
+
const word = value.choices === undefined ? `<${name}>` : `{${value.choices.join("|")}}`;
|
|
51
|
+
const repeatedWord = max === null ? `${word}...` : word;
|
|
52
|
+
return min === 0 ? `[${repeatedWord}]` : repeatedWord;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/** Validates structure without tripping on empty strings */
|
|
56
|
+
export function violatesConstraint(constraint, inputs) {
|
|
57
|
+
const supplied = (reference) => inputs[reference] !== undefined && inputs[reference] !== false;
|
|
58
|
+
switch (constraint.kind) {
|
|
59
|
+
case "exclusive":
|
|
60
|
+
return constraint.members.filter(supplied).length > 1;
|
|
61
|
+
case "requires":
|
|
62
|
+
return supplied(constraint.input) && !supplied(constraint.requires);
|
|
63
|
+
case "atLeastOne":
|
|
64
|
+
return !constraint.members.some(supplied);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export function getOptionInputs(values) {
|
|
68
|
+
return Object.fromEntries(Object.entries(values).map(([name, value]) => [`option:${name}`, value]));
|
|
69
|
+
}
|
|
70
|
+
/** Generates syntax without the explanatory help stuff */
|
|
71
|
+
export function generateInputUsage(input) {
|
|
72
|
+
const options = Object.entries(input.options).map(([name, option]) => {
|
|
73
|
+
const value = option.value;
|
|
74
|
+
const valueWord = `<${value?.name ?? "text"}>`;
|
|
75
|
+
const flag = `--${name}${option.type === "string" ? ` ${valueWord}` : ""}`;
|
|
76
|
+
return {
|
|
77
|
+
reference: `option:${name}`,
|
|
78
|
+
word: `${option.required ? flag : `[${flag}]`}${option.multiple ? "..." : ""}`,
|
|
79
|
+
};
|
|
80
|
+
});
|
|
81
|
+
return input.forms.map((form) => {
|
|
82
|
+
const words = new Map([
|
|
83
|
+
...form.positionals.map((positional, index) => [`positional:${positional.name}`, positionalWords(form)[index]]),
|
|
84
|
+
...options.map(({ reference, word }) => [reference, word]),
|
|
85
|
+
]);
|
|
86
|
+
// Text modifiers share one syntax group with the text they change
|
|
87
|
+
const textRequirement = input.constraints.find((constraint) => constraint.kind === "requires" && constraint.input === "option:append");
|
|
88
|
+
if (textRequirement?.kind === "requires") {
|
|
89
|
+
const textReference = textRequirement.requires;
|
|
90
|
+
const textName = textReference.slice("option:".length);
|
|
91
|
+
const textOption = input.options[textName];
|
|
92
|
+
const text = `--${textName} <${textOption.value?.name ?? "text"}>`;
|
|
93
|
+
const replacing = words.get("option:replacing").slice(1, -1);
|
|
94
|
+
const modifiers = `[--append|${replacing} ${words.get("option:all")}]`;
|
|
95
|
+
words.set(textReference, textOption.required ? `${text} ${modifiers}` : `[${text} ${modifiers}]`);
|
|
96
|
+
for (const name of ["append", "replacing", "all"]) {
|
|
97
|
+
words.delete(`option:${name}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
for (const constraint of input.constraints) {
|
|
101
|
+
const hasEveryMemberWord = constraint.kind === "exclusive" && constraint.members.every((member) => words.has(member));
|
|
102
|
+
if (!hasEveryMemberWord) {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const alternatives = constraint.members.map((member) => words.get(member).slice(1, -1));
|
|
106
|
+
words.set(constraint.members[0], `[${alternatives.join("|")}]`);
|
|
107
|
+
for (const member of constraint.members.slice(1)) {
|
|
108
|
+
words.delete(member);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return [...words.values()].join(" ");
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
export const usageLabel = "Usage: ";
|
|
115
|
+
export function generateCommandUsage(path, input) {
|
|
116
|
+
return generateInputUsage(input)
|
|
117
|
+
.map((syntax, index) => `${index === 0 ? usageLabel : " ".repeat(usageLabel.length)}zinn ${path}${syntax === "" ? "" : ` ${syntax}`}`)
|
|
118
|
+
.join("\n");
|
|
119
|
+
}
|
package/src/lib.d.ts
CHANGED
|
@@ -1,31 +1,66 @@
|
|
|
1
1
|
import type { parseArgs } from "node:util";
|
|
2
|
+
import type { InputForm, InputOptions, InputOption } from "./input.js";
|
|
3
|
+
import { CliError, type ErrorDetails } from "./errors.js";
|
|
2
4
|
type ParsedTokens = NonNullable<ReturnType<typeof parseArgs>["tokens"]>;
|
|
3
5
|
type OutputMode = "text" | "json";
|
|
4
6
|
export declare function setOutputMode(mode: OutputMode): void;
|
|
5
|
-
|
|
6
|
-
export declare function
|
|
7
|
+
/** Selects output before routing, without treating words after `--` as flags. */
|
|
8
|
+
export declare function prepareOutput(args: Array<string>): void;
|
|
9
|
+
/** Keeps JSON failures parseable while text-mode notices remain immediate. */
|
|
10
|
+
export declare function diagnostic(message: string): void;
|
|
11
|
+
export declare function flushDiagnostics(): void;
|
|
12
|
+
export declare function isJsonFlag(flag: string): boolean;
|
|
13
|
+
export declare function isAllowLegacyReadFlag(flag: string): boolean;
|
|
14
|
+
/** A flag the router reads wherever it appears before --, for every command. */
|
|
15
|
+
export declare function isRouterFlag(flag: string): boolean;
|
|
7
16
|
export declare function setLegacyReadAllowed(allowed: boolean): void;
|
|
8
17
|
export declare function isLegacyReadAllowed(): boolean;
|
|
9
18
|
export declare function output<T>(data: T, transformToString: (data: T) => string): void;
|
|
10
19
|
/** Asks before a change that cannot be undone, unless --yes already confirmed it. */
|
|
11
20
|
export declare function confirmAction(message: string, isPreconfirmed: boolean, jsonRefusal?: string): Promise<boolean>;
|
|
21
|
+
export declare const confirmationDescription = "Requires --yes with --json or without a terminal. Otherwise prompts on stderr and only y or Y confirms. Declining exits successfully without the requested mutation; interruption exits 130.";
|
|
12
22
|
/** Like confirmAction, without opening the database, for commands that bypass the open policy. */
|
|
13
23
|
export declare function askConfirmation(message: string, isPreconfirmed: boolean, jsonRefusal?: string): Promise<boolean>;
|
|
14
24
|
/** Whether someone can answer a prompt: stdin and stdout are both terminals, like how gh decides it. */
|
|
15
25
|
export declare function canPrompt(): boolean;
|
|
16
26
|
/** The message to report for a failed command, or fallback when the error has none. */
|
|
17
27
|
export declare function failureMessage(error: any, fallback: string): any;
|
|
18
|
-
/**
|
|
19
|
-
export declare function quit(reason: string, help?: string): never;
|
|
28
|
+
/** Stops a command while retaining its classified error for the outer renderer. */
|
|
29
|
+
export declare function quit(reason: string | Error, help?: string, details?: ErrorDetails): never;
|
|
30
|
+
/** Emits one handled failure, including any notices collected before it. */
|
|
31
|
+
export declare function renderFailure(reason: unknown): void;
|
|
20
32
|
/** A command was given the wrong words. The router prints the message with the command's usage lines. */
|
|
21
|
-
export declare class UsageError extends
|
|
33
|
+
export declare class UsageError extends CliError {
|
|
34
|
+
constructor(message: string, details?: ErrorDetails);
|
|
22
35
|
}
|
|
23
36
|
/**
|
|
24
37
|
* Checks a command's words against its usage line, with the words written the same way:
|
|
25
38
|
* `<task-key>` is required, `[<project-key>]` is optional and `<task-key>...` repeats.
|
|
26
39
|
* Optional words go last.
|
|
27
40
|
*/
|
|
28
|
-
export declare function validatePositionals(positionals: Array<string>,
|
|
29
|
-
export declare function validateUniqueOptions(tokens: ParsedTokens,
|
|
41
|
+
export declare function validatePositionals(positionals: Array<string>, form: InputForm): void;
|
|
42
|
+
export declare function validateUniqueOptions(tokens: ParsedTokens, options: InputOptions): void;
|
|
43
|
+
/** Selects an order form after checking its required leading words. */
|
|
44
|
+
export declare function validateOrderForms<Form extends InputForm>(positionals: string[], forms: readonly Form[]): Form;
|
|
45
|
+
export declare function requireStringOption(option: InputOption & {
|
|
46
|
+
required: true;
|
|
47
|
+
}, value: string | undefined, name: string, message: string): string;
|
|
30
48
|
export declare function isHelpFlag(flag: string): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Help sentence for a flag whose value may begin with a dash, such as a Markdown bullet.
|
|
51
|
+
* `parseArgs` refuses `--flag -value`, since a value that looks like an option may mean the value
|
|
52
|
+
* was forgotten, so we respect that refusal and not take the next word as the value
|
|
53
|
+
* @see {@link https://github.com/pkgjs/parseargs/issues/77}
|
|
54
|
+
*/
|
|
55
|
+
export declare function dashValueHelp(flag: string): string;
|
|
56
|
+
/** Quotes a word for a POSIX shell when it needs it, like Python's `shlex.quote` */
|
|
57
|
+
export declare function shellQuote(word: string): string;
|
|
58
|
+
/** The words of the command a user likely meant by a flag the command does not take, if known. */
|
|
59
|
+
type SuggestFor = (flag: string) => Array<string> | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* The message for an argument error from parseArgs, rewritten where parseArgs' own advice misleads.
|
|
62
|
+
* parseArgs names the flag only in its message text, which Node and Bun word the same.
|
|
63
|
+
* `null` for any other error.
|
|
64
|
+
*/
|
|
65
|
+
export declare function parseArgsMessage(error: any, args: Array<string>, suggestFor: SuggestFor): any;
|
|
31
66
|
export {};
|