@zinn-dev/cli 0.40.0 → 0.42.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 +11 -561
- package/index.js +24 -13
- package/package.json +8 -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 +162 -0
- package/src/capabilities-schema.js +59 -0
- package/src/capabilities.d.ts +29 -0
- package/src/capabilities.js +30 -0
- package/src/column-hint.d.ts +3 -0
- package/src/column-hint.js +225 -0
- package/src/database.d.ts +1 -1
- package/src/database.js +5 -8
- package/src/errors.d.ts +158 -0
- package/src/errors.js +226 -0
- package/src/input.d.ts +227 -0
- package/src/input.js +192 -0
- package/src/lib.d.ts +71 -8
- package/src/lib.js +275 -18
- package/src/presentation/activity-presentation.d.ts +9 -1
- package/src/presentation/activity-presentation.js +120 -23
- package/src/presentation/capabilities-presentation.d.ts +2 -0
- package/src/presentation/capabilities-presentation.js +3 -0
- package/src/presentation/column-presentation.d.ts +9 -5
- package/src/presentation/column-presentation.js +10 -0
- package/src/presentation/comment-presentation.d.ts +15 -0
- package/src/presentation/comment-presentation.js +29 -0
- package/src/presentation/description-presentation.d.ts +17 -0
- package/src/presentation/description-presentation.js +26 -4
- package/src/presentation/info-presentation.d.ts +4 -0
- package/src/presentation/info-presentation.js +96 -0
- package/src/presentation/label-presentation.d.ts +8 -0
- package/src/presentation/label-presentation.js +28 -0
- package/src/presentation/project-presentation.d.ts +7 -0
- package/src/presentation/project-presentation.js +19 -0
- package/src/presentation/relation-presentation.d.ts +7 -0
- package/src/presentation/relation-presentation.js +73 -0
- package/src/presentation/task-presentation.d.ts +4 -12
- package/src/presentation/task-presentation.js +22 -18
- package/src/project.d.ts +18 -3
- package/src/project.js +37 -12
- package/src/result-schemas.d.ts +1194 -0
- package/src/result-schemas.js +279 -0
- package/src/results.d.ts +40 -0
- package/src/results.js +36 -0
- package/src/routes/activity-router.d.ts +104 -0
- package/src/routes/activity-router.js +155 -34
- package/src/routes/aliases.d.ts +2 -0
- package/src/routes/aliases.js +11 -0
- 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 +34 -0
- package/src/routes/comment-router.d.ts +210 -8
- package/src/routes/comment-router.js +189 -65
- package/src/routes/info-router.d.ts +21 -0
- package/src/routes/info-router.js +29 -0
- package/src/routes/label-router.d.ts +217 -2
- package/src/routes/label-router.js +189 -62
- package/src/routes/migrate-router.d.ts +17 -0
- package/src/routes/migrate-router.js +17 -5
- package/src/routes/project-router.d.ts +373 -2
- package/src/routes/project-router.js +322 -132
- package/src/routes/reindex-router.d.ts +17 -0
- package/src/routes/reindex-router.js +15 -3
- package/src/routes/relation-router.d.ts +110 -14
- package/src/routes/relation-router.js +122 -79
- package/src/routes/restore-router.d.ts +29 -0
- package/src/routes/restore-router.js +31 -14
- package/src/routes/router.d.ts +1 -1
- package/src/routes/router.js +80 -32
- package/src/routes/routes.d.ts +1781 -8
- package/src/routes/routes.js +9 -1
- package/src/routes/task-router.d.ts +946 -4
- package/src/routes/task-router.js +524 -260
- package/src/routes/whoami-router.d.ts +21 -0
- package/src/routes/whoami-router.js +31 -0
- package/src/text-edit.d.ts +28 -6
- package/src/text-edit.js +40 -23
- package/src/text-input.d.ts +47 -0
- package/src/text-input.js +187 -0
- package/src/types.d.ts +37 -0
- package/src/types.js +9 -1
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { column, columnCreateSchema, parseTaskKey } from "@zinn-dev/core";
|
|
3
|
+
import { classifyFailure } from "./errors.js";
|
|
4
|
+
import { routerOptions, getOptionInputs, violatesConstraint } from "./input.js";
|
|
5
|
+
import { formatCommand, isAllowLegacyReadFlag, isRouterFlag, UsageError, validateUniqueOptions, } from "./lib.js";
|
|
6
|
+
import { chooseProjectKey } from "./project.js";
|
|
7
|
+
function isEligible(error, command) {
|
|
8
|
+
const failure = classifyFailure(error);
|
|
9
|
+
const isExtraWord = error instanceof UsageError && failure.details?.field?.length === 0;
|
|
10
|
+
const isMissingProject = failure.code === "not_found" && failure.details?.entity === "project";
|
|
11
|
+
const isUsageErrorFor = (argument) => error instanceof UsageError && failure.details?.argument === argument;
|
|
12
|
+
switch (command) {
|
|
13
|
+
case "project column create":
|
|
14
|
+
case "project column edit":
|
|
15
|
+
return isExtraWord;
|
|
16
|
+
case "project column order":
|
|
17
|
+
return isExtraWord || isUsageErrorFor("direction");
|
|
18
|
+
case "task create":
|
|
19
|
+
case "task list":
|
|
20
|
+
return isExtraWord || isMissingProject || isUsageErrorFor("--all-projects");
|
|
21
|
+
case "task move": {
|
|
22
|
+
const isMissingEntity = failure.code === "not_found" &&
|
|
23
|
+
["column", "task", "project"].includes(failure.details?.entity ?? "");
|
|
24
|
+
const isInvalidTaskKey = failure.code === "invalid_input" && failure.details?.field?.[0] === "taskKey";
|
|
25
|
+
return isMissingEntity || isInvalidTaskKey;
|
|
26
|
+
}
|
|
27
|
+
default:
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function joinWords(words) {
|
|
32
|
+
const first = words[0];
|
|
33
|
+
const isContiguous = first !== undefined && words.every((word, offset) => word.index === first.index + offset);
|
|
34
|
+
if (!isContiguous || words.length < 2) {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
start: first.index,
|
|
39
|
+
end: words.at(-1).index + 1,
|
|
40
|
+
value: words.map((word) => word.value).join(" "),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function hasColumn(context, projectKey, name) {
|
|
44
|
+
const parsedName = columnCreateSchema.shape.name.safeParse(name);
|
|
45
|
+
if (!parsedName.success) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
const project = context.projects.find((entry) => entry.key === projectKey.toUpperCase());
|
|
49
|
+
if (project === undefined) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
return project.columns.some((columnName) => columnName.toLowerCase() === parsedName.data.toLowerCase());
|
|
53
|
+
}
|
|
54
|
+
function applyMerges(args, merges) {
|
|
55
|
+
const corrected = [...args];
|
|
56
|
+
for (const merge of [...merges].sort((left, right) => right.start - left.start)) {
|
|
57
|
+
corrected.splice(merge.start, merge.end - merge.start, merge.value);
|
|
58
|
+
}
|
|
59
|
+
return corrected;
|
|
60
|
+
}
|
|
61
|
+
/** Recognizes only checked split-column mistakes after an invocation has failed. */
|
|
62
|
+
export function quotedColumnHint(error, route, rawArgs, routeWordCount) {
|
|
63
|
+
if (!isEligible(error, route.command)) {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
let argumentStart = 0;
|
|
68
|
+
let wordsSeen = 0;
|
|
69
|
+
for (const [index, word] of rawArgs.entries()) {
|
|
70
|
+
if (!isRouterFlag(word)) {
|
|
71
|
+
wordsSeen++;
|
|
72
|
+
}
|
|
73
|
+
if (wordsSeen === routeWordCount) {
|
|
74
|
+
argumentStart = index + 1;
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const args = rawArgs.slice(argumentStart);
|
|
79
|
+
const separatorIndex = rawArgs.indexOf("--");
|
|
80
|
+
const optionArgs = separatorIndex === -1 ? rawArgs : rawArgs.slice(0, separatorIndex);
|
|
81
|
+
const options = { ...route.input.options, ...routerOptions };
|
|
82
|
+
const { tokens, values } = parseArgs({
|
|
83
|
+
args,
|
|
84
|
+
options,
|
|
85
|
+
allowPositionals: true,
|
|
86
|
+
strict: true,
|
|
87
|
+
tokens: true,
|
|
88
|
+
});
|
|
89
|
+
validateUniqueOptions(tokens, options);
|
|
90
|
+
const optionInputs = getOptionInputs(values);
|
|
91
|
+
const hasMissingOption = Object.entries(route.input.options).some(([name, option]) => option.required && values[name] === undefined);
|
|
92
|
+
const hasInvalidChoice = Object.entries(route.input.options).some(([name, option]) => {
|
|
93
|
+
const value = values[name];
|
|
94
|
+
const comparableValue = typeof value === "string" && option.value?.caseInsensitive ? value.toLowerCase() : value;
|
|
95
|
+
return (typeof comparableValue === "string" &&
|
|
96
|
+
option.value?.choices !== undefined &&
|
|
97
|
+
!option.value.choices.includes(comparableValue));
|
|
98
|
+
});
|
|
99
|
+
const hasConflictingOptions = route.input.constraints.some((constraint) => violatesConstraint(constraint, optionInputs));
|
|
100
|
+
const cannotSuggest = hasMissingOption || hasInvalidChoice || hasConflictingOptions;
|
|
101
|
+
if (cannotSuggest) {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
const words = tokens.flatMap((token) => token.kind === "positional" ? [{ value: token.value, index: token.index }] : []);
|
|
105
|
+
const isMove = route.command === "task move";
|
|
106
|
+
const isColumnOption = route.command === "task create" || route.command === "task list";
|
|
107
|
+
const columnToken = tokens.find((token) => token.kind === "option" && token.name === "column");
|
|
108
|
+
const hasNoWords = words.length === 0;
|
|
109
|
+
const hasTooFewMoveWords = isMove && words.length < 3;
|
|
110
|
+
const hasNoColumnOption = isColumnOption && columnToken === undefined;
|
|
111
|
+
if (hasNoWords || hasTooFewMoveWords || hasNoColumnOption) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
const context = column.inspect({
|
|
115
|
+
taskKeys: isMove ? words.map((word) => word.value) : [],
|
|
116
|
+
allowsLegacyRead: optionArgs.some(isAllowLegacyReadFlag),
|
|
117
|
+
});
|
|
118
|
+
if (context === null) {
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
const candidates = new Set();
|
|
122
|
+
const addCandidate = (merges) => {
|
|
123
|
+
const corrected = [...rawArgs.slice(0, argumentStart), ...applyMerges(args, merges)];
|
|
124
|
+
candidates.add(formatCommand(corrected));
|
|
125
|
+
};
|
|
126
|
+
if (isMove) {
|
|
127
|
+
const targetWords = words.slice(-2);
|
|
128
|
+
const merge = joinWords(targetWords);
|
|
129
|
+
const existingKeys = new Set(context.taskKeys);
|
|
130
|
+
const absorbsTask = targetWords.some((word) => existingKeys.has(word.value.toUpperCase()));
|
|
131
|
+
const taskWords = words.slice(0, -2);
|
|
132
|
+
const validTasks = taskWords.every((word) => existingKeys.has(word.value.toUpperCase()));
|
|
133
|
+
const canSuggestMove = merge !== undefined &&
|
|
134
|
+
!absorbsTask &&
|
|
135
|
+
validTasks &&
|
|
136
|
+
taskWords.every((word) => hasColumn(context, parseTaskKey(word.value).projectKey, merge.value));
|
|
137
|
+
if (canSuggestMove) {
|
|
138
|
+
addCandidate([merge]);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
else if (isColumnOption && typeof columnToken?.value === "string") {
|
|
142
|
+
const columnValue = columnToken.value;
|
|
143
|
+
const valueIndex = columnToken.inlineValue ? columnToken.index : columnToken.index + 1;
|
|
144
|
+
const fragments = words.filter((word) => word.index > valueIndex);
|
|
145
|
+
for (let count = 1; count <= fragments.length; count++) {
|
|
146
|
+
const consumed = fragments.slice(0, count);
|
|
147
|
+
const merge = joinWords([{ value: columnValue, index: valueIndex }, ...consumed]);
|
|
148
|
+
const remaining = words.filter((word) => !consumed.includes(word));
|
|
149
|
+
const discardsProject = consumed.some((word) => context.projects.some((project) => project.key === word.value.toUpperCase()));
|
|
150
|
+
const conflictsWithAllProjects = values["all-projects"] === true && remaining.length > 0;
|
|
151
|
+
if (merge === undefined) {
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
const cannotSuggestSplit = remaining.length > 1 || discardsProject || conflictsWithAllProjects;
|
|
155
|
+
if (cannotSuggestSplit) {
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const choice = chooseProjectKey(remaining[0]?.value, {
|
|
159
|
+
required: route.command === "task create",
|
|
160
|
+
allProjects: values["all-projects"] === true,
|
|
161
|
+
});
|
|
162
|
+
const matches = choice === undefined
|
|
163
|
+
? context.projects.some((project) => hasColumn(context, project.key, merge.value))
|
|
164
|
+
: hasColumn(context, choice.key, merge.value);
|
|
165
|
+
if (matches) {
|
|
166
|
+
addCandidate([
|
|
167
|
+
{ ...merge, value: columnToken.inlineValue ? `--column=${merge.value}` : merge.value },
|
|
168
|
+
]);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
else if (route.command === "project column order") {
|
|
173
|
+
const projectKey = words[0].value;
|
|
174
|
+
const directionPosition = route.input.forms[0].positionals.findIndex((input) => input.name === "direction");
|
|
175
|
+
const originalDirection = words[directionPosition]?.value;
|
|
176
|
+
const hasRecognizedDirection = route.input.forms.some((form) => form.positionals[directionPosition].value.choices.includes(originalDirection ?? ""));
|
|
177
|
+
for (let directionIndex = directionPosition; directionIndex < words.length; directionIndex++) {
|
|
178
|
+
const replacesRecognizedDirection = hasRecognizedDirection && directionIndex !== directionPosition;
|
|
179
|
+
if (replacesRecognizedDirection) {
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
const direction = words[directionIndex].value;
|
|
183
|
+
const form = route.input.forms.find((candidate) => candidate.positionals
|
|
184
|
+
.find((input) => input.name === "direction")
|
|
185
|
+
?.value.choices?.includes(direction));
|
|
186
|
+
if (form === undefined) {
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
const sourceWords = words.slice(1, directionIndex);
|
|
190
|
+
const targetWords = words.slice(directionIndex + 1);
|
|
191
|
+
const needsTarget = form.positionals.some((input) => input.name === "target-column-name");
|
|
192
|
+
const hasTarget = targetWords.length > 0;
|
|
193
|
+
const isUnexpectedTarget = !needsTarget && hasTarget;
|
|
194
|
+
const isMissingTarget = needsTarget && !hasTarget;
|
|
195
|
+
if (isUnexpectedTarget || isMissingTarget) {
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
const source = sourceWords.map((word) => word.value).join(" ");
|
|
199
|
+
const target = targetWords.map((word) => word.value).join(" ");
|
|
200
|
+
const sourceMerge = joinWords(sourceWords);
|
|
201
|
+
const targetMerge = joinWords(targetWords);
|
|
202
|
+
const invalidSource = sourceWords.length > 1 && sourceMerge === undefined;
|
|
203
|
+
const invalidTarget = targetWords.length > 1 && targetMerge === undefined;
|
|
204
|
+
const merges = [sourceMerge, targetMerge].filter((merge) => merge !== undefined);
|
|
205
|
+
const matchesSource = hasColumn(context, projectKey, source);
|
|
206
|
+
const matchesTarget = !needsTarget || hasColumn(context, projectKey, target);
|
|
207
|
+
const canSuggestOrder = !invalidSource && !invalidTarget && merges.length > 0 && matchesSource && matchesTarget;
|
|
208
|
+
if (canSuggestOrder) {
|
|
209
|
+
addCandidate(merges);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
const merge = joinWords(words.slice(1));
|
|
215
|
+
if (merge !== undefined && hasColumn(context, words[0].value, merge.value)) {
|
|
216
|
+
addCandidate([merge]);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return candidates.size === 1 ? [...candidates][0] : undefined;
|
|
220
|
+
}
|
|
221
|
+
catch {
|
|
222
|
+
// A diagnostic lookup must not replace the original failure.
|
|
223
|
+
return undefined;
|
|
224
|
+
}
|
|
225
|
+
}
|
package/src/database.d.ts
CHANGED
|
@@ -3,4 +3,4 @@ import type { DatabaseFacts, LockWait, OpenPolicy } from "@zinn-dev/core";
|
|
|
3
3
|
export declare function reportLockWait({ budgetMs }: LockWait): void;
|
|
4
4
|
export declare function reportLegacyRead({ databasePath }: DatabaseFacts): void;
|
|
5
5
|
/** How the CLI asks core to open the database. */
|
|
6
|
-
export declare function
|
|
6
|
+
export declare function getCliOpenPolicy(): OpenPolicy;
|
package/src/database.js
CHANGED
|
@@ -1,26 +1,23 @@
|
|
|
1
1
|
import packageJson from "../package.json" with { type: "json" };
|
|
2
|
-
import { isLegacyReadAllowed } from "./lib.js";
|
|
2
|
+
import { diagnostic, isLegacyReadAllowed } from "./lib.js";
|
|
3
3
|
import { RELEASE_MODE } from "./release-identity.js";
|
|
4
4
|
const MS_PER_SECOND = 1000;
|
|
5
5
|
/** Says once on stderr that the command waits for another process to finish with the database. */
|
|
6
6
|
export function reportLockWait({ budgetMs }) {
|
|
7
|
-
|
|
7
|
+
diagnostic(`Another process is using the database. Waiting up to ${budgetMs / MS_PER_SECOND} seconds for it to finish.`);
|
|
8
8
|
}
|
|
9
9
|
export function reportLegacyRead({ databasePath }) {
|
|
10
|
-
|
|
10
|
+
diagnostic(`Warning: the database at ${databasePath} has no schema version. Reads are best effort and may fail or omit data.`);
|
|
11
11
|
}
|
|
12
12
|
/** How the CLI asks core to open the database. */
|
|
13
|
-
export function
|
|
13
|
+
export function getCliOpenPolicy() {
|
|
14
14
|
return {
|
|
15
15
|
client: "cli",
|
|
16
16
|
version: packageJson.version,
|
|
17
17
|
// The script the CLI runs from; the runtime itself when there is none.
|
|
18
18
|
executable: process.argv[1] ?? process.execPath,
|
|
19
19
|
release: RELEASE_MODE,
|
|
20
|
-
|
|
21
|
-
get allowsLegacyRead() {
|
|
22
|
-
return isLegacyReadAllowed();
|
|
23
|
-
},
|
|
20
|
+
allowsLegacyRead: isLegacyReadAllowed(),
|
|
24
21
|
onWait: reportLockWait,
|
|
25
22
|
onLegacyRead: reportLegacyRead,
|
|
26
23
|
};
|
package/src/errors.d.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export declare const errorCodeSchema: z.ZodEnum<{
|
|
3
|
+
invalid_input: "invalid_input";
|
|
4
|
+
not_found: "not_found";
|
|
5
|
+
conflict: "conflict";
|
|
6
|
+
storage: "storage";
|
|
7
|
+
internal: "internal";
|
|
8
|
+
}>;
|
|
9
|
+
export declare const errorDetailsSchema: z.ZodObject<{
|
|
10
|
+
argument: z.ZodOptional<z.ZodString>;
|
|
11
|
+
field: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
|
|
12
|
+
entity: z.ZodOptional<z.ZodEnum<{
|
|
13
|
+
project: "project";
|
|
14
|
+
task: "task";
|
|
15
|
+
comment: "comment";
|
|
16
|
+
label: "label";
|
|
17
|
+
column: "column";
|
|
18
|
+
relation: "relation";
|
|
19
|
+
activity: "activity";
|
|
20
|
+
database: "database";
|
|
21
|
+
backup: "backup";
|
|
22
|
+
}>>;
|
|
23
|
+
key: z.ZodOptional<z.ZodString>;
|
|
24
|
+
source: z.ZodOptional<z.ZodEnum<{
|
|
25
|
+
explicit: "explicit";
|
|
26
|
+
defaultProject: "defaultProject";
|
|
27
|
+
}>>;
|
|
28
|
+
settingsPath: z.ZodOptional<z.ZodString>;
|
|
29
|
+
reason: z.ZodOptional<z.ZodEnum<{
|
|
30
|
+
archived: "archived";
|
|
31
|
+
confirmation_required: "confirmation_required";
|
|
32
|
+
invalid_json: "invalid_json";
|
|
33
|
+
invalid_settings: "invalid_settings";
|
|
34
|
+
invalid_storage: "invalid_storage";
|
|
35
|
+
already_exists: "already_exists";
|
|
36
|
+
parent_exists: "parent_exists";
|
|
37
|
+
cycle: "cycle";
|
|
38
|
+
different_project: "different_project";
|
|
39
|
+
different_column: "different_column";
|
|
40
|
+
ambiguous_reference: "ambiguous_reference";
|
|
41
|
+
text_match_count: "text_match_count";
|
|
42
|
+
no_columns: "no_columns";
|
|
43
|
+
unmarked_database: "unmarked_database";
|
|
44
|
+
newer_database: "newer_database";
|
|
45
|
+
older_database: "older_database";
|
|
46
|
+
invalid_backup: "invalid_backup";
|
|
47
|
+
busy: "busy";
|
|
48
|
+
locked: "locked";
|
|
49
|
+
permission_denied: "permission_denied";
|
|
50
|
+
read_only: "read_only";
|
|
51
|
+
disk_full: "disk_full";
|
|
52
|
+
corrupt: "corrupt";
|
|
53
|
+
not_database: "not_database";
|
|
54
|
+
missing_index: "missing_index";
|
|
55
|
+
io: "io";
|
|
56
|
+
}>>;
|
|
57
|
+
matchCount: z.ZodOptional<z.ZodNumber>;
|
|
58
|
+
candidateIds: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
59
|
+
path: z.ZodOptional<z.ZodString>;
|
|
60
|
+
databasePath: z.ZodOptional<z.ZodString>;
|
|
61
|
+
backupDirectory: z.ZodOptional<z.ZodString>;
|
|
62
|
+
backupPath: z.ZodOptional<z.ZodString>;
|
|
63
|
+
schemaVersion: z.ZodOptional<z.ZodNumber>;
|
|
64
|
+
supportedSchemaVersion: z.ZodOptional<z.ZodNumber>;
|
|
65
|
+
cause: z.ZodOptional<z.ZodObject<{
|
|
66
|
+
code: z.ZodOptional<z.ZodString>;
|
|
67
|
+
errcode: z.ZodOptional<z.ZodNumber>;
|
|
68
|
+
message: z.ZodString;
|
|
69
|
+
}, z.core.$strict>>;
|
|
70
|
+
}, z.core.$strict>;
|
|
71
|
+
export declare const errorDocumentSchema: z.ZodObject<{
|
|
72
|
+
error: z.ZodObject<{
|
|
73
|
+
code: z.ZodEnum<{
|
|
74
|
+
invalid_input: "invalid_input";
|
|
75
|
+
not_found: "not_found";
|
|
76
|
+
conflict: "conflict";
|
|
77
|
+
storage: "storage";
|
|
78
|
+
internal: "internal";
|
|
79
|
+
}>;
|
|
80
|
+
message: z.ZodString;
|
|
81
|
+
help: z.ZodOptional<z.ZodString>;
|
|
82
|
+
details: z.ZodOptional<z.ZodObject<{
|
|
83
|
+
argument: z.ZodOptional<z.ZodString>;
|
|
84
|
+
field: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
|
|
85
|
+
entity: z.ZodOptional<z.ZodEnum<{
|
|
86
|
+
project: "project";
|
|
87
|
+
task: "task";
|
|
88
|
+
comment: "comment";
|
|
89
|
+
label: "label";
|
|
90
|
+
column: "column";
|
|
91
|
+
relation: "relation";
|
|
92
|
+
activity: "activity";
|
|
93
|
+
database: "database";
|
|
94
|
+
backup: "backup";
|
|
95
|
+
}>>;
|
|
96
|
+
key: z.ZodOptional<z.ZodString>;
|
|
97
|
+
source: z.ZodOptional<z.ZodEnum<{
|
|
98
|
+
explicit: "explicit";
|
|
99
|
+
defaultProject: "defaultProject";
|
|
100
|
+
}>>;
|
|
101
|
+
settingsPath: z.ZodOptional<z.ZodString>;
|
|
102
|
+
reason: z.ZodOptional<z.ZodEnum<{
|
|
103
|
+
archived: "archived";
|
|
104
|
+
confirmation_required: "confirmation_required";
|
|
105
|
+
invalid_json: "invalid_json";
|
|
106
|
+
invalid_settings: "invalid_settings";
|
|
107
|
+
invalid_storage: "invalid_storage";
|
|
108
|
+
already_exists: "already_exists";
|
|
109
|
+
parent_exists: "parent_exists";
|
|
110
|
+
cycle: "cycle";
|
|
111
|
+
different_project: "different_project";
|
|
112
|
+
different_column: "different_column";
|
|
113
|
+
ambiguous_reference: "ambiguous_reference";
|
|
114
|
+
text_match_count: "text_match_count";
|
|
115
|
+
no_columns: "no_columns";
|
|
116
|
+
unmarked_database: "unmarked_database";
|
|
117
|
+
newer_database: "newer_database";
|
|
118
|
+
older_database: "older_database";
|
|
119
|
+
invalid_backup: "invalid_backup";
|
|
120
|
+
busy: "busy";
|
|
121
|
+
locked: "locked";
|
|
122
|
+
permission_denied: "permission_denied";
|
|
123
|
+
read_only: "read_only";
|
|
124
|
+
disk_full: "disk_full";
|
|
125
|
+
corrupt: "corrupt";
|
|
126
|
+
not_database: "not_database";
|
|
127
|
+
missing_index: "missing_index";
|
|
128
|
+
io: "io";
|
|
129
|
+
}>>;
|
|
130
|
+
matchCount: z.ZodOptional<z.ZodNumber>;
|
|
131
|
+
candidateIds: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
132
|
+
path: z.ZodOptional<z.ZodString>;
|
|
133
|
+
databasePath: z.ZodOptional<z.ZodString>;
|
|
134
|
+
backupDirectory: z.ZodOptional<z.ZodString>;
|
|
135
|
+
backupPath: z.ZodOptional<z.ZodString>;
|
|
136
|
+
schemaVersion: z.ZodOptional<z.ZodNumber>;
|
|
137
|
+
supportedSchemaVersion: z.ZodOptional<z.ZodNumber>;
|
|
138
|
+
cause: z.ZodOptional<z.ZodObject<{
|
|
139
|
+
code: z.ZodOptional<z.ZodString>;
|
|
140
|
+
errcode: z.ZodOptional<z.ZodNumber>;
|
|
141
|
+
message: z.ZodString;
|
|
142
|
+
}, z.core.$strict>>;
|
|
143
|
+
}, z.core.$strict>>;
|
|
144
|
+
diagnostics: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
145
|
+
}, z.core.$strict>;
|
|
146
|
+
}, z.core.$strict>;
|
|
147
|
+
export type ErrorCode = z.infer<typeof errorCodeSchema>;
|
|
148
|
+
export type ErrorDetails = z.infer<typeof errorDetailsSchema>;
|
|
149
|
+
export type CliFailure = z.infer<typeof errorDocumentSchema>["error"];
|
|
150
|
+
/** A failure detected by the CLI, with only facts intended for its public output. */
|
|
151
|
+
export declare class CliError extends Error {
|
|
152
|
+
readonly code: ErrorCode;
|
|
153
|
+
readonly details?: ErrorDetails | undefined;
|
|
154
|
+
help?: string | undefined;
|
|
155
|
+
constructor(message: string, code?: ErrorCode, details?: ErrorDetails | undefined, help?: string | undefined);
|
|
156
|
+
}
|
|
157
|
+
/** Finds causes of initialization wrappers without serializing their error chains. */
|
|
158
|
+
export declare function classifyFailure(error: unknown): Omit<CliFailure, "message">;
|
package/src/errors.js
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
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
|
+
ENAMETOOLONG: "io",
|
|
113
|
+
ELOOP: "io",
|
|
114
|
+
ENOTDIR: "io",
|
|
115
|
+
EISDIR: "io",
|
|
116
|
+
EMFILE: "io",
|
|
117
|
+
ENFILE: "io",
|
|
118
|
+
EFBIG: "io",
|
|
119
|
+
ENXIO: "io",
|
|
120
|
+
ENODEV: "io",
|
|
121
|
+
EEXIST: "io",
|
|
122
|
+
};
|
|
123
|
+
const sqliteReasons = {
|
|
124
|
+
SQLITE_BUSY: "busy",
|
|
125
|
+
SQLITE_LOCKED: "locked",
|
|
126
|
+
SQLITE_PERM: "permission_denied",
|
|
127
|
+
SQLITE_AUTH: "permission_denied",
|
|
128
|
+
SQLITE_READONLY: "read_only",
|
|
129
|
+
SQLITE_FULL: "disk_full",
|
|
130
|
+
SQLITE_CORRUPT: "corrupt",
|
|
131
|
+
SQLITE_NOTADB: "not_database",
|
|
132
|
+
SQLITE_IOERR: "io",
|
|
133
|
+
SQLITE_CANTOPEN: "io",
|
|
134
|
+
SQLITE_NOLFS: "io",
|
|
135
|
+
};
|
|
136
|
+
/** Selects public facts explicitly
|
|
137
|
+
* Error properties and submitted objects do not go to output
|
|
138
|
+
*/
|
|
139
|
+
function publicDetails(facts) {
|
|
140
|
+
const details = {
|
|
141
|
+
argument: facts.argument,
|
|
142
|
+
field: facts.field,
|
|
143
|
+
entity: facts.entity,
|
|
144
|
+
key: facts.key,
|
|
145
|
+
source: facts.source,
|
|
146
|
+
settingsPath: facts.settingsPath,
|
|
147
|
+
reason: facts.reason,
|
|
148
|
+
matchCount: facts.matchCount,
|
|
149
|
+
candidateIds: facts.candidateIds,
|
|
150
|
+
path: facts.path,
|
|
151
|
+
databasePath: facts.databasePath,
|
|
152
|
+
backupDirectory: facts.backupDirectory,
|
|
153
|
+
backupPath: facts.backupPath,
|
|
154
|
+
schemaVersion: facts.schemaVersion,
|
|
155
|
+
supportedSchemaVersion: facts.supportedSchemaVersion,
|
|
156
|
+
};
|
|
157
|
+
return Object.fromEntries(Object.entries(details).filter(([, value]) => value !== undefined));
|
|
158
|
+
}
|
|
159
|
+
function nativeFailure(error) {
|
|
160
|
+
const code = "code" in error && typeof error.code === "string" ? error.code : undefined;
|
|
161
|
+
if (code === undefined) {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
const reason = error instanceof SqliteError
|
|
165
|
+
? Object.entries(sqliteReasons).find(([primary]) => code === primary || code.startsWith(`${primary}_`))?.[1]
|
|
166
|
+
: osReasons[code];
|
|
167
|
+
if (reason === undefined) {
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
reason,
|
|
172
|
+
...("path" in error && typeof error.path === "string" ? { path: error.path } : {}),
|
|
173
|
+
cause: {
|
|
174
|
+
code,
|
|
175
|
+
...(error instanceof SqliteError ? { errcode: error.errcode } : {}),
|
|
176
|
+
message: error.message,
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
/** Finds causes of initialization wrappers without serializing their error chains. */
|
|
181
|
+
export function classifyFailure(error) {
|
|
182
|
+
const visited = new Set();
|
|
183
|
+
let current = error;
|
|
184
|
+
let classified;
|
|
185
|
+
while (current instanceof Error && !visited.has(current)) {
|
|
186
|
+
visited.add(current);
|
|
187
|
+
if (current instanceof CliError) {
|
|
188
|
+
return { code: current.code, details: current.details, help: current.help };
|
|
189
|
+
}
|
|
190
|
+
if (current instanceof CoreError && classified === undefined) {
|
|
191
|
+
const details = publicDetails(current.facts);
|
|
192
|
+
if (current.kind === "missing" && current.facts.key !== undefined) {
|
|
193
|
+
details.source = "explicit";
|
|
194
|
+
}
|
|
195
|
+
classified = { code: coreCodes[current.kind], details };
|
|
196
|
+
}
|
|
197
|
+
if (current instanceof z.ZodError && classified === undefined) {
|
|
198
|
+
classified = {
|
|
199
|
+
code: "invalid_input",
|
|
200
|
+
details: {
|
|
201
|
+
field: current.issues[0]?.path.map((part) => typeof part === "symbol" ? String(part) : part),
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
const native = nativeFailure(current);
|
|
206
|
+
if (classified?.code === "storage" && current instanceof SqliteError) {
|
|
207
|
+
classified.details = {
|
|
208
|
+
...native,
|
|
209
|
+
...classified.details,
|
|
210
|
+
cause: { code: current.code, errcode: current.errcode, message: current.message },
|
|
211
|
+
};
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
if (native !== undefined) {
|
|
215
|
+
if (classified === undefined) {
|
|
216
|
+
return { code: "storage", details: native };
|
|
217
|
+
}
|
|
218
|
+
if (classified.code === "storage") {
|
|
219
|
+
classified.details = { ...native, ...classified.details, cause: native.cause };
|
|
220
|
+
}
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
current = current.cause;
|
|
224
|
+
}
|
|
225
|
+
return classified ?? { code: "internal" };
|
|
226
|
+
}
|