@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
@@ -0,0 +1,185 @@
1
+ import { parseArgs } from "node:util";
2
+ import { failureMessage, isAllowLegacyReadFlag, isHelpFlag, isJsonFlag, quit, setLegacyReadAllowed, setOutputMode, UsageError, validateUniqueOptions, } from "../lib.js";
3
+ function getIsRunnable(obj) {
4
+ return Object.hasOwn(obj, "run");
5
+ }
6
+ function isRouterFlag(arg) {
7
+ return isJsonFlag(arg) || isAllowLegacyReadFlag(arg);
8
+ }
9
+ // --json and --allow-legacy-read can each be given once, like a command's own flags.
10
+ function validateRouterFlags(routableArgs) {
11
+ const { tokens } = parseArgs({
12
+ args: routableArgs.filter(isRouterFlag),
13
+ options: { json: { type: "boolean" }, "allow-legacy-read": { type: "boolean" } },
14
+ strict: true,
15
+ tokens: true,
16
+ });
17
+ validateUniqueOptions(tokens);
18
+ }
19
+ /** The usage lines that open a command's help, up to its first blank line. */
20
+ function getUsage(help) {
21
+ return help.split("\n\n")[0];
22
+ }
23
+ function toCommand(args) {
24
+ return args.join(" ");
25
+ }
26
+ // Groups sibling commands into one line, e.g. " comment {create|list|edit|delete}".
27
+ // A command without siblings is not a choice, so it gets no braces, e.g. " backup".
28
+ // Top-level commands in the root help are unrelated to each other, so each gets its own line.
29
+ function formatCommands(parsedRoutes, prefix) {
30
+ const isRootHelp = prefix === "";
31
+ const siblingsByParent = new Map();
32
+ for (const { command } of parsedRoutes) {
33
+ if (!command.startsWith(prefix)) {
34
+ continue;
35
+ }
36
+ const words = command.slice(prefix.length).split(" ");
37
+ const parent = toCommand(words.slice(0, -1));
38
+ siblingsByParent.set(parent, [...(siblingsByParent.get(parent) ?? []), words.at(-1)]);
39
+ }
40
+ return [...siblingsByParent]
41
+ .flatMap(([parent, siblings]) => {
42
+ const isTopLevel = isRootHelp && parent === "";
43
+ if (isTopLevel) {
44
+ return siblings.map((command) => ` ${command}`);
45
+ }
46
+ const commands = siblings.length === 1 ? siblings[0] : `{${siblings.join("|")}}`;
47
+ return [` ${parent === "" ? "" : `${parent} `}${commands}`];
48
+ })
49
+ .join("\n");
50
+ }
51
+ function getRootHelp(parsedRoutes) {
52
+ // TODO: maybe "brand" text that is exported and used here for title and desc
53
+ return `ZINN - A kanban workflow in the terminal
54
+
55
+ Usage: zinn <command>
56
+
57
+ Options:
58
+ -h, --help Show help
59
+ -v, --version Show version
60
+ --json Print command output as JSON
61
+ --allow-legacy-read Read a database without a schema version, best effort and read-only
62
+
63
+ Commands:
64
+ ${formatCommands(parsedRoutes, "")}
65
+
66
+ Run zinn <namespace> --help or zinn <command> --help for more information.
67
+ --help shows help for the command words before it and runs nothing. The rest of the line is ignored.
68
+ -- 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').
69
+ Flags after it are not read.
70
+ Running zinn without a command shows this help.`;
71
+ }
72
+ function getNamespaceHelp(parsedRoutes, namespace) {
73
+ const commands = formatCommands(parsedRoutes, `${namespace} `);
74
+ if (commands === "") {
75
+ return null;
76
+ }
77
+ return `Usage: zinn ${namespace} <command>\n\nCommands:\n${commands}`;
78
+ }
79
+ // The longest leading run of words that names a namespace, or the root namespace "" when none does.
80
+ function findNearestNamespace(parsedRoutes, words) {
81
+ for (let endIndex = words.length; endIndex > 0; endIndex--) {
82
+ const namespace = toCommand(words.slice(0, endIndex));
83
+ const help = getNamespaceHelp(parsedRoutes, namespace);
84
+ if (help != null) {
85
+ return { namespace, wordCount: endIndex, help };
86
+ }
87
+ }
88
+ return { namespace: "", wordCount: 0, help: getRootHelp(parsedRoutes) };
89
+ }
90
+ // Prints help for a namespace when asked, or exits with an error for a missing or unknown command in it.
91
+ function routeToNamespace(parsedRoutes, words, isHelpRequest) {
92
+ const { namespace, wordCount, help } = findNearestNamespace(parsedRoutes, words);
93
+ const unknownWord = words[wordCount];
94
+ const commandName = namespace === "" ? "zinn" : `zinn ${namespace}`;
95
+ if (unknownWord == null && isHelpRequest) {
96
+ console.info(help);
97
+ return;
98
+ }
99
+ if (unknownWord == null) {
100
+ quit(`Missing a command for "${commandName}"`, help);
101
+ }
102
+ const reason = `Unrecognized command "${unknownWord}" for "${commandName}"`;
103
+ if (isHelpRequest) {
104
+ quit(reason, help);
105
+ }
106
+ quit(`${reason}. Run \`${commandName} --help\` for a list of available commands`);
107
+ }
108
+ export function parseRoutes(routesDef, prefix) {
109
+ const routeDefinitions = [];
110
+ if (process.env.DEBUG) {
111
+ console.debug(`::router.parseRoutes[${prefix ?? ""}]`, routesDef);
112
+ }
113
+ for (const key of Object.keys(routesDef)) {
114
+ const definition = routesDef[key];
115
+ const isRunnable = getIsRunnable(definition);
116
+ const combinedRoute = prefix ? `${prefix} ${key}` : key;
117
+ if (isRunnable) {
118
+ routeDefinitions.push({ command: combinedRoute, ...definition });
119
+ }
120
+ else {
121
+ const result = parseRoutes(definition, combinedRoute);
122
+ routeDefinitions.push(...result);
123
+ }
124
+ }
125
+ return routeDefinitions;
126
+ }
127
+ async function route(parsedRoutes, rawArgs) {
128
+ // Anything after "--" belongs to the command, so "--json" there is search text, not a flag.
129
+ const separatorIndex = rawArgs.indexOf("--");
130
+ const routableArgs = separatorIndex === -1 ? rawArgs : rawArgs.slice(0, separatorIndex);
131
+ const passthroughArgs = separatorIndex === -1 ? [] : rawArgs.slice(separatorIndex);
132
+ const filteredRoutableArgs = routableArgs.filter((arg) => !isRouterFlag(arg));
133
+ if (routableArgs.some(isJsonFlag)) {
134
+ setOutputMode("json");
135
+ }
136
+ if (routableArgs.some(isAllowLegacyReadFlag)) {
137
+ setLegacyReadAllowed(true);
138
+ }
139
+ // A help flag asks for help with the command words before it, so nothing after it is read.
140
+ const helpFlagIndex = filteredRoutableArgs.findIndex(isHelpFlag);
141
+ const isHelpRequest = helpFlagIndex !== -1;
142
+ const args = isHelpRequest
143
+ ? filteredRoutableArgs.slice(0, helpFlagIndex)
144
+ : [...filteredRoutableArgs, ...passthroughArgs];
145
+ let match = null;
146
+ let nestingLevel = 0;
147
+ for (let endIndex = args.length; endIndex >= 0; endIndex--) {
148
+ const command = toCommand(args.slice(0, endIndex));
149
+ const matchedRoute = parsedRoutes.find((r) => r.command === command);
150
+ if (matchedRoute) {
151
+ match = matchedRoute;
152
+ nestingLevel = endIndex;
153
+ break;
154
+ }
155
+ }
156
+ if (process.env.DEBUG) {
157
+ console.log("::router.route", { args, result: parsedRoutes.map((r) => r.command), match });
158
+ }
159
+ if (match == null) {
160
+ routeToNamespace(parsedRoutes, args, isHelpRequest);
161
+ return;
162
+ }
163
+ if (isHelpRequest) {
164
+ console.info(match.help);
165
+ return;
166
+ }
167
+ validateRouterFlags(routableArgs);
168
+ const commandArgs = args.slice(nestingLevel);
169
+ try {
170
+ await match.run(commandArgs);
171
+ }
172
+ catch (err) {
173
+ if (err instanceof UsageError) {
174
+ quit(err.message, getUsage(match.help));
175
+ }
176
+ quit(failureMessage(err, `Something went wrong while running "${match.command}" with args: ${commandArgs.join(",")}`));
177
+ }
178
+ }
179
+ export function createRouter(routesDef) {
180
+ const parsedRoutes = parseRoutes(routesDef);
181
+ return {
182
+ route: (args) => route(parsedRoutes, args),
183
+ help: getRootHelp(parsedRoutes),
184
+ };
185
+ }
@@ -0,0 +1,170 @@
1
+ export declare const routes: {
2
+ project: {
3
+ create: {
4
+ run: (args: Array<string>) => void;
5
+ help: string;
6
+ };
7
+ list: {
8
+ run: (args: Array<string>) => void;
9
+ help: string;
10
+ };
11
+ view: {
12
+ run: (args: Array<string>) => void;
13
+ help: string;
14
+ };
15
+ edit: {
16
+ run: (args: string[]) => void;
17
+ help: string;
18
+ };
19
+ delete: {
20
+ run: (args: Array<string>) => Promise<void>;
21
+ help: string;
22
+ };
23
+ column: {
24
+ create: {
25
+ run: (args: Array<string>) => void;
26
+ help: string;
27
+ };
28
+ list: {
29
+ run: (args: Array<string>) => void;
30
+ help: string;
31
+ };
32
+ edit: {
33
+ run: (args: Array<string>) => void;
34
+ help: string;
35
+ };
36
+ order: {
37
+ run: (args: Array<string>) => void;
38
+ help: string;
39
+ };
40
+ };
41
+ };
42
+ task: {
43
+ create: {
44
+ run: (args: string[]) => void;
45
+ help: string;
46
+ };
47
+ list: {
48
+ run: (args: string[]) => void;
49
+ help: string;
50
+ };
51
+ search: {
52
+ run: (args: string[]) => void;
53
+ help: string;
54
+ };
55
+ view: {
56
+ run: (args: string[]) => void;
57
+ help: string;
58
+ };
59
+ edit: {
60
+ run: (args: string[]) => void;
61
+ help: string;
62
+ };
63
+ move: {
64
+ run: (args: string[]) => void;
65
+ help: string;
66
+ };
67
+ order: {
68
+ run: (args: string[]) => void;
69
+ help: string;
70
+ };
71
+ archive: {
72
+ run: (args: Array<string>) => void;
73
+ help: string;
74
+ };
75
+ unarchive: {
76
+ run: (args: Array<string>) => void;
77
+ help: string;
78
+ };
79
+ delete: {
80
+ run: (args: Array<string>) => Promise<void>;
81
+ help: string;
82
+ };
83
+ relation: {
84
+ create: {
85
+ run: (args: string[]) => void;
86
+ help: string;
87
+ };
88
+ list: {
89
+ run: (args: string[]) => void;
90
+ help: string;
91
+ };
92
+ delete: {
93
+ run: (args: string[]) => void;
94
+ help: string;
95
+ };
96
+ };
97
+ comment: {
98
+ create: {
99
+ run: (args: string[]) => void;
100
+ help: string;
101
+ };
102
+ list: {
103
+ run: (args: string[]) => void;
104
+ help: string;
105
+ };
106
+ edit: {
107
+ run: (args: string[]) => void;
108
+ help: string;
109
+ };
110
+ delete: {
111
+ run: (args: string[]) => Promise<void>;
112
+ help: string;
113
+ };
114
+ };
115
+ label: {
116
+ add: {
117
+ run: (args: string[]) => void;
118
+ help: string;
119
+ };
120
+ remove: {
121
+ run: (args: string[]) => void;
122
+ help: string;
123
+ };
124
+ };
125
+ };
126
+ label: {
127
+ create: {
128
+ run: (args: string[]) => void;
129
+ help: string;
130
+ };
131
+ list: {
132
+ run: (args: string[]) => void;
133
+ help: string;
134
+ };
135
+ edit: {
136
+ run: (args: string[]) => void;
137
+ help: string;
138
+ };
139
+ delete: {
140
+ run: (args: string[]) => Promise<void>;
141
+ help: string;
142
+ };
143
+ };
144
+ activity: {
145
+ list: {
146
+ run: (args: string[]) => void;
147
+ help: string;
148
+ };
149
+ view: {
150
+ run: (args: string[]) => void;
151
+ help: string;
152
+ };
153
+ };
154
+ backup: {
155
+ run: (args: string[]) => void;
156
+ help: string;
157
+ };
158
+ restore: {
159
+ run: (args: string[]) => Promise<void>;
160
+ help: string;
161
+ };
162
+ migrate: {
163
+ run: (args: string[]) => void;
164
+ help: string;
165
+ };
166
+ reindex: {
167
+ run: (args: string[]) => void;
168
+ help: string;
169
+ };
170
+ };
@@ -0,0 +1,19 @@
1
+ import { projectRouter } from "./project-router.js";
2
+ import { taskRouter } from "./task-router.js";
3
+ import { labelRouter } from "./label-router.js";
4
+ import { backupCommand } from "./backup-router.js";
5
+ import { restoreCommand } from "./restore-router.js";
6
+ import { migrateCommand } from "./migrate-router.js";
7
+ import { reindexCommand } from "./reindex-router.js";
8
+ import { activityRouter } from "./activity-router.js";
9
+ // TODO: lazy load?
10
+ export const routes = {
11
+ project: projectRouter,
12
+ task: taskRouter,
13
+ label: labelRouter,
14
+ activity: activityRouter,
15
+ backup: backupCommand,
16
+ restore: restoreCommand,
17
+ migrate: migrateCommand,
18
+ reindex: reindexCommand,
19
+ };
@@ -0,0 +1,84 @@
1
+ export declare const taskRouter: {
2
+ create: {
3
+ run: (args: string[]) => void;
4
+ help: string;
5
+ };
6
+ list: {
7
+ run: (args: string[]) => void;
8
+ help: string;
9
+ };
10
+ search: {
11
+ run: (args: string[]) => void;
12
+ help: string;
13
+ };
14
+ view: {
15
+ run: (args: string[]) => void;
16
+ help: string;
17
+ };
18
+ edit: {
19
+ run: (args: string[]) => void;
20
+ help: string;
21
+ };
22
+ move: {
23
+ run: (args: string[]) => void;
24
+ help: string;
25
+ };
26
+ order: {
27
+ run: (args: string[]) => void;
28
+ help: string;
29
+ };
30
+ archive: {
31
+ run: (args: Array<string>) => void;
32
+ help: string;
33
+ };
34
+ unarchive: {
35
+ run: (args: Array<string>) => void;
36
+ help: string;
37
+ };
38
+ delete: {
39
+ run: (args: Array<string>) => Promise<void>;
40
+ help: string;
41
+ };
42
+ relation: {
43
+ create: {
44
+ run: (args: string[]) => void;
45
+ help: string;
46
+ };
47
+ list: {
48
+ run: (args: string[]) => void;
49
+ help: string;
50
+ };
51
+ delete: {
52
+ run: (args: string[]) => void;
53
+ help: string;
54
+ };
55
+ };
56
+ comment: {
57
+ create: {
58
+ run: (args: string[]) => void;
59
+ help: string;
60
+ };
61
+ list: {
62
+ run: (args: string[]) => void;
63
+ help: string;
64
+ };
65
+ edit: {
66
+ run: (args: string[]) => void;
67
+ help: string;
68
+ };
69
+ delete: {
70
+ run: (args: string[]) => Promise<void>;
71
+ help: string;
72
+ };
73
+ };
74
+ label: {
75
+ add: {
76
+ run: (args: string[]) => void;
77
+ help: string;
78
+ };
79
+ remove: {
80
+ run: (args: string[]) => void;
81
+ help: string;
82
+ };
83
+ };
84
+ };