@zinn-dev/cli 0.38.0 → 0.40.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.
Files changed (92) hide show
  1. package/README.md +47 -9
  2. package/index.d.ts +2 -0
  3. package/index.js +23 -0
  4. package/package.json +22 -18
  5. package/src/actor.d.ts +6 -0
  6. package/src/actor.js +62 -0
  7. package/src/database.d.ts +6 -0
  8. package/src/database.js +27 -0
  9. package/src/lib.d.ts +31 -0
  10. package/src/lib.js +137 -0
  11. package/src/presentation/activity-presentation.d.ts +23 -0
  12. package/src/presentation/activity-presentation.js +389 -0
  13. package/src/presentation/color.d.ts +9 -0
  14. package/src/presentation/color.js +21 -0
  15. package/src/presentation/column-presentation.d.ts +16 -0
  16. package/src/presentation/column-presentation.js +29 -0
  17. package/src/presentation/description-presentation.d.ts +3 -0
  18. package/src/presentation/description-presentation.js +11 -0
  19. package/src/presentation/row-layout.d.ts +22 -0
  20. package/src/presentation/row-layout.js +58 -0
  21. package/src/presentation/section.d.ts +4 -0
  22. package/src/presentation/section.js +9 -0
  23. package/src/presentation/task-presentation.d.ts +17 -0
  24. package/src/presentation/task-presentation.js +62 -0
  25. package/src/presentation/time.d.ts +1 -0
  26. package/src/presentation/time.js +13 -0
  27. package/src/project.d.ts +6 -0
  28. package/src/project.js +31 -0
  29. package/src/release-identity.d.ts +2 -0
  30. package/src/release-identity.js +1 -0
  31. package/src/routes/activity-router.d.ts +10 -0
  32. package/src/routes/activity-router.js +71 -0
  33. package/src/routes/backup-router.d.ts +4 -0
  34. package/src/routes/backup-router.js +18 -0
  35. package/src/routes/comment-router.d.ts +24 -0
  36. package/src/routes/comment-router.js +134 -0
  37. package/src/routes/label-router.d.ts +18 -0
  38. package/src/routes/label-router.js +136 -0
  39. package/src/routes/migrate-router.d.ts +4 -0
  40. package/src/routes/migrate-router.js +37 -0
  41. package/src/routes/project-router.d.ts +40 -0
  42. package/src/routes/project-router.js +328 -0
  43. package/src/routes/reindex-router.d.ts +4 -0
  44. package/src/routes/reindex-router.js +21 -0
  45. package/src/routes/relation-router.d.ts +28 -0
  46. package/src/routes/relation-router.js +163 -0
  47. package/src/routes/restore-router.d.ts +4 -0
  48. package/src/routes/restore-router.js +71 -0
  49. package/src/routes/router.d.ts +6 -0
  50. package/src/routes/router.js +185 -0
  51. package/src/routes/routes.d.ts +170 -0
  52. package/src/routes/routes.js +19 -0
  53. package/src/routes/task-router.d.ts +84 -0
  54. package/src/routes/task-router.js +533 -0
  55. package/src/text-edit.d.ts +25 -0
  56. package/src/text-edit.js +45 -0
  57. package/src/types.d.ts +10 -0
  58. package/src/types.js +1 -0
  59. package/index.ts +0 -26
  60. package/release-stamp.json +0 -1
  61. package/src/actor.test.ts +0 -41
  62. package/src/actor.ts +0 -78
  63. package/src/database.test.ts +0 -70
  64. package/src/database.ts +0 -41
  65. package/src/lib.test.ts +0 -86
  66. package/src/lib.ts +0 -145
  67. package/src/presentation/activity-presentation.test.ts +0 -71
  68. package/src/presentation/activity-presentation.ts +0 -489
  69. package/src/presentation/color.ts +0 -29
  70. package/src/presentation/column-presentation.ts +0 -48
  71. package/src/presentation/description-presentation.ts +0 -18
  72. package/src/presentation/row-layout.ts +0 -77
  73. package/src/presentation/section.ts +0 -16
  74. package/src/presentation/task-presentation.ts +0 -92
  75. package/src/presentation/time.ts +0 -17
  76. package/src/routes/activity-router.ts +0 -77
  77. package/src/routes/backup-router.ts +0 -24
  78. package/src/routes/comment-router.ts +0 -171
  79. package/src/routes/label-router.ts +0 -168
  80. package/src/routes/migrate-router.test.ts +0 -31
  81. package/src/routes/migrate-router.ts +0 -48
  82. package/src/routes/project-router.ts +0 -387
  83. package/src/routes/reindex-router.ts +0 -27
  84. package/src/routes/relation-router.ts +0 -194
  85. package/src/routes/restore-router.test.ts +0 -44
  86. package/src/routes/restore-router.ts +0 -90
  87. package/src/routes/router.test.ts +0 -136
  88. package/src/routes/router.ts +0 -248
  89. package/src/routes/routes.ts +0 -20
  90. package/src/routes/task-router.ts +0 -594
  91. package/src/text-edit.ts +0 -68
  92. package/src/types.ts +0 -6
@@ -1,90 +0,0 @@
1
- import type { Command } from "../types";
2
-
3
- import { parseArgs } from "node:util";
4
- import { resolve } from "node:path";
5
- import { backup, RestoreRefusedError, getStorage } from "@zinn-dev/core";
6
-
7
- import { reportLockWait } from "../database";
8
- import { askConfirmation, output, validatePositionals, validateUniqueOptions } from "../lib";
9
- import packageJson from "../../package.json";
10
-
11
- const build = { client: "cli", version: packageJson.version };
12
-
13
- export const restoreCommand = {
14
- run: (args) => {
15
- const { positionals, values, tokens } = parseArgs({
16
- args,
17
- strict: true,
18
- allowPositionals: true,
19
- tokens: true,
20
- options: { yes: { type: "boolean" } },
21
- });
22
- validateUniqueOptions(tokens);
23
- validatePositionals(positionals, ["<path>"]);
24
-
25
- const path = resolve(positionals[0]!);
26
- const { databasePath, backupDirectory } = getStorage();
27
- const isPreconfirmed = values.yes === true;
28
-
29
- // A backup that would be refused is refused before the prompt.
30
- if (!isPreconfirmed) {
31
- backup.check({ ...build, path });
32
- }
33
-
34
- const isConfirmed = askConfirmation(
35
- `Replace the database at ${databasePath} with the backup ${path}? Its current board is saved to ${backupDirectory} first.`,
36
- isPreconfirmed,
37
- );
38
-
39
- if (!isConfirmed) {
40
- output(null, () => `Restore cancelled. The database at ${databasePath} is unchanged`);
41
- return;
42
- }
43
-
44
- try {
45
- const result = backup.restore({ ...build, path, onWait: reportLockWait });
46
-
47
- output(result, (r) => {
48
- const restored = `Restored the board from ${path} at schema version ${r.restoredVersion}.`;
49
- return r.backupPath == null
50
- ? `${restored} There was no board to save`
51
- : `${restored} The board it replaced was saved to ${r.backupPath}`;
52
- });
53
- } catch (error) {
54
- // A refusal attempted nothing, so only a failed restore points at the backups.
55
- if (!(error instanceof RestoreRefusedError)) {
56
- const isDamagedDatabase =
57
- error instanceof Error &&
58
- "code" in error &&
59
- typeof error.code === "string" &&
60
- error.code.startsWith("SQLITE_CORRUPT");
61
-
62
- if (isDamagedDatabase) {
63
- console.error(
64
- `The database at ${databasePath} is damaged. Nothing was changed. To restore it:\n` +
65
- `1. Close every program that has the database open.\n` +
66
- `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` +
67
- `3. Run the same zinn restore command again to create the database from the backup.`,
68
- );
69
- } else {
70
- console.error(
71
- `The database is unchanged. Backups taken before each restore attempt are kept in ${backupDirectory}.`,
72
- );
73
- }
74
- }
75
-
76
- throw error;
77
- }
78
- },
79
- help: `Usage: zinn restore <path> [--yes]
80
-
81
- Replace the board with the one in a backup file, such as one saved by \`zinn backup\` or by an upgrade, after confirmation.
82
-
83
- The current board is saved first, in the same place as \`zinn backup\`, and the restore is applied completely or not at all.
84
- 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\`.
85
- Restoring onto a missing database creates it.
86
- 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.
87
- Use --yes to skip the confirmation prompt, for example in scripts or with --json.
88
-
89
- Example: zinn restore ~/.zinn/backups/2026-10-06T12-30-05Z-cli-0.40.0-1a2b3c4d.sqlite`,
90
- } satisfies Command;
@@ -1,136 +0,0 @@
1
- import type { Command, RouteDef } from "../types";
2
-
3
- import { test, expect, mock, spyOn } from "bun:test";
4
- import { isLegacyReadAllowed, setLegacyReadAllowed } from "../lib";
5
- import { createRouter, parseRoutes } from "./router";
6
-
7
- const mockCommand: Command = { run: mock(() => {}), help: `` };
8
-
9
- test("command nesting is properly parsed", () => {
10
- const routes: RouteDef = {
11
- foo: {
12
- create: mockCommand,
13
- bar: {
14
- create: mockCommand,
15
- baz: {
16
- create: mockCommand,
17
- foo: {
18
- create: mockCommand,
19
- },
20
- },
21
- },
22
- },
23
- };
24
- expect(parseRoutes(routes).map((r) => r.command)).toEqual([
25
- "foo create",
26
- "foo bar create",
27
- "foo bar baz create",
28
- "foo bar baz foo create",
29
- ]);
30
- });
31
-
32
- test("router calls the run functions", () => {
33
- const routes: RouteDef = {
34
- foo: {
35
- create: mockCommand,
36
- bar: {
37
- create: mockCommand,
38
- },
39
- },
40
- };
41
-
42
- let router = createRouter(routes);
43
- router.route(["foo", "create"]);
44
- expect(mockCommand.run).toHaveBeenCalledTimes(1);
45
-
46
- router.route(["foo", "bar", "create"]);
47
- expect(mockCommand.run).toHaveBeenCalledTimes(2);
48
- });
49
-
50
- test("router calls the deepest nesting command", () => {
51
- const fooCreate = mock(() => {});
52
- const barCreate = mock(() => {});
53
- const routes = {
54
- foo: {
55
- create: { run: fooCreate, help: "" },
56
- bar: {
57
- create: { run: barCreate, help: "" },
58
- },
59
- },
60
- } satisfies RouteDef;
61
-
62
- let router = createRouter(routes);
63
- router.route(["foo", "bar", "create"]);
64
- expect(routes.foo.bar.create.run).toHaveBeenCalledTimes(1);
65
- expect(routes.foo.create.run).not.toHaveBeenCalled();
66
- });
67
-
68
- test("only arguments before the first -- are treated as router flags", () => {
69
- const run = mock(() => {});
70
- const router = createRouter({ foo: { run, help: "" } });
71
-
72
- router.route(["foo", "--", "--json", "--help"]);
73
- expect(run).toHaveBeenCalledWith(["--", "--json", "--help"]);
74
- });
75
-
76
- test("router flags before a -- still apply", () => {
77
- const run = mock(() => {});
78
- const router = createRouter({ foo: { run, help: "" } });
79
-
80
- router.route(["foo", "--json", "--", "literal"]);
81
- expect(run).toHaveBeenCalledWith(["--", "literal"]);
82
- });
83
-
84
- test("a help request never runs a command even when its help is empty", () => {
85
- const run = mock(() => {});
86
- const info = spyOn(console, "info").mockImplementation(() => {});
87
- const router = createRouter({ foo: { run, help: "" } });
88
-
89
- try {
90
- router.route(["foo", "--help"]);
91
- expect(run).not.toHaveBeenCalled();
92
- expect(info).toHaveBeenCalledWith("");
93
- } finally {
94
- info.mockRestore();
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
- });
@@ -1,248 +0,0 @@
1
- import type { Command, ParsedRoute, RouteDef } from "../types";
2
-
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";
16
-
17
- function getIsRunnable(obj: any): obj is Command {
18
- return Object.hasOwn(obj, "run");
19
- }
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
-
42
- function toCommand(args: Array<string>) {
43
- return args.join(" ");
44
- }
45
-
46
- // Groups sibling commands into one line, e.g. " comment {create|list|edit|delete}".
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.
49
- function formatCommands(parsedRoutes: Array<ParsedRoute>, prefix: string) {
50
- const isRootHelp = prefix === "";
51
- const siblingsByParent = new Map<string, Array<string>>();
52
- for (const { command } of parsedRoutes) {
53
- if (!command.startsWith(prefix)) {
54
- continue;
55
- }
56
-
57
- const words = command.slice(prefix.length).split(" ");
58
- const parent = toCommand(words.slice(0, -1));
59
- siblingsByParent.set(parent, [...(siblingsByParent.get(parent) ?? []), words.at(-1)!]);
60
- }
61
-
62
- return [...siblingsByParent]
63
- .flatMap(([parent, siblings]) => {
64
- const isTopLevel = isRootHelp && parent === "";
65
- if (isTopLevel) {
66
- return siblings.map((command) => ` ${command}`);
67
- }
68
-
69
- const commands = siblings.length === 1 ? siblings[0] : `{${siblings.join("|")}}`;
70
- return [` ${parent === "" ? "" : `${parent} `}${commands}`];
71
- })
72
- .join("\n");
73
- }
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
-
97
- function getNamespaceHelp(parsedRoutes: Array<ParsedRoute>, namespace: string) {
98
- const commands = formatCommands(parsedRoutes, `${namespace} `);
99
-
100
- if (commands === "") {
101
- return null;
102
- }
103
-
104
- return `Usage: zinn ${namespace} <command>\n\nCommands:\n${commands}`;
105
- }
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
-
149
- export function parseRoutes(routesDef: RouteDef, prefix?: string) {
150
- const routeDefinitions: Array<ParsedRoute> = [];
151
- if (process.env.DEBUG) {
152
- console.debug(`::router.parseRoutes[${prefix ?? ""}]`, routesDef);
153
- }
154
-
155
- for (const key of Object.keys(routesDef)) {
156
- const definition = routesDef[key as keyof typeof routesDef]!;
157
-
158
- const isRunnable = getIsRunnable(definition);
159
- const combinedRoute = prefix ? `${prefix} ${key}` : key;
160
- if (isRunnable) {
161
- routeDefinitions.push({ command: combinedRoute, ...definition });
162
- } else {
163
- const result = parseRoutes(definition, combinedRoute);
164
- routeDefinitions.push(...result);
165
- }
166
- }
167
-
168
- return routeDefinitions;
169
- }
170
-
171
- function route(parsedRoutes: Array<ParsedRoute>, rawArgs: Array<string>) {
172
- // Anything after "--" belongs to the command, so "--json" there is search text, not a flag.
173
- const separatorIndex = rawArgs.indexOf("--");
174
- const routableArgs = separatorIndex === -1 ? rawArgs : rawArgs.slice(0, separatorIndex);
175
- const passthroughArgs = separatorIndex === -1 ? [] : rawArgs.slice(separatorIndex);
176
- const filteredRoutableArgs = routableArgs.filter((arg) => !isRouterFlag(arg));
177
-
178
- if (routableArgs.some(isJsonFlag)) {
179
- setOutputMode("json");
180
- }
181
-
182
- if (routableArgs.some(isAllowLegacyReadFlag)) {
183
- setLegacyReadAllowed(true);
184
- }
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
-
193
- let match: ParsedRoute | null = null;
194
- let nestingLevel = 0;
195
-
196
- for (let endIndex = args.length; endIndex >= 0; endIndex--) {
197
- const command = toCommand(args.slice(0, endIndex));
198
- const matchedRoute = parsedRoutes.find((r) => r.command === command);
199
-
200
- if (matchedRoute) {
201
- match = matchedRoute;
202
- nestingLevel = endIndex;
203
- break;
204
- }
205
- }
206
-
207
- if (process.env.DEBUG) {
208
- console.log("::router.route", { args, result: parsedRoutes.map((r) => r.command), match });
209
- }
210
-
211
- if (match == null) {
212
- routeToNamespace(parsedRoutes, args, isHelpRequest);
213
- return;
214
- }
215
-
216
- if (isHelpRequest) {
217
- console.info(match.help);
218
- return;
219
- }
220
-
221
- validateRouterFlags(routableArgs);
222
-
223
- const commandArgs = args.slice(nestingLevel);
224
-
225
- try {
226
- match.run(commandArgs);
227
- } catch (err: any) {
228
- if (err instanceof UsageError) {
229
- quit(err.message, getUsage(match.help));
230
- }
231
-
232
- quit(
233
- failureMessage(
234
- err,
235
- `Something went wrong while running "${match.command}" with args: ${commandArgs.join(",")}`,
236
- ),
237
- );
238
- }
239
- }
240
-
241
- export function createRouter(routesDef: RouteDef) {
242
- const parsedRoutes = parseRoutes(routesDef);
243
-
244
- return {
245
- route: (args: Array<string>) => route(parsedRoutes, args),
246
- help: getRootHelp(parsedRoutes),
247
- };
248
- }
@@ -1,20 +0,0 @@
1
- import { projectRouter } from "./project-router";
2
- import { taskRouter } from "./task-router";
3
- import { labelRouter } from "./label-router";
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";
9
-
10
- // TODO: lazy load?
11
- export const routes = {
12
- project: projectRouter,
13
- task: taskRouter,
14
- label: labelRouter,
15
- activity: activityRouter,
16
- backup: backupCommand,
17
- restore: restoreCommand,
18
- migrate: migrateCommand,
19
- reindex: reindexCommand,
20
- };