Skip to content

Commit

Permalink
adding dotenv and envalid to manage environment variable properly
Browse files Browse the repository at this point in the history
  • Loading branch information
asere committed Sep 25, 2021
1 parent 6740f9d commit 5ceb6e3
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@
/BackEnd/configs/OS.js
/BackEnd/src/configs/OS.ts
/BackEnd/dist

/BackEnd/.env*
3 changes: 3 additions & 0 deletions BackEnd/nodemon.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"events": {
"start": "clear"
},
"execMap": {
"ts": "ts-node -r dotenv/config"
},
"ext": "ts",
"ignore": [".git", "node_modules", "dist", "uploads"],
"watch": ["./src"]
Expand Down
28 changes: 28 additions & 0 deletions BackEnd/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion BackEnd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions BackEnd/src/env/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default {
}
15 changes: 15 additions & 0 deletions BackEnd/src/env/index.ts
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions BackEnd/src/env/server.ts
Original file line number Diff line number Diff line change
@@ -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'
})
}
11 changes: 11 additions & 0 deletions BackEnd/src/env/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function ownDefault<T> ({ 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
}
}
1 change: 1 addition & 0 deletions BackEnd/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 5ceb6e3

Please sign in to comment.