Skip to content

Commit

Permalink
Merge pull request #115 from openimis/feature/OP-1750
Browse files Browse the repository at this point in the history
OP-1750: add try catch block when loading modules, avoid modules duplicates
  • Loading branch information
jdolkowski authored Jan 29, 2024
2 parents 066d999 + 2250210 commit d7e9e66
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 106 deletions.
212 changes: 107 additions & 105 deletions dev_tools/installModuleLocally.js
Original file line number Diff line number Diff line change
@@ -1,131 +1,133 @@
const fs = require('fs');
const shell = require('shelljs');
const fs = require("fs");
const shell = require("shelljs");
const scriptPath = shell.cd(__dirname);
const path = require('path');
const { argv } = require('process');
const { has } = require('lodash');
const path = require("path");
const { argv } = require("process");
const { has } = require("lodash");

const myArgs = process.argv.slice(2);
const moduleRepoUrl = myArgs[0];
const branch = myArgs[1];

const moduleName = moduleRepoUrl.split('/').pop().split('.')[0];
const splitedModuleName = moduleName.split('openimis-')[1].split('_js')[0];
const separatedName = splitedModuleName.split('-')[1];


function dowloadModule(moduleRepoUrl, branch){
shell.cd(__dirname);
shell.cd('..');
shell.cd('..');
shell.exec('git clone '+moduleRepoUrl);

console.log(moduleName);

//checkout to chosen branch
shell.cd(moduleName);
shell.exec('git checkout '+ branch, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
packageVersion = prepareModuleForLocalDevelopment(moduleName);
updateModuleInAssembly(packageVersion);
});
const moduleName = moduleRepoUrl.split("/").pop().split(".")[0];
const splitedModuleName = moduleName.split("openimis-")[1].split("_js")[0];
const separatedName = splitedModuleName.split("-")[1];

function dowloadModule(moduleRepoUrl, branch) {
shell.cd(__dirname);
shell.cd("..");
shell.cd("..");
shell.exec("git clone " + moduleRepoUrl);

console.log(moduleName);

//checkout to chosen branch
shell.cd(moduleName);
shell.exec("git checkout " + branch, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
packageVersion = prepareModuleForLocalDevelopment(moduleName);
updateModuleInAssembly(packageVersion);
});
}


function prepareModuleForLocalDevelopment(moduleName){
shell.cd(moduleName);
shell.exec('yarn unlink');
shell.exec('yarn install');
shell.exec('yarn build');
shell.exec('yarn link');
module_path = shell.pwd();
var pjson = require(path.join(module_path.stdout, 'package.json'));
return pjson.version
function prepareModuleForLocalDevelopment(moduleName) {
shell.cd(moduleName);
shell.exec("yarn unlink");
shell.exec("yarn install");
shell.exec("yarn build");
shell.exec("yarn link");
module_path = shell.pwd();
var pjson = require(path.join(module_path.stdout, "package.json"));
return pjson.version;
}

function updateModuleInAssembly(packageVersion) {
const imisJsonPath = path.normalize(path.join(__dirname, ".."));
fs.readFile(path.join(imisJsonPath, "openimis.json"), "utf8", (error, data) => {
if (error) {
console.log(error);
return;
}
const imisJSON = JSON.parse(data);

function updateModuleInAssembly(packageVersion){
imisJsonPath = path.normalize(path.join(__dirname, '..'));
fs.readFile(path.join(imisJsonPath, 'openimis.json'), 'utf8', (error, data) => {
if(error){
console.log(error);
return;
}
imisJSON = JSON.parse(data);
imisJSON["modules"].push({
"name": titleCase(camalize(separatedName))+"Module",
"npm": '@openimis/'+splitedModuleName+'@'+packageVersion
});

console.log("removing from openimis.json eventual duplicates entries");
imisJSON["modules"] = imisJSON["modules"].filter((obj, pos, arr) => {
return arr
.map(mapObj => mapObj.name)
.indexOf(obj.name) == pos;
});
fs.writeFileSync(path.join(imisJsonPath, 'openimis.json'), JSON.stringify(imisJSON,null,2),{encoding:'utf8',flag:'w'});
console.log("openimis.json is updated");

reinstallAssemblyModule();
})
}
const newModuleName = titleCase(camalize(separatedName)).toLowerCase() + "module";

const moduleExists = imisJSON["modules"].some((module) => module.name.toLowerCase() === newModuleName);

function updatePackageInAssembly(){
imisPackagePath = path.normalize(path.join(__dirname, '..'));
fs.readFile(path.join(imisPackagePath, 'package.json'), 'utf8', (error, data) => {
if(error){
console.log(error);
return;
}
imisPackageJSON = JSON.parse(data);

if("@openimis/"+splitedModuleName in imisPackageJSON["dependencies"]){
imisPackageJSON["dependencies"]["@openimis/"+splitedModuleName]="file:../"+moduleName;
}
else{
imisPackageJSON["dependencies"]["@openimis/"+splitedModuleName]="file:../"+moduleName;
}

fs.writeFileSync(path.join(imisPackagePath, 'package.json'), JSON.stringify(imisPackageJSON,null,2),{encoding:'utf8',flag:'w'});
console.log("package.json is updated");
//do last step to install app assembly again
shell.exec('yarn link '+'"@openimis/'+splitedModuleName+'"');
shell.exec('yarn install');
console.log("Application has been updated!");
})
}
if (!moduleExists) {
imisJSON["modules"].push({
"name": titleCase(camalize(separatedName)) + "Module",
"npm": "@openimis/" + splitedModuleName + "@" + packageVersion,
});
}

imisJSON["modules"] = imisJSON["modules"].filter((obj, pos, arr) => {
return arr.map((mapObj) => mapObj.name.toLowerCase()).indexOf(obj.name.toLowerCase()) === pos;
});

function reinstallAssemblyModule(){
console.log("Link local module");
shell.cd(__dirname);
shell.cd('..');
shell.exec("node modules-config.js");
fs.writeFileSync(path.join(imisJsonPath, "openimis.json"), JSON.stringify(imisJSON, null, 2), {
encoding: "utf8",
flag: "w",
});
console.log("openimis.json is updated");

console.log('uninstall external module');
shell.exec('yarn remove @openimis/'+splitedModuleName, (error, data) => {
if(error){
console.log(error);
}
updatePackageInAssembly();
reinstallAssemblyModule();
});
}

function updatePackageInAssembly() {
imisPackagePath = path.normalize(path.join(__dirname, ".."));
fs.readFile(path.join(imisPackagePath, "package.json"), "utf8", (error, data) => {
if (error) {
console.log(error);
return;
}
imisPackageJSON = JSON.parse(data);

if ("@openimis/" + splitedModuleName in imisPackageJSON["dependencies"]) {
imisPackageJSON["dependencies"]["@openimis/" + splitedModuleName] = "file:../" + moduleName;
} else {
imisPackageJSON["dependencies"]["@openimis/" + splitedModuleName] = "file:../" + moduleName;
}

fs.writeFileSync(path.join(imisPackagePath, "package.json"), JSON.stringify(imisPackageJSON, null, 2), {
encoding: "utf8",
flag: "w",
});
console.log("package.json is updated");
//do last step to install app assembly again
shell.exec("yarn link " + '"@openimis/' + splitedModuleName + '"');
shell.exec("yarn install");
console.log("Application has been updated!");
});
}

function reinstallAssemblyModule() {
console.log("Link local module");
shell.cd(__dirname);
shell.cd("..");
shell.exec("node modules-config.js");

console.log("uninstall external module");
shell.exec("yarn remove @openimis/" + splitedModuleName, (error, data) => {
if (error) {
console.log(error);
}
updatePackageInAssembly();
});
}

function camalize(str) {
return str.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase());
return str.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase());
}


function titleCase(string){
return string[0].toUpperCase() + string.slice(1);
function titleCase(string) {
return string[0].toUpperCase() + string.slice(1);
}


dowloadModule(moduleRepoUrl, branch);
9 changes: 8 additions & 1 deletion src/ModulesManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import { ensureArray } from "@openimis/fe-core";
class ModulesManager {
constructor(cfg) {
this.cfg = cfg;
this.modules = loadModules(cfg);
try {
this.modules = loadModules(cfg);
} catch (error) {
throw new Error(
"Loading modules failed in ModulesManager.js. This might be caused by duplicated modules in /src/modules.js. \n ORIGINAL ERROR: " +
error,
);
}
this.contributionsCache = {};
this.controlsCache = this.buildControlsCache();
this.refsCache = this.buildRefsCache();
Expand Down

0 comments on commit d7e9e66

Please sign in to comment.