-
Ramesh Kumar authoredRamesh Kumar authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
prepareTemplateSchemas_dev.js 7.39 KiB
//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);
// 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(await replace(hostReplaceOptions));
const hostReplaceOptions1 = {
//files: 'schemas/scheduling_constraints_template/constraints-1.json',
files: files,
from: /https:\/\/tmss.lofar.eu\/api/g,
to: 'http://localhost:3000',
};
console.log(await replace(hostReplaceOptions1));
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(await replace(fileReplaceOptions));
verRegx = new RegExp(`/${ver}#`, 'g');
const fileReplaceOptions1 = {
files: files,
from: verRegx,
to: verReplacer
};
console.log(await replace(fileReplaceOptions1));
}
/*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(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(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)
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(file);
const data = fs.readFileSync(file, 'utf8');
let strategyTemplate = JSON.parse(data);
strategyTemplate.id = index;
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");
});
getTemplateFiles("build/schemas/task_template").then(async(strategyFiles) => {
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));
}
}
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";
strategyTemplate.type_value = "observation";
} 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";
strategyTemplate.type_value = "cleanup";
} else if (strategyTemplate.name.indexOf("ingest") >= 0) {
strategyTemplate.type = "http://localhost:3000/api/task_type/ingest";
strategyTemplate.type_value = "ingest";
}
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");
});
});
});