@zinn-dev/cli 0.39.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 (93) hide show
  1. package/README.md +5 -2
  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.ts → activity-router.js} +36 -46
  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 -159
  67. package/src/presentation/activity-presentation.test.ts +0 -112
  68. package/src/presentation/activity-presentation.ts +0 -490
  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/project.test.ts +0 -479
  77. package/src/project.ts +0 -53
  78. package/src/routes/backup-router.ts +0 -24
  79. package/src/routes/comment-router.ts +0 -171
  80. package/src/routes/label-router.ts +0 -168
  81. package/src/routes/migrate-router.test.ts +0 -31
  82. package/src/routes/migrate-router.ts +0 -48
  83. package/src/routes/project-router.ts +0 -415
  84. package/src/routes/reindex-router.ts +0 -27
  85. package/src/routes/relation-router.ts +0 -194
  86. package/src/routes/restore-router.test.ts +0 -44
  87. package/src/routes/restore-router.ts +0 -90
  88. package/src/routes/router.test.ts +0 -136
  89. package/src/routes/router.ts +0 -248
  90. package/src/routes/routes.ts +0 -20
  91. package/src/routes/task-router.ts +0 -649
  92. package/src/text-edit.ts +0 -68
  93. package/src/types.ts +0 -6
@@ -1,92 +0,0 @@
1
- import type { LabelColor, TaskPriority, TaskWithKey } from "@zinn-dev/core";
2
- import type { RowCell } from "./row-layout";
3
-
4
- import { column, label, task } from "@zinn-dev/core";
5
-
6
- import { styleOutputText } from "./color";
7
- import { columnNameCell } from "./column-presentation";
8
- import { formatCellSeparator, formatRows } from "./row-layout";
9
-
10
- export type PresentedTask = {
11
- key: string;
12
- status: "Active" | "Archived";
13
- column: string;
14
- title: string;
15
- description: string | null;
16
- priority: TaskPriority | null;
17
- labels: Array<string>;
18
- };
19
-
20
- const EMPTY_CELL_MARKER = "-";
21
- const LABEL_NAME_SEPARATOR = ", ";
22
-
23
- function taskPriorityCell(priority: TaskPriority | null, showsFullPriority: boolean): RowCell {
24
- if (priority == null) {
25
- return { text: EMPTY_CELL_MARKER, style: "dim" };
26
- }
27
-
28
- const priorityInitial = priority[0]!.toUpperCase();
29
- return { text: showsFullPriority ? `${priorityInitial}${priority.slice(1)}` : priorityInitial };
30
- }
31
-
32
- /**
33
- * `PresentedTask` carries label names only, so JSON stays a list of names.
34
- * Text output looks the colors up from the label definitions instead.
35
- */
36
- function getLabelColorsByName() {
37
- return new Map(label.getAll().map((labelMatch) => [labelMatch.name, labelMatch.color]));
38
- }
39
-
40
- function formatLabelNames(labelNames: Array<string>, labelColorsByName: Map<string, LabelColor>) {
41
- return labelNames
42
- .map((labelName) => styleOutputText(labelColorsByName.get(labelName)!, labelName))
43
- .join(LABEL_NAME_SEPARATOR);
44
- }
45
-
46
- export function presentTask(taskMatch: TaskWithKey): PresentedTask {
47
- return {
48
- key: taskMatch.key,
49
- status: taskMatch.archived_at == null ? "Active" : "Archived",
50
- column: column.getById(taskMatch.column_id)!.name,
51
- title: taskMatch.title,
52
- description: taskMatch.description,
53
- priority: taskMatch.priority,
54
- labels: task.label.getAll(taskMatch.key).map((label) => label.name),
55
- };
56
- }
57
-
58
- export function formatTaskRow(presented: PresentedTask) {
59
- return formatTaskList([presented]);
60
- }
61
-
62
- export function formatTaskList(
63
- presentedTasks: Array<PresentedTask>,
64
- options: { showsStatus?: boolean; showsFullPriority?: boolean } = {},
65
- ) {
66
- const labelColorsByName = getLabelColorsByName();
67
-
68
- return formatRows(
69
- presentedTasks.map((presentedTask) => {
70
- const statusCells: Array<RowCell> = options.showsStatus
71
- ? [{ text: presentedTask.status }]
72
- : [];
73
- const hasLabels = presentedTask.labels.length > 0;
74
- // The last cell is never padded, so the color codes in its separator and label names do not affect column widths.
75
- const labels = hasLabels
76
- ? `${formatCellSeparator()}${formatLabelNames(presentedTask.labels, labelColorsByName)}`
77
- : "";
78
-
79
- return [
80
- { text: presentedTask.key },
81
- ...statusCells,
82
- columnNameCell(presentedTask.column),
83
- taskPriorityCell(presentedTask.priority, options.showsFullPriority ?? false),
84
- { text: `${presentedTask.title}${labels}` },
85
- ];
86
- }),
87
- );
88
- }
89
-
90
- export function formatTaskListWithStatus(presentedTasks: Array<PresentedTask>) {
91
- return formatTaskList(presentedTasks, { showsStatus: true });
92
- }
@@ -1,17 +0,0 @@
1
- const MINUTES_PER_HOUR = 60;
2
-
3
- export function formatLocalTime(timestamp: number) {
4
- const date = new Date(timestamp);
5
- const offsetMinutes = -date.getTimezoneOffset();
6
- const offsetSign = offsetMinutes >= 0 ? "+" : "-";
7
- const absoluteOffsetMinutes = Math.abs(offsetMinutes);
8
- const offsetHours = Math.floor(absoluteOffsetMinutes / MINUTES_PER_HOUR);
9
- const remainingOffsetMinutes = absoluteOffsetMinutes % MINUTES_PER_HOUR;
10
- const pad = (value: number) => String(value).padStart(2, "0");
11
-
12
- return (
13
- `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ` +
14
- `${pad(date.getHours())}:${pad(date.getMinutes())} ` +
15
- `${offsetSign}${pad(offsetHours)}:${pad(remainingOffsetMinutes)}`
16
- );
17
- }
@@ -1,479 +0,0 @@
1
- import { expect, test } from "bun:test";
2
- import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3
- import { tmpdir } from "node:os";
4
- import { join } from "node:path";
5
-
6
- import { runCliInTerminal } from "../testing";
7
-
8
- const PREFIX = join(tmpdir(), "zinndefaulttest-");
9
- // Each precedence matrix starts several dozen CLI processes.
10
- const COMMAND_MATRIX_TIMEOUT_MS = 30_000;
11
-
12
- function withTestDirectory(run: (directory: string) => void) {
13
- const directory = mkdtempSync(PREFIX);
14
-
15
- try {
16
- run(directory);
17
- } finally {
18
- removeTestDirectory(directory);
19
- }
20
- }
21
-
22
- // Same as withTestDirectory, but removes the directory only after an async run settles.
23
- async function withTestDirectoryAsync(run: (directory: string) => Promise<void>) {
24
- const directory = mkdtempSync(PREFIX);
25
-
26
- try {
27
- await run(directory);
28
- } finally {
29
- removeTestDirectory(directory);
30
- }
31
- }
32
-
33
- function removeTestDirectory(directory: string) {
34
- if (!directory.startsWith(PREFIX)) {
35
- throw new Error(`Refusing to delete "${directory}": not a ${PREFIX}* directory`);
36
- }
37
-
38
- rmSync(directory, { recursive: true, force: true });
39
- }
40
-
41
- function runZinn(args: Array<string>, zinnDirectory: string) {
42
- const proc = Bun.spawnSync(["bun", join(import.meta.dir, "..", "index.ts"), ...args], {
43
- env: { ...process.env, ZINN_DIR: zinnDirectory },
44
- });
45
-
46
- return { stdout: proc.stdout.toString(), stderr: proc.stderr.toString(), code: proc.exitCode };
47
- }
48
-
49
- function runZinnFrom(
50
- args: Array<string>,
51
- directory: string,
52
- cwd: string,
53
- additionalEnv: Record<string, string> = {},
54
- ) {
55
- const env: Record<string, string | undefined> = { ...process.env, HOME: join(directory, "home") };
56
- delete env.ZINN_DIR;
57
- Object.assign(env, additionalEnv);
58
- const cliPath = join(import.meta.dir, "..", "index.ts");
59
- const proc = Bun.spawnSync(["bun", cliPath, ...args], { cwd, env });
60
-
61
- return { stdout: proc.stdout.toString(), stderr: proc.stderr.toString(), code: proc.exitCode };
62
- }
63
-
64
- function runZinnWithPipedYes(args: Array<string>, directory: string, cwd: string) {
65
- const env: Record<string, string | undefined> = { ...process.env, HOME: join(directory, "home") };
66
- delete env.ZINN_DIR;
67
- const cliPath = join(import.meta.dir, "..", "index.ts");
68
- const proc = Bun.spawnSync(
69
- ["/bin/sh", "-c", 'printf "y\\n" | exec bun "$@"', "zinn-confirm", cliPath, ...args],
70
- { cwd, env },
71
- );
72
-
73
- return { stdout: proc.stdout.toString(), stderr: proc.stderr.toString(), code: proc.exitCode };
74
- }
75
-
76
- /** Runs the CLI attached to a pseudo-terminal and types the input; stdout and stderr arrive together. */
77
- function runZinnInTerminal(args: Array<string>, directory: string, cwd: string, input: string) {
78
- const env: Record<string, string | undefined> = { ...process.env, HOME: join(directory, "home") };
79
- delete env.ZINN_DIR;
80
-
81
- return runCliInTerminal(args, { cwd, env, input });
82
- }
83
-
84
- function makeRepository(directory: string, localSettings: Record<string, unknown> = {}) {
85
- const repository = join(directory, "repository");
86
- const globalDirectory = join(directory, "home", ".zinn");
87
- mkdirSync(join(repository, ".git"), { recursive: true });
88
- mkdirSync(globalDirectory, { recursive: true });
89
- writeFileSync(join(globalDirectory, "settings.json"), JSON.stringify({ storage: { directory: join(directory, "store") } }));
90
- writeFileSync(join(repository, "zinn.local.json"), JSON.stringify(localSettings));
91
-
92
- return { repository, globalSettingsPath: join(globalDirectory, "settings.json"), localSettingsPath: join(repository, "zinn.local.json") };
93
- }
94
-
95
- function writeGlobalSettings(directory: string, settings: Record<string, unknown>) {
96
- const globalSettingsPath = join(directory, "home", ".zinn", "settings.json");
97
- writeFileSync(globalSettingsPath, JSON.stringify({ storage: { directory: join(directory, "store") }, ...settings }));
98
- }
99
-
100
- function seedProjects(directory: string, store = join(directory, "store")) {
101
- expect(runZinn(["project", "create", "DEF", "--name", "Default"], store).code).toBe(0);
102
- expect(runZinn(["project", "create", "ALT", "--name", "Alternate"], store).code).toBe(0);
103
- expect(runZinn(["task", "create", "DEF", "--title", "needle default"], store).code).toBe(0);
104
- expect(runZinn(["task", "create", "ALT", "--title", "needle alternate"], store).code).toBe(0);
105
- expect(runZinn(["project", "column", "create", "DEF", "Only DEF"], store).code).toBe(0);
106
- expect(runZinn(["project", "column", "create", "ALT", "Only ALT"], store).code).toBe(0);
107
- }
108
-
109
- test("ZINN_DIR selects an empty or populated store without applying the repository default", () => {
110
- withTestDirectory((directory) => {
111
- const { repository } = makeRepository(directory, { defaultProject: "DEF" });
112
- const emptyStore = join(directory, "empty-store");
113
- const emptyRun = (...args: Array<string>) => runZinnFrom(args, directory, repository, { ZINN_DIR: emptyStore });
114
-
115
- for (const args of [["task", "list"], ["task", "search", "needle"]]) {
116
- const result = emptyRun(...args, "--json");
117
- expect(result.code).toBe(0);
118
- expect(JSON.parse(result.stdout)).toEqual([]);
119
- }
120
-
121
- const populatedStore = join(directory, "populated-store");
122
- seedProjects(directory, populatedStore);
123
- const populatedRun = (...args: Array<string>) => runZinnFrom(args, directory, repository, { ZINN_DIR: populatedStore });
124
- expect(JSON.parse(populatedRun("task", "list", "--json").stdout).map((task: { key: string }) => task.key).sort()).toEqual([
125
- "ALT-1",
126
- "DEF-1",
127
- ]);
128
- expect(JSON.parse(populatedRun("task", "search", "needle", "--json").stdout).map((task: { key: string }) => task.key).sort()).toEqual([
129
- "ALT-1",
130
- "DEF-1",
131
- ]);
132
- });
133
- });
134
-
135
- test("ZINN_DIR uses its own default and reports unknown defaults from its settings file", () => {
136
- withTestDirectory((directory) => {
137
- const { repository } = makeRepository(directory, { defaultProject: "DEF" });
138
- const store = join(directory, "store");
139
- mkdirSync(store, { recursive: true });
140
- writeFileSync(join(store, "settings.json"), JSON.stringify({ defaultProject: "ALT" }));
141
- seedProjects(directory, store);
142
- const invoke = (...args: Array<string>) => runZinnFrom(args, directory, repository, { ZINN_DIR: store });
143
-
144
- expect(JSON.parse(invoke("task", "list", "--json").stdout).map((task: { key: string }) => task.key)).toEqual(["ALT-1"]);
145
- expect(JSON.parse(invoke("task", "search", "needle", "--json").stdout).map((task: { key: string }) => task.key)).toEqual(["ALT-1"]);
146
-
147
- writeFileSync(join(store, "settings.json"), JSON.stringify({ defaultProject: "NOPE" }));
148
- const unknown = invoke("task", "list", "--json");
149
- expect(unknown.code).toBe(1);
150
- expect(unknown.stderr).toContain("NOPE");
151
- expect(unknown.stderr).toContain(join(store, "settings.json"));
152
-
153
- expect(JSON.parse(invoke("task", "list", "ALT", "--json").stdout).map((task: { key: string }) => task.key)).toEqual(["ALT-1"]);
154
- expect(JSON.parse(invoke("task", "list", "--all-projects", "--json").stdout)).toHaveLength(2);
155
- expect(JSON.parse(invoke("task", "search", " alt-1 ", "--json").stdout).map((task: { key: string }) => task.key)).toEqual(["ALT-1"]);
156
- });
157
- });
158
-
159
- test("ZINN_DIR ignores malformed repository and home settings but validates its own settings", () => {
160
- withTestDirectory((directory) => {
161
- const { repository, localSettingsPath, globalSettingsPath } = makeRepository(directory);
162
- const store = join(directory, "store");
163
- writeFileSync(localSettingsPath, "{");
164
- writeFileSync(globalSettingsPath, "{");
165
- const invoke = (...args: Array<string>) => runZinnFrom(args, directory, repository, { ZINN_DIR: store });
166
-
167
- const ignoredMalformedFiles = invoke("task", "list", "--json");
168
- expect(ignoredMalformedFiles.code).toBe(0);
169
- expect(JSON.parse(ignoredMalformedFiles.stdout)).toEqual([]);
170
- expect(ignoredMalformedFiles.stderr).not.toContain(localSettingsPath);
171
- expect(ignoredMalformedFiles.stderr).not.toContain(globalSettingsPath);
172
-
173
- const selectedStoreSettingsPath = join(store, "settings.json");
174
- writeFileSync(selectedStoreSettingsPath, JSON.stringify({ defaultProject: 42 }));
175
- const invalidSelectedStoreSettings = invoke("task", "list", "--json");
176
- expect(invalidSelectedStoreSettings.code).toBe(1);
177
- expect(invalidSelectedStoreSettings.stderr).toContain(selectedStoreSettingsPath);
178
- expect(invalidSelectedStoreSettings.stderr).toContain("defaultProject");
179
- });
180
- });
181
-
182
- async function expectDefaultCommands(directory: string, repository: string, expectedKey: string, otherKey: string) {
183
- const invoke = (...args: Array<string>) => runZinnFrom(args, directory, repository);
184
- const list = invoke("task", "list", "--json");
185
- const search = invoke("task", "search", "needle", "--json");
186
- const created = invoke("task", "create", "--title", "created by default", "--json");
187
- const viewed = invoke("project", "view", "--json");
188
- const columns = invoke("project", "column", "list", "--json");
189
- const edited = invoke("project", "edit", "--name", "Edited by default", "--json");
190
-
191
- expect(list.code).toBe(0);
192
- expect(JSON.parse(list.stdout).map((task: { key: string }) => task.key)).toEqual([`${expectedKey}-1`]);
193
- expect(search.code).toBe(0);
194
- expect(JSON.parse(search.stdout).map((task: { key: string }) => task.key)).toEqual([`${expectedKey}-1`]);
195
- expect(created.code).toBe(0);
196
- expect(JSON.parse(created.stdout).key).toBe(`${expectedKey}-2`);
197
- expect(viewed.code).toBe(0);
198
- expect(JSON.parse(viewed.stdout).key).toBe(expectedKey);
199
- expect(columns.code).toBe(0);
200
- expect(JSON.parse(columns.stdout).map((column: { name: string }) => column.name)).toContain(`Only ${expectedKey}`);
201
- expect(edited.code).toBe(0);
202
- expect(JSON.parse(edited.stdout)).toMatchObject({ key: expectedKey, name: "Edited by default" });
203
-
204
- const explicitList = invoke("task", "list", otherKey, "--json");
205
- const explicitSearch = invoke("task", "search", "needle", otherKey, "--json");
206
- const explicitCreate = invoke("task", "create", otherKey, "--title", "created explicitly", "--json");
207
- const explicitView = invoke("project", "view", otherKey, "--json");
208
- const explicitColumns = invoke("project", "column", "list", otherKey, "--json");
209
- const explicitEdit = invoke("project", "edit", otherKey, "--name", "Edited explicitly", "--json");
210
-
211
- expect(JSON.parse(explicitList.stdout).map((task: { key: string }) => task.key)).toEqual([`${otherKey}-1`]);
212
- expect(JSON.parse(explicitSearch.stdout).map((task: { key: string }) => task.key)).toEqual([`${otherKey}-1`]);
213
- expect(JSON.parse(explicitCreate.stdout).key).toBe(`${otherKey}-2`);
214
- expect(JSON.parse(explicitView.stdout).key).toBe(otherKey);
215
- expect(JSON.parse(explicitColumns.stdout).map((column: { name: string }) => column.name)).toContain(`Only ${otherKey}`);
216
- expect(JSON.parse(explicitEdit.stdout)).toMatchObject({ key: otherKey, name: "Edited explicitly" });
217
- expect(JSON.parse(invoke("task", "view", `${otherKey}-1`, "--json").stdout).key).toBe(`${otherKey}-1`);
218
-
219
- const deleted = await runZinnInTerminal(["project", "delete"], directory, repository, "y\n");
220
- expect(deleted.code).toBe(0);
221
- expect(deleted.output).toContain(expectedKey);
222
- expect(JSON.parse(invoke("project", "list", "--json").stdout).map((project: { key: string }) => project.key)).toEqual([otherKey]);
223
- const explicitDelete = runZinnFrom(["project", "delete", otherKey, "--yes", "--json"], directory, repository);
224
- expect(explicitDelete.code).toBe(0);
225
- expect(JSON.parse(explicitDelete.stdout).key).toBe(otherKey);
226
- }
227
-
228
- test("each default-project command uses local settings, overrides global settings and yields to a typed key", async () => {
229
- await withTestDirectoryAsync(async (directory) => {
230
- makeRepository(directory, { defaultProject: "def" });
231
- writeGlobalSettings(directory, { defaultProject: "ALT" });
232
- seedProjects(directory);
233
-
234
- await expectDefaultCommands(directory, join(directory, "repository"), "DEF", "ALT");
235
- });
236
- }, COMMAND_MATRIX_TIMEOUT_MS);
237
-
238
- test("each default-project command falls back to global settings when local settings omit the field", async () => {
239
- await withTestDirectoryAsync(async (directory) => {
240
- makeRepository(directory, {});
241
- writeGlobalSettings(directory, { defaultProject: "DEF" });
242
- seedProjects(directory);
243
-
244
- await expectDefaultCommands(directory, join(directory, "repository"), "DEF", "ALT");
245
- });
246
- }, COMMAND_MATRIX_TIMEOUT_MS);
247
-
248
- test("commands without a default keep their existing project behavior, and task-key commands stay project-specific", () => {
249
- withTestDirectory((directory) => {
250
- const { repository } = makeRepository(directory);
251
- seedProjects(directory);
252
- const invoke = (...args: Array<string>) => runZinnFrom(args, directory, repository);
253
-
254
- expect(JSON.parse(invoke("task", "list", "--json").stdout)).toHaveLength(2);
255
- expect(JSON.parse(invoke("task", "search", "needle", "--json").stdout)).toHaveLength(2);
256
- expect(JSON.parse(invoke("task", "list", "--all-projects", "--json").stdout)).toHaveLength(2);
257
- expect(JSON.parse(invoke("task", "search", "needle", "--all-projects", "--json").stdout)).toHaveLength(2);
258
- expect(invoke("task", "create", "--title", "needs a project").code).toBe(1);
259
- expect(invoke("project", "view").code).toBe(1);
260
- expect(invoke("project", "column", "list").code).toBe(1);
261
- expect(invoke("project", "edit", "--name", "No project").code).toBe(1);
262
- expect(invoke("project", "delete").code).toBe(1);
263
- expect(JSON.parse(invoke("task", "view", "ALT-1", "--json").stdout).key).toBe("ALT-1");
264
- });
265
- });
266
-
267
- test("all-projects bypasses an unknown default, conflicts with typed keys, and keeps list filters", () => {
268
- withTestDirectory((directory) => {
269
- const { repository } = makeRepository(directory, { defaultProject: "NOPE" });
270
- seedProjects(directory);
271
- const invoke = (...args: Array<string>) => runZinnFrom(args, directory, repository);
272
-
273
- for (const args of [["task", "list"], ["task", "search", "needle"]]) {
274
- const allProjects = invoke(...args, "--all-projects", "--json");
275
- expect(allProjects.code).toBe(0);
276
- expect(JSON.parse(allProjects.stdout)).toHaveLength(2);
277
- const typedConflict = args[1] === "list"
278
- ? invoke("task", "list", "ALT", "--all-projects", "--json")
279
- : invoke("task", "search", "needle", "ALT", "--all-projects", "--json");
280
- expect(typedConflict.code).toBe(1);
281
- }
282
-
283
- const defaultUnknown = invoke("task", "list", "--json");
284
- expect(defaultUnknown.code).toBe(1);
285
- expect(defaultUnknown.stderr).toContain("NOPE");
286
- expect(defaultUnknown.stderr).toContain(join(repository, "zinn.local.json"));
287
-
288
- writeFileSync(join(repository, "zinn.local.json"), JSON.stringify({ defaultProject: "DEF" }));
289
- expect(runZinn(["task", "archive", "DEF-1"], join(directory, "store")).code).toBe(0);
290
- const active = invoke("task", "list", "--all-projects", "--json");
291
- const all = invoke("task", "list", "--all-projects", "--all", "--json");
292
- const archived = invoke("task", "list", "--all-projects", "--archived", "--json");
293
- const searchAll = invoke("task", "search", "needle", "--all-projects", "--all", "--json");
294
- const searchArchived = invoke("task", "search", "needle", "--all-projects", "--archived", "--json");
295
- expect(JSON.parse(active.stdout)).toHaveLength(1);
296
- expect(JSON.parse(all.stdout)).toHaveLength(2);
297
- expect(JSON.parse(archived.stdout).map((task: { key: string }) => task.key)).toEqual(["DEF-1"]);
298
- expect(JSON.parse(searchAll.stdout)).toHaveLength(2);
299
- expect(JSON.parse(searchArchived.stdout).map((task: { key: string }) => task.key)).toEqual(["DEF-1"]);
300
- });
301
- });
302
-
303
- test("task-key search queries use the matching project instead of a default and keep an explicit project filter", () => {
304
- withTestDirectory((directory) => {
305
- const { repository } = makeRepository(directory, { defaultProject: "DEF" });
306
- seedProjects(directory);
307
- const invoke = (...args: Array<string>) => runZinnFrom(args, directory, repository);
308
-
309
- expect(JSON.parse(invoke("task", "search", "needle", "--json").stdout).map((task: { key: string }) => task.key)).toEqual(["DEF-1"]);
310
- expect(JSON.parse(invoke("task", "search", "ALT-1", "--json").stdout).map((task: { key: string }) => task.key)).toEqual(["ALT-1"]);
311
-
312
- writeFileSync(join(repository, "zinn.local.json"), JSON.stringify({ defaultProject: "NOPE" }));
313
- expect(JSON.parse(invoke("task", "search", " alt-1 ", "--json").stdout).map((task: { key: string }) => task.key)).toEqual(["ALT-1"]);
314
-
315
- const filtered = invoke("task", "search", " ALT-1 ", "DEF", "--json");
316
- expect(filtered.code).toBe(0);
317
- expect(JSON.parse(filtered.stdout)).toEqual([]);
318
-
319
- const conflict = invoke("task", "search", "ALT-1", "DEF", "--all-projects", "--json");
320
- expect(conflict.code).toBe(1);
321
- expect(conflict.stdout).toBe("");
322
- expect(conflict.stderr).toContain("--all-projects");
323
- });
324
- });
325
-
326
- test("task list and search report conflicting archive flags before resolving an unknown default", () => {
327
- withTestDirectory((directory) => {
328
- const { repository, localSettingsPath } = makeRepository(directory, { defaultProject: "NOPE" });
329
- seedProjects(directory);
330
- const invoke = (...args: Array<string>) => runZinnFrom(args, directory, repository);
331
-
332
- for (const args of [["task", "list"], ["task", "search", "needle"]]) {
333
- const result = invoke(...args, "--archived", "--all", "--json");
334
- expect(result.code).toBe(1);
335
- expect(result.stdout).toBe("");
336
- expect(result.stderr).toContain("--archived");
337
- expect(result.stderr).toContain("--all");
338
- expect(result.stderr).not.toContain("NOPE");
339
- expect(result.stderr).not.toContain(localSettingsPath);
340
- }
341
- });
342
- });
343
-
344
- test("a missing conditional project key reports its value and optional usage", () => {
345
- withTestDirectory((directory) => {
346
- const { repository } = makeRepository(directory);
347
- seedProjects(directory);
348
- const result = runZinnFrom(["project", "view", "--json"], directory, repository);
349
-
350
- expect(result.code).toBe(1);
351
- expect(result.stdout).toBe("");
352
- const error = JSON.parse(result.stderr).error;
353
- expect(error.message).toContain("<project-key>");
354
- expect(error.message).toContain("defaultProject");
355
- expect(error.help).toContain("Usage: zinn project view [<project-key>]");
356
- });
357
- });
358
-
359
- test("unknown defaults identify their key and settings file", () => {
360
- withTestDirectory((directory) => {
361
- const { repository, globalSettingsPath, localSettingsPath } = makeRepository(directory, { defaultProject: "NOPE" });
362
- seedProjects(directory);
363
- const invoke = (...args: Array<string>) => runZinnFrom(args, directory, repository);
364
- const assertUnknownDefaultDoesNotMutate = (settingsPath: string) => {
365
- for (const args of [
366
- ["project", "view", "--json"],
367
- ["task", "create", "--title", "must not be created", "--json"],
368
- ["project", "edit", "--name", "Must not be applied", "--json"],
369
- ["project", "delete", "--yes"],
370
- ]) {
371
- const result = invoke(...args);
372
- expect(result.code).toBe(1);
373
- expect(result.stderr).toContain("NOPE");
374
- expect(result.stderr).toContain(settingsPath);
375
- }
376
-
377
- expect(JSON.parse(invoke("project", "view", "DEF", "--json").stdout).name).toBe("Default");
378
- expect(JSON.parse(invoke("task", "list", "DEF", "--json").stdout)).toHaveLength(1);
379
- };
380
-
381
- assertUnknownDefaultDoesNotMutate(localSettingsPath);
382
-
383
- const explicitList = invoke("task", "list", "ALT", "--json");
384
- const explicitSearch = invoke("task", "search", "needle", "ALT", "--json");
385
- const explicitCreate = invoke("task", "create", "ALT", "--title", "typed key bypasses unknown default", "--json");
386
- const explicitView = invoke("project", "view", "ALT", "--json");
387
- const explicitColumns = invoke("project", "column", "list", "ALT", "--json");
388
- const explicitEdit = invoke("project", "edit", "ALT", "--name", "Edited explicitly", "--json");
389
- const explicitDelete = invoke("project", "delete", "ALT", "--yes", "--json");
390
- expect(explicitList.code).toBe(0);
391
- expect(explicitSearch.code).toBe(0);
392
- expect(JSON.parse(explicitCreate.stdout).key).toBe("ALT-2");
393
- expect(JSON.parse(explicitView.stdout).key).toBe("ALT");
394
- expect(explicitColumns.code).toBe(0);
395
- expect(JSON.parse(explicitEdit.stdout).name).toBe("Edited explicitly");
396
- expect(JSON.parse(explicitDelete.stdout).key).toBe("ALT");
397
- expect(JSON.parse(invoke("project", "view", "DEF", "--json").stdout).name).toBe("Default");
398
-
399
- writeFileSync(join(repository, "zinn.local.json"), "{}");
400
- writeGlobalSettings(directory, { defaultProject: "NOPE" });
401
- assertUnknownDefaultDoesNotMutate(globalSettingsPath);
402
- });
403
- });
404
-
405
- test("project delete with a default prompts despite --yes, and refuses without a terminal or with JSON", async () => {
406
- const directory = mkdtempSync(PREFIX);
407
-
408
- try {
409
- const { repository } = makeRepository(directory, { defaultProject: "DEF" });
410
- seedProjects(directory);
411
- const jsonRefusal = runZinnFrom(["project", "delete", "--yes", "--json"], directory, repository);
412
- expect(jsonRefusal.code).toBe(1);
413
- expect(jsonRefusal.stdout).toBe("");
414
- const refusal = JSON.parse(jsonRefusal.stderr);
415
- expect(Object.keys(refusal)).toEqual(["error"]);
416
- expect(Object.keys(refusal.error)).toEqual(["message"]);
417
- expect(typeof refusal.error.message).toBe("string");
418
- expect(JSON.parse(runZinnFrom(["project", "list", "--json"], directory, repository).stdout)).toHaveLength(2);
419
-
420
- const noInput = runZinnFrom(["project", "delete", "--yes"], directory, repository);
421
- const pipedInput = runZinnWithPipedYes(["project", "delete", "--yes"], directory, repository);
422
- for (const result of [noInput, pipedInput]) {
423
- expect(result.code).toBe(1);
424
- expect(result.stdout).toBe("");
425
- expect(result.stderr).toContain("zinn project delete DEF --yes");
426
- }
427
- expect(JSON.parse(runZinnFrom(["project", "list", "--json"], directory, repository).stdout)).toHaveLength(2);
428
-
429
- const prompted = await runZinnInTerminal(["project", "delete", "--yes"], directory, repository, "y\n");
430
- expect(prompted.code).toBe(0);
431
- expect(prompted.output).toContain("--yes");
432
- expect(prompted.output).toContain("Default");
433
- expect(JSON.parse(runZinnFrom(["project", "list", "--json"], directory, repository).stdout).map((project: { key: string }) => project.key)).toEqual(["ALT"]);
434
- } finally {
435
- removeTestDirectory(directory);
436
- }
437
- });
438
-
439
- test("malformed defaultProject settings still fail with a typed key or --all-projects", () => {
440
- withTestDirectory((directory) => {
441
- const { repository, localSettingsPath } = makeRepository(directory, { defaultProject: 42 });
442
- seedProjects(directory);
443
- const typed = runZinnFrom(["task", "list", "ALT", "--json"], directory, repository);
444
- const allProjects = runZinnFrom(["task", "list", "--all-projects", "--json"], directory, repository);
445
-
446
- for (const result of [typed, allProjects]) {
447
- expect(result.code).toBe(1);
448
- expect(result.stderr).toContain(localSettingsPath);
449
- expect(result.stderr).toContain("defaultProject");
450
- }
451
- });
452
- });
453
-
454
- test("project delete with a typed key and --yes deletes without prompting", () => {
455
- withTestDirectory((directory) => {
456
- const { repository } = makeRepository(directory, { defaultProject: "DEF" });
457
- seedProjects(directory);
458
-
459
- const result = runZinnFrom(["project", "delete", "ALT", "--yes", "--json"], directory, repository);
460
- expect(result.code).toBe(0);
461
- expect(JSON.parse(result.stdout).key).toBe("ALT");
462
- expect(JSON.parse(runZinnFrom(["project", "list", "--json"], directory, repository).stdout).map((project: { key: string }) => project.key)).toEqual(["DEF"]);
463
- });
464
- });
465
-
466
- test("column writes still require an explicit project key when a default is set", () => {
467
- withTestDirectory((directory) => {
468
- const { repository } = makeRepository(directory, { defaultProject: "DEF" });
469
- seedProjects(directory);
470
- const invoke = (...args: Array<string>) => runZinnFrom(args, directory, repository);
471
-
472
- expect(invoke("project", "column", "create", "Later", "--json").code).toBe(1);
473
- expect(invoke("project", "column", "edit", "Backlog", "--name", "Later", "--json").code).toBe(1);
474
- expect(invoke("project", "column", "order", "Backlog", "first", "--json").code).toBe(1);
475
- const listedColumns = invoke("project", "column", "list", "--json");
476
- expect(listedColumns.code).toBe(0);
477
- expect(JSON.parse(listedColumns.stdout).map((column: { name: string }) => column.name)).toContain("Only DEF");
478
- });
479
- });
package/src/project.ts DELETED
@@ -1,53 +0,0 @@
1
- import { homedir } from "node:os";
2
- import { project, resolveSettings } from "@zinn-dev/core";
3
- import { UsageError } from "./lib";
4
-
5
- /** Chooses the explicit project or the configured default, retaining unscoped list/search behavior. */
6
- export function resolveProjectKey(
7
- typedKey: string | undefined,
8
- {
9
- required = false,
10
- allProjects = false,
11
- useDefault = true,
12
- }: {
13
- required?: boolean;
14
- allProjects?: boolean;
15
- useDefault?: boolean;
16
- } = {},
17
- ): string | undefined {
18
- if (allProjects) {
19
- if (typedKey !== undefined) {
20
- throw new UsageError("--all-projects cannot be combined with a project key");
21
- }
22
- return undefined;
23
- }
24
-
25
- if (typedKey !== undefined) {
26
- return typedKey;
27
- }
28
-
29
- if (!useDefault) {
30
- return undefined;
31
- }
32
-
33
- const settings = resolveSettings({ cwd: process.cwd(), env: process.env, home: homedir() });
34
- const defaultProject = settings.merged.defaultProject;
35
- if (defaultProject === undefined) {
36
- if (required) {
37
- throw new UsageError(
38
- "Missing argument <project-key>. Supply a key or configure defaultProject",
39
- );
40
- }
41
- return undefined;
42
- }
43
-
44
- const settingsFile =
45
- settings.local?.settings.defaultProject === undefined ? settings.global! : settings.local;
46
- if (project.getByKey(defaultProject) == null) {
47
- throw new Error(
48
- `Default project "${defaultProject}" from settings file ${settingsFile.path} does not exist`,
49
- );
50
- }
51
-
52
- return defaultProject;
53
- }
@@ -1,24 +0,0 @@
1
- import type { Command } from "../types";
2
-
3
- import { parseArgs } from "node:util";
4
- import { backup } from "@zinn-dev/core";
5
-
6
- import { output, validatePositionals } from "../lib";
7
- import packageJson from "../../package.json";
8
-
9
- export const backupCommand = {
10
- run: (args) => {
11
- const { positionals } = parseArgs({ args, strict: true, allowPositionals: true });
12
- validatePositionals(positionals, []);
13
-
14
- const created = backup.create({ client: "cli", version: packageJson.version });
15
-
16
- output(created, (b) => `Saved a backup to ${b.path}`);
17
- },
18
- help: `Usage: zinn backup
19
-
20
- Save a copy of the database to ~/.zinn/backups/, or to backups/ in ZINN_DIR when it is set.
21
-
22
- The copy includes recent writes and is safe to take while Zinn is in use.
23
- Fails when there is no database yet. Existing backups are never overwritten.`,
24
- } satisfies Command;