A smarter `.env`
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
ポール ウェッブ 36dcfb4f32 1.3.0 9 mēnešus atpakaļ
.editorconfig Array support! 1 gadu atpakaļ
.eslintrc Updated deps 1 gadu atpakaļ
.gitignore Array support! 1 gadu atpakaļ
.npmrc Array support! 1 gadu atpakaļ
README.md Updated READNE 1 gadu atpakaļ
index.js forEach is better 9 mēnešus atpakaļ
package.json 1.3.0 9 mēnešus atpakaļ

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!