@ease-ops/onboard 0.0.0-stage → 2.0.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 +143 -2
- package/SKILL.md +230 -0
- package/bin/onboard.mjs +410 -0
- package/lib/clients.mjs +106 -0
- package/lib/gitignore.mjs +31 -0
- package/lib/jsonconf.mjs +86 -0
- package/lib/mcp.mjs +166 -0
- package/lib/skill.mjs +71 -0
- package/package.json +21 -4
- package/references/clients.md +232 -0
- package/references/detect.md +189 -0
- package/references/errors.md +289 -0
- package/references/payload.md +686 -0
- package/references/verify.md +67 -0
package/bin/onboard.mjs
ADDED
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// ease-ops 接入安装器:init / doctor / remove
|
|
3
|
+
// 设计见 ../../docs/design/skill-onboarding.md §5.4,客户端差异见 references/clients.md
|
|
4
|
+
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import process from 'node:process';
|
|
8
|
+
|
|
9
|
+
import { CLIENTS, ENV_NAME, SERVER_NAME, getClient, needsInlineToken, resolveTokenValue } from '../lib/clients.mjs';
|
|
10
|
+
import { readConfig, snippetFor, upsertServer, removeServer, writeConfig } from '../lib/jsonconf.mjs';
|
|
11
|
+
import { ensureIgnored, gitRoot, isGitRepo, isIgnored } from '../lib/gitignore.mjs';
|
|
12
|
+
import { PACKAGE_ROOT, SKILL_NAME, compareVersions, installSkill, readVersion, removeSkill } from '../lib/skill.mjs';
|
|
13
|
+
import { callTool, normalizeUrl } from '../lib/mcp.mjs';
|
|
14
|
+
|
|
15
|
+
const RULES_BODY = `# ease-ops 接入
|
|
16
|
+
|
|
17
|
+
当用户说"接入 ease-ops""把这个项目接进平台""加个组件进流水线"时:
|
|
18
|
+
1. 读 \`.ease-ops/skill/SKILL.md\`,按其五阶段执行;
|
|
19
|
+
2. payload 字段只允许 \`.ease-ops/skill/references/payload.md\` 里的定义,未知字段直接报错;
|
|
20
|
+
3. 错误码与处置查 \`.ease-ops/skill/references/errors.md\`;不要把密钥写进任何文件或对话。
|
|
21
|
+
`;
|
|
22
|
+
|
|
23
|
+
function parseArgs(argv) {
|
|
24
|
+
const out = { _: [] };
|
|
25
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
26
|
+
const a = argv[i];
|
|
27
|
+
if (!a.startsWith('--')) {
|
|
28
|
+
out._.push(a);
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
const key = a.slice(2);
|
|
32
|
+
const boolKeys = new Set(['force', 'dry-run', 'no-verify', 'yes', 'help']);
|
|
33
|
+
if (boolKeys.has(key)) {
|
|
34
|
+
out[key] = true;
|
|
35
|
+
} else {
|
|
36
|
+
out[key] = argv[i + 1];
|
|
37
|
+
i += 1;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const c = {
|
|
44
|
+
dim: (s) => `\x1b[2m${s}\x1b[0m`,
|
|
45
|
+
bold: (s) => `\x1b[1m${s}\x1b[0m`,
|
|
46
|
+
red: (s) => `\x1b[31m${s}\x1b[0m`,
|
|
47
|
+
green: (s) => `\x1b[32m${s}\x1b[0m`,
|
|
48
|
+
yellow: (s) => `\x1b[33m${s}\x1b[0m`,
|
|
49
|
+
};
|
|
50
|
+
const color = process.stdout.isTTY ? c : { dim: (s) => s, bold: (s) => s, red: (s) => s, green: (s) => s, yellow: (s) => s };
|
|
51
|
+
|
|
52
|
+
const mark = {
|
|
53
|
+
ok: () => color.green('[ok] '),
|
|
54
|
+
skip: () => color.dim('[skip]'),
|
|
55
|
+
warn: () => color.yellow('[warn]'),
|
|
56
|
+
fail: () => color.red('[fail]'),
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
function say(kind, label, extra = '') {
|
|
60
|
+
process.stdout.write(`${mark[kind]()} ${label}${extra ? ` ${color.dim(extra)}` : ''}\n`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const isEmptyObject = (value) => Boolean(value) && typeof value === 'object' && Object.keys(value).length === 0;
|
|
64
|
+
|
|
65
|
+
// 从已写入的客户端配置里找回 MCP 地址:重跑 init / doctor 时不必再记 --url
|
|
66
|
+
function discoverServerUrl(root) {
|
|
67
|
+
for (const client of Object.values(CLIENTS)) {
|
|
68
|
+
if (!client.configPath) continue;
|
|
69
|
+
const conf = readConfig(path.join(root, client.configPath));
|
|
70
|
+
const url = conf.data?.[client.serversKey]?.[SERVER_NAME]?.url;
|
|
71
|
+
if (typeof url === 'string' && url) return url;
|
|
72
|
+
}
|
|
73
|
+
return '';
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function detectClients(root) {
|
|
77
|
+
return Object.values(CLIENTS)
|
|
78
|
+
.filter((client) => client.id !== 'generic')
|
|
79
|
+
.filter((client) => {
|
|
80
|
+
// .vscode/ 太常见(放 settings.json 的仓也在内),只有已存在 mcp.json 才算用了 MCP
|
|
81
|
+
if (client.id === 'vscode') return fs.existsSync(path.join(root, '.vscode', 'mcp.json'));
|
|
82
|
+
return client.probeDirs.some((d) => fs.existsSync(path.join(root, d)));
|
|
83
|
+
})
|
|
84
|
+
.map((client) => client.id);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function promptHidden(question) {
|
|
88
|
+
if (!process.stdin.isTTY) {
|
|
89
|
+
const chunks = [];
|
|
90
|
+
for await (const chunk of process.stdin) chunks.push(chunk);
|
|
91
|
+
return Buffer.concat(chunks).toString('utf8').trim();
|
|
92
|
+
}
|
|
93
|
+
return new Promise((resolve) => {
|
|
94
|
+
process.stdout.write(question);
|
|
95
|
+
const stdin = process.stdin;
|
|
96
|
+
stdin.setRawMode(true);
|
|
97
|
+
stdin.resume();
|
|
98
|
+
stdin.setEncoding('utf8');
|
|
99
|
+
let buf = '';
|
|
100
|
+
const done = (value) => {
|
|
101
|
+
stdin.setRawMode(false);
|
|
102
|
+
stdin.pause();
|
|
103
|
+
stdin.removeListener('data', onData);
|
|
104
|
+
process.stdout.write('\n');
|
|
105
|
+
resolve(value);
|
|
106
|
+
};
|
|
107
|
+
const onData = (chunk) => {
|
|
108
|
+
for (const ch of chunk) {
|
|
109
|
+
if (ch === '\r' || ch === '\n' || ch === '\u0004') return done(buf);
|
|
110
|
+
if (ch === '\u0003') {
|
|
111
|
+
stdin.setRawMode(false);
|
|
112
|
+
process.stdout.write('\n');
|
|
113
|
+
process.exit(130);
|
|
114
|
+
}
|
|
115
|
+
if (ch === '\u007f' || ch === '\b') buf = buf.slice(0, -1);
|
|
116
|
+
else buf += ch;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
stdin.on('data', onData);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async function resolveToken(args, clients) {
|
|
124
|
+
const fromArgs = args.token || process.env[ENV_NAME] || '';
|
|
125
|
+
if (fromArgs) return fromArgs;
|
|
126
|
+
// 只有"写入配置文件且不支持变量注入"的客户端才必须拿到 Key;generic 只打印片段
|
|
127
|
+
const needsInline = clients.some((client) => client.configPath && needsInlineToken(client));
|
|
128
|
+
if (needsInline) {
|
|
129
|
+
const typed = await promptHidden(`${ENV_NAME}(输入不回显,可留空改用变量注入): `);
|
|
130
|
+
return typed;
|
|
131
|
+
}
|
|
132
|
+
return '';
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async function verify(url, token) {
|
|
136
|
+
if (!token) {
|
|
137
|
+
say('skip', '连通性验证', '未提供 Key,跳过(配置已写入)');
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
const result = await callTool({ url, token, name: 'ease_ops_whoami' });
|
|
141
|
+
if (!result.ok) {
|
|
142
|
+
say('fail', '连通性验证', result.error.message);
|
|
143
|
+
if (result.error.detail) process.stdout.write(`${color.dim(` ${String(result.error.detail).replace(/\s+/g, ' ').slice(0, 200)}`)}\n`);
|
|
144
|
+
process.stdout.write(`${color.dim(' 排障对照:references/clients.md §7')}\n`);
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
const info = typeof result.data === 'object' ? result.data : { raw: result.data };
|
|
148
|
+
const who = info.user?.name || info.user?.username || info.username || info.name || '已认证';
|
|
149
|
+
const projects = info.projects?.length ?? info.project_count ?? null;
|
|
150
|
+
say('ok', '连通性验证', `${who}${projects !== null ? ` / ${projects} 个项目` : ''}`);
|
|
151
|
+
if (info.min_skill_version) {
|
|
152
|
+
const local = readVersion(PACKAGE_ROOT) || '0.0.0';
|
|
153
|
+
if (compareVersions(info.min_skill_version, local) > 0) {
|
|
154
|
+
say('warn', 'skill 版本偏低', `平台要求 ≥${info.min_skill_version},本地 ${local}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return info;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async function cmdInit(args) {
|
|
161
|
+
const root = path.resolve(args.root || gitRoot(process.cwd()) || process.cwd());
|
|
162
|
+
const dryRun = Boolean(args['dry-run']);
|
|
163
|
+
|
|
164
|
+
let ids = args.client && args.client !== 'all' ? [args.client] : args.client === 'all' ? Object.keys(CLIENTS) : detectClients(root);
|
|
165
|
+
if (ids.length === 0) ids = ['cursor'];
|
|
166
|
+
const clients = ids.map(getClient);
|
|
167
|
+
|
|
168
|
+
process.stdout.write(`${color.bold('ease-ops onboard · init')}\n`);
|
|
169
|
+
say('ok', '项目根', root);
|
|
170
|
+
if (dryRun) say('warn', 'dry-run', '不写任何文件');
|
|
171
|
+
say('ok', '客户端', clients.map((x) => x.label).join(', '));
|
|
172
|
+
|
|
173
|
+
const url = normalizeUrl(args.url || process.env.EASE_OPS_URL || discoverServerUrl(root));
|
|
174
|
+
if (!url) {
|
|
175
|
+
say('fail', 'endpoint', '缺少 --url 或 EASE_OPS_URL(例:https://ops.example.com)');
|
|
176
|
+
return 1;
|
|
177
|
+
}
|
|
178
|
+
if (!url.startsWith('https://') && !/^http:\/\/(127\.0\.0\.1|localhost)(:|\/)/.test(url)) {
|
|
179
|
+
say('warn', 'endpoint', '非 localhost 使用明文 http,Key 会明文过网');
|
|
180
|
+
}
|
|
181
|
+
say('ok', 'endpoint', url);
|
|
182
|
+
|
|
183
|
+
const token = await resolveToken(args, clients);
|
|
184
|
+
let failed = 0;
|
|
185
|
+
|
|
186
|
+
for (const client of clients) {
|
|
187
|
+
if (client.skillRoot) {
|
|
188
|
+
const roots = [client.skillRoot, ...(client.extraSkillRoots || [])];
|
|
189
|
+
for (const skillRoot of roots) {
|
|
190
|
+
const res = installSkill(path.join(root, skillRoot), {
|
|
191
|
+
force: Boolean(args.force),
|
|
192
|
+
dryRun,
|
|
193
|
+
name: client.skillDirName || SKILL_NAME,
|
|
194
|
+
});
|
|
195
|
+
const where = path.relative(root, res.dest) || res.dest;
|
|
196
|
+
if (res.action === 'skipped') {
|
|
197
|
+
say('warn', `${client.label} skill`, `本地 ${res.destVersion} 高于包内 ${res.srcVersion},加 --force 覆盖(${where})`);
|
|
198
|
+
} else {
|
|
199
|
+
say('ok', `${client.label} skill`, `${res.action} ${res.srcVersion} → ${where}`);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
} else {
|
|
203
|
+
say('skip', `${client.label} skill`, '该客户端不支持 skills');
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (client.rulesPath) {
|
|
207
|
+
const rulesFile = path.join(root, client.rulesPath);
|
|
208
|
+
if (!dryRun) {
|
|
209
|
+
fs.mkdirSync(path.dirname(rulesFile), { recursive: true });
|
|
210
|
+
fs.writeFileSync(rulesFile, RULES_BODY, 'utf8');
|
|
211
|
+
}
|
|
212
|
+
say('ok', `${client.label} rules`, `${client.rulesPath}(引用 .ease-ops/skill/)`);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (!client.configPath) {
|
|
216
|
+
const entry = client.serverEntry(url, resolveTokenValue(client, token || `<${ENV_NAME}>`));
|
|
217
|
+
process.stdout.write(`\n${color.dim('把下面这段合并进客户端的 MCP 配置:')}\n${snippetFor('mcpServers', SERVER_NAME, entry)}\n`);
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const configFile = path.join(root, client.configPath);
|
|
222
|
+
const current = readConfig(configFile);
|
|
223
|
+
if (current.hasComment || current.parseError) {
|
|
224
|
+
const entry = client.serverEntry(url, resolveTokenValue(client, token || `<${ENV_NAME}>`));
|
|
225
|
+
say('warn', `${client.label} 配置`, `${client.configPath} 含注释或无法解析,未写入`);
|
|
226
|
+
process.stdout.write(`${color.dim('请手工合并:')}\n${snippetFor(client.serversKey, SERVER_NAME, entry)}\n`);
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const tokenValue = resolveTokenValue(client, token);
|
|
231
|
+
if (needsInlineToken(client) && !tokenValue) {
|
|
232
|
+
say('fail', `${client.label} 配置`, '该客户端不支持变量注入,必须提供 Key');
|
|
233
|
+
failed += 1;
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
let next = upsertServer(current.data || {}, client.serversKey, SERVER_NAME, client.serverEntry(url, tokenValue));
|
|
238
|
+
if (client.topLevelExtras) {
|
|
239
|
+
const extras = client.topLevelExtras();
|
|
240
|
+
for (const [k, v] of Object.entries(extras)) {
|
|
241
|
+
const merged = Array.isArray(next[k]) ? next[k] : [];
|
|
242
|
+
const missing = v.filter((item) => !merged.some((m) => m.id === item.id));
|
|
243
|
+
next = { ...next, [k]: [...merged, ...missing] };
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (!dryRun) {
|
|
248
|
+
writeConfig(configFile, next, current.indent ?? 2);
|
|
249
|
+
if (needsInlineToken(client)) fs.chmodSync(configFile, 0o600);
|
|
250
|
+
}
|
|
251
|
+
const modeLabel = client.tokenMode === 'env' ? `变量注入 ${ENV_NAME}` : client.tokenMode === 'input' ? '输入框' : '明文 Key';
|
|
252
|
+
say('ok', `${client.label} 配置`, `${client.configPath}(${modeLabel})`);
|
|
253
|
+
|
|
254
|
+
if (needsInlineToken(client)) {
|
|
255
|
+
const ignore = ensureIgnored(root, client.configPath);
|
|
256
|
+
if (ignore.ok) say('ok', 'gitignore', ignore.added ? `已追加 ${client.configPath}` : `${client.configPath} 已被忽略`);
|
|
257
|
+
else if (ignore.reason === 'not_a_git_repo') say('warn', 'gitignore', '当前不是 git 仓库,无法校验');
|
|
258
|
+
else say('warn', 'gitignore', `${client.configPath} 未被忽略,明文 Key 有泄露风险`);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const verifyTarget = clients.find((x) => x.tokenMode !== 'input');
|
|
263
|
+
if (dryRun) {
|
|
264
|
+
say('skip', '连通性验证', 'dry-run');
|
|
265
|
+
} else if (!args['no-verify'] && verifyTarget) {
|
|
266
|
+
await verify(url, token);
|
|
267
|
+
} else if (args['no-verify']) {
|
|
268
|
+
say('skip', '连通性验证', '--no-verify');
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
process.stdout.write('\n');
|
|
272
|
+
if (clients.some((x) => x.tokenMode === 'env')) {
|
|
273
|
+
process.stdout.write(`${color.dim(`提醒:变量注入需要先 export ${ENV_NAME}=<key>,且必须对启动客户端的进程可见。`)}\n`);
|
|
274
|
+
}
|
|
275
|
+
if (clients.some((x) => x.configPath && x.tokenMode === 'literal')) {
|
|
276
|
+
process.stdout.write(`${color.dim('提醒:明文 Key 已写入配置文件并设为 0600,不要提交到仓库。')}\n`);
|
|
277
|
+
}
|
|
278
|
+
if (!isGitRepo(root)) process.stdout.write(`${color.dim('提醒:当前目录不是 git 仓库,接入任务仍可运行,但无法校验 gitignore。')}\n`);
|
|
279
|
+
|
|
280
|
+
return failed > 0 ? 1 : 0;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
async function cmdDoctor(args) {
|
|
284
|
+
const root = path.resolve(args.root || gitRoot(process.cwd()) || process.cwd());
|
|
285
|
+
process.stdout.write(`${color.bold('ease-ops onboard · doctor')}\n`);
|
|
286
|
+
say('ok', '项目根', root);
|
|
287
|
+
say('ok', '包版本', readVersion(PACKAGE_ROOT) || 'unknown');
|
|
288
|
+
|
|
289
|
+
for (const client of Object.values(CLIENTS)) {
|
|
290
|
+
const skillDirs = [client.skillRoot, ...(client.extraSkillRoots || [])].filter(Boolean);
|
|
291
|
+
const installed = skillDirs
|
|
292
|
+
.map((d) => ({ dir: d, version: readVersion(path.join(root, d, client.skillDirName || SKILL_NAME)) }))
|
|
293
|
+
.filter((x) => x.version);
|
|
294
|
+
if (installed.length) {
|
|
295
|
+
say('ok', `${client.label} skill`, installed.map((x) => `${x.dir}/${client.skillDirName || SKILL_NAME}@${x.version}`).join(' + '));
|
|
296
|
+
} else if (client.skillRoot && client.probeDirs.some((d) => fs.existsSync(path.join(root, d)))) {
|
|
297
|
+
say('warn', `${client.label} skill`, '未安装,重新跑 init 即可');
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (client.configPath) {
|
|
301
|
+
const configFile = path.join(root, client.configPath);
|
|
302
|
+
const conf = readConfig(configFile);
|
|
303
|
+
const hasEntry = Boolean(conf.data?.[client.serversKey]?.[SERVER_NAME]);
|
|
304
|
+
if (hasEntry) {
|
|
305
|
+
const mode = client.tokenMode;
|
|
306
|
+
say('ok', `${client.label} 配置`, `${client.configPath}(${mode})`);
|
|
307
|
+
if (needsInlineToken(client)) {
|
|
308
|
+
say(isIgnored(root, client.configPath) ? 'ok' : 'warn', ' gitignore',
|
|
309
|
+
isIgnored(root, client.configPath) ? '已忽略' : '未忽略,明文 Key 有泄露风险');
|
|
310
|
+
}
|
|
311
|
+
} else if (fs.existsSync(configFile)) {
|
|
312
|
+
say('warn', `${client.label} 配置`, `${client.configPath} 存在但没有 ${SERVER_NAME} 项`);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const token = args.token || process.env[ENV_NAME] || '';
|
|
318
|
+
const url = normalizeUrl(args.url || process.env.EASE_OPS_URL || discoverServerUrl(root));
|
|
319
|
+
if (url && token) {
|
|
320
|
+
await verify(url, token);
|
|
321
|
+
} else if (url) {
|
|
322
|
+
say('skip', '连通性验证', `缺少 token(--token 或 export ${ENV_NAME})`);
|
|
323
|
+
} else {
|
|
324
|
+
say('skip', '连通性验证', '缺少 --url 或 EASE_OPS_URL');
|
|
325
|
+
}
|
|
326
|
+
return 0;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
async function cmdRemove(args) {
|
|
330
|
+
const root = path.resolve(args.root || gitRoot(process.cwd()) || process.cwd());
|
|
331
|
+
const dryRun = Boolean(args['dry-run']);
|
|
332
|
+
process.stdout.write(`${color.bold('ease-ops onboard · remove')}\n`);
|
|
333
|
+
say('ok', '项目根', root);
|
|
334
|
+
|
|
335
|
+
// 指定 --client 时只动这一家;不带则全量清理
|
|
336
|
+
const targets = args.client && args.client !== 'all' ? [getClient(args.client)] : Object.values(CLIENTS);
|
|
337
|
+
for (const client of targets) {
|
|
338
|
+
for (const skillRoot of [client.skillRoot, ...(client.extraSkillRoots || [])].filter(Boolean)) {
|
|
339
|
+
const res = removeSkill(path.join(root, skillRoot), { dryRun, name: client.skillDirName || SKILL_NAME });
|
|
340
|
+
if (res.removed) say('ok', `${client.label} skill`, `${dryRun ? '将删除' : '已删除'} ${path.relative(root, res.dest)}`);
|
|
341
|
+
}
|
|
342
|
+
if (client.rulesPath) {
|
|
343
|
+
const rulesFile = path.join(root, client.rulesPath);
|
|
344
|
+
// 只删自己写的那份;用户改过就保留
|
|
345
|
+
if (fs.existsSync(rulesFile) && fs.readFileSync(rulesFile, 'utf8').trim() === RULES_BODY.trim()) {
|
|
346
|
+
if (!dryRun) fs.rmSync(rulesFile);
|
|
347
|
+
say('ok', `${client.label} rules`, `${dryRun ? '将删除' : '已删除'} ${client.rulesPath}`);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (!client.configPath) continue;
|
|
351
|
+
const configFile = path.join(root, client.configPath);
|
|
352
|
+
const conf = readConfig(configFile);
|
|
353
|
+
if (!conf.data) continue;
|
|
354
|
+
const res = removeServer(conf.data, client.serversKey, SERVER_NAME);
|
|
355
|
+
if (res.removed) {
|
|
356
|
+
if (!dryRun) {
|
|
357
|
+
// 移除后只剩空对象(例如 {"mcpServers":{}} 也会被写成 {}):不留空文件
|
|
358
|
+
if (isEmptyObject(res.data)) fs.rmSync(configFile, { force: true });
|
|
359
|
+
else writeConfig(configFile, res.data, conf.indent ?? 2);
|
|
360
|
+
}
|
|
361
|
+
say('ok', `${client.label} 配置`, isEmptyObject(res.data) ? `${dryRun ? '将移除并删除空配置' : '已移除并删除空配置'}` : `${dryRun ? '将移除' : '已移除'} ${SERVER_NAME} 项`);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
if (fs.existsSync(path.join(root, '.ease-ops'))) {
|
|
365
|
+
say('skip', '.ease-ops/', '保留(含 onboarding.yaml / onboarding.json)');
|
|
366
|
+
}
|
|
367
|
+
return 0;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function usage() {
|
|
371
|
+
process.stdout.write(`${color.bold('用法')}
|
|
372
|
+
npx @ease-ops/onboard init [options] 安装 skill + 配置 MCP + 验活
|
|
373
|
+
npx @ease-ops/onboard doctor [options] 检查安装状态与连通性
|
|
374
|
+
npx @ease-ops/onboard remove [options] 卸载 skill 与 MCP 配置项
|
|
375
|
+
|
|
376
|
+
${color.bold('选项')}
|
|
377
|
+
--client <id> cursor | claude | codebuddy | vscode | generic | all(默认自动探测)
|
|
378
|
+
--url <url> console 地址,可省略 /mcp;也可用 EASE_OPS_URL
|
|
379
|
+
--token <key> MCP Key;也可用 ${ENV_NAME},缺省时交互输入(不回显)
|
|
380
|
+
--root <dir> 项目根目录(默认取 git 根)
|
|
381
|
+
--force 目标 skill 版本更高时也覆盖
|
|
382
|
+
--dry-run 只打印动作,不写文件
|
|
383
|
+
--no-verify 跳过 whoami 连通性验证
|
|
384
|
+
-h, --help 显示本帮助
|
|
385
|
+
`);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
async function main() {
|
|
389
|
+
const args = parseArgs(process.argv.slice(2));
|
|
390
|
+
const command = args._[0] || (args.help ? 'help' : '');
|
|
391
|
+
|
|
392
|
+
if (!command || command === 'help' || args.help) {
|
|
393
|
+
usage();
|
|
394
|
+
return 0;
|
|
395
|
+
}
|
|
396
|
+
if (command === 'init') return cmdInit(args);
|
|
397
|
+
if (command === 'doctor') return cmdDoctor(args);
|
|
398
|
+
if (command === 'remove') return cmdRemove(args);
|
|
399
|
+
|
|
400
|
+
say('fail', '未知命令', command);
|
|
401
|
+
usage();
|
|
402
|
+
return 1;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
main()
|
|
406
|
+
.then((code) => process.exit(code ?? 0))
|
|
407
|
+
.catch((err) => {
|
|
408
|
+
say('fail', '未捕获异常', err?.stack || String(err));
|
|
409
|
+
process.exit(1);
|
|
410
|
+
});
|
package/lib/clients.mjs
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// 客户端差异只体现在三处:skill 落位、MCP 配置落位、Key 注入方式。
|
|
2
|
+
// 依据见 references/clients.md §2;未实测的路径一律标 requiresProbe。
|
|
3
|
+
|
|
4
|
+
const auth = (value) => ({ Authorization: `Bearer ${value}` });
|
|
5
|
+
|
|
6
|
+
export const ENV_NAME = 'EASE_OPS_TOKEN';
|
|
7
|
+
export const SERVER_NAME = 'ease-ops';
|
|
8
|
+
|
|
9
|
+
export const CLIENTS = {
|
|
10
|
+
cursor: {
|
|
11
|
+
id: 'cursor',
|
|
12
|
+
label: 'Cursor',
|
|
13
|
+
probeDirs: ['.cursor'],
|
|
14
|
+
skillRoot: '.cursor/skills',
|
|
15
|
+
configPath: '.cursor/mcp.json',
|
|
16
|
+
globalConfigPath: '~/.cursor/mcp.json',
|
|
17
|
+
serversKey: 'mcpServers',
|
|
18
|
+
tokenMode: 'env',
|
|
19
|
+
tokenExpression: () => `\${env:${ENV_NAME}}`,
|
|
20
|
+
serverEntry: (url, tokenValue) => ({ url, headers: auth(tokenValue) }),
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
claude: {
|
|
24
|
+
id: 'claude',
|
|
25
|
+
label: 'Claude Code',
|
|
26
|
+
probeDirs: ['.claude', '.mcp.json'],
|
|
27
|
+
skillRoot: '.claude/skills',
|
|
28
|
+
configPath: '.mcp.json',
|
|
29
|
+
serversKey: 'mcpServers',
|
|
30
|
+
tokenMode: 'env',
|
|
31
|
+
tokenExpression: () => `\${${ENV_NAME}}`,
|
|
32
|
+
// Claude Code 的 HTTP 传输必须显式声明 type
|
|
33
|
+
serverEntry: (url, tokenValue) => ({ type: 'http', url, headers: auth(tokenValue) }),
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
codebuddy: {
|
|
37
|
+
id: 'codebuddy',
|
|
38
|
+
label: 'CodeBuddy',
|
|
39
|
+
probeDirs: ['.codebuddy'],
|
|
40
|
+
skillRoot: '.codebuddy/skills',
|
|
41
|
+
extraSkillRoots: ['.claude/skills'], // CodeBuddy 兼容 .claude/skills
|
|
42
|
+
configPath: '.codebuddy/mcp.json',
|
|
43
|
+
serversKey: 'mcpServers',
|
|
44
|
+
tokenMode: 'literal',
|
|
45
|
+
serverEntry: (url, tokenValue) => ({ url, headers: auth(tokenValue) }),
|
|
46
|
+
requiresProbe: true, // 配置路径官方未明确,待实测
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
vscode: {
|
|
50
|
+
id: 'vscode',
|
|
51
|
+
label: 'VS Code (Copilot)',
|
|
52
|
+
probeDirs: ['.vscode'],
|
|
53
|
+
skillRoot: null,
|
|
54
|
+
configPath: '.vscode/mcp.json',
|
|
55
|
+
serversKey: 'servers',
|
|
56
|
+
tokenMode: 'input',
|
|
57
|
+
inputId: 'ease-ops-token',
|
|
58
|
+
topLevelExtras: () => ({
|
|
59
|
+
inputs: [
|
|
60
|
+
{
|
|
61
|
+
id: 'ease-ops-token',
|
|
62
|
+
type: 'promptString',
|
|
63
|
+
description: 'ease-ops MCP Key',
|
|
64
|
+
password: true,
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
}),
|
|
68
|
+
serverEntry: (url, tokenValue) => ({ type: 'http', url, headers: auth(tokenValue) }),
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
generic: {
|
|
72
|
+
id: 'generic',
|
|
73
|
+
label: '通用(不支持 skills)',
|
|
74
|
+
probeDirs: [],
|
|
75
|
+
skillRoot: '.ease-ops',
|
|
76
|
+
skillDirName: 'skill', // 降级 rules 直接引用 .ease-ops/skill/SKILL.md,不能再套一层
|
|
77
|
+
configPath: null,
|
|
78
|
+
serversKey: 'mcpServers',
|
|
79
|
+
tokenMode: 'literal',
|
|
80
|
+
serverEntry: (url, tokenValue) => ({ url, headers: auth(tokenValue) }),
|
|
81
|
+
rulesPath: '.trae/rules/ease-ops.md',
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export function getClient(id) {
|
|
86
|
+
const c = CLIENTS[id];
|
|
87
|
+
if (!c) {
|
|
88
|
+
throw new Error(`未知客户端 ${id},可选:${Object.keys(CLIENTS).join(' / ')}`);
|
|
89
|
+
}
|
|
90
|
+
return c;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// tokenMode=env 时写进配置的是变量表达式,而不是真实 Key
|
|
94
|
+
export function resolveTokenValue(client, token) {
|
|
95
|
+
if (client.tokenMode === 'env') return client.tokenExpression();
|
|
96
|
+
if (client.tokenMode === 'input') return `\${input:${client.inputId}}`;
|
|
97
|
+
return token;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function needsInlineToken(client) {
|
|
101
|
+
return client.tokenMode === 'literal';
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function requiresTokenForVerify(client) {
|
|
105
|
+
return client.tokenMode !== 'input';
|
|
106
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// MCP 配置文件必须被 git 忽略——本仓两个 .cursor/mcp.json 就是反面教材(未跟踪 + 未忽略 + 明文 Key)。
|
|
2
|
+
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { spawnSync } from 'node:child_process';
|
|
6
|
+
|
|
7
|
+
export function isGitRepo(cwd) {
|
|
8
|
+
const r = spawnSync('git', ['rev-parse', '--is-inside-work-tree'], { cwd, encoding: 'utf8' });
|
|
9
|
+
return r.status === 0 && r.stdout.trim() === 'true';
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function gitRoot(cwd) {
|
|
13
|
+
const r = spawnSync('git', ['rev-parse', '--show-toplevel'], { cwd, encoding: 'utf8' });
|
|
14
|
+
return r.status === 0 ? r.stdout.trim() : null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function isIgnored(cwd, relPath) {
|
|
18
|
+
const r = spawnSync('git', ['check-ignore', '-q', relPath], { cwd });
|
|
19
|
+
return r.status === 0;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function ensureIgnored(cwd, relPath) {
|
|
23
|
+
if (!isGitRepo(cwd)) return { ok: false, reason: 'not_a_git_repo', added: false };
|
|
24
|
+
if (isIgnored(cwd, relPath)) return { ok: true, added: false };
|
|
25
|
+
|
|
26
|
+
const file = path.join(cwd, '.gitignore');
|
|
27
|
+
const prev = fs.existsSync(file) ? fs.readFileSync(file, 'utf8') : '';
|
|
28
|
+
const sep = prev === '' || prev.endsWith('\n') ? '' : '\n';
|
|
29
|
+
fs.appendFileSync(file, `${sep}${relPath}\n`);
|
|
30
|
+
return { ok: isIgnored(cwd, relPath), added: true };
|
|
31
|
+
}
|
package/lib/jsonconf.mjs
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// JSON 配置的安全读写:只动自己那一项,其余条目与缩进尽量原样保留。
|
|
2
|
+
// 带注释/尾逗号的配置一律不写,改为打印片段让人手工合并。
|
|
3
|
+
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
|
|
7
|
+
export function hasJsonComment(raw) {
|
|
8
|
+
let inStr = false;
|
|
9
|
+
let esc = false;
|
|
10
|
+
for (let i = 0; i < raw.length; i += 1) {
|
|
11
|
+
const c = raw[i];
|
|
12
|
+
if (inStr) {
|
|
13
|
+
if (esc) {
|
|
14
|
+
esc = false;
|
|
15
|
+
} else if (c === '\\') {
|
|
16
|
+
esc = true;
|
|
17
|
+
} else if (c === '"') {
|
|
18
|
+
inStr = false;
|
|
19
|
+
}
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (c === '"') {
|
|
23
|
+
inStr = true;
|
|
24
|
+
} else if (c === '/' && (raw[i + 1] === '/' || raw[i + 1] === '*')) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function detectIndent(raw) {
|
|
32
|
+
const m = /^([ \t]+)"/m.exec(raw);
|
|
33
|
+
if (!m) return 2;
|
|
34
|
+
const pad = m[1];
|
|
35
|
+
return pad.includes('\t') ? '\t' : pad.length;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function readConfig(file) {
|
|
39
|
+
if (!fs.existsSync(file)) {
|
|
40
|
+
return { exists: false, data: {}, indent: 2, hasComment: false };
|
|
41
|
+
}
|
|
42
|
+
const raw = fs.readFileSync(file, 'utf8').replace(/^\uFEFF/, '');
|
|
43
|
+
if (raw.trim() === '') {
|
|
44
|
+
return { exists: true, data: {}, indent: 2, hasComment: false };
|
|
45
|
+
}
|
|
46
|
+
const hasComment = hasJsonComment(raw);
|
|
47
|
+
if (hasComment) {
|
|
48
|
+
return { exists: true, data: null, indent: detectIndent(raw), hasComment: true, raw };
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
const data = JSON.parse(raw);
|
|
52
|
+
if (data === null || typeof data !== 'object' || Array.isArray(data)) {
|
|
53
|
+
return { exists: true, data: null, indent: detectIndent(raw), parseError: '配置根节点不是对象' };
|
|
54
|
+
}
|
|
55
|
+
return { exists: true, data, indent: detectIndent(raw), hasComment: false };
|
|
56
|
+
} catch (err) {
|
|
57
|
+
return { exists: true, data: null, indent: detectIndent(raw), parseError: err.message };
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function writeConfig(file, data, indent = 2) {
|
|
62
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
63
|
+
fs.writeFileSync(file, `${JSON.stringify(data, null, indent)}\n`, 'utf8');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// 只替换 serversKey 下的目标项,保留其余 server、顶层字段与键顺序
|
|
67
|
+
export function upsertServer(data, serversKey, name, entry) {
|
|
68
|
+
const next = { ...data };
|
|
69
|
+
const servers = next[serversKey] && typeof next[serversKey] === 'object' ? next[serversKey] : {};
|
|
70
|
+
next[serversKey] = { ...servers, [name]: entry };
|
|
71
|
+
return next;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function removeServer(data, serversKey, name) {
|
|
75
|
+
if (!data || !data[serversKey]) return { data, removed: false };
|
|
76
|
+
const servers = { ...data[serversKey] };
|
|
77
|
+
if (!(name in servers)) return { data, removed: false };
|
|
78
|
+
delete servers[name];
|
|
79
|
+
const next = { ...data, [serversKey]: servers };
|
|
80
|
+
if (Object.keys(servers).length === 0) delete next[serversKey];
|
|
81
|
+
return { data: next, removed: true };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function snippetFor(serversKey, name, entry) {
|
|
85
|
+
return `${JSON.stringify({ [serversKey]: { [name]: entry } }, null, 2)}\n`;
|
|
86
|
+
}
|