compileAbilitiesConfig.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * Precompile the abilities config from Yaml to TS
  3. */
  4. import * as fs from 'fs'
  5. import * as path from 'path'
  6. import * as yaml from 'js-yaml'
  7. function loadYamlFile(filePath) {
  8. try {
  9. const fileContents = fs.readFileSync(filePath, 'utf8')
  10. return yaml.load(fileContents)
  11. } catch (error) {
  12. console.error(`Error loading YAML file ${filePath}:`, error)
  13. return {}
  14. }
  15. }
  16. function compileAbilitiesConfig() {
  17. const configDir = path.join(process.cwd(), 'config/abilities/pages')
  18. const outputPath = path.join(
  19. process.cwd(),
  20. 'config/abilities-config-precompiled.ts',
  21. )
  22. console.log('Starting abilities config compilation...')
  23. // Read all YAML files from the pages directory
  24. const yamlFiles = fs
  25. .readdirSync(configDir)
  26. .filter((file) => file.endsWith('.yaml'))
  27. const compiledAbilities = {}
  28. yamlFiles.forEach((file) => {
  29. const filePath = path.join(configDir, file)
  30. console.log(`Processing ${file}...`)
  31. const config = loadYamlFile(filePath)
  32. // Merge all abilities from this file into the compiled config
  33. Object.assign(compiledAbilities, config)
  34. })
  35. // Generate TypeScript content
  36. const header = `/**
  37. * AUTO-GENERATED FILE - DO NOT MODIFY MANUALLY
  38. *
  39. * This file is automatically generated from YAML configuration files
  40. * in config/abilities/pages/ directory.
  41. *
  42. * To make changes, edit the source YAML files and run the compilation script:
  43. * npm run compile:abilities
  44. *
  45. * Generated on: ${new Date().toISOString()}
  46. */
  47. `
  48. const tsContent = `${header}export default ${JSON.stringify(compiledAbilities, null, 2)} as const
  49. `
  50. // Write the compiled TypeScript file
  51. fs.writeFileSync(outputPath, tsContent, 'utf8')
  52. console.log(`✓ Abilities config compiled successfully to ${outputPath}`)
  53. console.log(`✓ Processed ${yamlFiles.length} YAML files`)
  54. console.log(
  55. `✓ Generated ${Object.keys(compiledAbilities).length} ability definitions`,
  56. )
  57. }
  58. // Run the compilation
  59. compileAbilitiesConfig()