A smarter `.env`
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ポール ウェッブ 36dcfb4f32 1.3.0 9 mesi fa
.editorconfig Array support! 1 anno fa
.eslintrc Updated deps 1 anno fa
.gitignore Array support! 1 anno fa
.npmrc Array support! 1 anno fa
README.md Updated READNE 1 anno fa
index.js forEach is better 9 mesi fa
package.json 1.3.0 9 mesi fa

README.md

vne

N

A clever .env

vne is not yet compatible with pnpm.

Installation

$ npm i vne -S

Usage

import env from "vne"; // const env = require("vne"); works too

console.log(env); // your .env file in object format

Example

Take this sample .env file:

tokens1="jumble-of-numbers-and-letters"
tokens2="letters-and-jumble-of-numbers"

portDatabase=55555
serviceApi="jumble-of-letters-and-numbers"

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:

{
  tokens: [
    "jumble-of-numbers-and-letters",
    "letters-and-jumble-of-numbers"
  ],
  portDatabase: "55555",
  serviceApi: "jumble-of-letters-and-numbers",
  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 . gets placed into a nested object for easy querying. That way, you will be able to do something like this (check the url parameter):

return new Promise((resolve, reject) => {
  request({
    body: {},
    json: true,
    method: "POST",
    url: process.env.NODE_ENV === "development" ? env.dev.api : env.prod.api // BOOM
  }).then(body => {
    if (!body) return reject(body);
    resolve(body);
  }).catch(error => {
    resolve(error);
  });
});

That one-liner checks to see if your app is running in a development environment or not (of course, this assumes that you are setting your environment when starting your app). Check out the scripts section of this package.json file for reference.

Anyhoo, if your app is running in development mode the parameter on the left side of the : will be called. If not (it’s running in production mode), the parameter on the right side is called.

Easy-peasy!