From 5ceb6e38f32ca3624a0c27817ab0d7f0fb6f7b4e Mon Sep 17 00:00:00 2001 From: Astyax Nourel Date: Sun, 25 Jul 2021 23:14:32 +0200 Subject: [PATCH] adding dotenv and envalid to manage environment variable properly --- .gitignore | 2 +- BackEnd/nodemon.json | 3 +++ BackEnd/package-lock.json | 28 ++++++++++++++++++++++++++++ BackEnd/package.json | 4 +++- BackEnd/src/env/db.ts | 2 ++ BackEnd/src/env/index.ts | 15 +++++++++++++++ BackEnd/src/env/server.ts | 19 +++++++++++++++++++ BackEnd/src/env/utils.ts | 11 +++++++++++ BackEnd/tsconfig.json | 1 + 9 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 BackEnd/src/env/db.ts create mode 100644 BackEnd/src/env/index.ts create mode 100644 BackEnd/src/env/server.ts create mode 100644 BackEnd/src/env/utils.ts diff --git a/.gitignore b/.gitignore index c423139..637b488 100644 --- a/.gitignore +++ b/.gitignore @@ -88,4 +88,4 @@ /BackEnd/configs/OS.js /BackEnd/src/configs/OS.ts /BackEnd/dist - +/BackEnd/.env* diff --git a/BackEnd/nodemon.json b/BackEnd/nodemon.json index 4f37b5a..efe78c1 100644 --- a/BackEnd/nodemon.json +++ b/BackEnd/nodemon.json @@ -2,6 +2,9 @@ "events": { "start": "clear" }, + "execMap": { + "ts": "ts-node -r dotenv/config" + }, "ext": "ts", "ignore": [".git", "node_modules", "dist", "uploads"], "watch": ["./src"] diff --git a/BackEnd/package-lock.json b/BackEnd/package-lock.json index 31ae54c..14a6fd1 100644 --- a/BackEnd/package-lock.json +++ b/BackEnd/package-lock.json @@ -14,7 +14,9 @@ "body-parser": "^1.19.0", "child_process": "^1.0.2", "cors": "2.8.5", + "dotenv": "^10.0.0", "ejs": "3.1.5", + "envalid": "^7.1.1", "errorhandler": "1.5.1", "express": "^4.17.1", "express-generator": "^4.16.1", @@ -3058,6 +3060,14 @@ "node": ">=4" } }, + "node_modules/dotenv": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz", + "integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==", + "engines": { + "node": ">=10" + } + }, "node_modules/duplexer2": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/duplexer2/-/duplexer2-0.1.4.tgz", @@ -3226,6 +3236,14 @@ "integrity": "sha512-f2LZMYl1Fzu7YSBKg+RoROelpOaNrcGmE9AZubeDfrCEia483oW4MI4VyFd5VNHIgQ/7qm1I0wUHK1eJnn2y2w==", "dev": true }, + "node_modules/envalid": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/envalid/-/envalid-7.1.1.tgz", + "integrity": "sha512-2MIPjXAWcAVsO2jTVbgMFRc66lQCy2bQDP+zPT3NucrbWMD3pij5iGZMwQiv3zdveyVfl0qAGpI1JRQGfkmu1w==", + "engines": { + "node": ">=8.12" + } + }, "node_modules/error-ex": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz", @@ -15016,6 +15034,11 @@ "is-obj": "^1.0.0" } }, + "dotenv": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz", + "integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==" + }, "duplexer2": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/duplexer2/-/duplexer2-0.1.4.tgz", @@ -15175,6 +15198,11 @@ "integrity": "sha512-f2LZMYl1Fzu7YSBKg+RoROelpOaNrcGmE9AZubeDfrCEia483oW4MI4VyFd5VNHIgQ/7qm1I0wUHK1eJnn2y2w==", "dev": true }, + "envalid": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/envalid/-/envalid-7.1.1.tgz", + "integrity": "sha512-2MIPjXAWcAVsO2jTVbgMFRc66lQCy2bQDP+zPT3NucrbWMD3pij5iGZMwQiv3zdveyVfl0qAGpI1JRQGfkmu1w==" + }, "error-ex": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz", diff --git a/BackEnd/package.json b/BackEnd/package.json index 240fc5d..7047927 100644 --- a/BackEnd/package.json +++ b/BackEnd/package.json @@ -5,7 +5,7 @@ "main": "app.ts", "scripts": { "build": "tsc -p tsconfig-build.json", - "nodemon": "nodemon src/app.ts", + "nodemon": "NODE_PATH=. nodemon src/app.ts", "dev": "NODE_ENV=development DOTENV_CONFIG_PATH=.env-dev npm run nodemon", "lint": "eslint src/**/*.ts --quiet" }, @@ -20,7 +20,9 @@ "body-parser": "^1.19.0", "child_process": "^1.0.2", "cors": "2.8.5", + "dotenv": "^10.0.0", "ejs": "3.1.5", + "envalid": "^7.1.1", "errorhandler": "1.5.1", "express": "^4.17.1", "express-generator": "^4.16.1", diff --git a/BackEnd/src/env/db.ts b/BackEnd/src/env/db.ts new file mode 100644 index 0000000..efba7fa --- /dev/null +++ b/BackEnd/src/env/db.ts @@ -0,0 +1,2 @@ +export default { +} diff --git a/BackEnd/src/env/index.ts b/BackEnd/src/env/index.ts new file mode 100644 index 0000000..adaec72 --- /dev/null +++ b/BackEnd/src/env/index.ts @@ -0,0 +1,15 @@ +import { cleanEnv, str } from 'envalid' + +import serverEnv from './server' +import dbEnv from './db' + +const env = cleanEnv(process.env, { + NODE_ENV: str({ + choices: ['development', 'test', 'production'] + }), + + ...serverEnv, + ...dbEnv +}) + +export default env diff --git a/BackEnd/src/env/server.ts b/BackEnd/src/env/server.ts new file mode 100644 index 0000000..f12f76f --- /dev/null +++ b/BackEnd/src/env/server.ts @@ -0,0 +1,19 @@ +import { bool, host, port, str } from 'envalid' +import { ownDefault } from './utils' + +export default { + SERVER_HOSTNAME: host({ devDefault: 'localhost' }), + SERVER_PORT: port({ devDefault: 3000 }), + + SERVER_DEBUG: bool({ + desc: 'Allow to see more debug from like error handlers', + default: false, + devDefault: ownDefault({ dev: true, test: false, all: false }) + }), + SERVER_MORGAN_FORMAT: str({ + choices: ['combined', 'common', 'dev', 'short', 'tiny'], + default: 'combined', + devDefault: 'dev', + docs: 'https://www.npmjs.com/package/morgan#predefined-formats' + }) +} diff --git a/BackEnd/src/env/utils.ts b/BackEnd/src/env/utils.ts new file mode 100644 index 0000000..41a5bcd --- /dev/null +++ b/BackEnd/src/env/utils.ts @@ -0,0 +1,11 @@ +export function ownDefault ({ dev, test, all }: + {dev: T, test: T, all: T}) { + switch (process.env.NODE_ENV) { + case 'test': + return test + case 'development': + return dev + default: + return all + } +} diff --git a/BackEnd/tsconfig.json b/BackEnd/tsconfig.json index 6d1981b..e257ab2 100644 --- a/BackEnd/tsconfig.json +++ b/BackEnd/tsconfig.json @@ -3,6 +3,7 @@ /* Visit https://aka.ms/tsconfig.json to read more about this file */ /* Basic Options */ + "baseUrl": ".", "lib" : [ "es2020" ], "module" : "commonjs", "outDir" : "./dist",