@zinn-dev/cli 0.37.0 → 0.38.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 +158 -80
- package/index.ts +1 -15
- package/package.json +2 -2
- package/src/lib.test.ts +28 -2
- package/src/lib.ts +34 -11
- package/src/presentation/activity-presentation.test.ts +71 -0
- package/src/presentation/activity-presentation.ts +489 -0
- package/src/presentation/column-presentation.ts +19 -9
- package/src/presentation/description-presentation.ts +7 -4
- package/src/presentation/task-presentation.ts +5 -4
- package/src/presentation/time.ts +17 -0
- package/src/routes/activity-router.ts +77 -0
- package/src/routes/backup-router.ts +3 -2
- package/src/routes/comment-router.ts +39 -27
- package/src/routes/label-router.ts +25 -17
- package/src/routes/migrate-router.ts +4 -2
- package/src/routes/project-router.ts +77 -83
- package/src/routes/reindex-router.ts +3 -2
- package/src/routes/relation-router.ts +9 -7
- package/src/routes/restore-router.ts +2 -2
- package/src/routes/router.test.ts +18 -0
- package/src/routes/router.ts +108 -22
- package/src/routes/routes.ts +2 -0
- package/src/routes/task-router.ts +111 -97
- package/src/text-edit.ts +68 -0
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type { RouteDef } from "../types";
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
PresentedColumn,
|
|
4
|
+
PresentedColumnTaskCount,
|
|
5
|
+
} from "../presentation/column-presentation";
|
|
3
6
|
|
|
4
7
|
import { parseArgs } from "node:util";
|
|
5
8
|
|
|
@@ -9,10 +12,12 @@ import {
|
|
|
9
12
|
failureMessage,
|
|
10
13
|
output,
|
|
11
14
|
quit,
|
|
12
|
-
|
|
15
|
+
UsageError,
|
|
16
|
+
validatePositionals,
|
|
13
17
|
validateUniqueOptions,
|
|
14
18
|
} from "../lib";
|
|
15
|
-
import {
|
|
19
|
+
import { resolveTextEdit, textEditHelp, textEditOptions, textEditUsage } from "../text-edit";
|
|
20
|
+
import { formatColumnList } from "../presentation/column-presentation";
|
|
16
21
|
import { formatDescriptionPreview } from "../presentation/description-presentation";
|
|
17
22
|
import { formatCellSeparator, formatRows } from "../presentation/row-layout";
|
|
18
23
|
import { formatSection } from "../presentation/section";
|
|
@@ -50,8 +55,6 @@ function formatProjectList(
|
|
|
50
55
|
);
|
|
51
56
|
}
|
|
52
57
|
|
|
53
|
-
type PresentedColumn = { project: string; name: string };
|
|
54
|
-
|
|
55
58
|
function presentColumn(columnMatch: { project_id: string; name: string }): PresentedColumn {
|
|
56
59
|
const columnProject = project.getById(columnMatch.project_id)!;
|
|
57
60
|
|
|
@@ -59,7 +62,7 @@ function presentColumn(columnMatch: { project_id: string; name: string }): Prese
|
|
|
59
62
|
}
|
|
60
63
|
|
|
61
64
|
function formatColumn(presented: PresentedColumn) {
|
|
62
|
-
return
|
|
65
|
+
return formatColumnList([presented], { showsProject: true });
|
|
63
66
|
}
|
|
64
67
|
|
|
65
68
|
export const projectRouter = {
|
|
@@ -73,15 +76,10 @@ export const projectRouter = {
|
|
|
73
76
|
tokens: true,
|
|
74
77
|
});
|
|
75
78
|
|
|
76
|
-
const projectKey = positionals[0];
|
|
77
|
-
|
|
78
|
-
if (projectKey == null) {
|
|
79
|
-
quit("Project key must be specified");
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
validatePositionalCount(positionals, 1, "Only one project key can be specified");
|
|
83
|
-
|
|
84
79
|
validateUniqueOptions(tokens);
|
|
80
|
+
validatePositionals(positionals, ["<project-key>"]);
|
|
81
|
+
|
|
82
|
+
const projectKey = positionals[0]!;
|
|
85
83
|
|
|
86
84
|
try {
|
|
87
85
|
const createdProject = project.create(
|
|
@@ -99,13 +97,16 @@ Create a project with the default columns.
|
|
|
99
97
|
Project keys are stored in uppercase.
|
|
100
98
|
|
|
101
99
|
An omitted name defaults to the uppercase project key.
|
|
102
|
-
Names cannot be blank.
|
|
100
|
+
Names are trimmed and cannot be blank or contain line breaks or control characters.
|
|
103
101
|
Descriptions may contain multiple lines. If omitted, they are saved as null.
|
|
104
102
|
|
|
105
103
|
Example: zinn project create MDR --name "Macrodata Refinement"`,
|
|
106
104
|
},
|
|
107
105
|
list: {
|
|
108
|
-
run: () => {
|
|
106
|
+
run: (args: Array<string>) => {
|
|
107
|
+
const { positionals } = parseArgs({ args, allowPositionals: true, strict: true });
|
|
108
|
+
validatePositionals(positionals, []);
|
|
109
|
+
|
|
109
110
|
const projects = project.getAll();
|
|
110
111
|
|
|
111
112
|
output(projects.map(presentProject), formatProjectList);
|
|
@@ -118,14 +119,10 @@ Example: zinn project list`,
|
|
|
118
119
|
},
|
|
119
120
|
view: {
|
|
120
121
|
run: (args: Array<string>) => {
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
if (projectKey == null) {
|
|
124
|
-
quit("Project key must be specified");
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
validatePositionalCount(args, 1, "Only one project key can be specified");
|
|
122
|
+
const { positionals } = parseArgs({ args, allowPositionals: true, strict: true });
|
|
123
|
+
validatePositionals(positionals, ["<project-key>"]);
|
|
128
124
|
|
|
125
|
+
const projectKey = positionals[0]!;
|
|
129
126
|
const projectMatch = project.getByKey(projectKey);
|
|
130
127
|
|
|
131
128
|
if (projectMatch == null) {
|
|
@@ -150,7 +147,8 @@ Example: zinn project list`,
|
|
|
150
147
|
sections.push(formatSection("Description", viewedProject.description));
|
|
151
148
|
}
|
|
152
149
|
|
|
153
|
-
const columnTaskCounts =
|
|
150
|
+
const columnTaskCounts = formatColumnList(viewedProject.columns, {
|
|
151
|
+
showsTaskCounts: true,
|
|
154
152
|
archivedTaskCount: viewedProject.archivedTaskCount,
|
|
155
153
|
});
|
|
156
154
|
sections.push(formatSection("Columns", columnTaskCounts));
|
|
@@ -169,52 +167,52 @@ Example: zinn project view MDR`,
|
|
|
169
167
|
run: (args) => {
|
|
170
168
|
const { values, positionals, tokens } = parseArgs({
|
|
171
169
|
args,
|
|
172
|
-
options: { name: { type: "string" }, description: { type: "string" } },
|
|
170
|
+
options: { name: { type: "string" }, description: { type: "string" }, ...textEditOptions },
|
|
173
171
|
allowPositionals: true,
|
|
174
172
|
strict: true,
|
|
175
173
|
tokens: true,
|
|
176
174
|
});
|
|
177
175
|
|
|
178
|
-
const projectKey = positionals[0];
|
|
179
|
-
|
|
180
|
-
if (projectKey == null) {
|
|
181
|
-
quit("Project key must be specified");
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
validatePositionalCount(positionals, 1, "Only one project key can be specified");
|
|
185
|
-
|
|
186
176
|
validateUniqueOptions(tokens);
|
|
177
|
+
validatePositionals(positionals, ["<project-key>"]);
|
|
178
|
+
|
|
179
|
+
const projectKey = positionals[0]!;
|
|
180
|
+
const description = resolveTextEdit(values.description, values, "--description");
|
|
187
181
|
|
|
188
|
-
if (values.name === undefined &&
|
|
182
|
+
if (values.name === undefined && description === undefined) {
|
|
189
183
|
quit("Provide an edit flag: --name or --description");
|
|
190
184
|
}
|
|
191
185
|
|
|
192
186
|
const editedProject = project.edit(
|
|
193
|
-
{ projectKey, name: values.name, description
|
|
187
|
+
{ projectKey, name: values.name, description },
|
|
194
188
|
currentActor(),
|
|
195
189
|
);
|
|
196
190
|
output(presentProject(editedProject), formatProject);
|
|
197
191
|
},
|
|
198
|
-
help: `Usage: zinn project edit <project-key> [--name <text>] [--description <text>]
|
|
192
|
+
help: `Usage: zinn project edit <project-key> [--name <text>] [--description <text> ${textEditUsage}]
|
|
199
193
|
|
|
200
194
|
Change the supplied project fields. At least one edit flag is required.
|
|
201
195
|
Use --description "" to clear the description.
|
|
202
196
|
|
|
203
|
-
|
|
197
|
+
${textEditHelp("--description")}
|
|
198
|
+
|
|
199
|
+
Examples:
|
|
200
|
+
zinn project edit MDR --name "Macrodata Refinement"
|
|
201
|
+
zinn project edit MDR --description "database" --replacing "board" --all`,
|
|
204
202
|
},
|
|
205
203
|
delete: {
|
|
206
204
|
run: (args: Array<string>) => {
|
|
207
|
-
const { values, positionals } = parseArgs({
|
|
205
|
+
const { values, positionals, tokens } = parseArgs({
|
|
208
206
|
args,
|
|
209
207
|
options: { yes: { type: "boolean" } },
|
|
210
208
|
allowPositionals: true,
|
|
211
209
|
strict: true,
|
|
210
|
+
tokens: true,
|
|
212
211
|
});
|
|
213
|
-
|
|
212
|
+
validateUniqueOptions(tokens);
|
|
213
|
+
validatePositionals(positionals, ["<project-key>"]);
|
|
214
214
|
|
|
215
|
-
|
|
216
|
-
quit("You need to specificy which project to delete");
|
|
217
|
-
}
|
|
215
|
+
const projectKey = positionals[0]!;
|
|
218
216
|
|
|
219
217
|
try {
|
|
220
218
|
const projectMatch = project.getByKey(projectKey);
|
|
@@ -248,12 +246,11 @@ Example: zinn project delete MDR`,
|
|
|
248
246
|
column: {
|
|
249
247
|
create: {
|
|
250
248
|
run: (args: Array<string>) => {
|
|
251
|
-
const
|
|
252
|
-
|
|
249
|
+
const { positionals } = parseArgs({ args, allowPositionals: true, strict: true });
|
|
250
|
+
validatePositionals(positionals, ["<project-key>", "<column-name>"]);
|
|
253
251
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}
|
|
252
|
+
const projectKey = positionals[0]!;
|
|
253
|
+
const columnName = positionals[1]!;
|
|
257
254
|
|
|
258
255
|
try {
|
|
259
256
|
const createdColumn = column.create({ projectKey, name: columnName }, currentActor());
|
|
@@ -271,19 +268,23 @@ Example: zinn project column create MDR "Boardlog"`,
|
|
|
271
268
|
},
|
|
272
269
|
list: {
|
|
273
270
|
run: (args: Array<string>) => {
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
quit("Project key needs to be specified to list columns");
|
|
277
|
-
}
|
|
271
|
+
const { positionals } = parseArgs({ args, allowPositionals: true, strict: true });
|
|
272
|
+
validatePositionals(positionals, ["<project-key>"]);
|
|
278
273
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
274
|
+
const taskCounts = column.getTaskCountsByProjectKey(positionals[0]!);
|
|
275
|
+
const columns = taskCounts.columns.map((columnCount) => ({
|
|
276
|
+
name: columnCount.column.name,
|
|
277
|
+
taskCount: columnCount.taskCount,
|
|
278
|
+
}));
|
|
279
|
+
output(columns, (presentedColumns) =>
|
|
280
|
+
formatColumnList(presentedColumns, {
|
|
281
|
+
showsTaskCounts: true,
|
|
282
|
+
}),
|
|
283
|
+
);
|
|
283
284
|
},
|
|
284
285
|
help: `Usage: zinn project column list <project-key>
|
|
285
286
|
|
|
286
|
-
List a project's columns in board order.
|
|
287
|
+
List a project's columns in board order with their unarchived task counts.
|
|
287
288
|
|
|
288
289
|
Example: zinn project column list MDR`,
|
|
289
290
|
},
|
|
@@ -297,12 +298,8 @@ Example: zinn project column list MDR`,
|
|
|
297
298
|
tokens: true,
|
|
298
299
|
});
|
|
299
300
|
|
|
300
|
-
validatePositionalCount(
|
|
301
|
-
positionals,
|
|
302
|
-
2,
|
|
303
|
-
"Both the project key and the column name must be specified",
|
|
304
|
-
);
|
|
305
301
|
validateUniqueOptions(tokens);
|
|
302
|
+
validatePositionals(positionals, ["<project-key>", "<column-name>"]);
|
|
306
303
|
|
|
307
304
|
if (values.name === undefined) {
|
|
308
305
|
quit("Provide an edit flag: --name");
|
|
@@ -331,31 +328,30 @@ Example: zinn project column edit MDR "In Progress" --name "Refining"`,
|
|
|
331
328
|
},
|
|
332
329
|
order: {
|
|
333
330
|
run: (args: Array<string>) => {
|
|
334
|
-
const
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
331
|
+
const { positionals } = parseArgs({ args, allowPositionals: true, strict: true });
|
|
332
|
+
/**
|
|
333
|
+
* The words every direction needs. Only whether they are there is checked here,
|
|
334
|
+
* since the direction decides whether a target column follows.
|
|
335
|
+
*/
|
|
336
|
+
const leadingWords = [
|
|
337
|
+
"<project-key>",
|
|
338
|
+
"<column-name>",
|
|
339
|
+
"{first|previous|next|last|before|after}",
|
|
340
|
+
];
|
|
341
|
+
validatePositionals(positionals.slice(0, leadingWords.length), leadingWords);
|
|
342
|
+
|
|
343
|
+
const projectKey = positionals[0]!;
|
|
344
|
+
const columnName = positionals[1]!;
|
|
345
|
+
const direction = positionals[2]!;
|
|
344
346
|
let orderedColumn: ReturnType<typeof column.order>;
|
|
345
347
|
|
|
346
348
|
switch (direction) {
|
|
347
349
|
case "before":
|
|
348
350
|
case "after": {
|
|
349
|
-
|
|
350
|
-
quit(`Target column must be specified for "${direction}"`);
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
if (extraArgs.length > 0) {
|
|
354
|
-
quit("Only one target column can be specified");
|
|
355
|
-
}
|
|
351
|
+
validatePositionals(positionals, [...leadingWords, "<target-column-name>"]);
|
|
356
352
|
|
|
357
353
|
orderedColumn = column.order(
|
|
358
|
-
{ projectKey, columnName, direction, targetColumnName },
|
|
354
|
+
{ projectKey, columnName, direction, targetColumnName: positionals[3]! },
|
|
359
355
|
currentActor(),
|
|
360
356
|
);
|
|
361
357
|
break;
|
|
@@ -364,15 +360,13 @@ Example: zinn project column edit MDR "In Progress" --name "Refining"`,
|
|
|
364
360
|
case "previous":
|
|
365
361
|
case "next":
|
|
366
362
|
case "last": {
|
|
367
|
-
|
|
368
|
-
quit(`Order direction "${direction}" does not accept a target column`);
|
|
369
|
-
}
|
|
363
|
+
validatePositionals(positionals, leadingWords);
|
|
370
364
|
|
|
371
365
|
orderedColumn = column.order({ projectKey, columnName, direction }, currentActor());
|
|
372
366
|
break;
|
|
373
367
|
}
|
|
374
368
|
default:
|
|
375
|
-
|
|
369
|
+
throw new UsageError(`Unknown order direction "${direction}"`);
|
|
376
370
|
}
|
|
377
371
|
|
|
378
372
|
// TODO: maybe also print the new full column state?
|
|
@@ -4,11 +4,12 @@ import { parseArgs } from "node:util";
|
|
|
4
4
|
import { reindex } from "@zinn-dev/core";
|
|
5
5
|
|
|
6
6
|
import { reportLockWait } from "../database";
|
|
7
|
-
import { output } from "../lib";
|
|
7
|
+
import { output, validatePositionals } from "../lib";
|
|
8
8
|
|
|
9
9
|
export const reindexCommand = {
|
|
10
10
|
run: (args) => {
|
|
11
|
-
parseArgs({ args, strict: true, allowPositionals:
|
|
11
|
+
const { positionals } = parseArgs({ args, strict: true, allowPositionals: true });
|
|
12
|
+
validatePositionals(positionals, []);
|
|
12
13
|
|
|
13
14
|
const result = reindex.run({ onWait: reportLockWait });
|
|
14
15
|
|
|
@@ -2,15 +2,16 @@ import type { TaskRelationType, TaskWithKey } from "@zinn-dev/core";
|
|
|
2
2
|
import type { RouteDef } from "../types";
|
|
3
3
|
|
|
4
4
|
import { parseArgs } from "node:util";
|
|
5
|
-
import { relation, relationCreateSchema, task } from "@zinn-dev/core";
|
|
6
|
-
import { output, quit,
|
|
5
|
+
import { column, relation, relationCreateSchema, task } from "@zinn-dev/core";
|
|
6
|
+
import { output, quit, validatePositionals, validateUniqueOptions } from "../lib";
|
|
7
7
|
import { currentActor } from "../actor";
|
|
8
8
|
import { formatCellSeparator, formatRows } from "../presentation/row-layout";
|
|
9
|
+
import { columnNameCell } from "../presentation/column-presentation";
|
|
9
10
|
|
|
10
11
|
type PresentedRelation = {
|
|
11
12
|
type: TaskRelationType;
|
|
12
13
|
direction: "outgoing" | "incoming" | null;
|
|
13
|
-
task: { key: string; title: string; status: "Active" | "Archived" };
|
|
14
|
+
task: { key: string; column: string; title: string; status: "Active" | "Archived" };
|
|
14
15
|
};
|
|
15
16
|
|
|
16
17
|
export function presentRelations(taskMatch: TaskWithKey): Array<PresentedRelation> {
|
|
@@ -31,6 +32,7 @@ export function presentRelations(taskMatch: TaskWithKey): Array<PresentedRelatio
|
|
|
31
32
|
direction,
|
|
32
33
|
task: {
|
|
33
34
|
key: other.key,
|
|
35
|
+
column: column.getById(other.column_id)!.name,
|
|
34
36
|
title: other.title,
|
|
35
37
|
status: other.archived_at == null ? "Active" : "Archived",
|
|
36
38
|
},
|
|
@@ -67,6 +69,7 @@ export function formatRelations(relations: Array<PresentedRelation>) {
|
|
|
67
69
|
return [
|
|
68
70
|
{ text: label },
|
|
69
71
|
{ text: entry.task.key },
|
|
72
|
+
columnNameCell(entry.task.column),
|
|
70
73
|
{ text: `${entry.task.title}${archived}` },
|
|
71
74
|
];
|
|
72
75
|
}),
|
|
@@ -84,9 +87,8 @@ function parseRelationArgs(args: string[]) {
|
|
|
84
87
|
tokens: true,
|
|
85
88
|
});
|
|
86
89
|
|
|
87
|
-
validatePositionalCount(positionals, 2, "Exactly two task keys are required: source, target");
|
|
88
|
-
|
|
89
90
|
validateUniqueOptions(tokens);
|
|
91
|
+
validatePositionals(positionals, ["<source-task-key>", "<target-task-key>"]);
|
|
90
92
|
|
|
91
93
|
const typeResult = relationCreateSchema.shape.relation_type.safeParse(values.type);
|
|
92
94
|
|
|
@@ -148,12 +150,12 @@ Example: zinn task relation create APP-1 APP-2 --type dependency`,
|
|
|
148
150
|
list: {
|
|
149
151
|
run: (args) => {
|
|
150
152
|
const { positionals } = parseArgs({ args, allowPositionals: true, strict: true });
|
|
151
|
-
|
|
153
|
+
validatePositionals(positionals, ["<task-key>"]);
|
|
152
154
|
output(presentRelations(task.getByKey(positionals[0]!)), formatRelations);
|
|
153
155
|
},
|
|
154
156
|
help: `Usage: zinn task relation list <task-key>
|
|
155
157
|
|
|
156
|
-
Show incoming and outgoing relations with connected task keys and titles.
|
|
158
|
+
Show incoming and outgoing relations with connected task keys, columns and titles.
|
|
157
159
|
Archived connected tasks are included and marked Archived.
|
|
158
160
|
An empty list prints nothing.
|
|
159
161
|
|
|
@@ -5,7 +5,7 @@ import { resolve } from "node:path";
|
|
|
5
5
|
import { backup, RestoreRefusedError, getStorage } from "@zinn-dev/core";
|
|
6
6
|
|
|
7
7
|
import { reportLockWait } from "../database";
|
|
8
|
-
import { askConfirmation, output,
|
|
8
|
+
import { askConfirmation, output, validatePositionals, validateUniqueOptions } from "../lib";
|
|
9
9
|
import packageJson from "../../package.json";
|
|
10
10
|
|
|
11
11
|
const build = { client: "cli", version: packageJson.version };
|
|
@@ -20,7 +20,7 @@ export const restoreCommand = {
|
|
|
20
20
|
options: { yes: { type: "boolean" } },
|
|
21
21
|
});
|
|
22
22
|
validateUniqueOptions(tokens);
|
|
23
|
-
|
|
23
|
+
validatePositionals(positionals, ["<path>"]);
|
|
24
24
|
|
|
25
25
|
const path = resolve(positionals[0]!);
|
|
26
26
|
const { databasePath, backupDirectory } = getStorage();
|
|
@@ -95,6 +95,24 @@ test("a help request never runs a command even when its help is empty", () => {
|
|
|
95
95
|
}
|
|
96
96
|
});
|
|
97
97
|
|
|
98
|
+
test("a help flag anywhere before -- prints help for the words before it and runs nothing", () => {
|
|
99
|
+
const run = mock(() => {});
|
|
100
|
+
const info = spyOn(console, "info").mockImplementation(() => {});
|
|
101
|
+
const router = createRouter({ foo: { bar: { run, help: "bar help" } } });
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
router.route(["foo", "bar", "--help", "--name", "x"]);
|
|
105
|
+
router.route(["foo", "bar", "x", "-h"]);
|
|
106
|
+
expect(run).not.toHaveBeenCalled();
|
|
107
|
+
expect(info.mock.calls).toEqual([["bar help"], ["bar help"]]);
|
|
108
|
+
|
|
109
|
+
router.route(["foo", "--help", "bar"]);
|
|
110
|
+
expect(info.mock.calls.at(-1)![0]).toContain("Usage: zinn foo <command>");
|
|
111
|
+
} finally {
|
|
112
|
+
info.mockRestore();
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
98
116
|
test("--allow-legacy-read before a -- is taken out of the arguments and allows legacy reads", () => {
|
|
99
117
|
const run = mock(() => {});
|
|
100
118
|
const router = createRouter({ foo: { run, help: "" } });
|
package/src/routes/router.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { Command, ParsedRoute, RouteDef } from "../types";
|
|
2
2
|
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
|
|
3
5
|
import {
|
|
4
6
|
failureMessage,
|
|
5
7
|
isAllowLegacyReadFlag,
|
|
@@ -8,12 +10,35 @@ import {
|
|
|
8
10
|
quit,
|
|
9
11
|
setLegacyReadAllowed,
|
|
10
12
|
setOutputMode,
|
|
13
|
+
UsageError,
|
|
14
|
+
validateUniqueOptions,
|
|
11
15
|
} from "../lib";
|
|
12
16
|
|
|
13
17
|
function getIsRunnable(obj: any): obj is Command {
|
|
14
18
|
return Object.hasOwn(obj, "run");
|
|
15
19
|
}
|
|
16
20
|
|
|
21
|
+
function isRouterFlag(arg: string) {
|
|
22
|
+
return isJsonFlag(arg) || isAllowLegacyReadFlag(arg);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// --json and --allow-legacy-read can each be given once, like a command's own flags.
|
|
26
|
+
function validateRouterFlags(routableArgs: Array<string>) {
|
|
27
|
+
const { tokens } = parseArgs({
|
|
28
|
+
args: routableArgs.filter(isRouterFlag),
|
|
29
|
+
options: { json: { type: "boolean" }, "allow-legacy-read": { type: "boolean" } },
|
|
30
|
+
strict: true,
|
|
31
|
+
tokens: true,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
validateUniqueOptions(tokens);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The usage lines that open a command's help, up to its first blank line. */
|
|
38
|
+
function getUsage(help: string) {
|
|
39
|
+
return help.split("\n\n")[0]!;
|
|
40
|
+
}
|
|
41
|
+
|
|
17
42
|
function toCommand(args: Array<string>) {
|
|
18
43
|
return args.join(" ");
|
|
19
44
|
}
|
|
@@ -47,6 +72,28 @@ function formatCommands(parsedRoutes: Array<ParsedRoute>, prefix: string) {
|
|
|
47
72
|
.join("\n");
|
|
48
73
|
}
|
|
49
74
|
|
|
75
|
+
function getRootHelp(parsedRoutes: Array<ParsedRoute>) {
|
|
76
|
+
// TODO: maybe "brand" text that is exported and used here for title and desc
|
|
77
|
+
return `ZINN - A kanban workflow in the terminal
|
|
78
|
+
|
|
79
|
+
Usage: zinn <command>
|
|
80
|
+
|
|
81
|
+
Options:
|
|
82
|
+
-h, --help Show help
|
|
83
|
+
-v, --version Show version
|
|
84
|
+
--json Print command output as JSON
|
|
85
|
+
--allow-legacy-read Read a database without a schema version, best effort and read-only
|
|
86
|
+
|
|
87
|
+
Commands:
|
|
88
|
+
${formatCommands(parsedRoutes, "")}
|
|
89
|
+
|
|
90
|
+
Run zinn <namespace> --help or zinn <command> --help for more information.
|
|
91
|
+
--help shows help for the command words before it and runs nothing. The rest of the line is ignored.
|
|
92
|
+
-- 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').
|
|
93
|
+
Flags after it are not read.
|
|
94
|
+
Running zinn without a command shows this help.`;
|
|
95
|
+
}
|
|
96
|
+
|
|
50
97
|
function getNamespaceHelp(parsedRoutes: Array<ParsedRoute>, namespace: string) {
|
|
51
98
|
const commands = formatCommands(parsedRoutes, `${namespace} `);
|
|
52
99
|
|
|
@@ -57,6 +104,48 @@ function getNamespaceHelp(parsedRoutes: Array<ParsedRoute>, namespace: string) {
|
|
|
57
104
|
return `Usage: zinn ${namespace} <command>\n\nCommands:\n${commands}`;
|
|
58
105
|
}
|
|
59
106
|
|
|
107
|
+
// The longest leading run of words that names a namespace, or the root namespace "" when none does.
|
|
108
|
+
function findNearestNamespace(parsedRoutes: Array<ParsedRoute>, words: Array<string>) {
|
|
109
|
+
for (let endIndex = words.length; endIndex > 0; endIndex--) {
|
|
110
|
+
const namespace = toCommand(words.slice(0, endIndex));
|
|
111
|
+
const help = getNamespaceHelp(parsedRoutes, namespace);
|
|
112
|
+
|
|
113
|
+
if (help != null) {
|
|
114
|
+
return { namespace, wordCount: endIndex, help };
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return { namespace: "", wordCount: 0, help: getRootHelp(parsedRoutes) };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Prints help for a namespace when asked, or exits with an error for a missing or unknown command in it.
|
|
122
|
+
function routeToNamespace(
|
|
123
|
+
parsedRoutes: Array<ParsedRoute>,
|
|
124
|
+
words: Array<string>,
|
|
125
|
+
isHelpRequest: boolean,
|
|
126
|
+
) {
|
|
127
|
+
const { namespace, wordCount, help } = findNearestNamespace(parsedRoutes, words);
|
|
128
|
+
const unknownWord = words[wordCount];
|
|
129
|
+
const commandName = namespace === "" ? "zinn" : `zinn ${namespace}`;
|
|
130
|
+
|
|
131
|
+
if (unknownWord == null && isHelpRequest) {
|
|
132
|
+
console.info(help);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (unknownWord == null) {
|
|
137
|
+
quit(`Missing a command for "${commandName}"`, help);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const reason = `Unrecognized command "${unknownWord}" for "${commandName}"`;
|
|
141
|
+
|
|
142
|
+
if (isHelpRequest) {
|
|
143
|
+
quit(reason, help);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
quit(`${reason}. Run \`${commandName} --help\` for a list of available commands`);
|
|
147
|
+
}
|
|
148
|
+
|
|
60
149
|
export function parseRoutes(routesDef: RouteDef, prefix?: string) {
|
|
61
150
|
const routeDefinitions: Array<ParsedRoute> = [];
|
|
62
151
|
if (process.env.DEBUG) {
|
|
@@ -84,9 +173,7 @@ function route(parsedRoutes: Array<ParsedRoute>, rawArgs: Array<string>) {
|
|
|
84
173
|
const separatorIndex = rawArgs.indexOf("--");
|
|
85
174
|
const routableArgs = separatorIndex === -1 ? rawArgs : rawArgs.slice(0, separatorIndex);
|
|
86
175
|
const passthroughArgs = separatorIndex === -1 ? [] : rawArgs.slice(separatorIndex);
|
|
87
|
-
const filteredRoutableArgs = routableArgs.filter(
|
|
88
|
-
(arg) => !isJsonFlag(arg) && !isAllowLegacyReadFlag(arg),
|
|
89
|
-
);
|
|
176
|
+
const filteredRoutableArgs = routableArgs.filter((arg) => !isRouterFlag(arg));
|
|
90
177
|
|
|
91
178
|
if (routableArgs.some(isJsonFlag)) {
|
|
92
179
|
setOutputMode("json");
|
|
@@ -96,17 +183,12 @@ function route(parsedRoutes: Array<ParsedRoute>, rawArgs: Array<string>) {
|
|
|
96
183
|
setLegacyReadAllowed(true);
|
|
97
184
|
}
|
|
98
185
|
|
|
99
|
-
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
if (namespaceHelp != null) {
|
|
106
|
-
console.info(namespaceHelp);
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
186
|
+
// A help flag asks for help with the command words before it, so nothing after it is read.
|
|
187
|
+
const helpFlagIndex = filteredRoutableArgs.findIndex(isHelpFlag);
|
|
188
|
+
const isHelpRequest = helpFlagIndex !== -1;
|
|
189
|
+
const args = isHelpRequest
|
|
190
|
+
? filteredRoutableArgs.slice(0, helpFlagIndex)
|
|
191
|
+
: [...filteredRoutableArgs, ...passthroughArgs];
|
|
110
192
|
|
|
111
193
|
let match: ParsedRoute | null = null;
|
|
112
194
|
let nestingLevel = 0;
|
|
@@ -127,22 +209,26 @@ function route(parsedRoutes: Array<ParsedRoute>, rawArgs: Array<string>) {
|
|
|
127
209
|
}
|
|
128
210
|
|
|
129
211
|
if (match == null) {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
);
|
|
212
|
+
routeToNamespace(parsedRoutes, args, isHelpRequest);
|
|
213
|
+
return;
|
|
133
214
|
}
|
|
134
215
|
|
|
135
|
-
|
|
136
|
-
const isCommandHelpRequest = commandArgs.length === 1 && isHelpFlag(commandArgs[0]!);
|
|
137
|
-
|
|
138
|
-
if (isCommandHelpRequest) {
|
|
216
|
+
if (isHelpRequest) {
|
|
139
217
|
console.info(match.help);
|
|
140
218
|
return;
|
|
141
219
|
}
|
|
142
220
|
|
|
221
|
+
validateRouterFlags(routableArgs);
|
|
222
|
+
|
|
223
|
+
const commandArgs = args.slice(nestingLevel);
|
|
224
|
+
|
|
143
225
|
try {
|
|
144
226
|
match.run(commandArgs);
|
|
145
227
|
} catch (err: any) {
|
|
228
|
+
if (err instanceof UsageError) {
|
|
229
|
+
quit(err.message, getUsage(match.help));
|
|
230
|
+
}
|
|
231
|
+
|
|
146
232
|
quit(
|
|
147
233
|
failureMessage(
|
|
148
234
|
err,
|
|
@@ -157,6 +243,6 @@ export function createRouter(routesDef: RouteDef) {
|
|
|
157
243
|
|
|
158
244
|
return {
|
|
159
245
|
route: (args: Array<string>) => route(parsedRoutes, args),
|
|
160
|
-
|
|
246
|
+
help: getRootHelp(parsedRoutes),
|
|
161
247
|
};
|
|
162
248
|
}
|
package/src/routes/routes.ts
CHANGED
|
@@ -5,12 +5,14 @@ import { backupCommand } from "./backup-router";
|
|
|
5
5
|
import { restoreCommand } from "./restore-router";
|
|
6
6
|
import { migrateCommand } from "./migrate-router";
|
|
7
7
|
import { reindexCommand } from "./reindex-router";
|
|
8
|
+
import { activityRouter } from "./activity-router";
|
|
8
9
|
|
|
9
10
|
// TODO: lazy load?
|
|
10
11
|
export const routes = {
|
|
11
12
|
project: projectRouter,
|
|
12
13
|
task: taskRouter,
|
|
13
14
|
label: labelRouter,
|
|
15
|
+
activity: activityRouter,
|
|
14
16
|
backup: backupCommand,
|
|
15
17
|
restore: restoreCommand,
|
|
16
18
|
migrate: migrateCommand,
|