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 Jul 28, 2021
1 parent 68cfd56 commit 9638a53
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 1 deletion.
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*
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;
2 changes: 2 additions & 0 deletions BackEnd/src/env/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default {
}
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 9638a53

Please sign in to comment.