神刀安全网

Tiny wrapper around NPM's "dotenv" library

dotenv-helper

Providing a wrapper to add/remove lines of .env file used with dotenv NPM library

Note that "dotenv" is not a declared dependency of this library.

You must have it already installed and have .env file in your project root directory – those are the presumptions dotenv-helper takes.

Argumentation

I was in the middle of building library which was similar as functionality to "dotenv". Then I found it, but I still needed some helper functions to help me out accessing programmatically the ".env" file.

Example content of.envfile

firebasePassword=widerlich npmGlobal=jasmine,webpack,jsdoc2md 

Example usage

// npm i -S dotenv-helper  const dotenvHelper = require("dotenv-helper")  console.log(dotenvHelper.getEnv("firebasePassword")) //=> widerlich console.log(dotenvHelper.getEnv("noOneHere")) //=> null console.log(dotenvHelper.getEnv("npmGlobal")) //=> ["jasmine", ...] dotenvHelper.addEnv("npmGlobal", "babel-cli").then(console.log) //=> false  // previous line shows you, that you cannot edit an already existing key // what you can do is get the value, request delete and then make the edit // in this case you may prefer using getEnvSecure which read the file itself // not showing you this the edit part, just a common add new key/value pair  dotenvHelper.addEnv("newKey", "withNewvalue").then(console.log) //=> null(save is made)  console.log(dotenvHelper.getEnvSecure("newKey")) //=> "withNewValue"  // and I finish with the delete method dotenvHelper.delEnv("newKey").then(console.log) //=> true(delete is made) dotenvHelper.delEnv("ledZeppelin").then(console.log) //=> false(Robert Plant is not here)  

Functions

getEnv(key)⇒ string | Array

Takes a key and search for it in process.env

getEnvSecure(key)⇒ string | Array

Takes a key and search for .env file It is actually a helper function used for deleting env variable, as process.env is not reliable then

addEnv(key, value)⇒ string | Array

Takes a key and search for it in process.env

delEnv(key)⇒ Promise

Takes a key and try to remove it from in process.env

getEnv(key) ⇒ string | Array

Takes a key and search for it in process.env

Kind: global function

Returns: string | Array – returns null if no match; string if match and array if match with "," inside

Param Type
key string

getEnvSecure(key) ⇒ string | Array

Takes a key and search for .env file It is actually a helper function used for deleting env variable, as process.env is not reliable then

Kind: global function

Returns: string | Array – returns null if no match; string if match and array if match with "," inside

Param Type
key string

addEnv(key, value) ⇒ string | Array

Takes a key and search for it in process.env

Kind: global function

Returns: string | Array – returns null if no match; string if match and array if match with "," inside

Param Type
key string
value string

delEnv(key) ⇒ Promise

Takes a key and try to remove it from in process.env

Kind: global function

Returns: Promise – Promise => boolean depends whether delete operation was perfomed or not

Param Type
key string

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Tiny wrapper around NPM's "dotenv" library

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址