From c619de0b3eda66d62b303b4525903dd76c02e71a Mon Sep 17 00:00:00 2001 From: bedi gupta Date: Thu, 10 Oct 2024 20:56:01 +0530 Subject: [PATCH 1/2] Removed the async keyword from the Promise executor function and undo the last commit --- apps/generator/lib/hooksRegistry.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/apps/generator/lib/hooksRegistry.js b/apps/generator/lib/hooksRegistry.js index 2c21389fb..1e1eb43a6 100644 --- a/apps/generator/lib/hooksRegistry.js +++ b/apps/generator/lib/hooksRegistry.js @@ -24,11 +24,12 @@ module.exports.registerHooks = async (hooks, templateConfig, templateDir, hooksD * @param {String} templateDir Directory where template is located. * @param {String} hooksDir Directory where local hooks are located. */ -async function registerLocalHooks(hooks, templateDir, hooksDir) { - return new Promise(async (resolve, reject) => { +function registerLocalHooks(hooks, templateDir, hooksDir) { + return new Promise((resolve, reject) => { const localHooks = path.resolve(templateDir, hooksDir); - if (!await exists(localHooks)) return resolve(hooks); + exists(localHooks).then(localHooksExist => { + if (!localHooksExist) return resolve(hooks); const walker = xfs.walk(localHooks, { followLinks: false @@ -58,7 +59,8 @@ async function registerLocalHooks(hooks, templateDir, hooksDir) { walker.on('end', async () => { resolve(hooks); }); - }); + }).catch(reject); + }); } /** From e5c1fc7618315fb119852e7f16f5e58762c5141b Mon Sep 17 00:00:00 2001 From: bedi gupta Date: Mon, 14 Oct 2024 15:59:16 +0530 Subject: [PATCH 2/2] implemented suggestion of reviewer and match with sonarcloud --- apps/generator/lib/hooksRegistry.js | 57 +++++++++++++++-------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/apps/generator/lib/hooksRegistry.js b/apps/generator/lib/hooksRegistry.js index 1e1eb43a6..9c2aa5212 100644 --- a/apps/generator/lib/hooksRegistry.js +++ b/apps/generator/lib/hooksRegistry.js @@ -24,43 +24,46 @@ module.exports.registerHooks = async (hooks, templateConfig, templateDir, hooksD * @param {String} templateDir Directory where template is located. * @param {String} hooksDir Directory where local hooks are located. */ -function registerLocalHooks(hooks, templateDir, hooksDir) { - return new Promise((resolve, reject) => { - const localHooks = path.resolve(templateDir, hooksDir); +async function registerLocalHooks(hooks, templateDir, hooksDir) { + const localHooks = path.resolve(templateDir, hooksDir); - exists(localHooks).then(localHooksExist => { - if (!localHooksExist) return resolve(hooks); + try { + const localHooksExist = await exists(localHooks); + if (!localHooksExist) return hooks; - const walker = xfs.walk(localHooks, { - followLinks: false - }); + return new Promise((resolve, reject) => { + const walker = xfs.walk(localHooks, { + followLinks: false + }); - walker.on('file', async (root, stats, next) => { - try { - const filePath = path.resolve(templateDir, path.resolve(root, stats.name)); + walker.on('file', async (root, stats, next) => { + try { + const filePath = path.resolve(templateDir, path.resolve(root, stats.name)); - registerTypeScript(filePath); + await registerTypeScript(filePath); - delete require.cache[require.resolve(filePath)]; - const mod = require(filePath); + delete require.cache[require.resolve(filePath)]; + const mod = require(filePath); - addHook(hooks, mod); + addHook(hooks, mod); - next(); - } catch (e) { - reject(e); - } - }); + next(); + } catch (e) { + reject(e); + } + }); - walker.on('errors', (root, nodeStatsArray) => { - reject(nodeStatsArray); - }); + walker.on('errors', (root, nodeStatsArray) => { + reject(nodeStatsArray); + }); - walker.on('end', async () => { - resolve(hooks); + walker.on('end', () => { + resolve(hooks); + }); }); - }).catch(reject); - }); + } catch (error) { + throw error; + } } /**