From d9ff17910bc92c5af4ec04e487df5169d5373c07 Mon Sep 17 00:00:00 2001 From: Dannon Baker Date: Fri, 23 Aug 2024 21:40:30 -0400 Subject: [PATCH] Refactor async npm package installation --- client/gulpfile.js | 86 +++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 51 deletions(-) diff --git a/client/gulpfile.js b/client/gulpfile.js index 3dc198121fb5..abf037453efd 100644 --- a/client/gulpfile.js +++ b/client/gulpfile.js @@ -77,8 +77,6 @@ PATHS.pluginBuildModules = [ path.join(PATHS.pluginBaseDir, `{visualizations,welcome_page}/{${PLUGIN_BUILD_IDS.join(",")}}/package.json`), ]; -const parser = new xml2js.Parser(); - function stageLibs(callback) { Object.keys(PATHS.stagedLibraries).forEach((lib) => { var p1 = path.resolve(path.join(PATHS.nodeModules, lib, PATHS.stagedLibraries[lib][0])); @@ -227,59 +225,45 @@ async function installPlugins(callback) { // Function to parse the XML and install dependencies async function installDependenciesFromXML(xmlPath, pluginDir) { - return new Promise((resolve, reject) => { - fs.readFile(xmlPath, (err, data) => { - if (err) { - console.error("Error reading XML file:", err); - reject(err); - } - parser.parseString(data, async (err, result) => { - if (err) { - console.error("Error parsing XML:", err); - reject(err); - } - // Navigate to the dependencies - const requirements = result.visualization.requirements[0].requirement; - // Extract the details - requirements.forEach(async (dep) => { - const reqType = dep.$.type; - const pkgName = dep.$.package; - const version = dep.$.version; + try { + const pluginXML = fs.readFileSync(xmlPath); + const parsedXML = await xml2js.parseStringPromise(pluginXML); + const requirements = parsedXML.visualization.requirements[0].requirement; + + const installPromises = requirements.map(async (dep) => { + const { type: reqType, package: pkgName, version } = dep.$; - if (reqType == "package" && pkgName && version) { - // install the package. - if ( - child_process.spawnSync( - "npm", - ["install", "--silent", "--no-save", `${pkgName}@${version}`], - { - cwd: pluginDir, - stdio: "inherit", - shell: true, - } - ).status === 0 - ) { - // Copy static from the installed package to the - // plugin's static directory. - // This keeps separation from standard staging. - try { - await fs.copy( - path.join(pluginDir, "node_modules", pkgName, "static"), - path.join(pluginDir, "static") - ); - console.log(`Installed package ${pkgName}@${version} in ${pluginDir}`); - } catch (err) { - console.error(`Error installing package ${pkgName}@${version} in ${pluginDir}`); - } - } else { - console.error(`Error installing package ${pkgName}@${version} in ${pluginDir}`); + if (reqType === "package" && pkgName && version) { + try { + const installResult = child_process.spawnSync( + "npm", + ["install", "--silent", "--no-save", `${pkgName}@${version}`], + { + cwd: pluginDir, + stdio: "inherit", + shell: true, } + ); + + if (installResult.status === 0) { + await fs.copy( + path.join(pluginDir, "node_modules", pkgName, "static"), + path.join(pluginDir, "static") + ); + console.log(`Installed package ${pkgName}@${version} in ${pluginDir}`); + } else { + console.error(`Error installing package ${pkgName}@${version} in ${pluginDir}`); } - }); - resolve(); - }); + } catch (err) { + console.error(`Error handling package ${pkgName}@${version} in ${pluginDir}:`, err); + } + } }); - }); + + await Promise.all(installPromises); + } catch (err) { + console.error(`Error processing XML file ${xmlPath}:`, err); + } } function forceBuildPlugins(callback) {