|
| 1 | +import {dereference} from '@apidevtools/json-schema-ref-parser'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import {fileURLToPath} from 'url'; |
| 5 | + |
| 6 | +const __filename = fileURLToPath(import.meta.url); |
| 7 | +const __dirname = path.dirname(__filename); |
| 8 | + |
| 9 | +// Project root is two levels up from this script (frontend/deployment-scripts) |
| 10 | +const projectRoot = path.join(__dirname, '..', '..'); |
| 11 | +const schemaDir = path.join(projectRoot, 'model', 'schema'); |
| 12 | +const outDir = path.join(projectRoot, 'frontend', 'public', 'flatschema'); |
| 13 | + |
| 14 | +async function flatten(input, output) { |
| 15 | + try { |
| 16 | + const schema = await dereference(input); |
| 17 | + fs.writeFileSync(output, JSON.stringify(schema, null, 2)); |
| 18 | + console.log(`Schema für ${path.basename(input)} gespeichert unter ${output}`); |
| 19 | + } catch (err) { |
| 20 | + console.error(`Fehler beim Verarbeiten von ${input}:`, err.message); |
| 21 | + process.exit(1); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +if (!fs.existsSync(outDir)) { |
| 26 | + fs.mkdirSync(outDir, { recursive: true }); |
| 27 | +} |
| 28 | + |
| 29 | +const langSuffixes = ['en', 'fr']; |
| 30 | +const geometryTypes = ['Point', 'LineString', 'Polygon']; |
| 31 | +const geometryFileNames = ['point', 'line', 'polygon']; |
| 32 | + |
| 33 | +// 1. Process project_core_schema files with geometry variants |
| 34 | +const coreSchemaFiles = fs.readdirSync(schemaDir).filter(f => |
| 35 | + f.startsWith('project_core_schema_') && (f.endsWith('_en.json') || f.endsWith('_fr.json')) |
| 36 | +); |
| 37 | + |
| 38 | +for (const file of coreSchemaFiles) { |
| 39 | + const inputPath = path.join(schemaDir, file); |
| 40 | + |
| 41 | + // Generate geometry-specific files |
| 42 | + for (let i = 0; i < geometryTypes.length; i++) { |
| 43 | + const geomType = geometryTypes[i]; |
| 44 | + const geomFileName = geometryFileNames[i]; |
| 45 | + |
| 46 | + // Read and parse the schema |
| 47 | + const schema = JSON.parse(fs.readFileSync(inputPath, 'utf-8')); |
| 48 | + |
| 49 | + // Add geom property with const |
| 50 | + if (!schema.properties) { |
| 51 | + schema.properties = {}; |
| 52 | + } |
| 53 | + schema.properties.geom = { |
| 54 | + const: geomType |
| 55 | + }; |
| 56 | + |
| 57 | + // Add geom to required array |
| 58 | + if (!schema.required) { |
| 59 | + schema.required = []; |
| 60 | + } |
| 61 | + if (!schema.required.includes('geom')) { |
| 62 | + schema.required.push('geom'); |
| 63 | + } |
| 64 | + |
| 65 | + // Write temporary file with geom attribute |
| 66 | + const tempFile = path.join(schemaDir, `temp_${file.replace('.json', `_${geomFileName}.json`)}`); |
| 67 | + fs.writeFileSync(tempFile, JSON.stringify(schema, null, 2)); |
| 68 | + |
| 69 | + // Flatten and save |
| 70 | + const geomFile = file.replace('.json', `_${geomFileName}.json`); |
| 71 | + const geomOutput = path.join(outDir, geomFile); |
| 72 | + await flatten(tempFile, geomOutput); |
| 73 | + |
| 74 | + // Clean up temp file |
| 75 | + fs.unlinkSync(tempFile); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// 2. Process feature_project_schema.json for each language |
| 80 | +const featureSchemaPath = path.join(schemaDir, 'feature_project_schema.json'); |
| 81 | +if (fs.existsSync(featureSchemaPath)) { |
| 82 | + const featureSchema = JSON.parse(fs.readFileSync(featureSchemaPath, 'utf-8')); |
| 83 | + |
| 84 | + for (const lang of langSuffixes) { |
| 85 | + // Create a copy of the schema |
| 86 | + const langSchema = JSON.parse(JSON.stringify(featureSchema)); |
| 87 | + |
| 88 | + // Replace the reference to project_core_schema.json with language-specific version |
| 89 | + if (langSchema.properties && langSchema.properties.properties && langSchema.properties.properties.$ref) { |
| 90 | + langSchema.properties.properties.$ref = `project_core_schema_${lang}.json`; |
| 91 | + } |
| 92 | + |
| 93 | + // Write temporary file with language-specific reference |
| 94 | + const tempFile = path.join(schemaDir, `temp_feature_project_schema_${lang}.json`); |
| 95 | + fs.writeFileSync(tempFile, JSON.stringify(langSchema, null, 2)); |
| 96 | + |
| 97 | + // Flatten and save |
| 98 | + const output = path.join(outDir, `feature_project_schema_${lang}.json`); |
| 99 | + await flatten(tempFile, output); |
| 100 | + |
| 101 | + // Clean up temp file |
| 102 | + fs.unlinkSync(tempFile); |
| 103 | + } |
| 104 | +} |
0 commit comments