@finebi/finebi-cli 1.3.0 → 1.4.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.
@@ -1,94 +1,94 @@
1
- import {createHash, randomUUID} from "node:crypto";
2
- import {existsSync} from "node:fs";
3
- import {cp, mkdir, lstat, mkdtemp, readFile, readdir, rename, rm} from "node:fs/promises";
4
- import {homedir} from "node:os";
5
- import {join, dirname, resolve} from "node:path";
6
-
7
- export function agentCatalog({home = homedir(), env = process.env} = {}) {
8
- const config = env.XDG_CONFIG_HOME || join(home, ".config");
9
- return [
10
- ["claude-code", "Claude Code", env.CLAUDE_CONFIG_DIR || join(home, ".claude")],
11
- ["codex", "Codex", env.CODEX_HOME || join(home, ".codex")],
12
- ["cursor", "Cursor", join(home, ".cursor")],
13
- ["gemini-cli", "Gemini CLI", join(home, ".gemini")],
14
- ["github-copilot", "GitHub Copilot", join(home, ".copilot")],
15
- ["opencode", "OpenCode", join(config, "opencode")],
16
- ["cline", "Cline", join(home, ".agents")],
17
- ["windsurf", "Windsurf", join(home, ".codeium", "windsurf")],
18
- ["trae", "Trae", join(home, ".trae")],
19
- ["trae-cn", "Trae CN", join(home, ".trae-cn")],
20
- ["openclaw", "OpenClaw", [".openclaw", ".clawdbot", ".moltbot"].map(p => join(home, p)).find(existsSync) || join(home, ".openclaw")],
21
- ["roo", "Roo Code", join(home, ".roo")],
22
- ["qwen-code", "Qwen Code", join(home, ".qwen")],
23
- ].map(([id, name, root]) => ({id, name, directory: join(root, "skills"), detected: existsSync(root)}));
24
- }
25
-
26
- export async function hashTree(root, prefix = "") {
27
- const files = {};
28
- for (const entry of (await readdir(join(root, prefix), {withFileTypes: true})).sort((a, b) => a.name.localeCompare(b.name))) {
29
- const name = prefix ? `${prefix}/${entry.name}` : entry.name;
30
- if (entry.isSymbolicLink()) throw new Error(`Skill contains a symbolic link: ${name}`);
31
- if (entry.isDirectory()) Object.assign(files, await hashTree(root, name));
32
- else files[name] = createHash("sha256").update(await readFile(join(root, name))).digest("hex");
33
- }
34
- return files;
35
- }
36
-
37
- function sameHashes(left, right) {
38
- return left && right && Object.keys(left).length === Object.keys(right).length
39
- && Object.keys(left).every(key => left[key] === right[key]);
40
- }
41
-
42
- export async function installSkills({source, home, env, agents, replace = false, previous = []}) {
43
- const catalog = agentCatalog({home, env});
44
- const selected = agents.map(id => {
45
- const agent = catalog.find(item => item.id === id);
46
- if (!agent) throw new Error(`Unknown Agent: ${id}`);
47
- return agent;
48
- });
49
- const sourceHashes = await hashTree(source);
50
- const results = [];
51
- for (const agent of selected) {
52
- const target = join(agent.directory, "finebi-cli-agent");
53
- let backup;
54
- let candidate;
55
- try {
56
- const stat = await lstat(target).catch(error => { if (error.code !== "ENOENT") throw error; });
57
- if (stat) {
58
- if (stat.isSymbolicLink()) throw new Error("Skill target is a symbolic link; refusing to replace it.");
59
- const actual = await hashTree(target);
60
- if (sameHashes(sourceHashes, actual)) {
61
- results.push({agent: agent.id, path: target, status: "unchanged", hashes: sourceHashes});
62
- continue;
63
- }
64
- const recorded = previous.find(item => item.agent === agent.id && item.path === target)?.hashes;
65
- if (!replace && !sameHashes(recorded, actual)) {
66
- results.push({agent: agent.id, path: target, status: "kept"});
67
- continue;
68
- }
69
- backup = join(home, ".finebi-cli", "skill-backups", `${agent.id}-${randomUUID()}`);
70
- await mkdir(dirname(backup), {recursive: true});
71
- await cp(target, backup, {recursive: true, errorOnExist: true, force: false});
72
- if (!sameHashes(actual, await hashTree(backup))) throw new Error("Skill backup integrity check failed.");
73
- }
74
- await mkdir(agent.directory, {recursive: true});
75
- candidate = await mkdtemp(join(agent.directory, ".finebi-cli-stage-"));
76
- await cp(source, candidate, {recursive: true});
77
- if (!sameHashes(sourceHashes, await hashTree(candidate))) throw new Error("Skill copy integrity check failed.");
78
- const old = `${candidate}-old`;
79
- if (stat) await rename(target, old);
80
- try { await rename(candidate, target); }
81
- catch (error) { if (stat) await rename(old, target); throw error; }
82
- candidate = undefined;
83
- // Delete only the relocated, backed-up directory inside this Agent's skills directory.
84
- if (stat && backup && dirname(resolve(old)) === resolve(agent.directory)) await rm(old, {recursive: true});
85
- if (!sameHashes(sourceHashes, await hashTree(target))) throw new Error("Installed Skill integrity check failed.");
86
- results.push({agent: agent.id, path: target, status: "installed", hashes: sourceHashes, ...(backup ? {backup} : {})});
87
- } catch (error) {
88
- results.push({agent: agent.id, path: target, status: "failed", error: error.message, ...(backup ? {backup} : {})});
89
- } finally {
90
- if (candidate && dirname(resolve(candidate)) === resolve(agent.directory)) await rm(candidate, {recursive: true, force: true});
91
- }
92
- }
93
- return results;
94
- }
1
+ import {createHash, randomUUID} from "node:crypto";
2
+ import {existsSync} from "node:fs";
3
+ import {cp, mkdir, lstat, mkdtemp, readFile, readdir, rename, rm} from "node:fs/promises";
4
+ import {homedir} from "node:os";
5
+ import {join, dirname, resolve} from "node:path";
6
+
7
+ export function agentCatalog({home = homedir(), env = process.env} = {}) {
8
+ const config = env.XDG_CONFIG_HOME || join(home, ".config");
9
+ return [
10
+ ["claude-code", "Claude Code", env.CLAUDE_CONFIG_DIR || join(home, ".claude")],
11
+ ["codex", "Codex", env.CODEX_HOME || join(home, ".codex")],
12
+ ["cursor", "Cursor", join(home, ".cursor")],
13
+ ["gemini-cli", "Gemini CLI", join(home, ".gemini")],
14
+ ["github-copilot", "GitHub Copilot", join(home, ".copilot")],
15
+ ["opencode", "OpenCode", join(config, "opencode")],
16
+ ["cline", "Cline", join(home, ".agents")],
17
+ ["windsurf", "Windsurf", join(home, ".codeium", "windsurf")],
18
+ ["trae", "Trae", join(home, ".trae")],
19
+ ["trae-cn", "Trae CN", join(home, ".trae-cn")],
20
+ ["openclaw", "OpenClaw", [".openclaw", ".clawdbot", ".moltbot"].map(p => join(home, p)).find(existsSync) || join(home, ".openclaw")],
21
+ ["roo", "Roo Code", join(home, ".roo")],
22
+ ["qwen-code", "Qwen Code", join(home, ".qwen")],
23
+ ].map(([id, name, root]) => ({id, name, directory: join(root, "skills"), detected: existsSync(root)}));
24
+ }
25
+
26
+ export async function hashTree(root, prefix = "") {
27
+ const files = {};
28
+ for (const entry of (await readdir(join(root, prefix), {withFileTypes: true})).sort((a, b) => a.name.localeCompare(b.name))) {
29
+ const name = prefix ? `${prefix}/${entry.name}` : entry.name;
30
+ if (entry.isSymbolicLink()) throw new Error(`Skill contains a symbolic link: ${name}`);
31
+ if (entry.isDirectory()) Object.assign(files, await hashTree(root, name));
32
+ else files[name] = createHash("sha256").update(await readFile(join(root, name))).digest("hex");
33
+ }
34
+ return files;
35
+ }
36
+
37
+ function sameHashes(left, right) {
38
+ return left && right && Object.keys(left).length === Object.keys(right).length
39
+ && Object.keys(left).every(key => left[key] === right[key]);
40
+ }
41
+
42
+ export async function installSkills({source, home, env, agents, replace = false, previous = []}) {
43
+ const catalog = agentCatalog({home, env});
44
+ const selected = agents.map(id => {
45
+ const agent = catalog.find(item => item.id === id);
46
+ if (!agent) throw new Error(`Unknown Agent: ${id}`);
47
+ return agent;
48
+ });
49
+ const sourceHashes = await hashTree(source);
50
+ const results = [];
51
+ for (const agent of selected) {
52
+ const target = join(agent.directory, "finebi-cli-agent");
53
+ let backup;
54
+ let candidate;
55
+ try {
56
+ const stat = await lstat(target).catch(error => { if (error.code !== "ENOENT") throw error; });
57
+ if (stat) {
58
+ if (stat.isSymbolicLink()) throw new Error("Skill target is a symbolic link; refusing to replace it.");
59
+ const actual = await hashTree(target);
60
+ if (sameHashes(sourceHashes, actual)) {
61
+ results.push({agent: agent.id, path: target, status: "unchanged", hashes: sourceHashes});
62
+ continue;
63
+ }
64
+ const recorded = previous.find(item => item.agent === agent.id && item.path === target)?.hashes;
65
+ if (!replace && !sameHashes(recorded, actual)) {
66
+ results.push({agent: agent.id, path: target, status: "kept"});
67
+ continue;
68
+ }
69
+ backup = join(home, ".finebi-cli", "skill-backups", `${agent.id}-${randomUUID()}`);
70
+ await mkdir(dirname(backup), {recursive: true});
71
+ await cp(target, backup, {recursive: true, errorOnExist: true, force: false});
72
+ if (!sameHashes(actual, await hashTree(backup))) throw new Error("Skill backup integrity check failed.");
73
+ }
74
+ await mkdir(agent.directory, {recursive: true});
75
+ candidate = await mkdtemp(join(agent.directory, ".finebi-cli-stage-"));
76
+ await cp(source, candidate, {recursive: true});
77
+ if (!sameHashes(sourceHashes, await hashTree(candidate))) throw new Error("Skill copy integrity check failed.");
78
+ const old = `${candidate}-old`;
79
+ if (stat) await rename(target, old);
80
+ try { await rename(candidate, target); }
81
+ catch (error) { if (stat) await rename(old, target); throw error; }
82
+ candidate = undefined;
83
+ // Delete only the relocated, backed-up directory inside this Agent's skills directory.
84
+ if (stat && backup && dirname(resolve(old)) === resolve(agent.directory)) await rm(old, {recursive: true});
85
+ if (!sameHashes(sourceHashes, await hashTree(target))) throw new Error("Installed Skill integrity check failed.");
86
+ results.push({agent: agent.id, path: target, status: "installed", hashes: sourceHashes, ...(backup ? {backup} : {})});
87
+ } catch (error) {
88
+ results.push({agent: agent.id, path: target, status: "failed", error: error.message, ...(backup ? {backup} : {})});
89
+ } finally {
90
+ if (candidate && dirname(resolve(candidate)) === resolve(agent.directory)) await rm(candidate, {recursive: true, force: true});
91
+ }
92
+ }
93
+ return results;
94
+ }