| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- /**
- * Precompile the abilities config from Yaml to TS
- */
- import * as fs from 'fs'
- import * as path from 'path'
- import * as yaml from 'js-yaml'
- function loadYamlFile(filePath) {
- try {
- const fileContents = fs.readFileSync(filePath, 'utf8')
- return yaml.load(fileContents)
- } catch (error) {
- console.error(`Error loading YAML file ${filePath}:`, error)
- return {}
- }
- }
- function compileAbilitiesConfig() {
- const configDir = path.join(process.cwd(), 'config/abilities/pages')
- const outputPath = path.join(
- process.cwd(),
- 'config/abilities-config-precompiled.ts',
- )
- console.log('Starting abilities config compilation...')
- // Read all YAML files from the pages directory
- const yamlFiles = fs
- .readdirSync(configDir)
- .filter((file) => file.endsWith('.yaml'))
- const compiledAbilities = {}
- yamlFiles.forEach((file) => {
- const filePath = path.join(configDir, file)
- console.log(`Processing ${file}...`)
- const config = loadYamlFile(filePath)
- // Merge all abilities from this file into the compiled config
- Object.assign(compiledAbilities, config)
- })
- // Generate TypeScript content
- const header = `/**
- * AUTO-GENERATED FILE - DO NOT MODIFY MANUALLY
- *
- * This file is automatically generated from YAML configuration files
- * in config/abilities/pages/ directory.
- *
- * To make changes, edit the source YAML files and run the compilation script:
- * npm run compile:abilities
- *
- * Generated on: ${new Date().toISOString()}
- */
- `
- const tsContent = `${header}export default ${JSON.stringify(compiledAbilities, null, 2)} as const
- `
- // Write the compiled TypeScript file
- fs.writeFileSync(outputPath, tsContent, 'utf8')
- console.log(`✓ Abilities config compiled successfully to ${outputPath}`)
- console.log(`✓ Processed ${yamlFiles.length} YAML files`)
- console.log(
- `✓ Generated ${Object.keys(compiledAbilities).length} ability definitions`,
- )
- }
- // Run the compilation
- compileAbilitiesConfig()
|