diff --git a/package.json b/package.json index 953d39d..0cfa9cc 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "dbup": "cd dev/db && docker-compose up -d", "test": "cross-env MB_ENV_FILE=dev/test.env yarn jest --runInBand --no-cache --forceExit --detectOpenHandles", "tdd": "cross-env MB_ENV_FILE=dev/test.env yarn jest --runInBand --no-cache --forceExit --detectOpenHandles --watchAll", - "knex": "cross-env MB_ENV_FILE=dev/dev.env knex --knexfile=./dev/db/dev.knexfile.ts", + "knex": "cross-env MB_ENV_FILE=dev/dev.env MB_ENV_FILE_OVR=dev-overrides.env knex --knexfile=./dev/db/dev.knexfile.ts", "knex:prod": "cross-env MB_ENV_FILE=/etc/mintbean-v4/config/prod.env knex --knexfile /etc/mintbean-v4/config/prod.env", "knex:test": "cross-env MB_ENV_FILE=dev/test.env knex --knexfile ./dev/test/test.knexfile.ts", "pristine": "yarn knex migrate:latest && yarn knex seed:run && yarn knex:test migrate:latest && yarn knex:test seed:run", diff --git a/src/configProvider.ts b/src/configProvider.ts index 53feba8..0b991ac 100644 --- a/src/configProvider.ts +++ b/src/configProvider.ts @@ -1,6 +1,7 @@ import { config, parse } from "dotenv"; -import fs from 'fs'; +import fs from "fs"; import path from "path"; +import * as yup from 'yup'; type Mapper = (x: string) => T; const defaultMapper: Mapper = (x) => x; @@ -16,49 +17,55 @@ function getConfig(key: string, mapper = defaultMapper): any { } } -interface ConfigInstance { - MB_KNEXFILE: string; - MB_SESSION_KEY: string; - MB_ENABLE_GRAPHQL_LOGGER: boolean; - MB_ENABLE_GRAPHIQL: boolean; - MB_FORGOT_PASSWORD_TOKEN_DAYS_TO_LIVE: number; - SENDGRID_KEY: string; - SENDGRID_PRINT_ONLY: boolean; - PORT: string; -} -let instance: ConfigInstance; +const instanceSchema = yup.object().shape({ + MB_KNEXFILE: yup.string().required(), + MB_SESSION_KEY: yup.string().required(), + MB_ENABLE_GRAPHQL_LOGGER: yup.bool().required(), + MB_ENABLE_GRAPHIQL: yup.bool().required(), + MB_FORGOT_PASSWORD_TOKEN_DAYS_TO_LIVE: yup.number().required(), + SENDGRID_KEY: yup.string().required(), + SENDGRID_PRINT_ONLY: yup.bool().required(), + PORT: yup.number().required(), +}).required(); + +let instance : yup.InferType; export default () => { if (!instance) { + const envFilePath = getConfig(`MB_ENV_FILE`); + + const overrideEnvFilePath = getConfig(`MB_ENV_FILE_OVR`); - const envFilePath = getConfig("MB_ENV_FILE"); - - const overrideEnvFilePath = getConfig("MB_ENV_FILE_OVR"); - config({ path: path.join(__dirname, `..`, envFilePath), }); // checks to see if the dev-overrides.env file is present in root directory - if(fs.existsSync(path.join(__dirname, "..", overrideEnvFilePath))) { + if (fs.existsSync(path.join(__dirname, `..`, overrideEnvFilePath))) { // override - const envConfig = parse(fs.readFileSync(path.join(__dirname, "..", overrideEnvFilePath))); + const envConfig = parse(fs.readFileSync(path.join(__dirname, `..`, overrideEnvFilePath))); - for(const key in envConfig) { + for (const key in envConfig) { process.env[key] = envConfig[key]; } } - instance = { - MB_KNEXFILE: getConfig("MB_KNEXFILE"), - MB_SESSION_KEY: getConfig("MB_SESSION_KEY"), - SENDGRID_KEY: getConfig("SENDGRID_KEY"), - MB_ENABLE_GRAPHQL_LOGGER: getConfig("MB_ENABLE_GRAPHQL_LOGGER", (val) => val === "true"), - MB_ENABLE_GRAPHIQL: getConfig("MB_ENABLE_GRAPHIQL", (val) => val === "true"), - SENDGRID_PRINT_ONLY: getConfig("SENDGRID_PRINT_ONLY", (val) => val === "true"), - MB_FORGOT_PASSWORD_TOKEN_DAYS_TO_LIVE: getConfig("MB_FORGOT_PASSWORD_TOKEN_DAYS_TO_LIVE", (val) => +val), - PORT: getConfig("PORT"), - }; + instance = instanceSchema.cast({ + MB_KNEXFILE: getConfig(`MB_KNEXFILE`), + MB_SESSION_KEY: getConfig(`MB_SESSION_KEY`), + SENDGRID_KEY: getConfig(`SENDGRID_KEY`), + MB_ENABLE_GRAPHQL_LOGGER: getConfig(`MB_ENABLE_GRAPHQL_LOGGER`), + MB_ENABLE_GRAPHIQL: getConfig(`MB_ENABLE_GRAPHIQL`), + SENDGRID_PRINT_ONLY: getConfig(`SENDGRID_PRINT_ONLY`), + MB_FORGOT_PASSWORD_TOKEN_DAYS_TO_LIVE: getConfig(`MB_FORGOT_PASSWORD_TOKEN_DAYS_TO_LIVE`), + PORT: getConfig(`PORT`), + }); + + try { + instanceSchema.validate(instance); + } catch (err) { + console.error(err.errors); + } } return instance; diff --git a/src/frontend/src/layouts/Toasts.jsx b/src/frontend/src/layouts/Toasts.jsx index 2de128b..d29d6fb 100644 --- a/src/frontend/src/layouts/Toasts.jsx +++ b/src/frontend/src/layouts/Toasts.jsx @@ -20,8 +20,6 @@ const countdownStyle = css` animation-fill-mode: forwards; `; -import Card, { CardBody, CardHeader, CardHeaderAction } from "../elements/Card"; - const ToastCard = styled(Card)` height: 6em; padding-top: 0; diff --git a/yarn.lock b/yarn.lock index e193daf..20401a0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1399,8 +1399,7 @@ dependencies: regenerator-runtime "^0.13.4" - -"@babel/runtime@^7.14.6": +"@babel/runtime@^7.14.6", "@babel/runtime@^7.5.5", "@babel/runtime@^7.8.7": version "7.14.6" resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.14.6.tgz#535203bc0892efc7dec60bdc27b2ecf6e409062d" integrity sha512-/PCB2uJ7oM44tz8YhC4Z/6PeOKXp4K588f+5M3clr1M4zbqztlo0XEfJ2LEzj/FgwfgGcIdl8n7YYjTCI0BYwg==