| | import fs from 'fs' |
| | import path from 'path' |
| | import { fileURLToPath } from 'url' |
| |
|
| | const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| |
|
| | interface DataSchemas { |
| | [key: string]: string |
| | } |
| |
|
| | |
| | function resolveSchemaPath(filename: string): string { |
| | |
| | const isTest = process.env.NODE_ENV === 'test' |
| |
|
| | if (isTest) { |
| | |
| | |
| | return `../lib/data-schemas/${filename}` |
| | } else { |
| | |
| | return `@/data-directory/lib/data-schemas/${filename}` |
| | } |
| | } |
| |
|
| | |
| | function loadTableSchemas(): DataSchemas { |
| | const tablesDir = path.join(process.cwd(), 'data/tables') |
| | const schemasDir = path.join(__dirname, 'tables') |
| | const tableSchemas: DataSchemas = {} |
| |
|
| | if (fs.existsSync(tablesDir)) { |
| | const yamlFiles = fs.readdirSync(tablesDir).filter((file) => file.endsWith('.yml')) |
| |
|
| | for (const yamlFile of yamlFiles) { |
| | const name = path.basename(yamlFile, '.yml') |
| | const schemaPath = path.join(schemasDir, `${name}.ts`) |
| |
|
| | if (fs.existsSync(schemaPath)) { |
| | |
| | tableSchemas[`data/tables/${yamlFile}`] = resolveSchemaPath(`tables/${name}.ts`) |
| | } |
| | } |
| | } |
| |
|
| | return tableSchemas |
| | } |
| |
|
| | |
| | const manualSchemas: DataSchemas = { |
| | 'data/features': resolveSchemaPath('features.ts'), |
| | 'data/variables': resolveSchemaPath('variables.ts'), |
| | 'data/learning-tracks': resolveSchemaPath('learning-tracks.ts'), |
| | 'data/release-notes': resolveSchemaPath('release-notes.ts'), |
| | 'data/code-languages.yml': resolveSchemaPath('code-languages.ts'), |
| | 'data/glossaries/candidates.yml': resolveSchemaPath('glossaries-candidates.ts'), |
| | 'data/glossaries/external.yml': resolveSchemaPath('glossaries-external.ts'), |
| | } |
| |
|
| | |
| | const dataSchemas: DataSchemas = { |
| | ...manualSchemas, |
| | ...loadTableSchemas(), |
| | } |
| |
|
| | export default dataSchemas |
| |
|