From 2522f2675ef198588a40c410b4c229e5f33d7321 Mon Sep 17 00:00:00 2001 From: 1000TurquoisePogs Date: Thu, 20 Jun 2024 14:00:52 +0200 Subject: [PATCH] Handle mkdirp throwing eexist Signed-off-by: 1000TurquoisePogs --- lib/initInstance.js | 18 +++++++++--------- lib/initUtils.js | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/initInstance.js b/lib/initInstance.js index 0477775..d261f70 100644 --- a/lib/initInstance.js +++ b/lib/initInstance.js @@ -44,43 +44,43 @@ const versionLocation = path.join(destination, 'component.json'); config.productDir = path.join(__dirname, '..', 'defaults'); //Begin generate any missing folders -fs.mkdirSync(destination, {mode: initUtils.FOLDER_MODE, recursive: true}); +initUtils.mkdirp(destination, initUtils.FOLDER_MODE); if (!config.siteDir) { config.siteDir = path.join(destination, 'site'); } const sitePluginStorage = path.join(config.siteDir, 'ZLUX', 'pluginStorage'); -fs.mkdirSync(sitePluginStorage, {mode: initUtils.FOLDER_MODE, recursive: true}); +initUtils.mkdirp(sitePluginStorage, initUtils.FOLDER_MODE); if (!config.instanceDir) { config.instanceDir = destination; } const instancePluginStorage = path.join(config.instanceDir, 'ZLUX', 'pluginStorage'); -fs.mkdirSync(instancePluginStorage, {mode: initUtils.FOLDER_MODE, recursive: true}); +initUtils.mkdirp(instancePluginStorage, initUtils.FOLDER_MODE); const recognizersPluginStorage = path.join(config.instanceDir, 'ZLUX/pluginStorage', 'org.zowe.zlux.ng2desktop/recognizers'); -fs.mkdirSync(recognizersPluginStorage, {mode: initUtils.FOLDER_MODE, recursive: true}); +initUtils.mkdirp(recognizersPluginStorage, initUtils.FOLDER_MODE); const actionsPluginStorage = path.join(config.instanceDir, 'ZLUX/pluginStorage/org.zowe.zlux.ng2desktop', 'actions'); -fs.mkdirSync(actionsPluginStorage, {mode: initUtils.FOLDER_MODE, recursive: true}); +initUtils.mkdirp(actionsPluginStorage, initUtils.FOLDER_MODE); const instanceConfig = path.join(config.instanceDir, 'serverConfig'); //750 specifically, to keep server config secure -fs.mkdirSync(instanceConfig, {mode: 0o0750}); +initUtils.mkdirp(instanceConfig, 0o0750}); if (!config.groupsDir) { config.groupsDir = path.join(config.instanceDir, 'groups'); } -fs.mkdirSync(config.groupsDir, {mode: initUtils.FOLDER_MODE, recursive: true}); +initUtils.mkdirp(config.groupsDir, initUtils.FOLDER_MODE); if (!config.usersDir) { config.usersDir = path.join(config.instanceDir, 'users'); } -fs.mkdirSync(config.usersDir, {mode: initUtils.FOLDER_MODE, recursive: true}); +initUtils.mkdirp(config.usersDir, initUtils.FOLDER_MODE); if (!config.pluginsDir) { config.pluginsDir = path.join(destination, 'plugins'); } -fs.mkdirSync(config.pluginsDir, {mode: initUtils.FOLDER_MODE, recursive: true}); +initUtils.mkdirp(config.pluginsDir, initUtils.FOLDER_MODE); function generateComponentJson() { let componentJsonContent; diff --git a/lib/initUtils.js b/lib/initUtils.js index 4161e96..3fc7f57 100644 --- a/lib/initUtils.js +++ b/lib/initUtils.js @@ -31,6 +31,16 @@ module.exports.registerBundledPlugins = function(destination, configDestination, }); } +function mkdirp(directory, mode) { + try { + fs.mkdirSync(directory, {mode: mode, recursive: true}); + } catch (e) { + if (e.code != 'EEXIST') { + throw e; + } + } +} +module.exports.mkdirp = mkdirp; function printFormattedMessage(level, message) { @@ -313,7 +323,7 @@ module.exports.registerBundledPlugin = registerBundledPlugin; module.exports.setTerminalDefaults = function(configDestination, instanceItems) { if (instanceItems.indexOf('org.zowe.terminal.vt.json') != -1) { let defaultConfigDir = path.join(configDestination,'org.zowe.terminal.vt','sessions'); - fs.mkdirSync(defaultConfigDir, {recursive: true}); + mkdirp(defaultConfigDir); try { fs.writeFileSync(path.join(defaultConfigDir,'_defaultVT.json'), JSON.stringify({host:process.env['ZWED_SSH_HOST'] ? process.env['ZWED_SSH_HOST'] : "", @@ -329,7 +339,7 @@ module.exports.setTerminalDefaults = function(configDestination, instanceItems) security = process.env['ZWED_TN3270_SECURITY']; } let defaultConfigDir = path.join(configDestination,'org.zowe.terminal.tn3270','sessions'); - fs.mkdirSync(defaultConfigDir, {recursive: true}); + mkdirp(defaultConfigDir); try { let tn3270Json = {host:process.env['ZWED_TN3270_HOST'] ? process.env['ZWED_TN3270_HOST'] : "", port: process.env['ZWED_TN3270_PORT'] ? process.env['ZWED_TN3270_PORT'] : 23,