| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- #!/usr/bin/env node
- const fs = require('fs');
- const os = require('os');
- const path = require('path');
- const { spawnSync } = require('child_process');
- const ROOT = __dirname;
- const EXCLUDED_ROOTS = new Set(['.claude', '.git', '.github', '.playwright-cli', '.vscode', 'coverage', 'dist', 'node_modules', 'output', 'outputs']);
- const EXCLUDED_FILES = new Set(['.env', '.env.local', '.npmrc', 'poll_reply.log', 'poll_state.json']);
- function usage() {
- return [
- '企业微信 Claude Code 技能包安装器',
- '',
- '用法:',
- ' node install.js --check',
- ' node install.js workspace <客户项目目录>',
- ' node install.js workspace <客户项目目录> --smoke',
- '',
- 'workspace 模式会写入 .claude/plugins、.claude/skills 和项目级 .mcp.json。',
- ].join('\n');
- }
- function readJson(filePath, fallback = {}) {
- try { return JSON.parse(fs.readFileSync(filePath, 'utf8')); }
- catch { return fallback; }
- }
- function writeJson(filePath, value) {
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
- fs.writeFileSync(filePath, `${JSON.stringify(value, null, 2)}\n`, 'utf8');
- }
- function assertRequiredFiles() {
- const required = [
- 'package.json',
- '.claude-plugin/plugin.json',
- 'skill-package-manifest.json',
- 'mcp/src/server.js',
- 'skills/qiwei-dashboard/SKILL.md',
- 'skills/qiwei-goal-management/SKILL.md',
- 'skills/qiwei-real-estate-auto-reply/SKILL.md',
- ];
- for (const relative of required) {
- if (!fs.existsSync(path.join(ROOT, relative))) throw new Error(`缺少安装文件:${relative}`);
- }
- const [major, minor] = process.versions.node.split('.').map(Number);
- if (!Number.isFinite(major) || major < 22 || (major === 22 && minor < 5)) {
- throw new Error(`需要 Node.js 22.5+(使用内置 node:sqlite),当前版本 ${process.version}`);
- }
- }
- function assertInside(target, parent) {
- const resolvedTarget = path.resolve(target);
- const resolvedParent = path.resolve(parent);
- const relative = path.relative(resolvedParent, resolvedTarget);
- if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) {
- throw new Error(`拒绝操作不安全路径:${resolvedTarget}`);
- }
- }
- function copyFilter(source) {
- const relative = path.relative(ROOT, source);
- if (!relative) return true;
- const parts = relative.split(path.sep);
- if (EXCLUDED_ROOTS.has(parts[0])) return false;
- if (EXCLUDED_FILES.has(path.basename(source))) return false;
- if (/\.(?:tgz|zip)$/i.test(source)) return false;
- if (/^\.env\..+/i.test(path.basename(source)) && path.basename(source) !== '.env.example') return false;
- return true;
- }
- function copyTree(source, destination, filter = () => true) {
- if (!filter(source)) return;
- const stat = fs.statSync(source);
- if (stat.isDirectory()) {
- fs.mkdirSync(destination, { recursive: true });
- for (const entry of fs.readdirSync(source)) {
- copyTree(path.join(source, entry), path.join(destination, entry), filter);
- }
- return;
- }
- fs.mkdirSync(path.dirname(destination), { recursive: true });
- fs.copyFileSync(source, destination);
- }
- function run(command, args, cwd, options = {}) {
- const useCmd = process.platform === 'win32' && command === 'npm';
- const result = spawnSync(useCmd ? 'cmd.exe' : command, useCmd ? ['/d', '/s', '/c', 'npm', ...args] : args, {
- cwd,
- env: { ...process.env, ...(options.env || {}) },
- stdio: 'inherit',
- encoding: 'utf8',
- windowsHide: true,
- });
- if (result.status !== 0) throw new Error(`${command} ${args.join(' ')} 执行失败`);
- }
- function installWorkspace(targetInput, options = {}) {
- const target = path.resolve(targetInput || process.cwd());
- fs.mkdirSync(target, { recursive: true });
- const pluginsRoot = path.join(target, '.claude', 'plugins');
- const skillsRoot = path.join(target, '.claude', 'skills');
- const pluginDir = path.join(pluginsRoot, 'qiwei-assistant');
- assertInside(pluginDir, pluginsRoot);
- fs.rmSync(pluginDir, { recursive: true, force: true });
- fs.mkdirSync(pluginDir, { recursive: true });
- copyTree(ROOT, pluginDir, copyFilter);
- for (const entry of fs.readdirSync(path.join(ROOT, 'skills'), { withFileTypes: true })) {
- if (!entry.isDirectory() || !fs.existsSync(path.join(ROOT, 'skills', entry.name, 'SKILL.md'))) continue;
- const destination = path.join(skillsRoot, entry.name);
- assertInside(destination, skillsRoot);
- fs.rmSync(destination, { recursive: true, force: true });
- fs.mkdirSync(path.dirname(destination), { recursive: true });
- copyTree(path.join(ROOT, 'skills', entry.name), destination);
- }
- const mcpPath = path.join(target, '.mcp.json');
- const mcp = readJson(mcpPath, { mcpServers: {} });
- mcp.mcpServers ||= {};
- mcp.mcpServers['qiwei-assistant'] = {
- command: process.execPath,
- args: [path.join(pluginDir, 'mcp', 'src', 'server.js')],
- cwd: pluginDir,
- };
- writeJson(mcpPath, mcp);
- if (!options.skipInstall) run('npm', ['install', '--omit=dev', '--ignore-scripts'], pluginDir);
- if (options.smoke) {
- const smokeRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'qiwei-install-smoke-'));
- const smokeOutputs = path.join(smokeRoot, 'outputs');
- const smokeEnv = {
- QIWEI_OUTPUTS_DIR: smokeOutputs,
- QIWEI_AGENT_DB_PATH: path.join(smokeOutputs, 'messages', 'agent-workbench.db'),
- };
- try {
- run('npm', ['run', 'check'], pluginDir, { env: smokeEnv });
- run('npm', ['run', 'agent:smoke'], pluginDir, { env: smokeEnv });
- run('npm', ['run', 'goal:smoke'], pluginDir, { env: smokeEnv });
- } finally {
- fs.rmSync(smokeRoot, { recursive: true, force: true });
- }
- }
- process.stdout.write([
- '',
- '企业微信技能包已安装到客户项目:',
- ` ${target}`,
- '',
- '下一步:',
- ' 1. 在 Fmode Studio 中打开该项目。',
- ' 2. 启动 Claude Code,调用 qiwei_agent_dashboard_start。',
- ' 3. 在项目终端运行 npm --prefix ".claude/plugins/qiwei-assistant" run agent:session:list 查看客户 Session。',
- '',
- ].join('\n'));
- }
- function main() {
- const args = process.argv.slice(2);
- if (args.includes('--help') || args.includes('-h')) {
- process.stdout.write(`${usage()}\n`);
- return;
- }
- assertRequiredFiles();
- if (args.includes('--check') || !args.length) {
- process.stdout.write('技能包结构检查通过。\n');
- if (!args.length) process.stdout.write(`${usage()}\n`);
- return;
- }
- if (args[0] !== 'workspace') throw new Error('仅支持 workspace 安装模式');
- const target = args[1] && !args[1].startsWith('--') ? args[1] : process.cwd();
- installWorkspace(target, {
- smoke: args.includes('--smoke'),
- skipInstall: args.includes('--skip-install'),
- });
- }
- try { main(); }
- catch (error) {
- process.stderr.write(`安装失败:${error.message}\n`);
- process.exit(1);
- }
|