|
| 1 | +import chalk from 'chalk'; |
| 2 | +import path from 'path'; |
| 3 | +import fs from 'fs'; |
| 4 | +import type { FileResult, Scope } from '../../types.js'; |
| 5 | +import { |
| 6 | + ASSETS_DIR, |
| 7 | + ensureDir, |
| 8 | + createSymlink, |
| 9 | + copyFile, |
| 10 | + copyDir, |
| 11 | + getAgentsDir, |
| 12 | + getCommandsDir, |
| 13 | + getSkillsDir, |
| 14 | + getArchitectureDir, |
| 15 | + getClaudeDir, |
| 16 | + getFiles, |
| 17 | + getDirs, |
| 18 | +} from '../../utils/symlink.js'; |
| 19 | +import { printInstalled } from './print.js'; |
| 20 | + |
| 21 | +/** |
| 22 | + * Claude native install: assets are symlinked (or copied) verbatim into |
| 23 | + * ~/.claude/{agents,commands,skills,architecture}. This is the only target |
| 24 | + * that supports symlinks and the full agents/commands/skills layout. |
| 25 | + */ |
| 26 | + |
| 27 | +/** Symlink or copy every file from a source dir into the target dir. */ |
| 28 | +const linkFiles = (sourceDir: string, targetDir: string, useSymlink: boolean): FileResult[] => { |
| 29 | + if (!fs.existsSync(sourceDir)) { |
| 30 | + return []; |
| 31 | + } |
| 32 | + return getFiles(sourceDir).map((file) => { |
| 33 | + const target = path.join(targetDir, path.basename(file)); |
| 34 | + return useSymlink ? createSymlink(file, target) : copyFile(file, target); |
| 35 | + }); |
| 36 | +}; |
| 37 | + |
| 38 | +const installAgents = (targetDir: string, useSymlink: boolean): FileResult[] => { |
| 39 | + ensureDir(targetDir); |
| 40 | + const results = [ |
| 41 | + ...linkFiles(path.join(ASSETS_DIR, 'agents', 'developers'), targetDir, useSymlink), |
| 42 | + ...linkFiles(path.join(ASSETS_DIR, 'agents', 'utilities'), targetDir, useSymlink), |
| 43 | + ]; |
| 44 | + printInstalled('Agents', targetDir, results); |
| 45 | + return results; |
| 46 | +}; |
| 47 | + |
| 48 | +const installCommands = (targetDir: string, useSymlink: boolean): FileResult[] => { |
| 49 | + ensureDir(targetDir); |
| 50 | + const results = linkFiles(path.join(ASSETS_DIR, 'commands'), targetDir, useSymlink); |
| 51 | + printInstalled('Commands', targetDir, results); |
| 52 | + return results; |
| 53 | +}; |
| 54 | + |
| 55 | +const installSkills = (targetDir: string, useSymlink: boolean): FileResult[] => { |
| 56 | + ensureDir(targetDir); |
| 57 | + const skillsDir = path.join(ASSETS_DIR, 'skills'); |
| 58 | + const results = fs.existsSync(skillsDir) |
| 59 | + ? getDirs(skillsDir).map((dir) => { |
| 60 | + const target = path.join(targetDir, path.basename(dir)); |
| 61 | + return useSymlink ? createSymlink(dir, target) : copyDir(dir, target); |
| 62 | + }) |
| 63 | + : []; |
| 64 | + printInstalled('Skills', targetDir, results); |
| 65 | + return results; |
| 66 | +}; |
| 67 | + |
| 68 | +const installArchitecture = (targetDir: string, useSymlink: boolean): FileResult[] => { |
| 69 | + ensureDir(targetDir); |
| 70 | + const results = linkFiles(path.join(ASSETS_DIR, 'architecture'), targetDir, useSymlink); |
| 71 | + printInstalled('Architecture', targetDir, results); |
| 72 | + return results; |
| 73 | +}; |
| 74 | + |
| 75 | +export const installScope = async (scope: Scope, useSymlink: boolean): Promise<void> => { |
| 76 | + const isGlobal = scope === 'global'; |
| 77 | + const label = isGlobal ? 'Global' : 'Project'; |
| 78 | + const targetPath = isGlobal ? '~/.claude/' : `${process.cwd()}/.claude/`; |
| 79 | + |
| 80 | + console.log(''); |
| 81 | + console.log(chalk.cyan(`>>> ${label} Installation`)); |
| 82 | + console.log(chalk.gray(` Target: ${targetPath}`)); |
| 83 | + console.log(''); |
| 84 | + |
| 85 | + ensureDir(getClaudeDir(scope)); |
| 86 | + |
| 87 | + installAgents(getAgentsDir(scope), useSymlink); |
| 88 | + if (isGlobal) { |
| 89 | + installCommands(getCommandsDir(scope), useSymlink); |
| 90 | + } |
| 91 | + installSkills(getSkillsDir(scope), useSymlink); |
| 92 | + installArchitecture(getArchitectureDir(scope), useSymlink); |
| 93 | + |
| 94 | + if (!isGlobal) { |
| 95 | + console.log(chalk.gray(' Note: Commands are installed globally only')); |
| 96 | + } |
| 97 | + |
| 98 | + console.log(''); |
| 99 | + console.log(chalk.green(`✓ ${label} installation complete!`)); |
| 100 | +}; |
0 commit comments