|
| 1 | +import { |
| 2 | + copyFileSync, |
| 3 | + existsSync, |
| 4 | + mkdirSync, |
| 5 | + readdirSync, |
| 6 | + statSync, |
| 7 | +} from 'node:fs'; |
| 8 | +import { join, dirname } from 'node:path'; |
| 9 | +import { homedir } from 'node:os'; |
| 10 | + |
| 11 | +/** |
| 12 | + * A custom skill bundled in this repository. |
| 13 | + * Unlike npx-installed skills, these are copied from src/skills/ to ~/.config/opencode/skills/ |
| 14 | + */ |
| 15 | +export interface CustomSkill { |
| 16 | + /** Skill name (folder name) */ |
| 17 | + name: string; |
| 18 | + /** Human-readable description */ |
| 19 | + description: string; |
| 20 | + /** List of agents that should auto-allow this skill */ |
| 21 | + allowedAgents: string[]; |
| 22 | + /** Source path in this repo (relative to project root) */ |
| 23 | + sourcePath: string; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Registry of custom skills bundled in this repository. |
| 28 | + */ |
| 29 | +export const CUSTOM_SKILLS: CustomSkill[] = [ |
| 30 | + { |
| 31 | + name: 'cartography', |
| 32 | + description: 'Repository understanding and hierarchical codemap generation', |
| 33 | + allowedAgents: ['orchestrator'], |
| 34 | + sourcePath: 'src/skills/cartography', |
| 35 | + }, |
| 36 | +]; |
| 37 | + |
| 38 | +/** |
| 39 | + * Get the target directory for custom skills installation. |
| 40 | + */ |
| 41 | +export function getCustomSkillsDir(): string { |
| 42 | + return join(homedir(), '.config', 'opencode', 'skills'); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Recursively copy a directory. |
| 47 | + */ |
| 48 | +function copyDirRecursive(src: string, dest: string): void { |
| 49 | + if (!existsSync(dest)) { |
| 50 | + mkdirSync(dest, { recursive: true }); |
| 51 | + } |
| 52 | + |
| 53 | + const entries = readdirSync(src); |
| 54 | + for (const entry of entries) { |
| 55 | + const srcPath = join(src, entry); |
| 56 | + const destPath = join(dest, entry); |
| 57 | + const stat = statSync(srcPath); |
| 58 | + |
| 59 | + if (stat.isDirectory()) { |
| 60 | + copyDirRecursive(srcPath, destPath); |
| 61 | + } else { |
| 62 | + const destDir = dirname(destPath); |
| 63 | + if (!existsSync(destDir)) { |
| 64 | + mkdirSync(destDir, { recursive: true }); |
| 65 | + } |
| 66 | + copyFileSync(srcPath, destPath); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Install a custom skill by copying from src/skills/ to ~/.config/opencode/skills/ |
| 73 | + * @param skill - The custom skill to install |
| 74 | + * @param projectRoot - Root directory of oh-my-opencode-slim project |
| 75 | + * @returns True if installation succeeded, false otherwise |
| 76 | + */ |
| 77 | +export function installCustomSkill( |
| 78 | + skill: CustomSkill, |
| 79 | + projectRoot: string, |
| 80 | +): boolean { |
| 81 | + try { |
| 82 | + const sourcePath = join(projectRoot, skill.sourcePath); |
| 83 | + const targetPath = join(getCustomSkillsDir(), skill.name); |
| 84 | + |
| 85 | + // Validate source exists |
| 86 | + if (!existsSync(sourcePath)) { |
| 87 | + console.error(`Custom skill source not found: ${sourcePath}`); |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + // Copy skill directory |
| 92 | + copyDirRecursive(sourcePath, targetPath); |
| 93 | + |
| 94 | + return true; |
| 95 | + } catch (error) { |
| 96 | + console.error(`Failed to install custom skill: ${skill.name}`, error); |
| 97 | + return false; |
| 98 | + } |
| 99 | +} |
0 commit comments