From c387ffff9de25743ab0370c010d5ec02578d1cd5 Mon Sep 17 00:00:00 2001 From: olewandowski1 Date: Fri, 26 Jan 2024 15:30:16 +0100 Subject: [PATCH 1/3] OP-1750: avoid modules duplicates --- dev_tools/installModuleLocally.js | 56 ++++++++++++++++++------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/dev_tools/installModuleLocally.js b/dev_tools/installModuleLocally.js index 1f244f3..3a35360 100644 --- a/dev_tools/installModuleLocally.js +++ b/dev_tools/installModuleLocally.js @@ -49,30 +49,38 @@ function prepareModuleForLocalDevelopment(moduleName){ } -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(); - }) +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); + + const newModuleName = titleCase(camalize(separatedName)).toLowerCase() + "module"; + + const moduleExists = imisJSON["modules"].some((module) => module.name.toLowerCase() === newModuleName); + + 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; + }); + + fs.writeFileSync(path.join(imisJsonPath, "openimis.json"), JSON.stringify(imisJSON, null, 2), { + encoding: "utf8", + flag: "w", + }); + console.log("openimis.json is updated"); + + reinstallAssemblyModule(); + }); } From 1b1c20b430339624cc9d012f126b1072f99e0b38 Mon Sep 17 00:00:00 2001 From: olewandowski1 Date: Fri, 26 Jan 2024 15:30:30 +0100 Subject: [PATCH 2/3] OP-1750: prettier --- dev_tools/installModuleLocally.js | 158 ++++++++++++++---------------- 1 file changed, 76 insertions(+), 82 deletions(-) diff --git a/dev_tools/installModuleLocally.js b/dev_tools/installModuleLocally.js index 3a35360..26e593a 100644 --- a/dev_tools/installModuleLocally.js +++ b/dev_tools/installModuleLocally.js @@ -1,53 +1,50 @@ -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); -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 + 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 updateModuleInAssembly(packageVersion) { const imisJsonPath = path.normalize(path.join(__dirname, "..")); @@ -83,57 +80,54 @@ function updateModuleInAssembly(packageVersion) { }); } +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); -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"); + if ("@openimis/" + splitedModuleName in imisPackageJSON["dependencies"]) { + imisPackageJSON["dependencies"]["@openimis/" + splitedModuleName] = "file:../" + moduleName; + } else { + imisPackageJSON["dependencies"]["@openimis/" + splitedModuleName] = "file:../" + moduleName; + } - console.log('uninstall external module'); - shell.exec('yarn remove @openimis/'+splitedModuleName, (error, data) => { - if(error){ - console.log(error); - } - updatePackageInAssembly(); + 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"); -function camalize(str) { - return str.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase()); + console.log("uninstall external module"); + shell.exec("yarn remove @openimis/" + splitedModuleName, (error, data) => { + if (error) { + console.log(error); + } + updatePackageInAssembly(); + }); } - -function titleCase(string){ - return string[0].toUpperCase() + string.slice(1); +function camalize(str) { + return str.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase()); } +function titleCase(string) { + return string[0].toUpperCase() + string.slice(1); +} dowloadModule(moduleRepoUrl, branch); From 2250210772ffa07a946a147e9e31f9176a3957f0 Mon Sep 17 00:00:00 2001 From: olewandowski1 Date: Fri, 26 Jan 2024 15:37:38 +0100 Subject: [PATCH 3/3] OP-1750: add try catch block when loading modules --- src/ModulesManager.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/ModulesManager.js b/src/ModulesManager.js index ce2957f..ff98902 100644 --- a/src/ModulesManager.js +++ b/src/ModulesManager.js @@ -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();