|
|
@@ -0,0 +1,88 @@
|
|
|
+/**
|
|
|
+ * Precompile the abilities config from Yaml to TS
|
|
|
+ */
|
|
|
+
|
|
|
+import * as fs from 'fs'
|
|
|
+import * as path from 'path'
|
|
|
+import * as yaml from 'js-yaml'
|
|
|
+
|
|
|
+interface AbilityCondition {
|
|
|
+ function: string
|
|
|
+ parameters?: unknown[]
|
|
|
+ expectedResult?: boolean
|
|
|
+}
|
|
|
+
|
|
|
+interface AbilityConfig {
|
|
|
+ action: string
|
|
|
+ conditions: AbilityCondition[]
|
|
|
+}
|
|
|
+
|
|
|
+interface CompiledAbilities {
|
|
|
+ [key: string]: AbilityConfig
|
|
|
+}
|
|
|
+
|
|
|
+function loadYamlFile(filePath: string): unknown {
|
|
|
+ try {
|
|
|
+ const fileContents = fs.readFileSync(filePath, 'utf8')
|
|
|
+ return yaml.load(fileContents)
|
|
|
+ } catch (error) {
|
|
|
+ console.error(`Error loading YAML file ${filePath}:`, error)
|
|
|
+ return {}
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function compileAbilitiesConfig(): void {
|
|
|
+ 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: 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 :
|
|
|
+ * yarn prepare
|
|
|
+ *
|
|
|
+ * 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()
|