@zinn-dev/cli 0.39.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 +171 -40
- package/index.d.ts +2 -0
- package/index.js +34 -0
- package/package.json +27 -18
- package/skills/zinn/SKILL.md +63 -0
- package/src/actor.d.ts +7 -0
- package/src/actor.js +129 -0
- 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 +6 -0
- package/src/database.js +27 -0
- 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 +66 -0
- package/src/lib.js +333 -0
- package/src/presentation/activity-presentation.d.ts +23 -0
- package/src/presentation/activity-presentation.js +403 -0
- package/src/presentation/color.d.ts +9 -0
- package/src/presentation/color.js +21 -0
- package/src/presentation/column-presentation.d.ts +14 -0
- package/src/presentation/column-presentation.js +29 -0
- package/src/presentation/description-presentation.d.ts +3 -0
- package/src/presentation/description-presentation.js +11 -0
- package/src/presentation/info-presentation.d.ts +4 -0
- package/src/presentation/info-presentation.js +96 -0
- package/src/presentation/row-layout.d.ts +22 -0
- package/src/presentation/row-layout.js +58 -0
- package/src/presentation/section.d.ts +4 -0
- package/src/presentation/section.js +9 -0
- package/src/presentation/task-presentation.d.ts +9 -0
- package/src/presentation/task-presentation.js +62 -0
- package/src/presentation/time.d.ts +1 -0
- package/src/presentation/time.js +13 -0
- package/src/project.d.ts +12 -0
- package/src/project.js +44 -0
- package/src/release-identity.d.ts +2 -0
- package/src/release-identity.js +1 -0
- 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 +81 -0
- package/src/routes/activity-router.js +119 -0
- package/src/routes/backup-router.d.ts +21 -0
- package/src/routes/backup-router.js +30 -0
- package/src/routes/capabilities-router.d.ts +21 -0
- package/src/routes/capabilities-router.js +28 -0
- package/src/routes/comment-router.d.ts +203 -0
- package/src/routes/comment-router.js +241 -0
- package/src/routes/info-router.d.ts +21 -0
- package/src/routes/info-router.js +29 -0
- package/src/routes/label-router.d.ts +183 -0
- package/src/routes/label-router.js +218 -0
- package/src/routes/migrate-router.d.ts +21 -0
- package/src/routes/migrate-router.js +49 -0
- package/src/routes/project-router.d.ts +389 -0
- package/src/routes/project-router.js +514 -0
- package/src/routes/reindex-router.d.ts +21 -0
- package/src/routes/reindex-router.js +33 -0
- package/src/routes/relation-router.d.ts +124 -0
- package/src/routes/relation-router.js +223 -0
- package/src/routes/restore-router.d.ts +33 -0
- package/src/routes/restore-router.js +88 -0
- package/src/routes/router.d.ts +6 -0
- package/src/routes/router.js +204 -0
- package/src/routes/routes.d.ts +1735 -0
- package/src/routes/routes.js +23 -0
- package/src/routes/task-router.d.ts +945 -0
- package/src/routes/task-router.js +789 -0
- package/src/text-edit.d.ts +43 -0
- package/src/text-edit.js +57 -0
- package/src/types.d.ts +24 -0
- package/src/types.js +1 -0
- package/index.ts +0 -26
- package/release-stamp.json +0 -1
- package/src/actor.test.ts +0 -41
- package/src/actor.ts +0 -78
- package/src/database.test.ts +0 -70
- package/src/database.ts +0 -41
- package/src/lib.test.ts +0 -86
- package/src/lib.ts +0 -159
- package/src/presentation/activity-presentation.test.ts +0 -112
- package/src/presentation/activity-presentation.ts +0 -490
- package/src/presentation/color.ts +0 -29
- package/src/presentation/column-presentation.ts +0 -48
- package/src/presentation/description-presentation.ts +0 -18
- package/src/presentation/row-layout.ts +0 -77
- package/src/presentation/section.ts +0 -16
- package/src/presentation/task-presentation.ts +0 -92
- package/src/presentation/time.ts +0 -17
- package/src/project.test.ts +0 -479
- package/src/project.ts +0 -53
- package/src/routes/activity-router.ts +0 -81
- package/src/routes/backup-router.ts +0 -24
- package/src/routes/comment-router.ts +0 -171
- package/src/routes/label-router.ts +0 -168
- package/src/routes/migrate-router.test.ts +0 -31
- package/src/routes/migrate-router.ts +0 -48
- package/src/routes/project-router.ts +0 -415
- package/src/routes/reindex-router.ts +0 -27
- package/src/routes/relation-router.ts +0 -194
- package/src/routes/restore-router.test.ts +0 -44
- package/src/routes/restore-router.ts +0 -90
- package/src/routes/router.test.ts +0 -136
- package/src/routes/router.ts +0 -248
- package/src/routes/routes.ts +0 -20
- package/src/routes/task-router.ts +0 -649
- package/src/text-edit.ts +0 -68
- package/src/types.ts +0 -6
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { PresentedRelation } from "../result-schemas.js";
|
|
2
|
+
import type { TaskWithKey } from "@zinn-dev/core";
|
|
3
|
+
export declare function presentRelations(taskMatch: TaskWithKey): Array<PresentedRelation>;
|
|
4
|
+
export declare function formatRelations(relations: Array<PresentedRelation>): string;
|
|
5
|
+
export declare const relationRouter: {
|
|
6
|
+
create: {
|
|
7
|
+
summary: string;
|
|
8
|
+
effects: {
|
|
9
|
+
domain: "create";
|
|
10
|
+
storage: "normal-open";
|
|
11
|
+
};
|
|
12
|
+
confirmation: {
|
|
13
|
+
kind: "none";
|
|
14
|
+
};
|
|
15
|
+
input: {
|
|
16
|
+
readonly options: {
|
|
17
|
+
readonly type: {
|
|
18
|
+
readonly type: "string";
|
|
19
|
+
readonly required: true;
|
|
20
|
+
readonly value: {
|
|
21
|
+
readonly kind: "enum";
|
|
22
|
+
readonly name: "type";
|
|
23
|
+
readonly choices: ("related" | "dependency" | "duplicate" | "subtask")[];
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
readonly forms: readonly [{
|
|
28
|
+
readonly id: "default";
|
|
29
|
+
readonly positionals: readonly [{
|
|
30
|
+
readonly name: "source-task-key";
|
|
31
|
+
readonly min: 1;
|
|
32
|
+
readonly max: 1;
|
|
33
|
+
readonly value: {
|
|
34
|
+
readonly kind: "task-key";
|
|
35
|
+
};
|
|
36
|
+
}, {
|
|
37
|
+
readonly name: "target-task-key";
|
|
38
|
+
readonly min: 1;
|
|
39
|
+
readonly max: 1;
|
|
40
|
+
readonly value: {
|
|
41
|
+
readonly kind: "task-key";
|
|
42
|
+
};
|
|
43
|
+
}];
|
|
44
|
+
}];
|
|
45
|
+
readonly constraints: readonly [];
|
|
46
|
+
};
|
|
47
|
+
results: import("../results.js").CommandResult[];
|
|
48
|
+
run: (args: string[]) => void;
|
|
49
|
+
help: string;
|
|
50
|
+
};
|
|
51
|
+
list: {
|
|
52
|
+
summary: string;
|
|
53
|
+
effects: {
|
|
54
|
+
domain: "read";
|
|
55
|
+
storage: "normal-open";
|
|
56
|
+
};
|
|
57
|
+
confirmation: {
|
|
58
|
+
kind: "none";
|
|
59
|
+
};
|
|
60
|
+
input: {
|
|
61
|
+
readonly options: {};
|
|
62
|
+
readonly forms: readonly [{
|
|
63
|
+
readonly id: "default";
|
|
64
|
+
readonly positionals: readonly [{
|
|
65
|
+
readonly name: "task-key";
|
|
66
|
+
readonly min: 1;
|
|
67
|
+
readonly max: 1;
|
|
68
|
+
readonly value: {
|
|
69
|
+
readonly kind: "task-key";
|
|
70
|
+
};
|
|
71
|
+
}];
|
|
72
|
+
}];
|
|
73
|
+
readonly constraints: readonly [];
|
|
74
|
+
};
|
|
75
|
+
results: import("../results.js").CommandResult[];
|
|
76
|
+
run: (args: string[]) => void;
|
|
77
|
+
help: string;
|
|
78
|
+
};
|
|
79
|
+
delete: {
|
|
80
|
+
summary: string;
|
|
81
|
+
effects: {
|
|
82
|
+
domain: "delete";
|
|
83
|
+
storage: "normal-open";
|
|
84
|
+
};
|
|
85
|
+
confirmation: {
|
|
86
|
+
kind: "none";
|
|
87
|
+
};
|
|
88
|
+
input: {
|
|
89
|
+
readonly options: {
|
|
90
|
+
readonly type: {
|
|
91
|
+
readonly type: "string";
|
|
92
|
+
readonly required: true;
|
|
93
|
+
readonly value: {
|
|
94
|
+
readonly kind: "enum";
|
|
95
|
+
readonly name: "type";
|
|
96
|
+
readonly choices: ("related" | "dependency" | "duplicate" | "subtask")[];
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
readonly forms: readonly [{
|
|
101
|
+
readonly id: "default";
|
|
102
|
+
readonly positionals: readonly [{
|
|
103
|
+
readonly name: "source-task-key";
|
|
104
|
+
readonly min: 1;
|
|
105
|
+
readonly max: 1;
|
|
106
|
+
readonly value: {
|
|
107
|
+
readonly kind: "task-key";
|
|
108
|
+
};
|
|
109
|
+
}, {
|
|
110
|
+
readonly name: "target-task-key";
|
|
111
|
+
readonly min: 1;
|
|
112
|
+
readonly max: 1;
|
|
113
|
+
readonly value: {
|
|
114
|
+
readonly kind: "task-key";
|
|
115
|
+
};
|
|
116
|
+
}];
|
|
117
|
+
}];
|
|
118
|
+
readonly constraints: readonly [];
|
|
119
|
+
};
|
|
120
|
+
results: import("../results.js").CommandResult[];
|
|
121
|
+
run: (args: string[]) => void;
|
|
122
|
+
help: string;
|
|
123
|
+
};
|
|
124
|
+
};
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { jsonResult } from "../results.js";
|
|
2
|
+
import { parseArgs } from "node:util";
|
|
3
|
+
import { column, relation, relationTypeSchema, task } from "@zinn-dev/core";
|
|
4
|
+
import { generateCommandUsage, taskKeyPositional } from "../input.js";
|
|
5
|
+
import { output, quit, requireStringOption, validatePositionals, validateUniqueOptions, } from "../lib.js";
|
|
6
|
+
import { currentActor } from "../actor.js";
|
|
7
|
+
import { formatCellSeparator, formatRows } from "../presentation/row-layout.js";
|
|
8
|
+
import { columnNameCell } from "../presentation/column-presentation.js";
|
|
9
|
+
export function presentRelations(taskMatch) {
|
|
10
|
+
return relation.getAll(taskMatch.key, { include: { tasks: true } }).map((entry) => {
|
|
11
|
+
const isSource = entry.source_task_id === taskMatch.id;
|
|
12
|
+
const other = isSource ? entry.target : entry.source;
|
|
13
|
+
if (other == null) {
|
|
14
|
+
throw new Error("Connected task details were not included in the relation");
|
|
15
|
+
}
|
|
16
|
+
let direction = null;
|
|
17
|
+
if (entry.relation_type !== "related") {
|
|
18
|
+
direction = isSource ? "outgoing" : "incoming";
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
type: entry.relation_type,
|
|
22
|
+
direction,
|
|
23
|
+
task: {
|
|
24
|
+
key: other.key,
|
|
25
|
+
column: column.getById(other.column_id).name,
|
|
26
|
+
title: other.title,
|
|
27
|
+
status: other.archived_at == null ? "Active" : "Archived",
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
export function formatRelations(relations) {
|
|
33
|
+
return formatRows(relations.map((entry) => {
|
|
34
|
+
const isOutgoing = entry.direction === "outgoing";
|
|
35
|
+
let label = entry.type;
|
|
36
|
+
switch (entry.type) {
|
|
37
|
+
case "related": {
|
|
38
|
+
label = "Related to";
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
case "dependency": {
|
|
42
|
+
label = isOutgoing ? "Depends on" : "Depended on by";
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
case "subtask": {
|
|
46
|
+
label = isOutgoing ? "Parent" : "Subtask";
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
case "duplicate": {
|
|
50
|
+
label = isOutgoing ? "Duplicate of" : "Duplicated by";
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const archived = entry.task.status === "Archived" ? `${formatCellSeparator()}Archived` : "";
|
|
55
|
+
return [
|
|
56
|
+
{ text: label },
|
|
57
|
+
{ text: entry.task.key },
|
|
58
|
+
columnNameCell(entry.task.column),
|
|
59
|
+
{ text: `${entry.task.title}${archived}` },
|
|
60
|
+
];
|
|
61
|
+
}));
|
|
62
|
+
}
|
|
63
|
+
function parseRelationArgs(args) {
|
|
64
|
+
const { values, positionals, tokens } = parseArgs({
|
|
65
|
+
args,
|
|
66
|
+
options: relationInputs.create.options,
|
|
67
|
+
allowPositionals: true,
|
|
68
|
+
strict: true,
|
|
69
|
+
tokens: true,
|
|
70
|
+
});
|
|
71
|
+
validateUniqueOptions(tokens, relationInputs.create.options);
|
|
72
|
+
validatePositionals(positionals, relationInputs.create.forms[0]);
|
|
73
|
+
const type = requireStringOption(relationInputs.create.options.type, values.type, "type", `--type must be one of these: ${relationInputs.create.options.type.value.choices.join(", ")}`);
|
|
74
|
+
const typeResult = relationTypeSchema.safeParse(type);
|
|
75
|
+
if (!typeResult.success) {
|
|
76
|
+
quit(`--type must be one of these: ${relationTypeSchema.options.join(", ")}`, undefined, {
|
|
77
|
+
argument: "--type",
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
const sourceTaskKey = positionals[0];
|
|
81
|
+
const targetTaskKey = positionals[1];
|
|
82
|
+
return { sourceTaskKey, targetTaskKey, relation_type: typeResult.data };
|
|
83
|
+
}
|
|
84
|
+
const relationInputs = {
|
|
85
|
+
create: {
|
|
86
|
+
options: {
|
|
87
|
+
type: {
|
|
88
|
+
type: "string",
|
|
89
|
+
required: true,
|
|
90
|
+
value: {
|
|
91
|
+
kind: "enum",
|
|
92
|
+
name: "type",
|
|
93
|
+
choices: relationTypeSchema.options,
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
forms: [
|
|
98
|
+
{
|
|
99
|
+
id: "default",
|
|
100
|
+
positionals: [
|
|
101
|
+
{ ...taskKeyPositional, name: "source-task-key" },
|
|
102
|
+
{ ...taskKeyPositional, name: "target-task-key" },
|
|
103
|
+
],
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
constraints: [],
|
|
107
|
+
},
|
|
108
|
+
list: {
|
|
109
|
+
options: {},
|
|
110
|
+
forms: [
|
|
111
|
+
{
|
|
112
|
+
id: "default",
|
|
113
|
+
positionals: [taskKeyPositional],
|
|
114
|
+
},
|
|
115
|
+
],
|
|
116
|
+
constraints: [],
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
export const relationRouter = {
|
|
120
|
+
create: {
|
|
121
|
+
summary: "Create a relation between two tasks.",
|
|
122
|
+
effects: { domain: "create", storage: "normal-open" },
|
|
123
|
+
confirmation: { kind: "none" },
|
|
124
|
+
input: relationInputs.create,
|
|
125
|
+
results: [jsonResult("RelationChange")],
|
|
126
|
+
run: (args) => {
|
|
127
|
+
const props = parseRelationArgs(args);
|
|
128
|
+
const { sourceTaskKey, targetTaskKey } = props;
|
|
129
|
+
relation.create(props, currentActor());
|
|
130
|
+
const source = task.getByKey(sourceTaskKey);
|
|
131
|
+
const target = task.getByKey(targetTaskKey);
|
|
132
|
+
const created = {
|
|
133
|
+
type: props.relation_type,
|
|
134
|
+
source: source.key,
|
|
135
|
+
target: target.key,
|
|
136
|
+
};
|
|
137
|
+
output(created, (r) => {
|
|
138
|
+
const label = {
|
|
139
|
+
related: "related to",
|
|
140
|
+
dependency: "depends on",
|
|
141
|
+
duplicate: "is a duplicate of",
|
|
142
|
+
subtask: "is a subtask of",
|
|
143
|
+
}[r.type];
|
|
144
|
+
return `${r.source} ${label} ${r.target}`;
|
|
145
|
+
});
|
|
146
|
+
},
|
|
147
|
+
help: `${generateCommandUsage("task relation create", relationInputs.create)}
|
|
148
|
+
|
|
149
|
+
Create a relation between two tasks.
|
|
150
|
+
|
|
151
|
+
Types:
|
|
152
|
+
related Connect two tasks, with any order provided to indicate relation.
|
|
153
|
+
dependency Source depends on target.
|
|
154
|
+
duplicate Source is a duplicate of target.
|
|
155
|
+
subtask Source is a subtask of target (child -> parent).
|
|
156
|
+
|
|
157
|
+
A task may have one parent and many subtasks. Nesting is allowed; cycles are rejected.
|
|
158
|
+
Remove an existing subtask relation before assigning a different parent.
|
|
159
|
+
|
|
160
|
+
Relations do not automatically move, complete, or archive tasks.
|
|
161
|
+
Deleting a parent removes its links and preserves its subtasks.
|
|
162
|
+
Tasks from separate projects can be related to each other. Tasks can also be related to archived tasks.
|
|
163
|
+
|
|
164
|
+
Example: zinn task relation create APP-1 APP-2 --type dependency`,
|
|
165
|
+
},
|
|
166
|
+
list: {
|
|
167
|
+
summary: "Show incoming and outgoing relations with connected task keys, columns and titles.",
|
|
168
|
+
effects: { domain: "read", storage: "normal-open" },
|
|
169
|
+
confirmation: { kind: "none" },
|
|
170
|
+
input: relationInputs.list,
|
|
171
|
+
results: [jsonResult("RelationList")],
|
|
172
|
+
run: (args) => {
|
|
173
|
+
const { positionals } = parseArgs({
|
|
174
|
+
options: relationInputs.list.options,
|
|
175
|
+
args,
|
|
176
|
+
allowPositionals: true,
|
|
177
|
+
strict: true,
|
|
178
|
+
});
|
|
179
|
+
validatePositionals(positionals, relationInputs.list.forms[0]);
|
|
180
|
+
output(presentRelations(task.getByKey(positionals[0])), formatRelations);
|
|
181
|
+
},
|
|
182
|
+
help: `${generateCommandUsage("task relation list", relationInputs.list)}
|
|
183
|
+
|
|
184
|
+
Show incoming and outgoing relations with connected task keys, columns and titles.
|
|
185
|
+
Archived connected tasks are included and marked Archived.
|
|
186
|
+
An empty list prints nothing.
|
|
187
|
+
|
|
188
|
+
Example: zinn task relation list APP-1`,
|
|
189
|
+
},
|
|
190
|
+
delete: {
|
|
191
|
+
summary: "Remove only the specified relation. No confirmation is required.",
|
|
192
|
+
effects: { domain: "delete", storage: "normal-open" },
|
|
193
|
+
confirmation: { kind: "none" },
|
|
194
|
+
input: relationInputs.create,
|
|
195
|
+
results: [jsonResult("RelationChange")],
|
|
196
|
+
run: (args) => {
|
|
197
|
+
const props = parseRelationArgs(args);
|
|
198
|
+
relation.delete(props, currentActor());
|
|
199
|
+
const source = task.getByKey(props.sourceTaskKey);
|
|
200
|
+
const target = task.getByKey(props.targetTaskKey);
|
|
201
|
+
const deleted = {
|
|
202
|
+
type: props.relation_type,
|
|
203
|
+
source: source.key,
|
|
204
|
+
target: target.key,
|
|
205
|
+
};
|
|
206
|
+
output(deleted, (r) => `Deleted ${r.type} relation from ${r.source} to ${r.target}`);
|
|
207
|
+
},
|
|
208
|
+
help: `${generateCommandUsage("task relation delete", relationInputs.create)}
|
|
209
|
+
|
|
210
|
+
Remove only the specified relation. No confirmation is required.
|
|
211
|
+
|
|
212
|
+
Types: ${relationTypeSchema.options.join(", ")}.
|
|
213
|
+
|
|
214
|
+
For related, either task order works.
|
|
215
|
+
For dependency, duplicate, and subtask, use the creation direction:
|
|
216
|
+
source depends on target, is a duplicate of target, or is a subtask of target.
|
|
217
|
+
|
|
218
|
+
Archived and cross-project tasks are supported.
|
|
219
|
+
A missing relation is an error.
|
|
220
|
+
|
|
221
|
+
Example: zinn task relation delete APP-1 APP-2 --type dependency`,
|
|
222
|
+
},
|
|
223
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export declare const restoreCommand: {
|
|
2
|
+
summary: string;
|
|
3
|
+
effects: {
|
|
4
|
+
domain: "replace";
|
|
5
|
+
storage: "restore";
|
|
6
|
+
};
|
|
7
|
+
confirmation: {
|
|
8
|
+
kind: "prompt";
|
|
9
|
+
description: string;
|
|
10
|
+
};
|
|
11
|
+
input: {
|
|
12
|
+
readonly options: {
|
|
13
|
+
readonly yes: {
|
|
14
|
+
readonly type: "boolean";
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
readonly forms: readonly [{
|
|
18
|
+
readonly id: "default";
|
|
19
|
+
readonly positionals: readonly [{
|
|
20
|
+
readonly name: "path";
|
|
21
|
+
readonly min: 1;
|
|
22
|
+
readonly max: 1;
|
|
23
|
+
readonly value: {
|
|
24
|
+
readonly kind: "path";
|
|
25
|
+
};
|
|
26
|
+
}];
|
|
27
|
+
}];
|
|
28
|
+
readonly constraints: readonly [];
|
|
29
|
+
};
|
|
30
|
+
results: import("../results.js").CommandResult[];
|
|
31
|
+
run: (args: string[]) => Promise<void>;
|
|
32
|
+
help: string;
|
|
33
|
+
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { backup, RestoreRefusedError, getStorage } from "@zinn-dev/core";
|
|
4
|
+
import { jsonResult } from "../results.js";
|
|
5
|
+
import { reportLockWait } from "../database.js";
|
|
6
|
+
import { confirmationDescription } from "../lib.js";
|
|
7
|
+
import { generateCommandUsage } from "../input.js";
|
|
8
|
+
import { askConfirmation, diagnostic, output, validatePositionals, validateUniqueOptions, } from "../lib.js";
|
|
9
|
+
import packageJson from "../../package.json" with { type: "json" };
|
|
10
|
+
const build = { client: "cli", version: packageJson.version };
|
|
11
|
+
const restoreInputs = {
|
|
12
|
+
command: {
|
|
13
|
+
options: { yes: { type: "boolean" } },
|
|
14
|
+
forms: [
|
|
15
|
+
{ id: "default", positionals: [{ name: "path", min: 1, max: 1, value: { kind: "path" } }] },
|
|
16
|
+
],
|
|
17
|
+
constraints: [],
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
export const restoreCommand = {
|
|
21
|
+
summary: "Replace the database with the one in a backup file, such as one saved by `zinn backup` or by an upgrade, after confirmation.",
|
|
22
|
+
effects: { domain: "replace", storage: "restore" },
|
|
23
|
+
confirmation: { kind: "prompt", description: confirmationDescription },
|
|
24
|
+
input: restoreInputs.command,
|
|
25
|
+
results: [jsonResult("Restore")],
|
|
26
|
+
run: async (args) => {
|
|
27
|
+
const { positionals, values, tokens } = parseArgs({
|
|
28
|
+
args,
|
|
29
|
+
strict: true,
|
|
30
|
+
allowPositionals: true,
|
|
31
|
+
tokens: true,
|
|
32
|
+
options: restoreInputs.command.options,
|
|
33
|
+
});
|
|
34
|
+
validateUniqueOptions(tokens, restoreInputs.command.options);
|
|
35
|
+
validatePositionals(positionals, restoreInputs.command.forms[0]);
|
|
36
|
+
const path = resolve(positionals[0]);
|
|
37
|
+
const { databasePath, backupDirectory } = getStorage();
|
|
38
|
+
const isPreconfirmed = values.yes === true;
|
|
39
|
+
// A backup that would be refused is refused before the prompt.
|
|
40
|
+
if (!isPreconfirmed) {
|
|
41
|
+
backup.check({ ...build, path });
|
|
42
|
+
}
|
|
43
|
+
const isConfirmed = await askConfirmation(`Replace the database at ${databasePath} with the backup ${path}? The current database is saved to ${backupDirectory} first.`, isPreconfirmed);
|
|
44
|
+
if (!isConfirmed) {
|
|
45
|
+
output(null, () => `Restore cancelled. The database at ${databasePath} is unchanged`);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
const result = backup.restore({ ...build, path, onWait: reportLockWait });
|
|
50
|
+
output(result, (r) => {
|
|
51
|
+
const restored = `Restored the database from ${path} at schema version ${r.restoredVersion}.`;
|
|
52
|
+
return r.backupPath == null
|
|
53
|
+
? `${restored} There was no database to save`
|
|
54
|
+
: `${restored} The database it replaced was saved to ${r.backupPath}`;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
// A refusal attempted nothing, so only a failed restore points at the backups.
|
|
59
|
+
if (!(error instanceof RestoreRefusedError)) {
|
|
60
|
+
const isDamagedDatabase = error instanceof Error &&
|
|
61
|
+
"code" in error &&
|
|
62
|
+
typeof error.code === "string" &&
|
|
63
|
+
error.code.startsWith("SQLITE_CORRUPT");
|
|
64
|
+
if (isDamagedDatabase) {
|
|
65
|
+
diagnostic(`The database at ${databasePath} is damaged. Nothing was changed. To restore it:\n` +
|
|
66
|
+
`1. Close every program that has the database open.\n` +
|
|
67
|
+
`2. Move ${databasePath}, ${databasePath}-wal and ${databasePath}-shm out of the data folder, keeping any that exist together. Keep these files; do not delete them.\n` +
|
|
68
|
+
`3. Run the same zinn restore command again to create the database from the backup.`);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
diagnostic(`The database is unchanged. Backups taken before each restore attempt are kept in ${backupDirectory}.`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
help: `${generateCommandUsage("restore", restoreInputs.command)}
|
|
78
|
+
|
|
79
|
+
Replace the database with the one in a backup file, such as one saved by \`zinn backup\` or by an upgrade, after confirmation.
|
|
80
|
+
|
|
81
|
+
The current database is saved first, in the same place as \`zinn backup\`, and the restore is applied completely or not at all.
|
|
82
|
+
A backup with an older schema version is restored at that version. Zinn then upgrades it like any older database: by itself, except for ~/.zinn run from a source checkout, which needs \`zinn migrate\`.
|
|
83
|
+
Restoring onto a missing database creates it.
|
|
84
|
+
Fails, leaving the database as it is, when the backup has a newer schema version than this Zinn or none at all, is damaged, or is not a Zinn database.
|
|
85
|
+
Use --yes to skip the confirmation prompt, for example in scripts or with --json.
|
|
86
|
+
|
|
87
|
+
Example: zinn restore ~/.zinn/backups/2026-10-06T12-30-05Z-cli-0.40.0-1a2b3c4d.sqlite`,
|
|
88
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ParsedRoute, RouteDef } from "../types.js";
|
|
2
|
+
export declare function parseRoutes(routesDef: RouteDef, prefix?: string): ParsedRoute[];
|
|
3
|
+
export declare function createRouter(routesDef: RouteDef, beforeRun?: (command: ParsedRoute) => void | Promise<void>): {
|
|
4
|
+
route: (args: Array<string>) => Promise<void>;
|
|
5
|
+
help: string;
|
|
6
|
+
};
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { routerOptions, helpSpellings, versionSpellings, usageLabel } from "../input.js";
|
|
3
|
+
import { dashValueHelp, diagnostic, flushDiagnostics, isAllowLegacyReadFlag, isHelpFlag, isRouterFlag, parseArgsMessage, prepareOutput, quit, renderFailure, setLegacyReadAllowed, UsageError, validateUniqueOptions, } from "../lib.js";
|
|
4
|
+
// ZINN_DEBUG=1 prints routing detail on stderr, so stdout and `--json` results stay unchanged.
|
|
5
|
+
function isDebugOutputEnabled() {
|
|
6
|
+
return process.env.ZINN_DEBUG === "1";
|
|
7
|
+
}
|
|
8
|
+
function getIsRunnable(obj) {
|
|
9
|
+
return Object.hasOwn(obj, "run");
|
|
10
|
+
}
|
|
11
|
+
// --json and --allow-legacy-read can each be given once, like a command's own flags.
|
|
12
|
+
function validateRouterFlags(routableArgs) {
|
|
13
|
+
const { tokens } = parseArgs({
|
|
14
|
+
args: routableArgs.filter(isRouterFlag),
|
|
15
|
+
options: routerOptions,
|
|
16
|
+
strict: true,
|
|
17
|
+
tokens: true,
|
|
18
|
+
});
|
|
19
|
+
validateUniqueOptions(tokens, routerOptions);
|
|
20
|
+
}
|
|
21
|
+
/** The usage lines that open a command's help, up to its first blank line. */
|
|
22
|
+
function getUsage(help) {
|
|
23
|
+
return help.split("\n\n")[0];
|
|
24
|
+
}
|
|
25
|
+
function toCommand(args) {
|
|
26
|
+
return args.join(" ");
|
|
27
|
+
}
|
|
28
|
+
// Groups sibling commands into one line, e.g. " comment {create|list|edit|delete}".
|
|
29
|
+
// A command without siblings is not a choice, so it gets no braces, e.g. " backup".
|
|
30
|
+
// Top-level commands in the root help are unrelated to each other, so each gets its own line.
|
|
31
|
+
function formatCommands(parsedRoutes, prefix) {
|
|
32
|
+
const isRootHelp = prefix === "";
|
|
33
|
+
const siblingsByParent = new Map();
|
|
34
|
+
for (const { command } of parsedRoutes) {
|
|
35
|
+
if (!command.startsWith(prefix)) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
const words = command.slice(prefix.length).split(" ");
|
|
39
|
+
const parent = toCommand(words.slice(0, -1));
|
|
40
|
+
siblingsByParent.set(parent, [...(siblingsByParent.get(parent) ?? []), words.at(-1)]);
|
|
41
|
+
}
|
|
42
|
+
return [...siblingsByParent]
|
|
43
|
+
.flatMap(([parent, siblings]) => {
|
|
44
|
+
const isTopLevel = isRootHelp && parent === "";
|
|
45
|
+
if (isTopLevel) {
|
|
46
|
+
return siblings.map((command) => ` ${command}`);
|
|
47
|
+
}
|
|
48
|
+
const commands = siblings.length === 1 ? siblings[0] : `{${siblings.join("|")}}`;
|
|
49
|
+
return [` ${parent === "" ? "" : `${parent} `}${commands}`];
|
|
50
|
+
})
|
|
51
|
+
.join("\n");
|
|
52
|
+
}
|
|
53
|
+
function getRootHelp(parsedRoutes) {
|
|
54
|
+
// TODO: maybe "brand" text that is exported and used here for title and desc
|
|
55
|
+
return `ZINN - A kanban workflow in the terminal
|
|
56
|
+
|
|
57
|
+
${usageLabel}zinn <command>
|
|
58
|
+
|
|
59
|
+
Options:
|
|
60
|
+
${helpSpellings.join(", ")} Show help
|
|
61
|
+
${versionSpellings.join(", ")} Show version
|
|
62
|
+
--json Print command output as JSON
|
|
63
|
+
--allow-legacy-read Read a database without a schema version, best effort and read-only
|
|
64
|
+
|
|
65
|
+
Commands:
|
|
66
|
+
${formatCommands(parsedRoutes, "")}
|
|
67
|
+
|
|
68
|
+
Run zinn <namespace> --help or zinn <command> --help for more information.
|
|
69
|
+
--help shows help for the command words before it and runs nothing. The rest of the line is ignored.
|
|
70
|
+
-- ends the options: everything after it is considered a plain word, even if it starts with a dash (e.g. 'zinn project column create SHOP -- -later').
|
|
71
|
+
Flags after it are not read.
|
|
72
|
+
${dashValueHelp("--description")}
|
|
73
|
+
Running zinn without a command shows this help.`;
|
|
74
|
+
}
|
|
75
|
+
function getNamespaceHelp(parsedRoutes, namespace) {
|
|
76
|
+
const commands = formatCommands(parsedRoutes, `${namespace} `);
|
|
77
|
+
if (commands === "") {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
return `${usageLabel}zinn ${namespace} <command>\n\nCommands:\n${commands}`;
|
|
81
|
+
}
|
|
82
|
+
// The longest leading run of words that names a namespace, or the root namespace "" when none does.
|
|
83
|
+
function findNearestNamespace(parsedRoutes, words) {
|
|
84
|
+
for (let endIndex = words.length; endIndex > 0; endIndex--) {
|
|
85
|
+
const namespace = toCommand(words.slice(0, endIndex));
|
|
86
|
+
const help = getNamespaceHelp(parsedRoutes, namespace);
|
|
87
|
+
if (help != null) {
|
|
88
|
+
return { namespace, wordCount: endIndex, help };
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return { namespace: "", wordCount: 0, help: getRootHelp(parsedRoutes) };
|
|
92
|
+
}
|
|
93
|
+
// Prints help for a namespace when asked, or exits with an error for a missing or unknown command in it.
|
|
94
|
+
function routeToNamespace(parsedRoutes, words, isHelpRequest) {
|
|
95
|
+
const { namespace, wordCount, help } = findNearestNamespace(parsedRoutes, words);
|
|
96
|
+
const unknownWord = words[wordCount];
|
|
97
|
+
const commandName = namespace === "" ? "zinn" : `zinn ${namespace}`;
|
|
98
|
+
if (unknownWord == null && isHelpRequest) {
|
|
99
|
+
console.info(help);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
if (unknownWord == null) {
|
|
103
|
+
quit(`Missing a command for "${commandName}"`, help, { argument: "command" });
|
|
104
|
+
}
|
|
105
|
+
const reason = `Unrecognized command "${unknownWord}" for "${commandName}"`;
|
|
106
|
+
if (isHelpRequest) {
|
|
107
|
+
quit(reason, help, { argument: "command" });
|
|
108
|
+
}
|
|
109
|
+
quit(`${reason}. Run \`${commandName} --help\` for a list of available commands`, undefined, {
|
|
110
|
+
argument: "command",
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
export function parseRoutes(routesDef, prefix) {
|
|
114
|
+
const routeDefinitions = [];
|
|
115
|
+
if (isDebugOutputEnabled()) {
|
|
116
|
+
diagnostic(`::router.parseRoutes[${prefix ?? ""}] ${Object.keys(routesDef).join(" ")}`);
|
|
117
|
+
}
|
|
118
|
+
for (const key of Object.keys(routesDef)) {
|
|
119
|
+
const definition = routesDef[key];
|
|
120
|
+
const isRunnable = getIsRunnable(definition);
|
|
121
|
+
const combinedRoute = prefix ? `${prefix} ${key}` : key;
|
|
122
|
+
if (isRunnable) {
|
|
123
|
+
routeDefinitions.push({ command: combinedRoute, ...definition });
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
const result = parseRoutes(definition, combinedRoute);
|
|
127
|
+
routeDefinitions.push(...result);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return routeDefinitions;
|
|
131
|
+
}
|
|
132
|
+
async function route(parsedRoutes, rawArgs, beforeRun) {
|
|
133
|
+
prepareOutput(rawArgs);
|
|
134
|
+
// Anything after "--" belongs to the command, so "--json" there is search text, not a flag.
|
|
135
|
+
const separatorIndex = rawArgs.indexOf("--");
|
|
136
|
+
const routableArgs = separatorIndex === -1 ? rawArgs : rawArgs.slice(0, separatorIndex);
|
|
137
|
+
const passthroughArgs = separatorIndex === -1 ? [] : rawArgs.slice(separatorIndex);
|
|
138
|
+
const filteredRoutableArgs = routableArgs.filter((arg) => !isRouterFlag(arg));
|
|
139
|
+
if (routableArgs.some(isAllowLegacyReadFlag)) {
|
|
140
|
+
setLegacyReadAllowed(true);
|
|
141
|
+
}
|
|
142
|
+
// A help flag asks for help with the command words before it, so nothing after it is read.
|
|
143
|
+
const helpFlagIndex = filteredRoutableArgs.findIndex(isHelpFlag);
|
|
144
|
+
const isHelpRequest = helpFlagIndex !== -1;
|
|
145
|
+
const args = isHelpRequest
|
|
146
|
+
? filteredRoutableArgs.slice(0, helpFlagIndex)
|
|
147
|
+
: [...filteredRoutableArgs, ...passthroughArgs];
|
|
148
|
+
let match = null;
|
|
149
|
+
let nestingLevel = 0;
|
|
150
|
+
for (let endIndex = args.length; endIndex >= 0; endIndex--) {
|
|
151
|
+
const command = toCommand(args.slice(0, endIndex));
|
|
152
|
+
const matchedRoute = parsedRoutes.find((r) => r.command === command);
|
|
153
|
+
if (matchedRoute) {
|
|
154
|
+
match = matchedRoute;
|
|
155
|
+
nestingLevel = endIndex;
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (isDebugOutputEnabled()) {
|
|
160
|
+
diagnostic(`::router.route ${match?.command ?? "namespace"}`);
|
|
161
|
+
}
|
|
162
|
+
if (match == null) {
|
|
163
|
+
routeToNamespace(parsedRoutes, args, isHelpRequest);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (isHelpRequest) {
|
|
167
|
+
console.info(match.help);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
validateRouterFlags(routableArgs);
|
|
171
|
+
const commandArgs = args.slice(nestingLevel);
|
|
172
|
+
try {
|
|
173
|
+
await beforeRun?.(match);
|
|
174
|
+
await match.run(commandArgs);
|
|
175
|
+
}
|
|
176
|
+
catch (err) {
|
|
177
|
+
if (err instanceof UsageError) {
|
|
178
|
+
err.help = getUsage(match.help);
|
|
179
|
+
throw err;
|
|
180
|
+
}
|
|
181
|
+
const suggestFor = (flag) => match.guessedFlags?.[flag]?.(commandArgs);
|
|
182
|
+
const parseArgsReason = parseArgsMessage(err, rawArgs, suggestFor);
|
|
183
|
+
if (parseArgsReason != null) {
|
|
184
|
+
const argument = /(?:Unknown option|Option) '(-[^' ]+)/.exec(err.message)?.[1];
|
|
185
|
+
quit(parseArgsReason, getUsage(match.help), argument === undefined ? undefined : { argument });
|
|
186
|
+
}
|
|
187
|
+
throw err;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
export function createRouter(routesDef, beforeRun) {
|
|
191
|
+
const parsedRoutes = parseRoutes(routesDef);
|
|
192
|
+
return {
|
|
193
|
+
route: async (args) => {
|
|
194
|
+
try {
|
|
195
|
+
await route(parsedRoutes, args, beforeRun);
|
|
196
|
+
flushDiagnostics();
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
renderFailure(error);
|
|
200
|
+
}
|
|
201
|
+
},
|
|
202
|
+
help: getRootHelp(parsedRoutes),
|
|
203
|
+
};
|
|
204
|
+
}
|