//const fs = require('fs').promises; const fs = require('fs'); const path = require('path'); const replace = require('replace-in-file'); const _ = require('lodash'); async function getTemplateFiles(dir) { let files = await fs.promises.readdir(dir); files = await Promise.all(files.map(async file => { const filePath = path.join(dir, file); const stats = await fs.promises.stat(filePath); if (stats.isDirectory()) return getTemplateFiles(filePath); else if (stats.isFile()) return filePath; })); return files.reduce((all, folderContents) => all.concat(folderContents), []); } console.log("Copying backend schema files..."); // Get and Copy all template files from backend source to frontend app folders and format as required getTemplateFiles('../../backend/src/tmss/tmssapp/schemas').then(async (backEndFiles) => { console.log(backEndFiles); // Copy backend template json files to public/schemas folder in frontend app for (let file of backEndFiles) { if (file.indexOf(".json") <= 0) { continue; } let folderName = file.substring(file.indexOf("schemas\\") + 8, file.lastIndexOf("\\")); let fileName = file.substring(file.lastIndexOf("\\") + 1); const data = fs.readFileSync(file, 'utf8') //console.log(data) if (!fs.existsSync("public/schemas/" + folderName)) { fs.mkdirSync("public/schemas/" + folderName, { recursive: true }); } console.log("Writing to file...", "public/schemas/" + folderName + "/" + fileName); fs.writeFileSync("public/schemas/" + folderName + "/" + fileName, data); } // Get all template files from public/schema folder and format as required getTemplateFiles('public/schemas').then(async (files) => { console.log("Updating schema ref path..."); console.log(files); console.log("Replace the host and port address of template apis to localhost") const hostReplaceOptions = { //files: 'schemas/scheduling_constraints_template/constraints-1.json', files: files, from: /http:\/\/127.0.0.1:8000\/api/g, to: 'http://localhost:3000', }; console.log(" \\ hostReplaceOptions (127.0.0.1 to localhost) ") try { const results = await replace(hostReplaceOptions); console.log('Replacement Results:', results) } catch (error) { console.error('Replacement Error occurred:', error); } console.log(" / hostReplaceOptions ") const hostReplaceOptionslofar = { //files: 'schemas/scheduling_constraints_template/constraints-1.json', files: files, from: /https:\/\/tmss.lofar.eu\/api/g, to: 'http://localhost:3000', }; console.log(" \\ hostReplaceOptionslofar (tms lofar to localhost) ") try { const results = await replace(hostReplaceOptionslofar); console.log('hostReplaceOptionslofar Results:', results) } catch (error) { console.error('hostReplaceOptionslofar Error occurred:', error); } console.log(" / hostReplaceOptionslofar ") for (const ver of _.range(1, 11)) { let verRegx = new RegExp(`/${ver}/#`, 'g'); let verReplacer = `-${ver}.json#`; // Replace db_id with file extension const fileReplaceOptions = { files: files, from: verRegx, to: verReplacer }; console.log("\\ fileReplaceOptions ") try { const results = await replace(fileReplaceOptions); console.log('fileReplaceOptions Results:', results) } catch (error) { console.error('fileReplaceOptions Error occurred:', error); } console.log("/ fileReplaceOptions ") verRegx = new RegExp(`/${ver}#`, 'g'); const fileReplaceOptions1 = { files: files, from: verRegx, to: verReplacer }; console.log("\\ fileReplaceOptions1 ") try { const results = await replace(fileReplaceOptions1); console.log('fileReplaceOfileReplaceOptions1ptions Results:', results) } catch (error) { console.error('fileReplaceOptions1 Error occurred:', error); } } console.log("Past Replacer One ") /*const fileReplaceOptions2 = { files: files, from: /.json#\/definitions/g, to: '.json#/schema/definitions', }; console.log(await replace(fileReplaceOptions2));*/ for (let file of files) { if (file.indexOf(".json") <= 0) { continue; } console.log("In Loop, Looking at " + file); let folderName = file.substring(file.indexOf("schemas\\") + 8, file.lastIndexOf("\\")); // Replae folder name let folderRef = folderName.replace(/_/g, ""); let folderRefRegex = new RegExp(folderRef, "g"); const folderReplaceOptions = { files: files, from: folderRefRegex, to: folderName, }; console.log("\\ folderReplaceOptions ") try { const results = await replace(folderReplaceOptions); console.log('folderReplaceOptions Results:', results) } catch (error) { console.error('folderReplaceOptions Error occurred:', error); } console.log("/ folderReplaceOptions ") console.log("Copy templates to build folder so that it can be referred by the test suites") const data = fs.readFileSync(file, 'utf8'); //console.log(data) if (!fs.existsSync("build/schemas/" + folderName)) { console.log("Creating folder.... build/schemas/" + folderName); fs.mkdirSync("build/schemas/" + folderName, { recursive: true }); } console.log("Copying files from ", file, file.replace("public", "build")); fs.writeFileSync(file.replace("public", "build"), data); // Update the json files in public/schemas folder to contain only the schema object const jsonData = JSON.parse(data); if (jsonData["schema"]) { console.log("Updating schema file...", file); fs.writeFileSync(file, JSON.stringify(jsonData["schema"], null, 4)); } } getTemplateFiles("build/schemas/scheduling_unit_observing_strategy_template").then(async (strategyFiles) => { let strategies = []; let index = 1; console.log("Getting observing_strategy_templates"); for (const file of strategyFiles) { console.log("StrategyFiles:" + file); const data = fs.readFileSync(file, 'utf8'); let strategyTemplate = JSON.parse(data); strategyTemplate.id = index; strategyTemplate.state_value = strategyTemplate.state; strategyTemplate.purpose_value = strategyTemplate.purpose; index++; strategies.push(strategyTemplate); } console.log("Creating observing_strategy_templates.json ..."); fs.writeFileSync("src/__mocks__/observing_strategy_templates.json", JSON.stringify({ strategies: strategies }, null, 4)); console.log("... Created observing_strategy_templates.json"); }); console.log("Getting definitions from common_schema_templates...."); let definitionTemplates = await getTemplateFiles("build/schemas/common_schema_template"); console.log(definitionTemplates); let definitions = {}, definitionFiles = []; for (const definitionTemplate of definitionTemplates) { const data = fs.readFileSync(definitionTemplate, 'utf8'); let definitionSchema = JSON.parse(data); if (definitionSchema.schema && definitionSchema.schema.definitions) { definitions = { ...definitions, ...definitionSchema.schema.definitions }; definitionFiles.push(definitionTemplate.substring(definitionTemplate.lastIndexOf("\\") + 1)); } } console.log("... got definitions"); getTemplateFiles("build/schemas/task_template").then(async (strategyFiles) => { let templates = []; let index = 1; console.log("Getting task_templates"); for (const file of strategyFiles) { const data = fs.readFileSync(file, 'utf8'); let taskTemplate = JSON.parse(data); taskTemplate.id = index; taskTemplate.ref_resolved_schema = { definitions: definitions, properties: taskTemplate.schema["properties"] }; if (taskTemplate.name.indexOf("observation") >= 0) { taskTemplate.type = "http://localhost:3000/api/task_type/observation"; taskTemplate.type_value = "observation"; } else if (taskTemplate.name.indexOf("pipeline") >= 0) { taskTemplate.type = "http://localhost:3000/api/task_type/pipeline"; taskTemplate.type_value = "pipeline"; } else if (taskTemplate.name.indexOf("cleanup") >= 0) { taskTemplate.type = "http://localhost:3000/api/task_type/cleanup"; taskTemplate.type_value = "cleanup"; } else if (taskTemplate.name.indexOf("ingest") >= 0) { taskTemplate.type = "http://localhost:3000/api/task_type/ingest"; taskTemplate.type_value = "ingest"; } if (taskTemplate?.schema?.properties?.station_configuration) { taskTemplate.schema.properties.station_configuration["$ref"] = "#/definitions/station_configuration"; } if (taskTemplate?.schema?.properties?.QA) taskTemplate.schema.properties.QA["$ref"] = "#/definitions/QA"; if (taskTemplate?.schema?.properties?.duration) taskTemplate.schema.properties.duration["$ref"] = "#/definitions/duration"; if (taskTemplate?.schema?.properties?.calibrator) taskTemplate.schema.properties.calibrator["$ref"] = "#/definitions/calibrator"; if (taskTemplate?.schema?.properties?.correlator) taskTemplate.schema.properties.correlator["$ref"] = "#/definitions/correlator"; index++; templates.push(taskTemplate); } templates = JSON.stringify(templates); templates = templates.replace(/http:\/\/localhost:3000\/schemas\/common_schema_template\//g, ""); for (const defFile of definitionFiles) { const fileRegex = new RegExp(defFile, 'g'); templates = templates.replace(fileRegex, ""); } console.log("Creating task_templates.json ..."); fs.writeFileSync("src/__mocks__/task_templates.json", JSON.stringify({ templates: JSON.parse(templates) }, null, 4)); console.log("... Created task_templates.json"); }); getTemplateFiles("build/schemas/scheduling_constraints_template").then(async (strategyFiles) => { let index = 0 const constraintDefinitionKeys = ["distance_on_sky", "elevation", "timedelta", "timestamp", "timewindow", "pointing"]; for (const file of strategyFiles) { const data = fs.readFileSync(file, 'utf8'); let constraintTemplate = JSON.parse(data); constraintTemplate.id = index; constraintTemplate.ref_resolved_schema = _.cloneDeep(constraintTemplate.schema); for (const defKey of constraintDefinitionKeys) { if (definitions[defKey]) { constraintTemplate.ref_resolved_schema.definitions[defKey] = definitions[defKey]; } } index++; constraintTemplate = JSON.stringify(constraintTemplate); constraintTemplate = constraintTemplate.replace(/http:\/\/localhost:3000\/schemas\/common_schema_template\//g, ""); for (const defFile of definitionFiles) { const fileRegex = new RegExp(defFile, 'g'); constraintTemplate = constraintTemplate.replace(fileRegex, ""); } fs.writeFileSync(file, JSON.stringify(JSON.parse(constraintTemplate), null, 4)); console.log("... added ref_resolved_schema to constraints_template"); } }); }); });