Dynamically register data/tables schemas (#57905)
This commit is contained in:
@@ -1,8 +1,37 @@
|
||||
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
|
||||
}
|
||||
|
||||
const dataSchemas: DataSchemas = {
|
||||
// Auto-discover table schemas from data/tables/ directory
|
||||
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}`] = `@/data-directory/lib/data-schemas/tables/${name}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return tableSchemas
|
||||
}
|
||||
|
||||
// Manual schema registrations for non-table data
|
||||
const manualSchemas: DataSchemas = {
|
||||
'data/features': '@/data-directory/lib/data-schemas/features.js',
|
||||
'data/variables': '@/data-directory/lib/data-schemas/variables',
|
||||
'data/learning-tracks': '@/data-directory/lib/data-schemas/learning-tracks.js',
|
||||
@@ -10,8 +39,12 @@ const dataSchemas: DataSchemas = {
|
||||
'data/code-languages.yml': '@/data-directory/lib/data-schemas/code-languages',
|
||||
'data/glossaries/candidates.yml': '@/data-directory/lib/data-schemas/glossaries-candidates.js',
|
||||
'data/glossaries/external.yml': '@/data-directory/lib/data-schemas/glossaries-external.js',
|
||||
'data/tables/supported-code-languages.yml':
|
||||
'@/data-directory/lib/data-schemas/supported-code-languages.js',
|
||||
}
|
||||
|
||||
// Combine manual registrations with auto-discovered table schemas
|
||||
const dataSchemas: DataSchemas = {
|
||||
...manualSchemas,
|
||||
...loadTableSchemas(),
|
||||
}
|
||||
|
||||
export default dataSchemas
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import yaml from 'js-yaml'
|
||||
import { readFileSync } from 'fs'
|
||||
import { extname, basename } from 'path'
|
||||
import { readFileSync, existsSync, readdirSync } from 'fs'
|
||||
import { extname, basename, join } from 'path'
|
||||
|
||||
import walk from 'walk-sync'
|
||||
import { beforeAll, describe, expect, test } from 'vitest'
|
||||
@@ -49,3 +49,24 @@ describe('single data files', () => {
|
||||
expect(isValid, formattedErrors).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('YAML-powered tables', () => {
|
||||
test('all table files have corresponding schemas', () => {
|
||||
const tablesDir = join(process.cwd(), 'data/tables')
|
||||
const schemasDir = join(__dirname, '../lib/data-schemas/tables')
|
||||
|
||||
if (existsSync(tablesDir)) {
|
||||
const yamlFiles = readdirSync(tablesDir).filter((file) => file.endsWith('.yml'))
|
||||
|
||||
for (const yamlFile of yamlFiles) {
|
||||
const name = basename(yamlFile, '.yml')
|
||||
const schemaPath = join(schemasDir, `${name}.ts`)
|
||||
expect(existsSync(schemaPath)).toBe(true)
|
||||
|
||||
// Also verify it's registered in the dataSchemas
|
||||
const dataKey = `data/tables/${yamlFile}`
|
||||
expect(dataSchemas[dataKey]).toBeDefined()
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user