compileAbilitiesConfig.js 2.0 KB

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