Repository navigation
Expand file tree
/
Copy pathinstall.js
More file actions
executable file
·326 lines (284 loc) · 9.45 KB
/
Copy pathinstall.js
File metadata and controls
executable file
·326 lines (284 loc) · 9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const os = require("os");
const { execSync } = require("child_process");
// ── 配置 ────────────────────────────────────────────────
// 自动从 SKILL.md frontmatter 读取 name,无需手动配置
const SKILL_SOURCE = path.resolve(__dirname);
function readSkillName() {
const skillPath = path.join(SKILL_SOURCE, "SKILL.md");
const content = fs.readFileSync(skillPath, "utf-8");
const match = content.match(/^name:\s*(.+)$/m);
if (!match) {
console.error("Error: 'name' not found in SKILL.md frontmatter");
process.exit(1);
}
return match[1].trim();
}
const SKILL_NAME = readSkillName();
// agent 安装目录(跨平台)
const AGENT_CONFIG = {
codex: {
dirs: [
path.join(os.homedir(), ".agents", "skills"),
path.join(os.homedir(), ".codex", "skills"),
],
commands: ["codex"],
label: "Codex",
},
claude: {
dirs: [path.join(os.homedir(), ".claude", "skills")],
commands: ["claude"],
label: "Claude Code",
},
opencode: {
dirs: [path.join(os.homedir(), ".opencode", "skills")],
commands: ["opencode"],
label: "OpenCode",
},
};
// ── 约定:排除规则 ──────────────────────────────────────
// 以下目录/文件不会复制到 agent skills 目录
//
// 排除原因 | 约定位置
// ──────────────────── | ──────────────────────
// 安装脚本和包元数据 | 根目录 install.js / package.json
// 开发文档 | docs/
// 测试数据和评分脚本 | evals/
// Git / 编辑器 / 依赖 | .git / .DS_Store / node_modules
// npm 打包配置 | .npmignore
// 本地密钥(GITHUB_TOKEN)| key.bash
const EXCLUDE = new Set([
// 安装脚本和包元数据
"install.js",
"package.json",
"package-lock.json",
// 开发文档(所有 MD 都放 docs/)
"docs",
// 测试数据
"evals",
// Git / 编辑器 / 依赖
".git",
".DS_Store",
".gitignore",
"node_modules",
// npm 打包配置
".npmignore",
// 本地密钥:绝不复制到 agent 目录
"key.bash",
]);
// ── 工具函数 ────────────────────────────────────────────
function copyRecursive(src, dest) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest, { recursive: true });
}
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
if (EXCLUDE.has(entry.name)) continue;
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
copyRecursive(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}
function detectAgents() {
return Object.entries(AGENT_CONFIG).filter(([, cfg]) => {
for (const cmd of cfg.commands) {
try {
execSync(`which ${cmd} 2>/dev/null || where ${cmd} 2>/dev/null`, {
stdio: "pipe",
});
return true;
} catch {}
}
for (const dir of cfg.dirs) {
if (fs.existsSync(path.dirname(dir))) return true;
}
return false;
});
}
// ── 命令: install ───────────────────────────────────────
function installTo(agentKey) {
const cfg = AGENT_CONFIG[agentKey];
if (!cfg) {
console.error(` Unknown agent: ${agentKey}`);
return false;
}
let installed = false;
for (const dir of cfg.dirs) {
const dest = path.join(dir, SKILL_NAME);
try {
// 先清空目标目录,避免上一版本被删除的文件残留
fs.rmSync(dest, { recursive: true, force: true });
copyRecursive(SKILL_SOURCE, dest);
console.log(` ✓ ${cfg.label} → ${dest}`);
installed = true;
} catch (err) {
console.error(
` ✗ ${cfg.label} install failed (${dest}): ${err.message}`
);
}
}
if (!installed) {
console.error(` ✗ ${cfg.label}: no writable skill directory found`);
}
return installed;
}
// ── 命令: uninstall ─────────────────────────────────────
function uninstallFrom(agentKey) {
const cfg = AGENT_CONFIG[agentKey];
if (!cfg) return 0;
let removed = 0;
for (const dir of cfg.dirs) {
const dest = path.join(dir, SKILL_NAME);
if (fs.existsSync(dest)) {
fs.rmSync(dest, { recursive: true, force: true });
console.log(` ✓ Removed from ${cfg.label} (${dest})`);
removed++;
}
}
return removed;
}
// ── 命令: list ──────────────────────────────────────────
function listSkills() {
console.log("\nInstalled skills:\n");
let found = false;
for (const [, cfg] of Object.entries(AGENT_CONFIG)) {
for (const dir of cfg.dirs) {
if (!fs.existsSync(dir)) continue;
const skills = fs
.readdirSync(dir)
.filter((name) => fs.statSync(path.join(dir, name)).isDirectory());
if (skills.length > 0) {
console.log(` ${cfg.label} (${dir}):`);
for (const s of skills) {
console.log(` - ${s}`);
}
found = true;
}
}
}
if (!found) {
console.log(" (no skills installed)");
}
console.log();
}
// ── 命令: status ────────────────────────────────────────
function showStatus() {
console.log(`\nSkill: ${SKILL_NAME}\n`);
let installed = 0;
for (const [, cfg] of Object.entries(AGENT_CONFIG)) {
for (const dir of cfg.dirs) {
const dest = path.join(dir, SKILL_NAME);
if (fs.existsSync(dest)) {
const files = fs.readdirSync(dest);
console.log(` ✓ ${cfg.label} (${dest})`);
for (const f of files) {
console.log(` ${f}`);
}
installed++;
} else {
console.log(` ✗ ${cfg.label} (${dest})`);
}
}
}
if (installed === 0) {
console.log("\n Not installed anywhere. Run: node install.js");
}
console.log();
}
// ── 主入口 ──────────────────────────────────────────────
const args = process.argv.slice(2);
const helpFlag = args.includes("--help") || args.includes("-h");
const agentFlagIdx = args.indexOf("--agent");
const uninstallFlag = args.includes("--uninstall");
const listFlag = args.includes("--list") || args.includes("-l");
const statusFlag = args.includes("--status") || args.includes("-s");
function readSkillDescription() {
const skillPath = path.join(SKILL_SOURCE, "SKILL.md");
if (!fs.existsSync(skillPath)) return "";
const content = fs.readFileSync(skillPath, "utf-8");
const match = content.match(/^description:\s*(.+)$/m);
return match ? match[1].trim() : "";
}
const SKILL_DESCRIPTION = readSkillDescription();
if (helpFlag) {
const agentList = Object.entries(AGENT_CONFIG)
.map(([, cfg]) => cfg.label)
.join(", ");
console.log(`
${SKILL_NAME} — ${SKILL_DESCRIPTION}
Usage:
node install.js Auto-detect and install
node install.js --agent claude Install to Claude Code only
node install.js --agent all Install to all agents
node install.js --uninstall Remove from all agents
node install.js --list List all installed skills
node install.js --status Show install status
node install.js --help Show this help
Supported agents: ${agentList}
`);
process.exit(0);
}
if (listFlag) {
listSkills();
process.exit(0);
}
if (statusFlag) {
showStatus();
process.exit(0);
}
if (uninstallFlag) {
console.log(`\nUninstalling ${SKILL_NAME}...\n`);
let totalRemoved = 0;
for (const key of Object.keys(AGENT_CONFIG)) {
totalRemoved += uninstallFrom(key);
}
if (totalRemoved === 0) console.log(" (not installed anywhere)");
console.log();
process.exit(0);
}
// install (default)
let targets;
if (agentFlagIdx !== -1 && args[agentFlagIdx + 1]) {
const agent = args[agentFlagIdx + 1];
targets = agent === "all" ? Object.keys(AGENT_CONFIG) : [agent];
} else {
const detected = detectAgents();
targets =
detected.length > 0
? detected.map(([key]) => key)
: Object.keys(AGENT_CONFIG);
}
console.log(`\nInstalling ${SKILL_NAME} skill...\n`);
let success = 0;
for (const key of targets) {
if (installTo(key)) success++;
}
if (success === 0) {
console.error(
`\nNo agents installed. Install ${Object.values(AGENT_CONFIG).map(c => c.label).join(", ")} first.\n`
);
process.exit(1);
}
function getTriggerHints() {
const skillPath = path.join(SKILL_SOURCE, "SKILL.md");
if (!fs.existsSync(skillPath)) return "";
const content = fs.readFileSync(skillPath, "utf-8");
const triggers = [];
const re = /["""]([^"""]+)["""]/g;
let match;
while ((match = re.exec(content)) !== null) {
triggers.push(match[1]);
}
// 只取前 2 个作为示例
if (triggers.length === 0) return "";
if (triggers.length === 1) return `"${triggers[0]}"`;
return `"${triggers[0]}" or "${triggers[1]}"`;
}
const hints = getTriggerHints();
const triggerMsg = hints ? ` Say ${hints} to activate.` : "";
console.log(`\nDone!${triggerMsg}\n`);