| 9 | * Fish automatically loads completions from ~/.config/fish/completions/ |
| 10 | */ |
| 11 | export class FishInstaller { |
| 12 | private readonly homeDir: string; |
| 13 | |
| 14 | constructor(homeDir: string = os.homedir()) { |
| 15 | this.homeDir = homeDir; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Get the installation path for Fish completions |
| 20 | * |
| 21 | * @returns Installation path |
| 22 | */ |
| 23 | getInstallationPath(): string { |
| 24 | return path.join(this.homeDir, '.config', 'fish', 'completions', 'openspec.fish'); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Backup an existing completion file if it exists |
| 29 | * |
| 30 | * @param targetPath - Path to the file to backup |
| 31 | * @returns Path to the backup file, or undefined if no backup was needed |
| 32 | */ |
| 33 | async backupExistingFile(targetPath: string): Promise<string | undefined> { |
| 34 | try { |
| 35 | await fs.access(targetPath); |
| 36 | // File exists, create a backup |
| 37 | const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); |
| 38 | const backupPath = `${targetPath}.backup-${timestamp}`; |
| 39 | await fs.copyFile(targetPath, backupPath); |
| 40 | return backupPath; |
| 41 | } catch { |
| 42 | // File doesn't exist, no backup needed |
| 43 | return undefined; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Install the completion script |
| 49 | * |
| 50 | * @param completionScript - The completion script content to install |
| 51 | * @returns Installation result with status and instructions |
| 52 | */ |
| 53 | async install(completionScript: string): Promise<InstallationResult> { |
| 54 | try { |
| 55 | const targetPath = this.getInstallationPath(); |
| 56 | |
| 57 | // Check if already installed with same content |
| 58 | let isUpdate = false; |
| 59 | try { |
| 60 | const existingContent = await fs.readFile(targetPath, 'utf-8'); |
| 61 | if (existingContent === completionScript) { |
| 62 | // Already installed and up to date |
| 63 | return { |
| 64 | success: true, |
| 65 | installedPath: targetPath, |
| 66 | message: 'Completion script is already installed (up to date)', |
| 67 | instructions: [ |
| 68 | 'The completion script is already installed and up to date.', |
nothing calls this directly
no outgoing calls
no test coverage detected