vne
Version:
A smarter .env
128 lines (83 loc) • 2.71 kB
Markdown
> A clever .env (made clever-er with [env-smart](https://github.com/jessety/env-smart))
```sh
npm i vne
```
```ts
import { vne } from "vne"; // use the named export...
import env from "vne"; // or, the default and name it whatever you want
// Your .env file, made more useful.
console.log(vne()); // or, env();
```
Type: `string` (optional)
`vne` has two assumptions about your project: 1) your environment file has a filename of `.env` and 2) this file in the root of your project. If one of those assumptions are incorrect, you can specify an **absolute** `path` to your environment file and `vne` will use that.
Take this sample `.env` file:
```ini
tokens1="jumb1e-0f-num8er5-and-l3tt3r5"
tokens2="l3tt3r5-and-jumb1e-0f-num8er5"
portDatabase=55555
serviceApi="jumb1e-0f-l3tt3r5-and-num8er5"
dev-api="http://localhost:3000"
prod-api="https://api.domain.tld"
dev-app="http://localhost:3001"
prod-app="https://app.domain.tld"
dev-marketing="http://localhost:3002"
prod-marketing="https://domain.tld"
```
`vne` takes the `.env` file and produces an object like this:
```ts
{
tokens: [
"jumb1e-0f-num8er5-and-l3tt3r5",
"l3tt3r5-and-jumb1e-0f-num8er5"
],
portDatabase: "55555",
serviceApi: "jumb1e-0f-l3tt3r5-and-num8er5",
dev: {
api: "http://localhost:3000",
app: "http://localhost:3001",
marketing: "http://localhost:3002"
},
prod: {
api: "https://api.domain.tld",
app: "https://app.domain.tld",
marketing: "https://domain.tld"
}
}
```
Variables with names differentiated by numbers are intelligently placed into an array for easy iteration. A use case would be a handful of tokens you want to have admin access to your API.
Other variables in your `.env` file with a `-`, `.`, or `·` get placed into a nested object for easy querying. That way, you will be able to do something like this:
```ts
const { dev, prod } = vne();
try {
await request({
url: process.env.NODE_ENV === "development" ?
dev.api :
prod.api
});
} catch(_) {
// handle error
}
```
The above example checks to see if your app is running in a `development` environment (of course, this assumes that you are setting your environment when starting your app).
Easy-peasy!
```sh
$ npm test
$ npm run test:dependencies
$ npm run test:lint
$ npm run test:lint-assert
```
- [`@webb/order-object`](https://www.npmjs.com/package/@webb/order-object)
- [`@webb/stringify-object`](https://www.npmjs.com/package/@webb/stringify-object)