@zinn-dev/cli 0.36.1 → 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 +264 -78
- package/index.ts +7 -15
- package/package.json +7 -5
- package/release-stamp.json +1 -0
- package/src/database.test.ts +70 -0
- package/src/database.ts +41 -0
- package/src/lib.test.ts +86 -0
- package/src/lib.ts +75 -10
- package/src/presentation/activity-presentation.test.ts +71 -0
- package/src/presentation/activity-presentation.ts +489 -0
- package/src/presentation/color.ts +1 -1
- package/src/presentation/column-presentation.ts +48 -0
- package/src/presentation/description-presentation.ts +7 -4
- package/src/presentation/row-layout.ts +77 -0
- package/src/presentation/section.ts +16 -0
- package/src/presentation/task-presentation.ts +50 -63
- 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 +54 -38
- package/src/routes/label-router.ts +47 -27
- package/src/routes/migrate-router.test.ts +31 -0
- package/src/routes/migrate-router.ts +48 -0
- package/src/routes/project-router.ts +129 -106
- package/src/routes/reindex-router.ts +27 -0
- package/src/routes/relation-router.ts +20 -13
- package/src/routes/restore-router.test.ts +44 -0
- package/src/routes/restore-router.ts +90 -0
- package/src/routes/router.test.ts +41 -0
- package/src/routes/router.ts +133 -24
- package/src/routes/routes.ts +8 -0
- package/src/routes/task-router.ts +228 -142
- package/src/text-edit.ts +68 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Command, RouteDef } from "../types";
|
|
2
2
|
|
|
3
3
|
import { test, expect, mock, spyOn } from "bun:test";
|
|
4
|
+
import { isLegacyReadAllowed, setLegacyReadAllowed } from "../lib";
|
|
4
5
|
import { createRouter, parseRoutes } from "./router";
|
|
5
6
|
|
|
6
7
|
const mockCommand: Command = { run: mock(() => {}), help: `` };
|
|
@@ -93,3 +94,43 @@ test("a help request never runs a command even when its help is empty", () => {
|
|
|
93
94
|
info.mockRestore();
|
|
94
95
|
}
|
|
95
96
|
});
|
|
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
|
+
|
|
116
|
+
test("--allow-legacy-read before a -- is taken out of the arguments and allows legacy reads", () => {
|
|
117
|
+
const run = mock(() => {});
|
|
118
|
+
const router = createRouter({ foo: { run, help: "" } });
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
router.route(["foo", "--allow-legacy-read", "--", "literal"]);
|
|
122
|
+
expect(run).toHaveBeenCalledWith(["--", "literal"]);
|
|
123
|
+
expect(isLegacyReadAllowed()).toBe(true);
|
|
124
|
+
} finally {
|
|
125
|
+
setLegacyReadAllowed(false);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("--allow-legacy-read after a -- stays command input and allows nothing", () => {
|
|
130
|
+
const run = mock(() => {});
|
|
131
|
+
const router = createRouter({ foo: { run, help: "" } });
|
|
132
|
+
|
|
133
|
+
router.route(["foo", "--", "--allow-legacy-read"]);
|
|
134
|
+
expect(run).toHaveBeenCalledWith(["--", "--allow-legacy-read"]);
|
|
135
|
+
expect(isLegacyReadAllowed()).toBe(false);
|
|
136
|
+
});
|
package/src/routes/router.ts
CHANGED
|
@@ -1,18 +1,53 @@
|
|
|
1
1
|
import type { Command, ParsedRoute, RouteDef } from "../types";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
failureMessage,
|
|
7
|
+
isAllowLegacyReadFlag,
|
|
8
|
+
isHelpFlag,
|
|
9
|
+
isJsonFlag,
|
|
10
|
+
quit,
|
|
11
|
+
setLegacyReadAllowed,
|
|
12
|
+
setOutputMode,
|
|
13
|
+
UsageError,
|
|
14
|
+
validateUniqueOptions,
|
|
15
|
+
} from "../lib";
|
|
4
16
|
|
|
5
17
|
function getIsRunnable(obj: any): obj is Command {
|
|
6
18
|
return Object.hasOwn(obj, "run");
|
|
7
19
|
}
|
|
8
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
|
+
|
|
9
42
|
function toCommand(args: Array<string>) {
|
|
10
43
|
return args.join(" ");
|
|
11
44
|
}
|
|
12
45
|
|
|
13
46
|
// Groups sibling commands into one line, e.g. " comment {create|list|edit|delete}".
|
|
14
47
|
// A command without siblings is not a choice, so it gets no braces, e.g. " backup".
|
|
48
|
+
// Top-level commands in the root help are unrelated to each other, so each gets its own line.
|
|
15
49
|
function formatCommands(parsedRoutes: Array<ParsedRoute>, prefix: string) {
|
|
50
|
+
const isRootHelp = prefix === "";
|
|
16
51
|
const siblingsByParent = new Map<string, Array<string>>();
|
|
17
52
|
for (const { command } of parsedRoutes) {
|
|
18
53
|
if (!command.startsWith(prefix)) {
|
|
@@ -25,13 +60,40 @@ function formatCommands(parsedRoutes: Array<ParsedRoute>, prefix: string) {
|
|
|
25
60
|
}
|
|
26
61
|
|
|
27
62
|
return [...siblingsByParent]
|
|
28
|
-
.
|
|
63
|
+
.flatMap(([parent, siblings]) => {
|
|
64
|
+
const isTopLevel = isRootHelp && parent === "";
|
|
65
|
+
if (isTopLevel) {
|
|
66
|
+
return siblings.map((command) => ` ${command}`);
|
|
67
|
+
}
|
|
68
|
+
|
|
29
69
|
const commands = siblings.length === 1 ? siblings[0] : `{${siblings.join("|")}}`;
|
|
30
|
-
return ` ${parent === "" ? "" : `${parent} `}${commands}
|
|
70
|
+
return [` ${parent === "" ? "" : `${parent} `}${commands}`];
|
|
31
71
|
})
|
|
32
72
|
.join("\n");
|
|
33
73
|
}
|
|
34
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
|
+
|
|
35
97
|
function getNamespaceHelp(parsedRoutes: Array<ParsedRoute>, namespace: string) {
|
|
36
98
|
const commands = formatCommands(parsedRoutes, `${namespace} `);
|
|
37
99
|
|
|
@@ -42,6 +104,48 @@ function getNamespaceHelp(parsedRoutes: Array<ParsedRoute>, namespace: string) {
|
|
|
42
104
|
return `Usage: zinn ${namespace} <command>\n\nCommands:\n${commands}`;
|
|
43
105
|
}
|
|
44
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
|
+
|
|
45
149
|
export function parseRoutes(routesDef: RouteDef, prefix?: string) {
|
|
46
150
|
const routeDefinitions: Array<ParsedRoute> = [];
|
|
47
151
|
if (process.env.DEBUG) {
|
|
@@ -69,24 +173,23 @@ function route(parsedRoutes: Array<ParsedRoute>, rawArgs: Array<string>) {
|
|
|
69
173
|
const separatorIndex = rawArgs.indexOf("--");
|
|
70
174
|
const routableArgs = separatorIndex === -1 ? rawArgs : rawArgs.slice(0, separatorIndex);
|
|
71
175
|
const passthroughArgs = separatorIndex === -1 ? [] : rawArgs.slice(separatorIndex);
|
|
72
|
-
const filteredRoutableArgs = routableArgs.filter((arg) => !
|
|
176
|
+
const filteredRoutableArgs = routableArgs.filter((arg) => !isRouterFlag(arg));
|
|
73
177
|
|
|
74
|
-
if (
|
|
178
|
+
if (routableArgs.some(isJsonFlag)) {
|
|
75
179
|
setOutputMode("json");
|
|
76
180
|
}
|
|
77
181
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
if (isHelpRequest && args.length > 1) {
|
|
82
|
-
const namespace = toCommand(filteredRoutableArgs.slice(0, -1));
|
|
83
|
-
const namespaceHelp = getNamespaceHelp(parsedRoutes, namespace);
|
|
84
|
-
if (namespaceHelp != null) {
|
|
85
|
-
console.info(namespaceHelp);
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
182
|
+
if (routableArgs.some(isAllowLegacyReadFlag)) {
|
|
183
|
+
setLegacyReadAllowed(true);
|
|
88
184
|
}
|
|
89
185
|
|
|
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];
|
|
192
|
+
|
|
90
193
|
let match: ParsedRoute | null = null;
|
|
91
194
|
let nestingLevel = 0;
|
|
92
195
|
|
|
@@ -106,25 +209,31 @@ function route(parsedRoutes: Array<ParsedRoute>, rawArgs: Array<string>) {
|
|
|
106
209
|
}
|
|
107
210
|
|
|
108
211
|
if (match == null) {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
);
|
|
212
|
+
routeToNamespace(parsedRoutes, args, isHelpRequest);
|
|
213
|
+
return;
|
|
112
214
|
}
|
|
113
215
|
|
|
114
|
-
|
|
115
|
-
const isCommandHelpRequest = commandArgs.length === 1 && isHelpFlag(commandArgs[0]!);
|
|
116
|
-
|
|
117
|
-
if (isCommandHelpRequest) {
|
|
216
|
+
if (isHelpRequest) {
|
|
118
217
|
console.info(match.help);
|
|
119
218
|
return;
|
|
120
219
|
}
|
|
121
220
|
|
|
221
|
+
validateRouterFlags(routableArgs);
|
|
222
|
+
|
|
223
|
+
const commandArgs = args.slice(nestingLevel);
|
|
224
|
+
|
|
122
225
|
try {
|
|
123
226
|
match.run(commandArgs);
|
|
124
227
|
} catch (err: any) {
|
|
228
|
+
if (err instanceof UsageError) {
|
|
229
|
+
quit(err.message, getUsage(match.help));
|
|
230
|
+
}
|
|
231
|
+
|
|
125
232
|
quit(
|
|
126
|
-
|
|
233
|
+
failureMessage(
|
|
234
|
+
err,
|
|
127
235
|
`Something went wrong while running "${match.command}" with args: ${commandArgs.join(",")}`,
|
|
236
|
+
),
|
|
128
237
|
);
|
|
129
238
|
}
|
|
130
239
|
}
|
|
@@ -134,6 +243,6 @@ export function createRouter(routesDef: RouteDef) {
|
|
|
134
243
|
|
|
135
244
|
return {
|
|
136
245
|
route: (args: Array<string>) => route(parsedRoutes, args),
|
|
137
|
-
|
|
246
|
+
help: getRootHelp(parsedRoutes),
|
|
138
247
|
};
|
|
139
248
|
}
|
package/src/routes/routes.ts
CHANGED
|
@@ -2,11 +2,19 @@ import { projectRouter } from "./project-router";
|
|
|
2
2
|
import { taskRouter } from "./task-router";
|
|
3
3
|
import { labelRouter } from "./label-router";
|
|
4
4
|
import { backupCommand } from "./backup-router";
|
|
5
|
+
import { restoreCommand } from "./restore-router";
|
|
6
|
+
import { migrateCommand } from "./migrate-router";
|
|
7
|
+
import { reindexCommand } from "./reindex-router";
|
|
8
|
+
import { activityRouter } from "./activity-router";
|
|
5
9
|
|
|
6
10
|
// TODO: lazy load?
|
|
7
11
|
export const routes = {
|
|
8
12
|
project: projectRouter,
|
|
9
13
|
task: taskRouter,
|
|
10
14
|
label: labelRouter,
|
|
15
|
+
activity: activityRouter,
|
|
11
16
|
backup: backupCommand,
|
|
17
|
+
restore: restoreCommand,
|
|
18
|
+
migrate: migrateCommand,
|
|
19
|
+
reindex: reindexCommand,
|
|
12
20
|
};
|