Files
2026-11-16 17:26:44 -08:00

111 lines
5.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 把包里的 skill/ 装给本机的 Agent:Claude Code、Codex、Cursor。
//
// 用户级(默认):正本放 ~/.agents/skills/<name>,再从各 Agent 的 skills 目录软链过去 ——
// 和 `npx skills` 等安装器的约定一致,升级时只换正本,几个 Agent 同时生效。
// 项目级(--project):直接拷进仓库里各 Agent 的目录。仓库会被别人 clone,指向本人 home 的
// 软链在别人机器上是断的,所以这里不用软链。
import { cpSync, existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, rmSync, symlinkSync, unlinkSync } from "node:fs";
import { homedir } from "node:os";
import { dirname, join, relative, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { CliError } from "./http.js";
export const SKILL_NAME = "streamline-openapi";
const SRC = join(dirname(fileURLToPath(import.meta.url)), "..", "skill");
const home = homedir();
/** 各 Agent 的用户级 skills 目录;`root` 存在才算装了这个 Agent。 */
const AGENTS = {
claude: { label: "Claude Code", root: join(home, ".claude"), skills: join(home, ".claude", "skills"), project: ".claude/skills" },
codex: {
label: "Codex", root: process.env.CODEX_HOME || join(home, ".codex"),
skills: join(process.env.CODEX_HOME || join(home, ".codex"), "skills"), project: ".codex/skills",
},
cursor: { label: "Cursor", root: join(home, ".cursor"), skills: join(home, ".cursor", "skills"), project: ".cursor/skills" },
};
const CANONICAL = join(home, ".agents", "skills", SKILL_NAME);
function pickAgents(flag) {
if (typeof flag === "string" && flag !== "auto") {
const names = flag === "all" ? Object.keys(AGENTS) : flag.split(",").map((s) => s.trim()).filter(Boolean);
const bad = names.filter((n) => !AGENTS[n]);
if (bad.length) throw new CliError(`不认识的 Agent:${bad.join(", ")}`, "可选 claude、codex、cursor、all");
return names;
}
const found = Object.keys(AGENTS).filter((n) => existsSync(AGENTS[n].root));
// 一个都没检测到时至少装给 Claude Code,免得什么都没发生。
return found.length ? found : ["claude"];
}
/** 路径上有没有东西(含断掉的软链,existsSync 对它返回 false)。 */
function present(path) {
try { lstatSync(path); return true; } catch { return false; }
}
/** 删掉路径上的东西。rmSync(force) 会把断掉的软链当成不存在而跳过,所以软链单独 unlink。 */
function remove(path) {
if (!present(path)) return;
if (lstatSync(path).isSymbolicLink()) unlinkSync(path);
else rmSync(path, { recursive: true, force: true });
}
/** 目标位置上已有的东西是不是我们装的(软链,或含同名 SKILL.md 的目录)——只覆盖自己的。 */
function ours(path) {
if (!present(path)) return true;
const st = lstatSync(path);
if (st.isSymbolicLink()) return true;
if (!st.isDirectory()) return false;
try { return /^name:\s*streamline-openapi\s*$/m.test(readFileSync(join(path, "SKILL.md"), "utf8")); } catch { return false; }
}
function place(target, linkTo) {
if (!ours(target)) throw new CliError(`${target} 已存在且不是本 Skill,没有覆盖`, "确认后手动删除再重试");
const existed = present(target);
remove(target);
mkdirSync(dirname(target), { recursive: true });
if (linkTo) {
try {
// 相对路径要按真实目录算:skills 目录本身可能是软链(如 $CODEX_HOME/skills → ~/.codex/skills),
// 按字面路径算出来的 ../.. 落到真实目录里就多跳了几层,链接是断的。
symlinkSync(relative(realpathSync(dirname(target)), realpathSync(linkTo)), target, "dir");
if (!existsSync(join(target, "SKILL.md"))) throw new Error("dangling");
return { path: target, mode: "symlink", updated: existed };
} catch {
// 不支持软链(如 Windows 无权限)或链接没指对,就退回拷贝。
remove(target);
}
}
cpSync(linkTo || SRC, target, { recursive: true });
return { path: target, mode: "copy", updated: existed };
}
export function install(flags = {}) {
const agents = pickAgents(flags.agent);
let results;
if (typeof flags.dir === "string") {
results = [{ agent: "custom", ...place(join(resolve(flags.dir), SKILL_NAME), null) }];
} else if (flags.project) {
results = agents.map((a) => ({ agent: a, ...place(join(process.cwd(), AGENTS[a].project, SKILL_NAME), null) }));
} else {
remove(CANONICAL);
mkdirSync(dirname(CANONICAL), { recursive: true });
cpSync(SRC, CANONICAL, { recursive: true });
results = agents.map((a) => ({ agent: a, ...place(join(AGENTS[a].skills, SKILL_NAME), CANONICAL) }));
}
for (const r of results) process.stderr.write(`${r.updated ? "已更新" : "已安装"} ${AGENTS[r.agent]?.label ?? "Skill"}:${r.path}\n`);
process.stderr.write("新开一个 Agent 会话即可生效。下一步:streamline auth login\n");
return { installed: true, skill: SKILL_NAME, canonical: flags.dir || flags.project ? null : CANONICAL, targets: results };
}
export function uninstall(flags = {}) {
const agents = typeof flags.agent === "string" ? pickAgents(flags.agent) : Object.keys(AGENTS);
const removed = [];
const base = flags.project ? (a) => join(process.cwd(), AGENTS[a].project, SKILL_NAME) : (a) => join(AGENTS[a].skills, SKILL_NAME);
for (const a of agents) {
const p = base(a);
if (present(p) && ours(p)) { remove(p); removed.push(p); }
}
if (!flags.project && !flags.agent && present(CANONICAL)) { remove(CANONICAL); removed.push(CANONICAL); }
return { uninstalled: true, removed };
}