Skip to content

Commit

Permalink
refactor(env): ♻️ remove unused environment files and add dotenv conf…
Browse files Browse the repository at this point in the history
…iguration

- Add a new config/index.ts file for loading the environment variables from a .env file.
- Add validateEnvironment variables function that validates the required environment variables.
- Use validateEnvironment variables to ensure the required env variables are available at runtime.
- Update index.ts to utilize the validate environment variables function.
- Replace usage of consola library with console.log to output 1 + 2.
  • Loading branch information
navin-moorthy committed May 4, 2023
1 parent 4c9d392 commit 2d2f2f0
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 97 deletions.
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
NODE_ENV="production"
# API_URL=""
# Environment variables
NODE_ENV=production
7 changes: 3 additions & 4 deletions .github/DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ pairs in the following format:

- **`NODE_ENV`** (required): The environment in which the project is running.
This can be either `development` or `production`.
- **`API_URL`** (optional): The URL of the API for the project.

> Adding a new environmental variable requires a zod schema update in the
> `src/env` folder and a new entry in the `schema.ts` file in the
> `environmentSchema` variable.
> Adding a new environmental variable requires aa addition in the
> `src/config/validateEnvironment.ts` file and a new entry in the global type
> `interface NodeJS.ProcessEnv`.
## Installing the dependencies

Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ other amazing technologies mentioned below to build a modern web application.

- [typeScript](https://www.typescriptlang.org/)
- [ts-node](https:/TypeStrong/ts-node)
- [zod](https:/colinhacks/zod)
- [dotenv-cli](https:/entropitor/dotenv-cli)
- [consola](https:/unjs/consola)
- [pnpm](https://pnpm.io/)
- [eslint](https://eslint.org/)
- [prettier](https://prettier.io/)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
]
},
"dependencies": {
"consola": "^3.0.1",
"zod": "^3.21.4"
"dotenv": "^16.0.3",
"envalid": "^7.3.1"
},
"devDependencies": {
"@commitlint/cli": "17.5.1",
Expand Down
18 changes: 17 additions & 1 deletion pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions project-words.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
amannn
contributorsrc
envalid
execa
fbca
gacp
Expand Down
18 changes: 18 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { config } from "dotenv";

/**
* Loads environment variables from a .env file.
*
* @function
* @returns {void}
*/
export const loadEnvironmentVariables = (): void => {
config({ path: `.env.${process.env.NODE_ENV ?? "development"}` });
};

loadEnvironmentVariables();

/**
* The environment the application is running in.
*/
export const NODE_ENV = process.env.NODE_ENV;
24 changes: 24 additions & 0 deletions src/config/validateEnvironment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { cleanEnv, str } from "envalid";

/**
* Validates the environment variables needed for the application to run.
*
* @function
* @returns {void}
*/
export const validateEnvironmentVariables = (): void => {
cleanEnv(process.env, {
NODE_ENV: str(),
});
};

declare global {
// By default, we do not want any namespace in Start UI [web] as it is more
// error prone and not useful in front end applications.
namespace NodeJS {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions, unicorn/prevent-abbreviations
interface ProcessEnv {
NODE_ENV: "development" | "production" | "test";
}
}
}
32 changes: 0 additions & 32 deletions src/env/environment.ts

This file was deleted.

24 changes: 0 additions & 24 deletions src/env/schema.ts

This file was deleted.

27 changes: 0 additions & 27 deletions src/env/utils.ts

This file was deleted.

7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { consola } from "consola";
import { validateEnvironmentVariables } from "./config/validateEnvironment";

await import("./env/environment");
validateEnvironmentVariables();

consola.log("add 1 + 2: ", 1 + 2);
// eslint-disable-next-line no-console
console.log("add 1 + 2: ", 1 + 2);

0 comments on commit 2d2f2f0

Please sign in to comment.