@zinn-dev/cli 0.39.0 → 0.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +171 -40
- package/index.d.ts +2 -0
- package/index.js +34 -0
- package/package.json +27 -18
- package/skills/zinn/SKILL.md +63 -0
- package/src/actor.d.ts +7 -0
- package/src/actor.js +129 -0
- package/src/capabilities-schema.d.ts +163 -0
- package/src/capabilities-schema.js +124 -0
- package/src/capabilities.d.ts +21 -0
- package/src/capabilities.js +22 -0
- package/src/database.d.ts +6 -0
- package/src/database.js +27 -0
- package/src/errors.d.ts +158 -0
- package/src/errors.js +224 -0
- package/src/input.d.ts +116 -0
- package/src/input.js +119 -0
- package/src/lib.d.ts +66 -0
- package/src/lib.js +333 -0
- package/src/presentation/activity-presentation.d.ts +23 -0
- package/src/presentation/activity-presentation.js +403 -0
- package/src/presentation/color.d.ts +9 -0
- package/src/presentation/color.js +21 -0
- package/src/presentation/column-presentation.d.ts +14 -0
- package/src/presentation/column-presentation.js +29 -0
- package/src/presentation/description-presentation.d.ts +3 -0
- package/src/presentation/description-presentation.js +11 -0
- package/src/presentation/info-presentation.d.ts +4 -0
- package/src/presentation/info-presentation.js +96 -0
- package/src/presentation/row-layout.d.ts +22 -0
- package/src/presentation/row-layout.js +58 -0
- package/src/presentation/section.d.ts +4 -0
- package/src/presentation/section.js +9 -0
- package/src/presentation/task-presentation.d.ts +9 -0
- package/src/presentation/task-presentation.js +62 -0
- package/src/presentation/time.d.ts +1 -0
- package/src/presentation/time.js +13 -0
- package/src/project.d.ts +12 -0
- package/src/project.js +44 -0
- package/src/release-identity.d.ts +2 -0
- package/src/release-identity.js +1 -0
- package/src/result-schemas.d.ts +987 -0
- package/src/result-schemas.js +220 -0
- package/src/results.d.ts +23 -0
- package/src/results.js +17 -0
- package/src/routes/activity-router.d.ts +81 -0
- package/src/routes/activity-router.js +119 -0
- package/src/routes/backup-router.d.ts +21 -0
- package/src/routes/backup-router.js +30 -0
- package/src/routes/capabilities-router.d.ts +21 -0
- package/src/routes/capabilities-router.js +28 -0
- package/src/routes/comment-router.d.ts +203 -0
- package/src/routes/comment-router.js +241 -0
- package/src/routes/info-router.d.ts +21 -0
- package/src/routes/info-router.js +29 -0
- package/src/routes/label-router.d.ts +183 -0
- package/src/routes/label-router.js +218 -0
- package/src/routes/migrate-router.d.ts +21 -0
- package/src/routes/migrate-router.js +49 -0
- package/src/routes/project-router.d.ts +389 -0
- package/src/routes/project-router.js +514 -0
- package/src/routes/reindex-router.d.ts +21 -0
- package/src/routes/reindex-router.js +33 -0
- package/src/routes/relation-router.d.ts +124 -0
- package/src/routes/relation-router.js +223 -0
- package/src/routes/restore-router.d.ts +33 -0
- package/src/routes/restore-router.js +88 -0
- package/src/routes/router.d.ts +6 -0
- package/src/routes/router.js +204 -0
- package/src/routes/routes.d.ts +1735 -0
- package/src/routes/routes.js +23 -0
- package/src/routes/task-router.d.ts +945 -0
- package/src/routes/task-router.js +789 -0
- package/src/text-edit.d.ts +43 -0
- package/src/text-edit.js +57 -0
- package/src/types.d.ts +24 -0
- package/src/types.js +1 -0
- package/index.ts +0 -26
- package/release-stamp.json +0 -1
- package/src/actor.test.ts +0 -41
- package/src/actor.ts +0 -78
- package/src/database.test.ts +0 -70
- package/src/database.ts +0 -41
- package/src/lib.test.ts +0 -86
- package/src/lib.ts +0 -159
- package/src/presentation/activity-presentation.test.ts +0 -112
- package/src/presentation/activity-presentation.ts +0 -490
- package/src/presentation/color.ts +0 -29
- package/src/presentation/column-presentation.ts +0 -48
- package/src/presentation/description-presentation.ts +0 -18
- package/src/presentation/row-layout.ts +0 -77
- package/src/presentation/section.ts +0 -16
- package/src/presentation/task-presentation.ts +0 -92
- package/src/presentation/time.ts +0 -17
- package/src/project.test.ts +0 -479
- package/src/project.ts +0 -53
- package/src/routes/activity-router.ts +0 -81
- package/src/routes/backup-router.ts +0 -24
- package/src/routes/comment-router.ts +0 -171
- package/src/routes/label-router.ts +0 -168
- package/src/routes/migrate-router.test.ts +0 -31
- package/src/routes/migrate-router.ts +0 -48
- package/src/routes/project-router.ts +0 -415
- package/src/routes/reindex-router.ts +0 -27
- package/src/routes/relation-router.ts +0 -194
- package/src/routes/restore-router.test.ts +0 -44
- package/src/routes/restore-router.ts +0 -90
- package/src/routes/router.test.ts +0 -136
- package/src/routes/router.ts +0 -248
- package/src/routes/routes.ts +0 -20
- package/src/routes/task-router.ts +0 -649
- package/src/text-edit.ts +0 -68
- package/src/types.ts +0 -6
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { TextEdit } from "@zinn-dev/core";
|
|
2
|
+
/** The flags that change how a text flag (--description, --text) is applied to the stored text. */
|
|
3
|
+
export declare const textEditOptions: {
|
|
4
|
+
readonly append: {
|
|
5
|
+
readonly type: "boolean";
|
|
6
|
+
};
|
|
7
|
+
readonly replacing: {
|
|
8
|
+
readonly type: "string";
|
|
9
|
+
readonly value: {
|
|
10
|
+
readonly kind: "text";
|
|
11
|
+
readonly name: "old";
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
readonly all: {
|
|
15
|
+
readonly type: "boolean";
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export declare function textEditConstraints(textFlag: "--description" | "--text"): readonly [{
|
|
19
|
+
readonly kind: "exclusive";
|
|
20
|
+
readonly members: readonly ["option:append", "option:replacing"];
|
|
21
|
+
}, {
|
|
22
|
+
readonly kind: "requires";
|
|
23
|
+
readonly input: "option:append";
|
|
24
|
+
readonly requires: `option:${string}`;
|
|
25
|
+
}, {
|
|
26
|
+
readonly kind: "requires";
|
|
27
|
+
readonly input: "option:replacing";
|
|
28
|
+
readonly requires: `option:${string}`;
|
|
29
|
+
}, {
|
|
30
|
+
readonly kind: "requires";
|
|
31
|
+
readonly input: "option:all";
|
|
32
|
+
readonly requires: "option:replacing";
|
|
33
|
+
}];
|
|
34
|
+
type TextEditFlags = {
|
|
35
|
+
append?: boolean | undefined;
|
|
36
|
+
replacing?: string | undefined;
|
|
37
|
+
all?: boolean | undefined;
|
|
38
|
+
};
|
|
39
|
+
/** Help paragraph for the text-edit flags of a command whose text flag is `textFlag`. */
|
|
40
|
+
export declare function textEditHelp(textFlag: "--description" | "--text"): string;
|
|
41
|
+
/** The value to send to core for a text flag: the full text, or one edit object. */
|
|
42
|
+
export declare function resolveTextEdit(text: string | undefined, flags: TextEditFlags, textFlag: "--description" | "--text"): string | TextEdit | undefined;
|
|
43
|
+
export {};
|
package/src/text-edit.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { quit } from "./lib.js";
|
|
2
|
+
import { getOptionInputs, violatesConstraint } from "./input.js";
|
|
3
|
+
/** The flags that change how a text flag (--description, --text) is applied to the stored text. */
|
|
4
|
+
export const textEditOptions = {
|
|
5
|
+
append: { type: "boolean" },
|
|
6
|
+
replacing: { type: "string", value: { kind: "text", name: "old" } },
|
|
7
|
+
all: { type: "boolean" },
|
|
8
|
+
};
|
|
9
|
+
export function textEditConstraints(textFlag) {
|
|
10
|
+
const textInput = `option:${textFlag.slice(2)}`;
|
|
11
|
+
return [
|
|
12
|
+
{ kind: "exclusive", members: ["option:append", "option:replacing"] },
|
|
13
|
+
{ kind: "requires", input: "option:append", requires: textInput },
|
|
14
|
+
{ kind: "requires", input: "option:replacing", requires: textInput },
|
|
15
|
+
{ kind: "requires", input: "option:all", requires: "option:replacing" },
|
|
16
|
+
];
|
|
17
|
+
}
|
|
18
|
+
/** Help paragraph for the text-edit flags of a command whose text flag is `textFlag`. */
|
|
19
|
+
export function textEditHelp(textFlag) {
|
|
20
|
+
return `With --append, the ${textFlag} text is added as a new paragraph.
|
|
21
|
+
With --replacing <old>, it replaces the one exact, case-sensitive match of <old>
|
|
22
|
+
--all replaces every match.
|
|
23
|
+
|
|
24
|
+
No match, or several matches without --all, fails without changing anything.
|
|
25
|
+
An empty ${textFlag} with --replacing deletes the match.`;
|
|
26
|
+
}
|
|
27
|
+
/** The value to send to core for a text flag: the full text, or one edit object. */
|
|
28
|
+
export function resolveTextEdit(text, flags, textFlag) {
|
|
29
|
+
const isAppending = flags.append === true;
|
|
30
|
+
const [exclusiveModifiers, appendRequiresText, replacingRequiresText, allRequiresReplacing] = textEditConstraints(textFlag);
|
|
31
|
+
const inputs = getOptionInputs({ ...flags, [textFlag.slice(2)]: text });
|
|
32
|
+
if (violatesConstraint(exclusiveModifiers, inputs)) {
|
|
33
|
+
quit(`Option "--append" cannot be used with "--replacing"`, undefined, {
|
|
34
|
+
argument: "--append",
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
if (violatesConstraint(appendRequiresText, inputs) ||
|
|
38
|
+
violatesConstraint(replacingRequiresText, inputs)) {
|
|
39
|
+
const modifier = isAppending ? "--append" : "--replacing";
|
|
40
|
+
quit(`Option "${modifier}" needs "${textFlag}"`, undefined, { argument: textFlag });
|
|
41
|
+
}
|
|
42
|
+
if (violatesConstraint(allRequiresReplacing, inputs)) {
|
|
43
|
+
quit(`Option "--all" needs "--replacing"`, undefined, { argument: "--all" });
|
|
44
|
+
}
|
|
45
|
+
if (text === undefined) {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
if (isAppending) {
|
|
49
|
+
return { append: text };
|
|
50
|
+
}
|
|
51
|
+
if (flags.replacing !== undefined) {
|
|
52
|
+
return flags.all === true
|
|
53
|
+
? { find: flags.replacing, replace: text, all: true }
|
|
54
|
+
: { find: flags.replacing, replace: text };
|
|
55
|
+
}
|
|
56
|
+
return text;
|
|
57
|
+
}
|
package/src/types.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { CommandInput } from "./input.js";
|
|
2
|
+
import type { CommandResult } from "./results.js";
|
|
3
|
+
export type Command = {
|
|
4
|
+
input: CommandInput;
|
|
5
|
+
results: readonly CommandResult[];
|
|
6
|
+
summary: string;
|
|
7
|
+
effects: {
|
|
8
|
+
domain: "read" | "create" | "update" | "delete" | "replace" | "none";
|
|
9
|
+
storage: "normal-open" | "backup" | "restore" | "migrate" | "reindex" | "inspect" | "none";
|
|
10
|
+
};
|
|
11
|
+
confirmation: {
|
|
12
|
+
kind: "none" | "prompt" | "project-delete";
|
|
13
|
+
description?: string;
|
|
14
|
+
};
|
|
15
|
+
run: (args: Array<string>) => void | Promise<void>;
|
|
16
|
+
help: string;
|
|
17
|
+
guessedFlags?: Record<string, (args: Array<string>) => Array<string>>;
|
|
18
|
+
};
|
|
19
|
+
export type ParsedRoute = {
|
|
20
|
+
command: string;
|
|
21
|
+
} & Command;
|
|
22
|
+
export type RouteDef = {
|
|
23
|
+
[key: string]: Command | RouteDef;
|
|
24
|
+
};
|
package/src/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/index.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
|
|
3
|
-
import packageJson from "./package.json";
|
|
4
|
-
|
|
5
|
-
const args = process.argv.slice(2);
|
|
6
|
-
const isVersionRequest = args.length === 1 && ["-v", "--version"].includes(args[0]!);
|
|
7
|
-
|
|
8
|
-
if (isVersionRequest) {
|
|
9
|
-
console.info(packageJson.version);
|
|
10
|
-
} else {
|
|
11
|
-
const { isHelpFlag } = await import("./src/lib");
|
|
12
|
-
const { createRouter } = await import("./src/routes/router");
|
|
13
|
-
const { routes } = await import("./src/routes/routes");
|
|
14
|
-
const router = createRouter(routes);
|
|
15
|
-
|
|
16
|
-
if (args.length === 0 || isHelpFlag(args[0]!)) {
|
|
17
|
-
console.info(router.help);
|
|
18
|
-
} else {
|
|
19
|
-
const { configureDatabase } = await import("@zinn-dev/core");
|
|
20
|
-
const { cliOpenPolicy } = await import("./src/database");
|
|
21
|
-
|
|
22
|
-
// Only records the policy. core then opens the database when a command first uses it.
|
|
23
|
-
configureDatabase(cliOpenPolicy());
|
|
24
|
-
router.route(args);
|
|
25
|
-
}
|
|
26
|
-
}
|
package/release-stamp.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"release":"published"}
|
package/src/actor.test.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import type { Actor } from "@zinn-dev/core";
|
|
2
|
-
|
|
3
|
-
import { expect, test } from "bun:test";
|
|
4
|
-
|
|
5
|
-
import { detectAgent } from "./actor";
|
|
6
|
-
|
|
7
|
-
const agent = (name: string): Actor => ({ name, email: null, kind: "agent" });
|
|
8
|
-
|
|
9
|
-
test("AI_AGENT is recorded as given, ahead of tool markers", () => {
|
|
10
|
-
expect(detectAgent({ AI_AGENT: "claude-code_2-1-280_agent", CLAUDECODE: "1" })).toEqual(
|
|
11
|
-
agent("claude-code_2-1-280_agent"),
|
|
12
|
-
);
|
|
13
|
-
expect(detectAgent({ AI_AGENT: "hermes" })).toEqual(agent("hermes"));
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
test("AGENT comes before CLAUDECODE, since Amp sets both", () => {
|
|
17
|
-
expect(detectAgent({ AGENT: "amp", CLAUDECODE: "1" })).toEqual(agent("amp"));
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test("tool-specific markers map to fixed names", () => {
|
|
21
|
-
const cases: Array<[Record<string, string>, string]> = [
|
|
22
|
-
[{ CLAUDECODE: "1" }, "claude-code"],
|
|
23
|
-
[{ CODEX_THREAD_ID: "thread" }, "codex"],
|
|
24
|
-
[{ CODEX_SANDBOX: "seatbelt" }, "codex"],
|
|
25
|
-
[{ OPENCODE: "1" }, "opencode"],
|
|
26
|
-
[{ GEMINI_CLI: "1" }, "gemini-cli"],
|
|
27
|
-
[{ CLINE_ACTIVE: "true" }, "cline"],
|
|
28
|
-
[{ CURSOR_AGENT: "1" }, "cursor"],
|
|
29
|
-
[{ COPILOT_AGENT_SESSION_ID: "session" }, "copilot"],
|
|
30
|
-
[{ CRUSH: "1" }, "crush"],
|
|
31
|
-
];
|
|
32
|
-
|
|
33
|
-
for (const [env, name] of cases) {
|
|
34
|
-
expect(detectAgent(env)).toEqual(agent(name));
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
test("empty or unrelated variables are not agents", () => {
|
|
39
|
-
expect(detectAgent({})).toBeNull();
|
|
40
|
-
expect(detectAgent({ AI_AGENT: " ", AGENT: "", HOME: "/home/someone" })).toBeNull();
|
|
41
|
-
});
|
package/src/actor.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import type { Actor } from "@zinn-dev/core";
|
|
2
|
-
|
|
3
|
-
import { execFileSync } from "node:child_process";
|
|
4
|
-
import { userInfo } from "node:os";
|
|
5
|
-
|
|
6
|
-
type Env = Record<string, string | undefined>;
|
|
7
|
-
type AgentMarker = { variables: Array<string>; name: string | null };
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Environment variables that coding agents set in the shells they run.
|
|
11
|
-
* Undetected tools can set `AI_AGENT` through their own env configuration.
|
|
12
|
-
*/
|
|
13
|
-
const AGENT_MARKERS: Array<AgentMarker> = [
|
|
14
|
-
{ variables: ["AI_AGENT"], name: null },
|
|
15
|
-
{ variables: ["AGENT"], name: null },
|
|
16
|
-
{ variables: ["CLAUDECODE"], name: "claude-code" },
|
|
17
|
-
{ variables: ["CODEX_THREAD_ID", "CODEX_SANDBOX"], name: "codex" },
|
|
18
|
-
{ variables: ["OPENCODE"], name: "opencode" },
|
|
19
|
-
{ variables: ["GEMINI_CLI"], name: "gemini-cli" },
|
|
20
|
-
{ variables: ["CLINE_ACTIVE"], name: "cline" },
|
|
21
|
-
{ variables: ["CURSOR_AGENT"], name: "cursor" },
|
|
22
|
-
{ variables: ["COPILOT_AGENT_SESSION_ID"], name: "copilot" },
|
|
23
|
-
{ variables: ["CRUSH"], name: "crush" },
|
|
24
|
-
];
|
|
25
|
-
|
|
26
|
-
/** A marker without a fixed name records the variable's own value, e.g. `AI_AGENT=claude-code_2-1-280_agent`. */
|
|
27
|
-
export function detectAgent(env: Env): Actor | null {
|
|
28
|
-
for (const marker of AGENT_MARKERS) {
|
|
29
|
-
for (const variable of marker.variables) {
|
|
30
|
-
const value = env[variable]?.trim();
|
|
31
|
-
if (value == null || value.length === 0) {
|
|
32
|
-
continue;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return { name: marker.name ?? value, email: null, kind: "agent" };
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function readGitConfig(key: string) {
|
|
43
|
-
try {
|
|
44
|
-
const value = execFileSync("git", ["config", key], {
|
|
45
|
-
encoding: "utf-8",
|
|
46
|
-
stdio: ["ignore", "pipe", "ignore"],
|
|
47
|
-
}).trim();
|
|
48
|
-
|
|
49
|
-
return value.length > 0 ? value : null;
|
|
50
|
-
} catch {
|
|
51
|
-
// git is missing or the key is unset
|
|
52
|
-
return null;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function resolveActor(): Actor {
|
|
57
|
-
const agent = detectAgent(process.env);
|
|
58
|
-
if (agent != null) {
|
|
59
|
-
return agent;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const gitName = readGitConfig("user.name");
|
|
63
|
-
if (gitName != null) {
|
|
64
|
-
return { name: gitName, email: readGitConfig("user.email"), kind: "human" };
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return { name: userInfo().username, email: null, kind: "human" };
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
let actor: Actor | null = null;
|
|
71
|
-
|
|
72
|
-
export function currentActor(): Actor {
|
|
73
|
-
if (actor == null) {
|
|
74
|
-
actor = resolveActor();
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return actor;
|
|
78
|
-
}
|
package/src/database.test.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { expect, spyOn, test } from "bun:test";
|
|
2
|
-
import { existsSync } from "node:fs";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
import { fileURLToPath } from "node:url";
|
|
5
|
-
|
|
6
|
-
import packageJson from "../package.json";
|
|
7
|
-
import { cliOpenPolicy, reportLegacyRead, reportLockWait } from "./database";
|
|
8
|
-
import { setLegacyReadAllowed } from "./lib";
|
|
9
|
-
|
|
10
|
-
const CLI_DIRECTORY = fileURLToPath(new URL("..", import.meta.url));
|
|
11
|
-
// The file name core's release reading looks for.
|
|
12
|
-
const RELEASE_STAMP_FILE = "release-stamp.json";
|
|
13
|
-
|
|
14
|
-
test("the CLI opens the database as the cli client of its own release mode, without legacy reads", () => {
|
|
15
|
-
const { onWait, onLegacyRead, ...policy } = cliOpenPolicy();
|
|
16
|
-
const stderr = spyOn(console, "error").mockImplementation(() => {});
|
|
17
|
-
|
|
18
|
-
try {
|
|
19
|
-
onWait?.({ databasePath: "/data/zinn.sqlite", budgetMs: 30000 });
|
|
20
|
-
|
|
21
|
-
expect(policy).toEqual({
|
|
22
|
-
client: "cli",
|
|
23
|
-
version: packageJson.version,
|
|
24
|
-
executable: process.argv[1] ?? process.execPath,
|
|
25
|
-
release: "source",
|
|
26
|
-
allowsLegacyRead: false,
|
|
27
|
-
});
|
|
28
|
-
expect(onWait).toBe(reportLockWait);
|
|
29
|
-
expect(stderr.mock.calls).toEqual([
|
|
30
|
-
["Another process is using the database. Waiting up to 30 seconds for it to finish."],
|
|
31
|
-
]);
|
|
32
|
-
} finally {
|
|
33
|
-
stderr.mockRestore();
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
test("the policy allows legacy reads only once --allow-legacy-read was handled", () => {
|
|
38
|
-
const policy = cliOpenPolicy();
|
|
39
|
-
|
|
40
|
-
try {
|
|
41
|
-
expect(policy.allowsLegacyRead).toBe(false);
|
|
42
|
-
setLegacyReadAllowed(true);
|
|
43
|
-
expect(policy.allowsLegacyRead).toBe(true);
|
|
44
|
-
} finally {
|
|
45
|
-
setLegacyReadAllowed(false);
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test("the policy warns on stderr about the unmarked database that opened for reading", () => {
|
|
50
|
-
const stderr = spyOn(console, "error").mockImplementation(() => {});
|
|
51
|
-
|
|
52
|
-
try {
|
|
53
|
-
expect(cliOpenPolicy().onLegacyRead).toBe(reportLegacyRead);
|
|
54
|
-
reportLegacyRead({ databasePath: "/data/zinn.sqlite", version: 0, latestVersion: 1 });
|
|
55
|
-
|
|
56
|
-
expect(stderr.mock.calls).toHaveLength(1);
|
|
57
|
-
expect(stderr.mock.calls[0]![0]).toContain("/data/zinn.sqlite");
|
|
58
|
-
expect(stderr.mock.calls[0]![0]).toContain("no schema version");
|
|
59
|
-
} finally {
|
|
60
|
-
stderr.mockRestore();
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
test("this source folder has no stamp", () => {
|
|
65
|
-
expect(existsSync(join(CLI_DIRECTORY, RELEASE_STAMP_FILE))).toBe(false);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
test("the published package includes the stamp", () => {
|
|
69
|
-
expect(packageJson.files).toContain(RELEASE_STAMP_FILE);
|
|
70
|
-
});
|
package/src/database.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import type { DatabaseFacts, LockWait, OpenPolicy } from "@zinn-dev/core";
|
|
2
|
-
|
|
3
|
-
import { fileURLToPath } from "node:url";
|
|
4
|
-
import { readReleaseMode } from "@zinn-dev/core";
|
|
5
|
-
|
|
6
|
-
import packageJson from "../package.json";
|
|
7
|
-
import { isLegacyReadAllowed } from "./lib";
|
|
8
|
-
|
|
9
|
-
const MS_PER_SECOND = 1000;
|
|
10
|
-
/** The CLI's package folder, which holds its release stamp once it is packed. */
|
|
11
|
-
const PACKAGE_DIRECTORY = fileURLToPath(new URL("..", import.meta.url));
|
|
12
|
-
|
|
13
|
-
/** Says once on stderr that the command waits for another process to finish with the database. */
|
|
14
|
-
export function reportLockWait({ budgetMs }: LockWait) {
|
|
15
|
-
console.error(
|
|
16
|
-
`Another process is using the database. Waiting up to ${budgetMs / MS_PER_SECOND} seconds for it to finish.`,
|
|
17
|
-
);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function reportLegacyRead({ databasePath }: DatabaseFacts) {
|
|
21
|
-
console.error(
|
|
22
|
-
`Warning: the database at ${databasePath} has no schema version. Reads are best effort and may fail or omit data.`,
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** How the CLI asks core to open the database. */
|
|
27
|
-
export function cliOpenPolicy(): OpenPolicy {
|
|
28
|
-
return {
|
|
29
|
-
client: "cli",
|
|
30
|
-
version: packageJson.version,
|
|
31
|
-
// The script the CLI runs from; the runtime itself when there is none.
|
|
32
|
-
executable: process.argv[1] ?? process.execPath,
|
|
33
|
-
release: readReleaseMode(PACKAGE_DIRECTORY),
|
|
34
|
-
// Read when the database opens, after the router has handled `--allow-legacy-read`
|
|
35
|
-
get allowsLegacyRead() {
|
|
36
|
-
return isLegacyReadAllowed();
|
|
37
|
-
},
|
|
38
|
-
onWait: reportLockWait,
|
|
39
|
-
onLegacyRead: reportLegacyRead,
|
|
40
|
-
};
|
|
41
|
-
}
|
package/src/lib.test.ts
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import type { OpenPolicy } from "@zinn-dev/core";
|
|
2
|
-
|
|
3
|
-
import { expect, test } from "bun:test";
|
|
4
|
-
import { ReadOnlyDatabaseError, TextEditMatchError, UnmarkedDatabaseError } from "@zinn-dev/core";
|
|
5
|
-
|
|
6
|
-
import { failureMessage, UsageError, validatePositionals } from "./lib";
|
|
7
|
-
|
|
8
|
-
const FALLBACK = "Something went wrong";
|
|
9
|
-
const POLICY: OpenPolicy = {
|
|
10
|
-
client: "cli",
|
|
11
|
-
version: "1.0.0",
|
|
12
|
-
executable: "/test/zinn",
|
|
13
|
-
release: "source",
|
|
14
|
-
allowsLegacyRead: false,
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
function readOnlyError(reason: "older" | "newer" | "unmarked") {
|
|
18
|
-
const versions = { older: 1, newer: 3, unmarked: 0 };
|
|
19
|
-
const version = versions[reason];
|
|
20
|
-
return new ReadOnlyDatabaseError(
|
|
21
|
-
reason,
|
|
22
|
-
{ databasePath: "/data/zinn.sqlite", version, latestVersion: 2 },
|
|
23
|
-
POLICY,
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
test("an older database that stays read-only points at zinn migrate in the same line", () => {
|
|
28
|
-
const error = readOnlyError("older");
|
|
29
|
-
|
|
30
|
-
expect(failureMessage(error, FALLBACK)).toBe(
|
|
31
|
-
`${error.message}. Run \`zinn migrate\` to upgrade it.`,
|
|
32
|
-
);
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
test("other failures keep their own message, or the fallback when they have none", () => {
|
|
36
|
-
const newer = readOnlyError("newer");
|
|
37
|
-
|
|
38
|
-
expect(failureMessage(newer, FALLBACK)).toBe(newer.message);
|
|
39
|
-
expect(failureMessage(new Error("Task does not exist"), FALLBACK)).toBe("Task does not exist");
|
|
40
|
-
expect(failureMessage(undefined, FALLBACK)).toBe(FALLBACK);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
test("an unmarked database that was not opened names --allow-legacy-read", () => {
|
|
44
|
-
const error = new UnmarkedDatabaseError({
|
|
45
|
-
databasePath: "/data/zinn.sqlite",
|
|
46
|
-
version: 0,
|
|
47
|
-
latestVersion: 2,
|
|
48
|
-
});
|
|
49
|
-
const message = failureMessage(error, FALLBACK);
|
|
50
|
-
|
|
51
|
-
expect(message).toStartWith(error.message);
|
|
52
|
-
expect(message).toContain("/data/zinn.sqlite");
|
|
53
|
-
expect(message).toContain("--allow-legacy-read");
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
test("an unmarked database opened for reading refuses changes without pointing at the flag", () => {
|
|
57
|
-
const error = readOnlyError("unmarked");
|
|
58
|
-
|
|
59
|
-
expect(failureMessage(error, FALLBACK)).toBe(error.message);
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
test("a text edit that matches several places points at --all, and one that matches none does not", () => {
|
|
63
|
-
const several = new TextEditMatchError(3, "ZINN-1's description");
|
|
64
|
-
const none = new TextEditMatchError(0, "ZINN-1's description");
|
|
65
|
-
|
|
66
|
-
expect(failureMessage(several, FALLBACK)).toBe(
|
|
67
|
-
`${several.message} Use --all to replace every match.`,
|
|
68
|
-
);
|
|
69
|
-
expect(failureMessage(none, FALLBACK)).toBe(none.message);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
test("validatePositionals names the first missing word, or the first word past the most it takes", () => {
|
|
73
|
-
const words = ["<project-key>", "<column-name>"];
|
|
74
|
-
|
|
75
|
-
expect(() => validatePositionals(["SHOP", "Later"], words)).not.toThrow();
|
|
76
|
-
expect(() => validatePositionals(["SHOP"], words)).toThrow(UsageError);
|
|
77
|
-
expect(() => validatePositionals(["SHOP"], words)).toThrow("<column-name>");
|
|
78
|
-
expect(() => validatePositionals([], words)).toThrow("<project-key>");
|
|
79
|
-
expect(() => validatePositionals(["SHOP", "Needs", "Review", "Now"], words)).toThrow('"Review"');
|
|
80
|
-
|
|
81
|
-
// ? Written as in the usage lines: [...] marks an optional word and a trailing ... a word that repeats.
|
|
82
|
-
expect(() => validatePositionals([], ["[<project-key>]"])).not.toThrow();
|
|
83
|
-
expect(() => validatePositionals(["SHOP", "extra"], ["[<project-key>]"])).toThrow('"extra"');
|
|
84
|
-
expect(() => validatePositionals(["A-1", "A-2", "A-3"], ["<task-key>..."])).not.toThrow();
|
|
85
|
-
expect(() => validatePositionals(["A-1"], ["<task-key>...", "<target-column>"])).toThrow("<target-column>");
|
|
86
|
-
});
|
package/src/lib.ts
DELETED
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
import type { parseArgs } from "node:util";
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
assertWritable,
|
|
5
|
-
ReadOnlyDatabaseError,
|
|
6
|
-
TextEditMatchError,
|
|
7
|
-
UnmarkedDatabaseError,
|
|
8
|
-
} from "@zinn-dev/core";
|
|
9
|
-
|
|
10
|
-
type ParsedTokens = NonNullable<ReturnType<typeof parseArgs>["tokens"]>;
|
|
11
|
-
type OutputMode = "text" | "json";
|
|
12
|
-
|
|
13
|
-
let outputMode: OutputMode = "text";
|
|
14
|
-
let legacyReadAllowed = false;
|
|
15
|
-
|
|
16
|
-
export function setOutputMode(mode: OutputMode) {
|
|
17
|
-
outputMode = mode;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function isJsonFlag(flag: string) {
|
|
21
|
-
return flag === "--json";
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function isAllowLegacyReadFlag(flag: string) {
|
|
25
|
-
return flag === "--allow-legacy-read";
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function setLegacyReadAllowed(allowed: boolean) {
|
|
29
|
-
legacyReadAllowed = allowed;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function isLegacyReadAllowed() {
|
|
33
|
-
return legacyReadAllowed;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function output<T>(data: T, transformToString: (data: T) => string) {
|
|
37
|
-
if (outputMode === "json") {
|
|
38
|
-
console.info(JSON.stringify(data, null, 2));
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const dataText = transformToString(data);
|
|
43
|
-
|
|
44
|
-
if (dataText.length > 0) {
|
|
45
|
-
console.info(dataText);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** Asks before a change that cannot be undone, unless --yes already confirmed it. */
|
|
50
|
-
export function confirmAction(message: string, isPreconfirmed: boolean, jsonRefusal?: string) {
|
|
51
|
-
// Throws on a read-only database, to avoid asking for confirmation to an already refused action
|
|
52
|
-
assertWritable();
|
|
53
|
-
|
|
54
|
-
return askConfirmation(message, isPreconfirmed, jsonRefusal);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** Like confirmAction, without opening the database, for commands that bypass the open policy. */
|
|
58
|
-
export function askConfirmation(
|
|
59
|
-
message: string,
|
|
60
|
-
isPreconfirmed: boolean,
|
|
61
|
-
jsonRefusal = "Commands that ask for confirmation need --yes when used with --json",
|
|
62
|
-
) {
|
|
63
|
-
if (isPreconfirmed) {
|
|
64
|
-
return true;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (outputMode === "json") {
|
|
68
|
-
quit(jsonRefusal);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// Refuses rather than treating an unanswered prompt as declined, so scripts do not see a false success
|
|
72
|
-
if (!canPrompt()) {
|
|
73
|
-
quit("Commands that ask for confirmation need --yes when not running in a terminal");
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return confirm(message);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** Whether someone can answer a prompt: stdin and stdout are both terminals, like how gh decides it. */
|
|
80
|
-
export function canPrompt() {
|
|
81
|
-
return process.stdin.isTTY === true && process.stdout.isTTY === true;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** The message to report for a failed command, or fallback when the error has none. */
|
|
85
|
-
export function failureMessage(error: any, fallback: string) {
|
|
86
|
-
const isOlderReadOnly = error instanceof ReadOnlyDatabaseError && error.reason === "older";
|
|
87
|
-
if (isOlderReadOnly) {
|
|
88
|
-
return `${error.message}. Run \`zinn migrate\` to upgrade it.`;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if (error instanceof UnmarkedDatabaseError) {
|
|
92
|
-
return `${error.message}. Add --allow-legacy-read to read it anyway.`;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (error instanceof TextEditMatchError && error.matchCount > 1) {
|
|
96
|
-
return `${error.message} Use --all to replace every match.`;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (error?.message === "no such table: task_search") {
|
|
100
|
-
return `${error.message}. Run \`zinn reindex\` to rebuild the search index.`;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return error?.message ?? fallback;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/** Prints the reason as an error, followed by help when given, and exits with code 1. */
|
|
107
|
-
export function quit(reason: string, help?: string): never {
|
|
108
|
-
if (outputMode === "json") {
|
|
109
|
-
const error = help == null ? { message: reason } : { message: reason, help };
|
|
110
|
-
console.error(JSON.stringify({ error }, null, 2));
|
|
111
|
-
} else {
|
|
112
|
-
console.error(help == null ? reason : `${reason}\n\n${help}`);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
process.exit(1);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/** A command was given the wrong words. The router prints the message with the command's usage lines. */
|
|
119
|
-
export class UsageError extends Error {}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Checks a command's words against its usage line, with the words written the same way:
|
|
123
|
-
* `<task-key>` is required, `[<project-key>]` is optional and `<task-key>...` repeats.
|
|
124
|
-
* Optional words go last.
|
|
125
|
-
*/
|
|
126
|
-
export function validatePositionals(positionals: Array<string>, words: Array<string>) {
|
|
127
|
-
const requiredWords = words.filter((word) => !word.startsWith("["));
|
|
128
|
-
const missingWord = requiredWords[positionals.length];
|
|
129
|
-
if (missingWord !== undefined) {
|
|
130
|
-
throw new UsageError(`Missing argument ${missingWord}`);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const hasRepeatingWord = words.some((word) => word.endsWith("..."));
|
|
134
|
-
const extraWord = positionals[words.length];
|
|
135
|
-
if (!hasRepeatingWord && extraWord !== undefined) {
|
|
136
|
-
throw new UsageError(`Unexpected argument "${extraWord}"`);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
export function validateUniqueOptions(tokens: ParsedTokens, repeatableOptions: Array<string> = []) {
|
|
141
|
-
const parsedArgs = new Set<string>();
|
|
142
|
-
|
|
143
|
-
for (const token of tokens) {
|
|
144
|
-
if (token.kind !== "option") {
|
|
145
|
-
continue;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const isRepeatable = repeatableOptions.includes(token.name);
|
|
149
|
-
if (parsedArgs.has(token.name) && !isRepeatable) {
|
|
150
|
-
quit(`Option "--${token.name}" can only be specified once`);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
parsedArgs.add(token.name);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
export function isHelpFlag(flag: string) {
|
|
158
|
-
return ["-h", "--help"].includes(flag);
|
|
159
|
-
}
|