|
| 1 | +// ABOUTME: Compiles the context template with code examples into rules and skill outputs. |
| 2 | +// ABOUTME: Generates dist/ai-context/checkly.rules.md and skills/monitoring/SKILL.md |
| 3 | + |
| 4 | +import { mkdir, readFile, writeFile } from 'fs/promises' |
| 5 | +import { join } from 'path' |
| 6 | +import { EXAMPLE_CONFIGS } from '../src/ai-context/context' |
| 7 | + |
| 8 | +const EXAMPLES_DIR = join(__dirname, '../gen/') |
| 9 | +const CONTEXT_TEMPLATE_PATH = join( |
| 10 | + __dirname, |
| 11 | + '../src/ai-context/checkly.context.template.md', |
| 12 | +) |
| 13 | +const RULES_OUTPUT_DIR = join(__dirname, '../dist/ai-context') |
| 14 | +const SKILL_OUTPUT_DIR = join(__dirname, '../../../skills/monitoring') |
| 15 | + |
| 16 | +function stripYamlFrontmatter (content: string): string { |
| 17 | + const frontmatterRegex = /^---\n[\s\S]*?\n---\n+/ |
| 18 | + return content.replace(frontmatterRegex, '') |
| 19 | +} |
| 20 | + |
| 21 | +async function writeOutput (content: string, dir: string, filename: string): Promise<void> { |
| 22 | + await mkdir(dir, { recursive: true }) |
| 23 | + const outputPath = join(dir, filename) |
| 24 | + await writeFile(outputPath, content, 'utf8') |
| 25 | + // eslint-disable-next-line no-console |
| 26 | + console.log(`✅ Compiled to ${outputPath}`) |
| 27 | +} |
| 28 | + |
| 29 | +async function compileContext () { |
| 30 | + try { |
| 31 | + // eslint-disable-next-line no-console |
| 32 | + console.log('📝 Compiling context template with examples...') |
| 33 | + |
| 34 | + let content = await readFile(CONTEXT_TEMPLATE_PATH, 'utf8') |
| 35 | + const examples = await readExampleCode() |
| 36 | + |
| 37 | + for (const example of examples) { |
| 38 | + content = content.replace(example.templateString, example.code) |
| 39 | + } |
| 40 | + |
| 41 | + await writeOutput(content, SKILL_OUTPUT_DIR, 'SKILL.md') |
| 42 | + await writeOutput( |
| 43 | + stripYamlFrontmatter(content), |
| 44 | + RULES_OUTPUT_DIR, |
| 45 | + 'checkly.rules.md', |
| 46 | + ) |
| 47 | + } catch (error) { |
| 48 | + // eslint-disable-next-line no-console |
| 49 | + console.error('❌ Failed to compile context:', error) |
| 50 | + process.exit(1) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +async function readExampleCode (): Promise< |
| 55 | + { templateString: string, code: string }[] |
| 56 | +> { |
| 57 | + const examples: { templateString: string, code: string }[] = [] |
| 58 | + |
| 59 | + for (const config of Object.values(EXAMPLE_CONFIGS)) { |
| 60 | + let code: string | undefined |
| 61 | + |
| 62 | + if (config.exampleConfig) { |
| 63 | + code = config.exampleConfig |
| 64 | + } else if (config.exampleConfigPath) { |
| 65 | + const filePath = join(EXAMPLES_DIR, config.exampleConfigPath) |
| 66 | + try { |
| 67 | + code = await readFile(filePath, 'utf8') |
| 68 | + } catch { |
| 69 | + // eslint-disable-next-line no-console |
| 70 | + console.warn( |
| 71 | + `Warning: Could not read example for ${config.templateString} from ${filePath}. It might not exist or be accessible.`, |
| 72 | + ) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (code) { |
| 77 | + const wrappedCode = `**Reference:** ${config.reference}\n\n\`\`\`typescript\n${code}\`\`\`` |
| 78 | + examples.push({ |
| 79 | + templateString: config.templateString, |
| 80 | + code: wrappedCode, |
| 81 | + }) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return examples |
| 86 | +} |
| 87 | + |
| 88 | +compileContext() |
0 commit comments