#!/usr/bin/env node /** * OpenClaw VOC Skills 一键部署脚本 * * 功能:将 skills 文件夹中的所有技能部署到 OpenClaw 的 skills 目录 * 兼容:Windows / macOS / Linux * 依赖:仅需 Node.js(OpenClaw 已自带) * * 使用方法: * node install.js # 部署所有技能 * node install.js --list # 仅列出技能,不部署 * node install.js --only a,b,c # 仅部署指定技能 * node install.js --skip a,b,c # 跳过指定技能 * node install.js --target /path # 指定目标目录(默认 ~/.openclaw/skills) * node install.js --clean # 部署前清空目标目录 * node install.js --dry-run # 预览模式,不实际写入 */ const fs = require('fs'); const path = require('path'); const os = require('os'); // ============================================ // 配置 // ============================================ const VERSION = '1.0.0'; const SKILLS_SUBDIR = 'skills'; // zip 包内的技能目录名 const TOOLS_SUBDIR = 'tools'; const WORKSPACE_BUNDLE_DIRS = ['douyin-speaking-daily']; // ============================================ // 参数解析 // ============================================ function parseArgs() { const args = process.argv.slice(2); const opts = { list: false, only: [], skip: [], target: '', clean: false, dryRun: false, help: false }; for (let i = 0; i < args.length; i++) { switch (args[i]) { case '--list': case '-l': opts.list = true; break; case '--only': opts.only = (args[++i] || '').split(',').filter(Boolean); break; case '--skip': opts.skip = (args[++i] || '').split(',').filter(Boolean); break; case '--target': case '-t': opts.target = args[++i] || ''; break; case '--clean': opts.clean = true; break; case '--dry-run': opts.dryRun = true; break; case '--help': case '-h': opts.help = true; break; } } return opts; } // ============================================ // 工具函数 // ============================================ function getOpenClawDir() { return path.join(os.homedir(), '.openclaw'); } function getOpenClawSkillsDir() { return path.join(getOpenClawDir(), 'skills'); } function getOpenClawToolsDir() { return path.join(getOpenClawDir(), 'tools'); } function getOpenClawWorkspaceToolsDir() { return path.join(getOpenClawDir(), 'workspace', 'scripts', 'tools'); } function getOpenClawWorkspaceDir() { return path.join(getOpenClawDir(), 'workspace'); } function ensureDir(dir) { if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } } function copyFileSync(src, dest) { ensureDir(path.dirname(dest)); fs.copyFileSync(src, dest); } function removeDir(dir) { if (fs.existsSync(dir)) { fs.rmSync(dir, { recursive: true, force: true }); } } function getFileSize(filePath) { try { return fs.statSync(filePath).size; } catch { return 0; } } function formatSize(bytes) { if (bytes < 1024) return bytes + ' B'; if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB'; return (bytes / 1024 / 1024).toFixed(1) + ' MB'; } // ============================================ // 核心逻辑 // ============================================ function discoverSkills(sourceDir) { if (!fs.existsSync(sourceDir)) { return []; } return fs.readdirSync(sourceDir) .filter(name => { const fullPath = path.join(sourceDir, name); return fs.statSync(fullPath).isDirectory() && fs.existsSync(path.join(fullPath, 'SKILL.md')); }) .map(name => { const skillDir = path.join(sourceDir, name); const files = []; let totalSize = 0; for (const fileName of fs.readdirSync(skillDir)) { const filePath = path.join(skillDir, fileName); if (fs.statSync(filePath).isFile()) { const size = getFileSize(filePath); files.push({ name: fileName, path: filePath, size }); totalSize += size; } } return { name, dir: skillDir, files, totalSize }; }) .sort((a, b) => a.name.localeCompare(b.name)); } function filterSkills(skills, opts) { let filtered = skills; if (opts.only.length > 0) { filtered = filtered.filter(s => opts.only.includes(s.name)); } if (opts.skip.length > 0) { filtered = filtered.filter(s => !opts.skip.includes(s.name)); } return filtered; } function showHelp() { console.log(` OpenClaw VOC Skills Installer v${VERSION} Usage: node install.js [options] Options: --list, -l List available skills without installing --only a,b,c Install only specified skills --skip a,b,c Skip specified skills --target, -t PATH Custom target directory (default: ~/.openclaw/skills) --clean Remove existing skills before installing --dry-run Preview what would be installed --help, -h Show this help Tool installation: tools/* is installed to ~/.openclaw/tools tools/* is mirrored to ~/.openclaw/workspace/scripts/tools Examples: node install.js # Install all skills node install.js --list # List skills node install.js --only tiktok-video-search # Install one skill node install.js --skip test-billing,test-skill # Skip test skills node install.js --target ./my-skills # Custom directory `); } function listSkills(skills) { console.log(`\n Found ${skills.length} skills:\n`); let totalSize = 0; let totalFiles = 0; for (const skill of skills) { const fileNames = skill.files.map(f => f.name).join(', '); console.log(` ${skill.name.padEnd(35)} ${formatSize(skill.totalSize).padStart(8)} [${fileNames}]`); totalSize += skill.totalSize; totalFiles += skill.files.length; } console.log(`\n Total: ${skills.length} skills, ${totalFiles} files, ${formatSize(totalSize)}\n`); } function discoverFilesRecursive(sourceDir, baseDir = sourceDir) { if (!fs.existsSync(sourceDir)) return []; const files = []; for (const entry of fs.readdirSync(sourceDir, { withFileTypes: true })) { const fullPath = path.join(sourceDir, entry.name); if (entry.isDirectory()) { files.push(...discoverFilesRecursive(fullPath, baseDir)); } else if (entry.isFile()) { files.push({ name: path.relative(baseDir, fullPath), path: fullPath, size: getFileSize(fullPath) }); } } return files.sort((a, b) => a.name.localeCompare(b.name)); } function listTools(tools) { if (tools.length === 0) return; console.log('\n Found ' + tools.length + ' tool files:\n'); let totalSize = 0; for (const tool of tools) { console.log(' ' + tool.name.padEnd(55) + ' ' + formatSize(tool.size).padStart(8)); totalSize += tool.size; } console.log('\n Total tools: ' + tools.length + ' files, ' + formatSize(totalSize) + '\n'); } function installTools(tools, opts) { if (tools.length === 0) return { installed: 0, skipped: 0, errors: 0 }; const toolsTarget = getOpenClawToolsDir(); const workspaceToolsTarget = getOpenClawWorkspaceToolsDir(); const prefix = opts.dryRun ? '[DRY-RUN] ' : ''; console.log('\n' + prefix + 'Installing ' + tools.length + ' tool files to:'); console.log(' ' + toolsTarget); console.log(' ' + workspaceToolsTarget + '\n'); let installed = 0; let skipped = 0; let errors = 0; for (const tool of tools) { try { for (const targetRoot of [toolsTarget, workspaceToolsTarget]) { const destFile = path.join(targetRoot, tool.name); if (fs.existsSync(destFile)) { const existingContent = fs.readFileSync(destFile); const newContent = fs.readFileSync(tool.path); if (existingContent.equals(newContent)) { skipped++; continue; } } if (!opts.dryRun) { copyFileSync(tool.path, destFile); } installed++; } process.stdout.write(' [OK] ' + tool.name + '\n'); } catch (err) { process.stdout.write(' [ERROR] ' + tool.name + ': ' + err.message + '\n'); errors++; } } console.log('\n' + prefix + 'Tool files done! Installed: ' + installed + ', unchanged: ' + skipped + ', errors: ' + errors + '\n'); return { installed, skipped, errors }; } function installWorkspaceBundles(scriptDir, opts) { const workspaceRoot = getOpenClawWorkspaceDir(); const prefix = opts.dryRun ? '[DRY-RUN] ' : ''; let installed = 0; let skipped = 0; let errors = 0; for (const bundleName of WORKSPACE_BUNDLE_DIRS) { const sourceDir = path.join(scriptDir, bundleName); if (!fs.existsSync(sourceDir)) continue; const files = discoverFilesRecursive(sourceDir); console.log('\n' + prefix + 'Installing workspace bundle: ' + bundleName); console.log(' ' + path.join(workspaceRoot, bundleName) + '\n'); for (const file of files) { const destFile = path.join(workspaceRoot, bundleName, file.name); try { if (fs.existsSync(destFile)) { const existingContent = fs.readFileSync(destFile); const newContent = fs.readFileSync(file.path); if (existingContent.equals(newContent)) { skipped++; continue; } } if (!opts.dryRun) { copyFileSync(file.path, destFile); } installed++; } catch (err) { process.stdout.write(' [ERROR] ' + path.join(bundleName, file.name) + ': ' + err.message + '\n'); errors++; } } process.stdout.write(' [OK] ' + bundleName + ' (' + files.length + ' files)\n'); } if (installed || skipped || errors) { console.log('\n' + prefix + 'Workspace bundles done! Installed: ' + installed + ', unchanged: ' + skipped + ', errors: ' + errors + '\n'); } return { installed, skipped, errors }; } function installSkills(skills, targetDir, opts) { const prefix = opts.dryRun ? '[DRY-RUN] ' : ''; console.log(`\n${prefix}Installing ${skills.length} skills to:`); console.log(` ${targetDir}\n`); if (opts.clean && !opts.dryRun) { console.log(`${prefix}Cleaning target directory...`); for (const skill of skills) { const dest = path.join(targetDir, skill.name); if (fs.existsSync(dest)) { removeDir(dest); } } } let installed = 0; let skipped = 0; let errors = 0; for (const skill of skills) { const destDir = path.join(targetDir, skill.name); try { if (!opts.dryRun) { ensureDir(destDir); } for (const file of skill.files) { const destFile = path.join(destDir, file.name); // Check if file already exists and is identical if (fs.existsSync(destFile)) { const existingContent = fs.readFileSync(destFile); const newContent = fs.readFileSync(file.path); if (existingContent.equals(newContent)) { skipped++; continue; } } if (!opts.dryRun) { copyFileSync(file.path, destFile); } installed++; } process.stdout.write(` ✅ ${skill.name}\n`); } catch (err) { process.stdout.write(` ❌ ${skill.name}: ${err.message}\n`); errors++; } } console.log(`\n${'─'.repeat(50)}`); console.log(`${prefix}Done!`); console.log(` Installed: ${installed} files`); console.log(` Unchanged: ${skipped} files`); if (errors > 0) console.log(` Errors: ${errors}`); console.log(` Target: ${targetDir}`); console.log(''); if (!opts.dryRun && errors === 0) { console.log(' Skills are ready. Restart OpenClaw gateway to load them.'); console.log(''); } } // ============================================ // 主入口 // ============================================ function main() { const opts = parseArgs(); if (opts.help) { showHelp(); return; } console.log(''); console.log(' ╔══════════════════════════════════════════════╗'); console.log(` ║ OpenClaw VOC Skills Installer v${VERSION} ║`); console.log(' ╚══════════════════════════════════════════════╝'); // Determine source directory (skills/ next to this script) const scriptDir = __dirname; const sourceDir = path.join(scriptDir, SKILLS_SUBDIR); const toolsSourceDir = path.join(scriptDir, TOOLS_SUBDIR); if (!fs.existsSync(sourceDir)) { console.error(`\n ❌ Skills directory not found: ${sourceDir}`); console.error(' Make sure the "skills" folder is in the same directory as this script.\n'); process.exit(1); } // Discover skills const allSkills = discoverSkills(sourceDir); if (allSkills.length === 0) { console.error(`\n ❌ No skills found in: ${sourceDir}\n`); process.exit(1); } // Filter const skills = filterSkills(allSkills, opts); if (opts.list) { listSkills(skills); listTools(discoverFilesRecursive(toolsSourceDir)); return; } if (skills.length === 0) { console.log('\n No skills matched the filter.\n'); return; } // Target directory const targetDir = opts.target || getOpenClawSkillsDir(); // Pre-flight check const openclawDir = getOpenClawDir(); if (!opts.target && !fs.existsSync(openclawDir)) { console.error(`\n ⚠️ OpenClaw directory not found: ${openclawDir}`); console.error(' Is OpenClaw installed? You can specify a custom target with --target PATH\n'); process.exit(1); } const tools = discoverFilesRecursive(toolsSourceDir); listSkills(skills); installSkills(skills, targetDir, opts); installTools(tools, opts); installWorkspaceBundles(scriptDir, opts); } main();