|
| 1 | +import path from 'path'; |
| 2 | +import { FileSystemUtils } from '../../../utils/file-system.js'; |
| 3 | +import { TemplateManager, SlashCommandId } from '../../templates/index.js'; |
| 4 | +import { OPENSPEC_MARKERS } from '../../config.js'; |
| 5 | + |
| 6 | +export interface SlashCommandTarget { |
| 7 | + id: SlashCommandId; |
| 8 | + path: string; |
| 9 | + kind: 'slash'; |
| 10 | +} |
| 11 | + |
| 12 | +const ALL_COMMANDS: SlashCommandId[] = ['proposal', 'apply', 'archive']; |
| 13 | + |
| 14 | +export abstract class SlashCommandConfigurator { |
| 15 | + abstract readonly toolId: string; |
| 16 | + abstract readonly isAvailable: boolean; |
| 17 | + |
| 18 | + getTargets(): SlashCommandTarget[] { |
| 19 | + return ALL_COMMANDS.map((id) => ({ |
| 20 | + id, |
| 21 | + path: this.getRelativePath(id), |
| 22 | + kind: 'slash' |
| 23 | + })); |
| 24 | + } |
| 25 | + |
| 26 | + async generateAll(projectPath: string, _openspecDir: string): Promise<string[]> { |
| 27 | + const createdOrUpdated: string[] = []; |
| 28 | + |
| 29 | + for (const target of this.getTargets()) { |
| 30 | + const body = TemplateManager.getSlashCommandBody(target.id).trim(); |
| 31 | + const filePath = path.join(projectPath, target.path); |
| 32 | + |
| 33 | + if (await FileSystemUtils.fileExists(filePath)) { |
| 34 | + await this.updateBody(filePath, body); |
| 35 | + } else { |
| 36 | + const frontmatter = this.getFrontmatter(target.id); |
| 37 | + const sections: string[] = []; |
| 38 | + if (frontmatter) { |
| 39 | + sections.push(frontmatter.trim()); |
| 40 | + } |
| 41 | + sections.push(`${OPENSPEC_MARKERS.start}\n${body}\n${OPENSPEC_MARKERS.end}`); |
| 42 | + const content = sections.join('\n') + '\n'; |
| 43 | + await FileSystemUtils.writeFile(filePath, content); |
| 44 | + } |
| 45 | + |
| 46 | + createdOrUpdated.push(target.path); |
| 47 | + } |
| 48 | + |
| 49 | + return createdOrUpdated; |
| 50 | + } |
| 51 | + |
| 52 | + async updateExisting(projectPath: string, _openspecDir: string): Promise<string[]> { |
| 53 | + const updated: string[] = []; |
| 54 | + |
| 55 | + for (const target of this.getTargets()) { |
| 56 | + const filePath = path.join(projectPath, target.path); |
| 57 | + if (await FileSystemUtils.fileExists(filePath)) { |
| 58 | + const body = TemplateManager.getSlashCommandBody(target.id).trim(); |
| 59 | + await this.updateBody(filePath, body); |
| 60 | + updated.push(target.path); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return updated; |
| 65 | + } |
| 66 | + |
| 67 | + protected abstract getRelativePath(id: SlashCommandId): string; |
| 68 | + protected abstract getFrontmatter(id: SlashCommandId): string | undefined; |
| 69 | + |
| 70 | + private async updateBody(filePath: string, body: string): Promise<void> { |
| 71 | + const content = await FileSystemUtils.readFile(filePath); |
| 72 | + const startIndex = content.indexOf(OPENSPEC_MARKERS.start); |
| 73 | + const endIndex = content.indexOf(OPENSPEC_MARKERS.end); |
| 74 | + |
| 75 | + if (startIndex === -1 || endIndex === -1 || endIndex <= startIndex) { |
| 76 | + throw new Error(`Missing OpenSpec markers in ${filePath}`); |
| 77 | + } |
| 78 | + |
| 79 | + const before = content.slice(0, startIndex + OPENSPEC_MARKERS.start.length); |
| 80 | + const after = content.slice(endIndex); |
| 81 | + const updatedContent = `${before}\n${body}\n${after}`; |
| 82 | + |
| 83 | + await FileSystemUtils.writeFile(filePath, updatedContent); |
| 84 | + } |
| 85 | +} |
0 commit comments