17a7acd97SBrent Vatne/* 27a7acd97SBrent VatneThis script updates the necessary schema for the passed-in version. 37a7acd97SBrent Vatne 47a7acd97SBrent Vatneyarn run schema-sync 38 -> updates the schema that versions/v38.0.0/sdk/app-config.md uses 57a7acd97SBrent Vatneyarn run schema-sync unversioned -> updates the schema that versions/unversioned/sdk/app-config.md uses 67a7acd97SBrent Vatne*/ 77a7acd97SBrent Vatne 87a7acd97SBrent Vatneconst parser = require('@apidevtools/json-schema-ref-parser'); 97a7acd97SBrent Vatneconst axios = require('axios'); 107a7acd97SBrent Vatneconst fs = require('fs-extra'); 117a7acd97SBrent Vatneconst path = require('path'); 127a7acd97SBrent Vatne 137a7acd97SBrent Vatneconst version = process.argv[2]; 147a7acd97SBrent Vatne 157a7acd97SBrent Vatneasync function run() { 167a7acd97SBrent Vatne if (!version) { 177a7acd97SBrent Vatne console.log('Please enter a version number\n'); 187a7acd97SBrent Vatne console.log('E.g., "yarn run schema-sync 38" \nor, "yarn run schema-sync unversioned"'); 197a7acd97SBrent Vatne return; 207a7acd97SBrent Vatne } 217a7acd97SBrent Vatne 227a7acd97SBrent Vatne if (version === 'unversioned') { 237a7acd97SBrent Vatne const response = await axios.get( 247a7acd97SBrent Vatne `http://exp.host/--/api/v2/project/configuration/schema/UNVERSIONED` 257a7acd97SBrent Vatne ); 267a7acd97SBrent Vatne const schema = await preprocessSchema(response.data.data.schema); 277a7acd97SBrent Vatne 287a7acd97SBrent Vatne await fs.writeFile( 29*ab4f0d2dSBartosz Kaszubowski `public/static/schemas/unversioned/app-config-schema.json`, 307a7acd97SBrent Vatne JSON.stringify(schema.properties), 317a7acd97SBrent Vatne 'utf8' 327a7acd97SBrent Vatne ); 337a7acd97SBrent Vatne } else { 347a7acd97SBrent Vatne try { 357a7acd97SBrent Vatne console.log(`Fetching schema for ${version} from production...`); 367a7acd97SBrent Vatne await fetchAndWriteSchema(version, false); 377a7acd97SBrent Vatne } catch { 387a7acd97SBrent Vatne console.log(`Unable to fetch schema for ${version} from production, trying staging...`); 397a7acd97SBrent Vatne await fetchAndWriteSchema(version, true); 407a7acd97SBrent Vatne } 417a7acd97SBrent Vatne } 427a7acd97SBrent Vatne} 437a7acd97SBrent Vatne 447a7acd97SBrent Vatneasync function fetchAndWriteSchema(version, staging) { 45*ab4f0d2dSBartosz Kaszubowski const schemaPath = `public/static/schemas/v${version}.0.0/app-config-schema.json`; 467a7acd97SBrent Vatne fs.ensureDirSync(path.dirname(schemaPath)); 477a7acd97SBrent Vatne 487a7acd97SBrent Vatne const hostname = staging ? 'staging.exp.host' : 'exp.host'; 497a7acd97SBrent Vatne 507a7acd97SBrent Vatne const response = await axios.get( 517a7acd97SBrent Vatne `http://${hostname}/--/api/v2/project/configuration/schema/${version}.0.0` 527a7acd97SBrent Vatne ); 537a7acd97SBrent Vatne const schema = await preprocessSchema(response.data.data.schema); 547a7acd97SBrent Vatne 55*ab4f0d2dSBartosz Kaszubowski await fs.writeFile(schemaPath, JSON.stringify(schema.properties), 'utf8'); 567a7acd97SBrent Vatne} 577a7acd97SBrent Vatne 587a7acd97SBrent Vatneasync function preprocessSchema(schema) { 597a7acd97SBrent Vatne // replace all $ref references with the actual definitions 607a7acd97SBrent Vatne return await parser.dereference(schema); 617a7acd97SBrent Vatne} 627a7acd97SBrent Vatne 637a7acd97SBrent Vatnerun(); 64