From 3dc0b23a341ca701206ec299570b350e2be778b7 Mon Sep 17 00:00:00 2001 From: Carlos Cuesta Date: Thu, 29 Sep 2022 19:14:52 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Extract=20`getFile`=20func?= =?UTF-8?q?tion=20for=20configuration=20vault?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../configurationVault/getConfiguration.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/utils/configurationVault/getConfiguration.js b/src/utils/configurationVault/getConfiguration.js index 933299260c..f802e3a548 100644 --- a/src/utils/configurationVault/getConfiguration.js +++ b/src/utils/configurationVault/getConfiguration.js @@ -24,6 +24,14 @@ const LOCAL_CONFIGURATION: typeof Conf = new Conf({ } }) +const getFile = (path: string): Buffer | void => { + try { + return JSON.parse(readFileSync(path)) + } catch (error) { + return + } +} + const getConfiguration = (): { get: Function, set: Function } => { const loadConfig = (): { [$Values]: string | boolean @@ -31,14 +39,12 @@ const getConfiguration = (): { get: Function, set: Function } => { const packageJson = `${cwd()}/package.json` const configurationFile = `${cwd()}/.gitmojirc.json` - if (pathExistsSync(packageJson)) { - const config = JSON.parse(readFileSync(packageJson))?.gitmoji - if (config) return config + if (pathExistsSync(packageJson) && getFile(packageJson)?.gitmoji) { + return getFile(packageJson)?.gitmoji } - if (pathExistsSync(configurationFile)) { - const config = JSON.parse(readFileSync(configurationFile)) - if (config) return config + if (pathExistsSync(configurationFile) && getFile(configurationFile)) { + return getFile(configurationFile) } return LOCAL_CONFIGURATION.store