|
1 | 1 | import 'poml'; |
2 | 2 | import { ComponentSpec, Parameter } from 'poml/base'; |
3 | 3 |
|
4 | | -import { readFileSync, readdirSync, writeFile, writeFileSync } from 'fs'; |
| 4 | +import { readFileSync, readdirSync, writeFileSync } from 'fs'; |
5 | 5 | import { join } from 'path'; |
6 | 6 | import { formatComponentDocumentation } from './documentFormatter'; |
7 | 7 |
|
| 8 | +const checkMode = process.argv.includes('--check'); |
| 9 | +let hasChanges = false; |
| 10 | + |
| 11 | +function writeOrCheck(filePath: string, content: string) { |
| 12 | + let existing: string | undefined; |
| 13 | + try { |
| 14 | + existing = readFileSync(filePath, 'utf8'); |
| 15 | + } catch { |
| 16 | + existing = undefined; |
| 17 | + } |
| 18 | + |
| 19 | + if (existing !== content) { |
| 20 | + if (checkMode) { |
| 21 | + console.error(`${filePath} is out of date.`); |
| 22 | + hasChanges = true; |
| 23 | + } else { |
| 24 | + writeFileSync(filePath, content); |
| 25 | + console.log(`Updated ${filePath}`); |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
8 | 30 | const basicComponents: string[] = []; |
9 | 31 | const intentions: string[] = []; |
10 | 32 | const dataDisplays: string[] = []; |
@@ -287,7 +309,17 @@ class _TagLib: |
287 | 309 |
|
288 | 310 | const allDocs = scanComponentDocs('packages/poml'); |
289 | 311 | const pythonCode = generatePythonFile(allDocs); |
290 | | -writeFileSync('packages/poml/assets/componentDocs.json', JSON.stringify(allDocs, null, 2)); |
291 | | -writeFileSync('docs/language/components.md', docsToMarkdown(allDocs)); |
292 | | -writeFileSync('python/poml/_tags.py', pythonCode); |
293 | | -console.log('Component documentation generated successfully!'); |
| 312 | +writeOrCheck('packages/poml/assets/componentDocs.json', JSON.stringify(allDocs, null, 2)); |
| 313 | +writeOrCheck('docs/language/components.md', docsToMarkdown(allDocs)); |
| 314 | +writeOrCheck('python/poml/_tags.py', pythonCode); |
| 315 | + |
| 316 | +if (checkMode) { |
| 317 | + if (hasChanges) { |
| 318 | + console.error('Component documentation is out of date. Run `npm run generate-component-spec` to update.'); |
| 319 | + process.exit(1); |
| 320 | + } else { |
| 321 | + console.log('Component documentation is up to date.'); |
| 322 | + } |
| 323 | +} else { |
| 324 | + console.log('Component documentation generated successfully!'); |
| 325 | +} |
0 commit comments