Skip to content

Commit

Permalink
docs(readme): add usage to the README
Browse files Browse the repository at this point in the history
Add the detailed Usage section to the README.md file

Signed-off-by: Niloy Sikdar <[email protected]>
  • Loading branch information
niloysikdar committed Oct 7, 2022
1 parent 80c8082 commit fd9d737
Showing 1 changed file with 85 additions and 3 deletions.
88 changes: 85 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
### Supercharge your .env using `neoenv` with more advanced controls and powers ☄️🚀

Neoenv is a lightweight, zero-dependency, new-generation module for hassle-free, clean, typed and safe environment variables management for modern developers. 🔐🔥
It loads environment variables from a `.env` file into [`process.env`](https://nodejs.org/docs/latest/api/process.html#process_process_env) and automatically generates configuration object based on the keys. 🤯
It loads environment variables from a `.env` file into [`process.env`](https://nodejs.org/docs/latest/api/process.html#process_process_env) and automatically generates a configuration object based on the keys with a single command. 🤯
Now, you'll be able to access the environment variables based on your `.env` file in a type-safe way with some nice IntelliSense and autocomplete. 🤩
And guess what? It supports both JavaScript and TypeScript. 🎉

Find the npm package [here](https://www.npmjs.com/package/neoenv)

Expand All @@ -22,7 +24,7 @@ First, let us discuss the problem which we're currently having. 🤔

We use environment variables to store sensitive information like API keys, secrets, etc. which we don't want to commit to the repository. 🤫 That's why we use a `.env` file to store these variables and load them into `process.env`. Generally, we use the [`dotenv`](https://www.npmjs.com/package/dotenv) package to load the environment variables from the `.env` file. 📦 And we use the `process.env` object to access the variables.

> `📝` A big thanks to [motdotla](https://github.com/motdotla) for creating [dotenv](https://github.com/motdotla/dotenv)
> `📝` A big thanks to [motdotla](https://github.com/motdotla) for creating [dotenv](https://www.npmjs.com/package/dotenv)
**Now that's all good for a general use case. But there are some problems with this approach!**

Expand Down Expand Up @@ -99,6 +101,86 @@ npm install --save neoenv
yarn add neoenv
```

## Usage

If you've installed `neoenv` globally, you can use the `neoenv` command directly from the root of any project to generate the js/ts configuration file from the `.env` file. But you still need to install `neoenv` as a dependency in your project.

So, the recommended way is to install `neoenv` as a dependency and use the `neoenv` command from the `package.json` scripts.

After installing `neoenv` as a dependency, add a script to your `package.json` file to generate the config file using the `neoenv` command.

```json
{
"scripts": {
"gen:env": "neoenv --ts"
}
}
```

This will generate an `env.config.ts` file in the root of your project. You can also use the `--js` flag to generate an `env.config.js` file.

```json
{
"scripts": {
"gen:env": "neoenv --js"
}
}
```

Neoenv follows `Singleton Design Pattern`. Now, you can import the `envConfig` object from the generated file and use it to access the environment variables across your whole application in a type-safe way with some nice IntelliSense and autocomplete. 🤩

Your generated file (`env.config.ts`) will look something like this (depending on the keys you have in your `.env` file):

```ts
import { register } from 'neoenv';
register();

export const envConfig = {
MONGODB_URI: process.env.MONGODB_URI as string,
GITHUB_CLIENT_ID: process.env.GITHUB_CLIENT_ID as string,
CLOUDINARY_API_KEY: process.env.CLOUDINARY_API_KEY as string,
JWT_SECRET: process.env.JWT_SECRET as string,
};
```

Neoenv will handle everything from loading the environment variables from the `.env` file to generating the configuration object based on the keys available in the `.env` file automatically so that you don't have to do anything manually with copy-paste and you can safely focus on building your application. 🚀

_You can also add the command to your `pre` hook (`prestart` for `start`, `predev` for `dev`) to generate the config file automatically before running your application after doing some changes in the `.env` file._

```json
{
"scripts": {
"prestart": "neoenv --js",
"start": "node index.js"
}
}
```

or

```json
{
"scripts": {
"prestart": "yarn gen:env",
"start": "node index.js",
"gen:env": "neoenv --js"
}
}
```

or

```json
{
"scripts": {
"predev": "neoenv --js",
"dev": "nodemon index.js"
}
}
```

> **Note:** If you're using [nodemon](https://www.npmjs.com/package/nodemon) to run your application in development mode or using normal `node` (ex: `node index.js` or `node server.js`) with `start` command, you need to stop and restart the server after doing some changes in the `.env` file so that neoenv can load those new changes and generate the updated config file automatically.
## Bugs and Features

See the [issues](https://github.com/niloysikdar/neoenv/issues) for a list of proposed features (and known issues). Feel free to raise new issues.
Expand All @@ -109,7 +191,7 @@ Distributed under the Apache-2.0 License. See [LICENSE](https://github.com/niloy

## Author

### Niloy Sikdar
**Niloy Sikdar**

- [[email protected]](mailto:[email protected])
- [GitHub](https://github.com/niloysikdar)
Expand Down

0 comments on commit fd9d737

Please sign in to comment.