@zinn-dev/cli 0.40.0 → 0.42.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 (84) hide show
  1. package/README.md +11 -561
  2. package/index.js +24 -13
  3. package/package.json +8 -2
  4. package/skills/zinn/SKILL.md +63 -0
  5. package/src/actor.d.ts +2 -1
  6. package/src/actor.js +78 -11
  7. package/src/capabilities-schema.d.ts +162 -0
  8. package/src/capabilities-schema.js +59 -0
  9. package/src/capabilities.d.ts +29 -0
  10. package/src/capabilities.js +30 -0
  11. package/src/column-hint.d.ts +3 -0
  12. package/src/column-hint.js +225 -0
  13. package/src/database.d.ts +1 -1
  14. package/src/database.js +5 -8
  15. package/src/errors.d.ts +158 -0
  16. package/src/errors.js +226 -0
  17. package/src/input.d.ts +227 -0
  18. package/src/input.js +192 -0
  19. package/src/lib.d.ts +71 -8
  20. package/src/lib.js +275 -18
  21. package/src/presentation/activity-presentation.d.ts +9 -1
  22. package/src/presentation/activity-presentation.js +120 -23
  23. package/src/presentation/capabilities-presentation.d.ts +2 -0
  24. package/src/presentation/capabilities-presentation.js +3 -0
  25. package/src/presentation/column-presentation.d.ts +9 -5
  26. package/src/presentation/column-presentation.js +10 -0
  27. package/src/presentation/comment-presentation.d.ts +15 -0
  28. package/src/presentation/comment-presentation.js +29 -0
  29. package/src/presentation/description-presentation.d.ts +17 -0
  30. package/src/presentation/description-presentation.js +26 -4
  31. package/src/presentation/info-presentation.d.ts +4 -0
  32. package/src/presentation/info-presentation.js +96 -0
  33. package/src/presentation/label-presentation.d.ts +8 -0
  34. package/src/presentation/label-presentation.js +28 -0
  35. package/src/presentation/project-presentation.d.ts +7 -0
  36. package/src/presentation/project-presentation.js +19 -0
  37. package/src/presentation/relation-presentation.d.ts +7 -0
  38. package/src/presentation/relation-presentation.js +73 -0
  39. package/src/presentation/task-presentation.d.ts +4 -12
  40. package/src/presentation/task-presentation.js +22 -18
  41. package/src/project.d.ts +18 -3
  42. package/src/project.js +37 -12
  43. package/src/result-schemas.d.ts +1194 -0
  44. package/src/result-schemas.js +279 -0
  45. package/src/results.d.ts +40 -0
  46. package/src/results.js +36 -0
  47. package/src/routes/activity-router.d.ts +104 -0
  48. package/src/routes/activity-router.js +155 -34
  49. package/src/routes/aliases.d.ts +2 -0
  50. package/src/routes/aliases.js +11 -0
  51. package/src/routes/backup-router.d.ts +17 -0
  52. package/src/routes/backup-router.js +15 -3
  53. package/src/routes/capabilities-router.d.ts +21 -0
  54. package/src/routes/capabilities-router.js +34 -0
  55. package/src/routes/comment-router.d.ts +210 -8
  56. package/src/routes/comment-router.js +189 -65
  57. package/src/routes/info-router.d.ts +21 -0
  58. package/src/routes/info-router.js +29 -0
  59. package/src/routes/label-router.d.ts +217 -2
  60. package/src/routes/label-router.js +189 -62
  61. package/src/routes/migrate-router.d.ts +17 -0
  62. package/src/routes/migrate-router.js +17 -5
  63. package/src/routes/project-router.d.ts +373 -2
  64. package/src/routes/project-router.js +322 -132
  65. package/src/routes/reindex-router.d.ts +17 -0
  66. package/src/routes/reindex-router.js +15 -3
  67. package/src/routes/relation-router.d.ts +110 -14
  68. package/src/routes/relation-router.js +122 -79
  69. package/src/routes/restore-router.d.ts +29 -0
  70. package/src/routes/restore-router.js +31 -14
  71. package/src/routes/router.d.ts +1 -1
  72. package/src/routes/router.js +80 -32
  73. package/src/routes/routes.d.ts +1781 -8
  74. package/src/routes/routes.js +9 -1
  75. package/src/routes/task-router.d.ts +946 -4
  76. package/src/routes/task-router.js +524 -260
  77. package/src/routes/whoami-router.d.ts +21 -0
  78. package/src/routes/whoami-router.js +31 -0
  79. package/src/text-edit.d.ts +28 -6
  80. package/src/text-edit.js +40 -23
  81. package/src/text-input.d.ts +47 -0
  82. package/src/text-input.js +187 -0
  83. package/src/types.d.ts +37 -0
  84. package/src/types.js +9 -1
@@ -1,25 +1,38 @@
1
1
  import { parseArgs } from "node:util";
2
- import { failureMessage, isAllowLegacyReadFlag, isHelpFlag, isJsonFlag, quit, setLegacyReadAllowed, setOutputMode, UsageError, validateUniqueOptions, } from "../lib.js";
2
+ import { routerOptions, helpSpellings, versionSpellings, usageLabel } from "../input.js";
3
+ import { getCommandAliases } from "./aliases.js";
4
+ import { classifyFailure, CliError } from "../errors.js";
5
+ import { quotedColumnHint } from "../column-hint.js";
6
+ import { dashValueHelp, diagnostic, failureMessage, flushDiagnostics, isAllowLegacyReadFlag, isHelpFlag, isRouterFlag, parseArgsMessage, prepareOutput, quit, renderFailure, setLegacyReadAllowed, UsageError, validateUniqueOptions, } from "../lib.js";
7
+ // ZINN_DEBUG=1 prints routing detail on stderr, so stdout and `--json` results stay unchanged.
8
+ function isDebugOutputEnabled() {
9
+ return process.env.ZINN_DEBUG === "1";
10
+ }
3
11
  function getIsRunnable(obj) {
4
12
  return Object.hasOwn(obj, "run");
5
13
  }
6
- function isRouterFlag(arg) {
7
- return isJsonFlag(arg) || isAllowLegacyReadFlag(arg);
8
- }
9
14
  // --json and --allow-legacy-read can each be given once, like a command's own flags.
10
15
  function validateRouterFlags(routableArgs) {
11
16
  const { tokens } = parseArgs({
12
17
  args: routableArgs.filter(isRouterFlag),
13
- options: { json: { type: "boolean" }, "allow-legacy-read": { type: "boolean" } },
18
+ options: routerOptions,
14
19
  strict: true,
15
20
  tokens: true,
16
21
  });
17
- validateUniqueOptions(tokens);
22
+ validateUniqueOptions(tokens, routerOptions);
18
23
  }
19
24
  /** The usage lines that open a command's help, up to its first blank line. */
20
25
  function getUsage(help) {
21
26
  return help.split("\n\n")[0];
22
27
  }
28
+ function getCommandHelp(help, aliases) {
29
+ if (aliases.length === 0) {
30
+ return help;
31
+ }
32
+ const [usage, ...details] = help.split("\n\n");
33
+ const aliasLine = `Alias: ${aliases.map((path) => `zinn ${path.join(" ")}`).join(", ")}`;
34
+ return [usage, aliasLine, ...details].join("\n\n");
35
+ }
23
36
  function toCommand(args) {
24
37
  return args.join(" ");
25
38
  }
@@ -50,13 +63,13 @@ function formatCommands(parsedRoutes, prefix) {
50
63
  }
51
64
  function getRootHelp(parsedRoutes) {
52
65
  // TODO: maybe "brand" text that is exported and used here for title and desc
53
- return `ZINN - A kanban workflow in the terminal
66
+ return `ZINN - A kanban workflow in the terminal. Experimental.
54
67
 
55
- Usage: zinn <command>
68
+ ${usageLabel}zinn <command>
56
69
 
57
70
  Options:
58
- -h, --help Show help
59
- -v, --version Show version
71
+ ${helpSpellings.join(", ")} Show help
72
+ ${versionSpellings.join(", ")} Show version
60
73
  --json Print command output as JSON
61
74
  --allow-legacy-read Read a database without a schema version, best effort and read-only
62
75
 
@@ -65,16 +78,15 @@ ${formatCommands(parsedRoutes, "")}
65
78
 
66
79
  Run zinn <namespace> --help or zinn <command> --help for more information.
67
80
  --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.`;
81
+ -- ends the options; every word after it is positional, even if it starts with a dash.
82
+ ${dashValueHelp("--description")}`;
71
83
  }
72
84
  function getNamespaceHelp(parsedRoutes, namespace) {
73
85
  const commands = formatCommands(parsedRoutes, `${namespace} `);
74
86
  if (commands === "") {
75
87
  return null;
76
88
  }
77
- return `Usage: zinn ${namespace} <command>\n\nCommands:\n${commands}`;
89
+ return `${usageLabel}zinn ${namespace} <command>\n\nCommands:\n${commands}`;
78
90
  }
79
91
  // The longest leading run of words that names a namespace, or the root namespace "" when none does.
80
92
  function findNearestNamespace(parsedRoutes, words) {
@@ -97,25 +109,33 @@ function routeToNamespace(parsedRoutes, words, isHelpRequest) {
97
109
  return;
98
110
  }
99
111
  if (unknownWord == null) {
100
- quit(`Missing a command for "${commandName}"`, help);
112
+ quit(`Missing a command for "${commandName}"`, help, { argument: "command" });
101
113
  }
102
114
  const reason = `Unrecognized command "${unknownWord}" for "${commandName}"`;
103
115
  if (isHelpRequest) {
104
- quit(reason, help);
116
+ quit(reason, help, { argument: "command" });
105
117
  }
106
- quit(`${reason}. Run \`${commandName} --help\` for a list of available commands`);
118
+ quit(`${reason}. Run \`${commandName} --help\` for a list of available commands`, undefined, {
119
+ argument: "command",
120
+ });
107
121
  }
108
122
  export function parseRoutes(routesDef, prefix) {
109
123
  const routeDefinitions = [];
110
- if (process.env.DEBUG) {
111
- console.debug(`::router.parseRoutes[${prefix ?? ""}]`, routesDef);
124
+ if (isDebugOutputEnabled()) {
125
+ diagnostic(`::router.parseRoutes[${prefix ?? ""}] ${Object.keys(routesDef).join(" ")}`);
112
126
  }
113
127
  for (const key of Object.keys(routesDef)) {
114
128
  const definition = routesDef[key];
115
129
  const isRunnable = getIsRunnable(definition);
116
130
  const combinedRoute = prefix ? `${prefix} ${key}` : key;
117
131
  if (isRunnable) {
118
- routeDefinitions.push({ command: combinedRoute, ...definition });
132
+ const aliases = getCommandAliases(combinedRoute);
133
+ routeDefinitions.push({
134
+ ...definition,
135
+ command: combinedRoute,
136
+ aliases,
137
+ help: getCommandHelp(definition.help, aliases),
138
+ });
119
139
  }
120
140
  else {
121
141
  const result = parseRoutes(definition, combinedRoute);
@@ -124,15 +144,13 @@ export function parseRoutes(routesDef, prefix) {
124
144
  }
125
145
  return routeDefinitions;
126
146
  }
127
- async function route(parsedRoutes, rawArgs) {
147
+ async function route(parsedRoutes, rawArgs, beforeRun) {
148
+ prepareOutput(rawArgs);
128
149
  // Anything after "--" belongs to the command, so "--json" there is search text, not a flag.
129
150
  const separatorIndex = rawArgs.indexOf("--");
130
151
  const routableArgs = separatorIndex === -1 ? rawArgs : rawArgs.slice(0, separatorIndex);
131
152
  const passthroughArgs = separatorIndex === -1 ? [] : rawArgs.slice(separatorIndex);
132
153
  const filteredRoutableArgs = routableArgs.filter((arg) => !isRouterFlag(arg));
133
- if (routableArgs.some(isJsonFlag)) {
134
- setOutputMode("json");
135
- }
136
154
  if (routableArgs.some(isAllowLegacyReadFlag)) {
137
155
  setLegacyReadAllowed(true);
138
156
  }
@@ -146,15 +164,15 @@ async function route(parsedRoutes, rawArgs) {
146
164
  let nestingLevel = 0;
147
165
  for (let endIndex = args.length; endIndex >= 0; endIndex--) {
148
166
  const command = toCommand(args.slice(0, endIndex));
149
- const matchedRoute = parsedRoutes.find((r) => r.command === command);
167
+ const matchedRoute = parsedRoutes.find((r) => r.command === command || r.aliases.some((path) => toCommand(path) === command));
150
168
  if (matchedRoute) {
151
169
  match = matchedRoute;
152
170
  nestingLevel = endIndex;
153
171
  break;
154
172
  }
155
173
  }
156
- if (process.env.DEBUG) {
157
- console.log("::router.route", { args, result: parsedRoutes.map((r) => r.command), match });
174
+ if (isDebugOutputEnabled()) {
175
+ diagnostic(`::router.route ${match?.command ?? "namespace"}`);
158
176
  }
159
177
  if (match == null) {
160
178
  routeToNamespace(parsedRoutes, args, isHelpRequest);
@@ -167,19 +185,49 @@ async function route(parsedRoutes, rawArgs) {
167
185
  validateRouterFlags(routableArgs);
168
186
  const commandArgs = args.slice(nestingLevel);
169
187
  try {
188
+ await beforeRun?.(match);
170
189
  await match.run(commandArgs);
171
190
  }
172
191
  catch (err) {
173
192
  if (err instanceof UsageError) {
174
- quit(err.message, getUsage(match.help));
193
+ err.help = getUsage(match.help);
194
+ }
195
+ const columnHint = quotedColumnHint(err, match, rawArgs, nestingLevel);
196
+ if (columnHint !== undefined) {
197
+ const failure = classifyFailure(err);
198
+ throw new CliError(`${failureMessage(err, "Command failed")}. Did you mean: ${columnHint}`, failure.code, failure.details, failure.help ?? getUsage(match.help));
199
+ }
200
+ if (err instanceof UsageError) {
201
+ throw err;
202
+ }
203
+ if (match.command === "task search") {
204
+ const failure = classifyFailure(err);
205
+ const isMissingProject = failure.code === "not_found" && failure.details?.entity === "project";
206
+ if (isMissingProject) {
207
+ throw new CliError(failureMessage(err, "Search failed"), failure.code, failure.details, getUsage(match.help));
208
+ }
209
+ }
210
+ const suggestFor = (flag) => match.guessedFlags?.[flag]?.(commandArgs);
211
+ const parseArgsReason = parseArgsMessage(err, rawArgs, suggestFor);
212
+ if (parseArgsReason != null) {
213
+ const argument = /(?:Unknown option|Option) '(-[^' ]+)/.exec(err.message)?.[1];
214
+ quit(parseArgsReason, getUsage(match.help), argument === undefined ? undefined : { argument });
175
215
  }
176
- quit(failureMessage(err, `Something went wrong while running "${match.command}" with args: ${commandArgs.join(",")}`));
216
+ throw err;
177
217
  }
178
218
  }
179
- export function createRouter(routesDef) {
180
- const parsedRoutes = parseRoutes(routesDef);
219
+ export function createRouter(routesDef, beforeRun) {
220
+ const parsedRoutes = Array.isArray(routesDef) ? routesDef : parseRoutes(routesDef);
181
221
  return {
182
- route: (args) => route(parsedRoutes, args),
222
+ route: async (args) => {
223
+ try {
224
+ await route(parsedRoutes, args, beforeRun);
225
+ flushDiagnostics();
226
+ }
227
+ catch (error) {
228
+ renderFailure(error);
229
+ }
230
+ },
183
231
  help: getRootHelp(parsedRoutes),
184
232
  };
185
233
  }