|
| 1 | +import fs from 'fs' |
| 2 | +import path from 'path' |
| 3 | +import { fileURLToPath } from 'url' |
| 4 | + |
| 5 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 6 | + |
1 | 7 | interface DataSchemas { |
2 | 8 | [key: string]: string |
3 | 9 | } |
4 | 10 |
|
5 | | -const dataSchemas: DataSchemas = { |
| 11 | +// Auto-discover table schemas from data/tables/ directory |
| 12 | +function loadTableSchemas(): DataSchemas { |
| 13 | + const tablesDir = path.join(process.cwd(), 'data/tables') |
| 14 | + const schemasDir = path.join(__dirname, 'tables') |
| 15 | + const tableSchemas: DataSchemas = {} |
| 16 | + |
| 17 | + if (fs.existsSync(tablesDir)) { |
| 18 | + const yamlFiles = fs.readdirSync(tablesDir).filter((file) => file.endsWith('.yml')) |
| 19 | + |
| 20 | + for (const yamlFile of yamlFiles) { |
| 21 | + const name = path.basename(yamlFile, '.yml') |
| 22 | + const schemaPath = path.join(schemasDir, `${name}.ts`) |
| 23 | + |
| 24 | + if (fs.existsSync(schemaPath)) { |
| 25 | + tableSchemas[`data/tables/${yamlFile}`] = `@/data-directory/lib/data-schemas/tables/${name}` |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + return tableSchemas |
| 31 | +} |
| 32 | + |
| 33 | +// Manual schema registrations for non-table data |
| 34 | +const manualSchemas: DataSchemas = { |
6 | 35 | 'data/features': '@/data-directory/lib/data-schemas/features.js', |
7 | 36 | 'data/variables': '@/data-directory/lib/data-schemas/variables', |
8 | 37 | 'data/learning-tracks': '@/data-directory/lib/data-schemas/learning-tracks.js', |
9 | 38 | 'data/release-notes': '@/data-directory/lib/data-schemas/release-notes.js', |
10 | 39 | 'data/code-languages.yml': '@/data-directory/lib/data-schemas/code-languages', |
11 | 40 | 'data/glossaries/candidates.yml': '@/data-directory/lib/data-schemas/glossaries-candidates.js', |
12 | 41 | 'data/glossaries/external.yml': '@/data-directory/lib/data-schemas/glossaries-external.js', |
13 | | - 'data/tables/supported-code-languages.yml': |
14 | | - '@/data-directory/lib/data-schemas/supported-code-languages.js', |
| 42 | +} |
| 43 | + |
| 44 | +// Combine manual registrations with auto-discovered table schemas |
| 45 | +const dataSchemas: DataSchemas = { |
| 46 | + ...manualSchemas, |
| 47 | + ...loadTableSchemas(), |
15 | 48 | } |
16 | 49 |
|
17 | 50 | export default dataSchemas |
0 commit comments