bookworm-smart-assistant/scripts/import-claude-skills.js

191 lines
9.4 KiB
JavaScript
Raw Permalink Normal View History

#!/usr/bin/env node
/**
* 批量导入 claude-skills Bookworm 格式适配脚本
*
* 功能:
* 1. 复制技能目录到 Bookworm skills/
* 2. 转换 SKILL.md 前置元数据 (claude-skills Bookworm 格式)
* 3. 添加 Bookworm 专有字段 (allowed-tools, maturity, composable)
* 4. 转换 description 为中英双语触发格式
*/
const fs = require('fs');
const path = require('path');
const SRC_BASE = path.join(__dirname, '..', 'repos', 'claude-skills', 'skills');
const DST_BASE = path.join(__dirname, '..', 'skills');
// 导入列表: name → Bookworm 适配配置
const IMPORTS = {
'typescript-pro': {
descCN: 'TypeScript 深度专家。当用户需要 TypeScript 高级类型系统、泛型、条件类型、tRPC 全栈类型安全、tsconfig 优化、类型守卫、判别联合类型,或说 "TypeScript"、"类型安全"、"泛型" 时使用此技能。',
tools: 'Read, Glob, Grep, Edit, Write, Bash',
domain: 'development',
composable: { enhances: ['frontend-expert', 'backend-builder'] },
},
'python-pro': {
descCN: 'Python 深度专家。当用户需要 Python 3.11+ 高级特性、类型提示 typing、async/await 异步编程、dataclass、mypy 配置、pytest 高级用法、生产级 Python 模式,或说 "Python"、"类型提示"、"异步Python" 时使用此技能。',
tools: 'Read, Glob, Grep, Edit, Write, Bash',
domain: 'development',
composable: { enhances: ['backend-builder', 'data-engineer-expert'] },
},
'golang-pro': {
descCN: 'Go 语言深度专家。当用户需要 Go 1.21+ 并发编程、goroutine/channel、gRPC 服务、微服务架构、Go 性能优化、Go 模块管理,或说 "Go语言"、"Golang"、"goroutine" 时使用此技能。',
tools: 'Read, Glob, Grep, Edit, Write, Bash',
domain: 'development',
composable: { enhances: ['backend-builder', 'cloud-native-expert'] },
},
'api-designer': {
descCN: 'API 设计架构师。当用户需要设计 REST/GraphQL API、OpenAPI 3.1 规范、资源建模、API 版本策略、分页模式、错误处理标准、API 文档规范,或说 "API设计"、"OpenAPI"、"接口规范" 时使用此技能。',
tools: 'Read, Glob, Grep, Edit, Write, Bash',
domain: 'architecture',
composable: { enhances: ['backend-builder', 'tech-writer-expert'] },
},
'graphql-architect': {
descCN: 'GraphQL 架构专家。当用户需要 GraphQL Schema 设计、Resolver 实现、订阅 Subscription、Apollo/Relay 客户端、N+1 问题解决、DataLoader、GraphQL 缓存策略,或说 "GraphQL"、"Schema设计"、"Apollo" 时使用此技能。',
tools: 'Read, Glob, Grep, Edit, Write, Bash',
domain: 'architecture',
composable: { enhances: ['api-designer', 'frontend-expert'] },
},
'angular-architect': {
descCN: 'Angular 架构专家。当用户需要 Angular 17+ standalone 组件、Signals、RxJS 响应式编程、NgRx 状态管理、Angular 路由、Angular 性能优化、企业级 Angular 应用,或说 "Angular"、"RxJS"、"NgRx" 时使用此技能。',
tools: 'Read, Glob, Grep, Edit, Write, Bash',
domain: 'development',
composable: { enhances: ['frontend-expert', 'typescript-pro'] },
},
'cloud-architect': {
descCN: '云架构专家。当用户需要 AWS/GCP/Azure 云服务架构、EC2/Lambda/S3、Cloud Run/BigQuery、成本优化 FinOps、灾备策略、混合云部署、Well-Architected Framework或说 "云架构"、"AWS"、"GCP"、"Azure" 时使用此技能。',
tools: 'Read, Glob, Grep, Edit, Write, Bash',
domain: 'architecture',
composable: { enhances: ['architect-expert', 'devops-expert'] },
},
'kubernetes-specialist': {
descCN: 'Kubernetes 深度专家。当用户需要 K8s 集群配置、Helm Charts、RBAC 安全策略、NetworkPolicy、存储配置 PV/PVC、Pod 调度、K8s 故障排查、Operator 开发,或说 "K8s部署"、"Helm"、"K8s排错" 时使用此技能。注意: K8s+服务网格/Istio 路由到 cloud-native-expert。',
tools: 'Read, Glob, Grep, Edit, Write, Bash',
domain: 'devops',
composable: { enhances: ['cloud-native-expert', 'devops-expert'] },
},
'rust-engineer': {
descCN: 'Rust 系统编程专家。当用户需要 Rust 所有权/借用/生命周期、async/await、宏开发、trait 设计、错误处理 Result/Option、性能优化、WebAssembly/WASM或说 "Rust"、"所有权"、"借用检查" 时使用此技能。',
tools: 'Read, Glob, Grep, Edit, Write, Bash',
domain: 'development',
composable: { enhances: ['performance-expert', 'edge-computing-expert'] },
},
'vue-expert': {
descCN: 'Vue.js 深度专家。当用户需要 Vue 3 Composition API、Pinia 状态管理、Vue Router、SSR/Nuxt、组件设计模式、Vue 性能优化、TypeScript + Vue 集成,或说 "Vue3"、"Composition API"、"Pinia" 时使用此技能。',
tools: 'Read, Glob, Grep, Edit, Write, Bash',
domain: 'development',
composable: { enhances: ['frontend-expert', 'typescript-pro'] },
},
'flutter-expert': {
descCN: 'Flutter 跨平台专家。当用户需要 Flutter/Dart 开发、Widget 架构、状态管理 Riverpod/BLoC、Platform Channel、Flutter 性能优化、Flutter 测试,或说 "Flutter"、"Dart"、"Widget" 时使用此技能。',
tools: 'Read, Glob, Grep, Edit, Write, Bash',
domain: 'development',
composable: { enhances: ['mobile-expert'] },
},
'swift-expert': {
descCN: 'Swift/iOS 深度专家。当用户需要 Swift 5.9+/SwiftUI 开发、async/await 并发、Core Data、网络层架构、Swift 测试、App 性能优化,或说 "Swift"、"SwiftUI"、"iOS原生" 时使用此技能。',
tools: 'Read, Glob, Grep, Edit, Write, Bash',
domain: 'development',
composable: { enhances: ['mobile-expert'] },
},
'nextjs-developer': {
descCN: 'Next.js 全栈专家。当用户需要 Next.js 14+ App Router、RSC 服务端组件、Server Actions、ISR/SSG/SSR 渲染策略、Middleware、Next.js 性能优化,或说 "Next.js"、"App Router"、"Server Components" 时使用此技能。',
tools: 'Read, Glob, Grep, Edit, Write, Bash',
domain: 'development',
composable: { enhances: ['frontend-expert', 'typescript-pro'] },
},
'websocket-engineer': {
descCN: 'WebSocket 实时通信专家。当用户需要 WebSocket 协议实现、Socket.IO、实时消息推送、连接管理、心跳保活、断线重连、房间/频道管理,或说 "WebSocket"、"实时通信"、"Socket.IO" 时使用此技能。',
tools: 'Read, Glob, Grep, Edit, Write, Bash',
domain: 'development',
composable: { enhances: ['backend-builder', 'notification-system-expert'] },
},
'terraform-engineer': {
descCN: 'Terraform 基础设施即代码专家。当用户需要 Terraform/OpenTofu IaC、HCL 配置、模块设计、State 管理、Provider 开发、多云 IaC 编排,或说 "Terraform"、"IaC"、"基础设施即代码" 时使用此技能。',
tools: 'Read, Glob, Grep, Edit, Write, Bash',
domain: 'devops',
composable: { enhances: ['cloud-architect', 'devops-expert'] },
},
};
let imported = 0;
let skipped = 0;
const errors = [];
for (const [name, config] of Object.entries(IMPORTS)) {
const src = path.join(SRC_BASE, name);
const dst = path.join(DST_BASE, name);
// 复制目录 (如果已存在则只更新 SKILL.md)
if (!fs.existsSync(dst)) {
try {
copyDirSync(src, dst);
} catch (e) {
errors.push(`${name}: 复制失败 — ${e.message}`);
continue;
}
}
// 读取并转换 SKILL.md
const skillFile = path.join(dst, 'SKILL.md');
if (!fs.existsSync(skillFile)) {
errors.push(`${name}: SKILL.md 不存在`);
continue;
}
let content = fs.readFileSync(skillFile, 'utf8');
// 替换前置元数据
// 统一换行符为 LF
content = content.replace(/\r\n/g, '\n');
const fmMatch = content.match(/^---\n([\s\S]*?)\n---/);
if (!fmMatch) {
errors.push(`${name}: 无法解析前置元数据`);
continue;
}
// 构建 Bookworm 格式前置元数据
const composableBlock = config.composable
? `\ncomposable: true\n enhances: [${config.composable.enhances.join(', ')}]`
: '';
const newFrontmatter = `---
name: ${name}
description: >
${config.descCN}
allowed-tools: ${config.tools}
maturity: imported
last-reviewed: 2026-03-03${composableBlock}
---`;
content = content.replace(/^---\n[\s\S]*?\n---/, newFrontmatter);
fs.writeFileSync(skillFile, content, 'utf8');
console.log(`[OK] ${name} — 已导入并适配`);
imported++;
}
console.log(`\n═══════════════════════════════`);
console.log(`导入: ${imported} | 跳过: ${skipped} | 错误: ${errors.length}`);
if (errors.length > 0) {
console.log('错误详情:');
errors.forEach(e => console.log(` ! ${e}`));
}
console.log(`═══════════════════════════════`);
// === 工具函数 ===
function copyDirSync(src, dst) {
fs.mkdirSync(dst, { recursive: true });
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
const srcPath = path.join(src, entry.name);
const dstPath = path.join(dst, entry.name);
if (entry.isDirectory()) {
copyDirSync(srcPath, dstPath);
} else {
fs.copyFileSync(srcPath, dstPath);
}
}
}