From 28a12bd57307934b11a41d41ea2c9a5b478f2f41 Mon Sep 17 00:00:00 2001
From: kraaij <kraaij@astron.nl>
Date: Fri, 20 Oct 2023 14:41:03 +0200
Subject: [PATCH] format document

---
 .../tmss_webapp/prepareTemplateSchemas.js     | 176 +++++++++---------
 1 file changed, 88 insertions(+), 88 deletions(-)

diff --git a/SAS/TMSS/frontend/tmss_webapp/prepareTemplateSchemas.js b/SAS/TMSS/frontend/tmss_webapp/prepareTemplateSchemas.js
index 94213d8db82..8770a062875 100644
--- a/SAS/TMSS/frontend/tmss_webapp/prepareTemplateSchemas.js
+++ b/SAS/TMSS/frontend/tmss_webapp/prepareTemplateSchemas.js
@@ -5,30 +5,30 @@ 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), []);
+	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('../../../../install/opt/lofar/share/tmss/schemas').then(async(backEndFiles) =>  {
+getTemplateFiles('../../../../install/opt/lofar/share/tmss/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; }
+		if (file.indexOf(".json") <= 0) { continue; }
 		//'../../../../install/opt/lofar/share/tmss/schemas/common_schema_template/QA-1.json'
 		//public/schemas/../.././../../../../install/opt/lofar/share/tmss/schemas/common_schema_template/QA-1.json
 		//let folderName = file.substring(file.indexOf("schemas\\")+8, file.lastIndexOf("\\"));
-		let folderName = file.replace('../../../../install/opt/lofar/share/tmss/schemas',"")
+		let folderName = file.replace('../../../../install/opt/lofar/share/tmss/schemas', "")
 		folderName = folderName.substring(1, folderName.lastIndexOf("/"));
-		let fileName = file.substring(file.lastIndexOf("/")+1);
+		let fileName = file.substring(file.lastIndexOf("/") + 1);
 		const data = fs.readFileSync(file, 'utf8')
 		//console.log(data)
 		if (!fs.existsSync("public/schemas/" + folderName)) {
@@ -38,21 +38,21 @@ getTemplateFiles('../../../../install/opt/lofar/share/tmss/schemas').then(async(
 		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) =>  {
+	getTemplateFiles('public/schemas').then(async (files) => {
 		console.log("Updating schema ref path...");
 		console.log(files);
 
 		// 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',
+			//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(await replace(hostReplaceOptions));
-		
+
 		const hostReplaceOptions1 = {
 			//files: 'schemas/scheduling_constraints_template/constraints-1.json',
 			files: files,
@@ -60,8 +60,8 @@ getTemplateFiles('../../../../install/opt/lofar/share/tmss/schemas').then(async(
 			to: 'http://localhost:3000',
 		};
 		console.log(await replace(hostReplaceOptions1));
-		  
-		for (const ver of _.range(1,11)) {
+
+		for (const ver of _.range(1, 11)) {
 			let verRegx = new RegExp(`/${ver}/#`, 'g');
 			let verReplacer = `-${ver}.json#`;
 			// Replace db_id with file extension
@@ -71,7 +71,7 @@ getTemplateFiles('../../../../install/opt/lofar/share/tmss/schemas').then(async(
 				to: verReplacer
 			};
 			console.log(await replace(fileReplaceOptions));
-			
+
 			verRegx = new RegExp(`/${ver}#`, 'g');
 			const fileReplaceOptions1 = {
 				files: files,
@@ -86,22 +86,22 @@ getTemplateFiles('../../../../install/opt/lofar/share/tmss/schemas').then(async(
 		  to: '.json#/schema/definitions',
 		};
 		console.log(await replace(fileReplaceOptions2));*/
-		
+
 		for (let file of files) {
-			if (file.indexOf(".json") <=0) { continue; }
+			if (file.indexOf(".json") <= 0) { continue; }
 			console.log(file);
-			
-			let folderName = file.substring(file.indexOf("schemas/")+8, file.lastIndexOf("/"));
+
+			let folderName = file.substring(file.indexOf("schemas/") + 8, file.lastIndexOf("/"));
 			// Replae folder name		
-			let folderRef = folderName.replace(/_/g,"");
+			let folderRef = folderName.replace(/_/g, "");
 			let folderRefRegex = new RegExp(folderRef, "g");
 			const folderReplaceOptions = {
-			  files: files,
-			  from: folderRefRegex,
-			  to: folderName,
+				files: files,
+				from: folderRefRegex,
+				to: folderName,
 			};
 			console.log(await replace(folderReplaceOptions));
-			
+
 			// Copy templates to build folder so that it can be referred by the test suites
 			const data = fs.readFileSync(file, 'utf8');
 			//console.log(data)
@@ -109,9 +109,9 @@ getTemplateFiles('../../../../install/opt/lofar/share/tmss/schemas').then(async(
 				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);
-			
+			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"]) {
@@ -119,25 +119,25 @@ getTemplateFiles('../../../../install/opt/lofar/share/tmss/schemas').then(async(
 				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(file);
-                const data = fs.readFileSync(file, 'utf8');
-                let strategyTemplate = JSON.parse(data);
-                strategyTemplate.id = index;
+
+		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(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");
-        });
+				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");
@@ -147,62 +147,62 @@ getTemplateFiles('../../../../install/opt/lofar/share/tmss/schemas').then(async(
 			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));
+				definitions = { ...definitions, ...definitionSchema.schema.definitions };
+				definitionFiles.push(definitionTemplate.substring(definitionTemplate.lastIndexOf("/") + 1));
 			}
 		}
 		console.log("... got definitions");
-			
-		getTemplateFiles("build/schemas/task_template").then(async(strategyFiles) => {
+
+		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 strategyTemplate = JSON.parse(data);
-                strategyTemplate.id = index;
-				strategyTemplate.ref_resolved_schema = {definitions: definitions, properties: strategyTemplate.schema["properties"]};
-                if (strategyTemplate.name.indexOf("observation") >= 0) {
-					strategyTemplate.type =  "http://localhost:3000/api/task_type/observation";
+			let index = 1;
+			console.log("Getting task_templates");
+			for (const file of strategyFiles) {
+				const data = fs.readFileSync(file, 'utf8');
+				let strategyTemplate = JSON.parse(data);
+				strategyTemplate.id = index;
+				strategyTemplate.ref_resolved_schema = { definitions: definitions, properties: strategyTemplate.schema["properties"] };
+				if (strategyTemplate.name.indexOf("observation") >= 0) {
+					strategyTemplate.type = "http://localhost:3000/api/task_type/observation";
 					strategyTemplate.type_value = "observation";
-				}	else if (strategyTemplate.name.indexOf("pipeline") >= 0) {
-					strategyTemplate.type =  "http://localhost:3000/api/task_type/pipeline";
+				} else if (strategyTemplate.name.indexOf("pipeline") >= 0) {
+					strategyTemplate.type = "http://localhost:3000/api/task_type/pipeline";
 					strategyTemplate.type_value = "pipeline";
-				}	else if (strategyTemplate.name.indexOf("cleanup") >= 0) {
-					strategyTemplate.type =  "http://localhost:3000/api/task_type/cleanup";
+				} else if (strategyTemplate.name.indexOf("cleanup") >= 0) {
+					strategyTemplate.type = "http://localhost:3000/api/task_type/cleanup";
 					strategyTemplate.type_value = "cleanup";
-				}	else if (strategyTemplate.name.indexOf("ingest") >= 0) {
-					strategyTemplate.type =  "http://localhost:3000/api/task_type/ingest";
+				} else if (strategyTemplate.name.indexOf("ingest") >= 0) {
+					strategyTemplate.type = "http://localhost:3000/api/task_type/ingest";
 					strategyTemplate.type_value = "ingest";
 				}
 
 
-			// Fix bad references items. These where actually pointing to production urls.
-			
-			if (strategyTemplate?.schema?.properties?.station_configuration) { strategyTemplate.schema.properties.station_configuration["$ref"] = "#/definitions/station_configuration"; }
-			if ( strategyTemplate?.schema?.properties?.QA) strategyTemplate.schema.properties.QA["$ref"] = "#/definitions/QA";
-			if ( strategyTemplate?.schema?.properties?.duration) strategyTemplate.schema.properties.duration["$ref"] = "#/definitions/duration";
-			if ( strategyTemplate?.schema?.properties?.calibrator) strategyTemplate.schema.properties.calibrator["$ref"] = "#/definitions/calibrator";
-			if ( strategyTemplate?.schema?.properties?.correlator) strategyTemplate.schema.properties.correlator["$ref"] = "#/definitions/correlator";
-			
+				// Fix bad references items. These where actually pointing to production urls.
+
+				if (strategyTemplate?.schema?.properties?.station_configuration) { strategyTemplate.schema.properties.station_configuration["$ref"] = "#/definitions/station_configuration"; }
+				if (strategyTemplate?.schema?.properties?.QA) strategyTemplate.schema.properties.QA["$ref"] = "#/definitions/QA";
+				if (strategyTemplate?.schema?.properties?.duration) strategyTemplate.schema.properties.duration["$ref"] = "#/definitions/duration";
+				if (strategyTemplate?.schema?.properties?.calibrator) strategyTemplate.schema.properties.calibrator["$ref"] = "#/definitions/calibrator";
+				if (strategyTemplate?.schema?.properties?.correlator) strategyTemplate.schema.properties.correlator["$ref"] = "#/definitions/correlator";
 
-                index++;
-                templates.push(strategyTemplate);
-            }
+
+				index++;
+				templates.push(strategyTemplate);
+			}
 			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");
-        });
+			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) => {
+		getTemplateFiles("build/schemas/scheduling_constraints_template").then(async (strategyFiles) => {
 			let index = 0
-			const constraintDefinitionKeys = [ "distance_on_sky", "elevation", "timedelta", "timestamp", "timewindow", "pointing"];
+			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);
@@ -224,7 +224,7 @@ getTemplateFiles('../../../../install/opt/lofar/share/tmss/schemas').then(async(
 				console.log("... added ref_resolved_schema to constraints_template");
 			}
 		});
-		
+
 	});
 });
 
-- 
GitLab