From 17533d873f1d73e9b652b32bd816c71915e0119f Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 8 May 2023 12:18:14 +0200 Subject: [PATCH] refs #4823 Added checkConfig function --- README.md | 2 +- floriday.js | 2 +- utils.js | 22 +++++++++++++++++++--- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c693e0a..5a45c76 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ npm i ### .env file template ```bash -#FLORIDAY DATA +#FLORIDAY DATA CONFIG CLIENT_ID = xxxxxxxxxx CLIENT_SECRET = xxxxxxxxxx API_KEY = xxxxxxxxxx diff --git a/floriday.js b/floriday.js index 67362c7..18023bc 100644 --- a/floriday.js +++ b/floriday.js @@ -9,8 +9,8 @@ const env = process.env; class Floriday { async start() { try { + await utils.checkConfig(); this.tokenExpirationDate = await utils.requestToken(models); - if (!env.API_KEY) throw new Error(`You haven't provided the API key`) if (JSON.parse(env.SYNC_SEQUENCE)) await utils.syncSequence(); if (JSON.parse(env.SYNC_SUPPLIER)) await utils.syncSuppliers(); if (JSON.parse(env.SYNC_CONN)) await utils.syncConn(); diff --git a/utils.js b/utils.js index cb71606..190bb0f 100644 --- a/utils.js +++ b/utils.js @@ -16,9 +16,6 @@ export async function requestToken() { let spinner; try { spinner = ora(`Requesting new token...`).start(); - - if (!env.CLIENT_ID || !env.CLIENT_SECRET) - throw new Error(`You haven't provided the credentials`) const clientConfigData = await models.clientConfig.findOne(); @@ -95,6 +92,25 @@ export async function getCurrentToken() { let data = await models.clientConfig.findOne(); return data.currentToken } + +/** + * Check the floriday data config + */ +export async function checkConfig() { + const spinner = ora(`Checking config...`).start(); + + const excludedEnvVars = ['VSCODE_GIT_ASKPASS_EXTRA_ARGS']; + const requiredEnvVars = Object.keys(env); + const filteredEnvVars = requiredEnvVars.filter(reqEnvVar => !excludedEnvVars.includes(reqEnvVar)); + + for (const reqEnvVar of filteredEnvVars) { + if (!process.env[reqEnvVar]) { + spinner.fail(); + throw new Error(`You haven't provided the ${reqEnvVar} environment variable`); + } + } + spinner.succeed(); +} /** * Returns the expiration data of current token