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 22, 2021
1 parent 0d260e1 commit cc76d2f
Show file tree
Hide file tree
Showing 8 changed files with 82 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*
5 changes: 4 additions & 1 deletion BackEnd/nodemon.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"events": {
"start": "clear"
},
"execMap": {
"ts": "ts-node -r dotenv/config"
},
"ext": "ts",
"ignore": [".git", "node_modules", "dist"],
"ignore": [".git", "node_modules", "dist", "uploads"],
"watch": ["./src"]
}
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.

2 changes: 2 additions & 0 deletions BackEnd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit cc76d2f

Please sign in to comment.