Neverending Prototype Mode

npm

Need private packages and team management tools?Check out npm Orgs. »

dotenv-manipulator

1.3.4 • Public • Published

dotenv-manipulator

NPM

Build Status Build Status npm

Quality Gate Status Maintainability

dotenv-manipulator allows you to add, remove or update both environment variables and .env files on runtime using dotenv and dotenv-expand as its core dependencies.

Installation

npm i -S dotenv-manipulator

Behavior

dotenv-manipator will always :

  • Converts keys to uppercase (input and ouput).
  • Creates a .env file (when requiring) at the root of your project if there's none.

Example

const dotenvM = require('dotenv-manipulator')
 
dotenvM.add('public_ip', '255.255.0.1', (exist) => {
  if(exist)
    // do something
})
dotenvM.del('temporary_data', (doesntExist) => {
  if(doesntExist)
    // do something
})
 
dotenvM.update('node_env', 'production', (isNew) => {
  if(isNew)
    // do something
})
const dotenvM = require('dotenv-manipulator')
 
let obj = {
  'first': 'one',
  'second': 'two',
  'third': 'three'
}
 
dotenvM.bulkAdd(obj, (exist) => {
  if(exist)
     // loop through keys that couldn't be added because they were already set
})
const dotenvM = require('dotenv-manipulator')
 
let obj = {
  'first': 1',
  'second': 2,
  'fourth': 4
}
 
dotenvM.bulkUpdate(obj, (isNew) => {
  if(isNew)
     // loop through keys that have been added because they weren't set.
})
const dotenvM = require('dotenv-manipulator')
 
let arr = ['one', 'two']
 
dotenvM.bulkDel(arr, (doesntExist) => {
  if(doesntExist)
     // loop through keys that couldn't be deleted because they weren't set
})

API

add(key, value, callback)

index.js, line 200
  • Adds a key if it's not defined in the env.
  • If it's already defined
    • callback returns an array containing the key
  • Else
    • callback variable is undefined
Name Type Description
key String key name
value String value
callback Callback

Example :

const dotenvM = require('dotenv-manipulator')
 
dotenvM.add('public_ip', '255.255.0.1', (exist) => {
    if(exist)
        // ['PUBLIC_IP']
})

del(key, callback)

index.js, line 220
  • Deletes a key if it's defined in the env.
  • If the key is defined
    • callback returns an array containing the key
  • Else
    • callback's returned variable is undefined
Name Type Description
key String key name
callback Callback

Example :

const dotenvM = require('dotenv-manipulator')
 
dotenvM.del('fruits', (notDefined) => {
    if(notDefined)
      // ['FRUITS']
})

update(key, value, callback)

index.js, line 240
  • Updates a key if it's defined in the env.
  • If it's not defined
    • Acts as add()
    • callback returns an array containing every added keys
  • else
    • callback's returned variable is undefined
Name Type Description
key String key name
value String new value
callback Callback

Example :

const dotenvM = require('dotenv-manipulator')
 
dotenvM.update('node_env', 'production', (added) => {
    if(added)
      // ['NODE_ENV']
})

bulkAdd(obj, callback)

index.js, line 95
  • Adds keys that aren't defined in the env.
  • If one or more keys are already defined
    • They are skipped
    • callback returns an array containing the key(s)
  • Else
    • callback's returned variable is undefined
Name Type Description
obj Object.<string, string> Object containing keys and values
callback Callback

Example :

const dotenvM = require('dotenv-manipulator')
 
let obj = {
  'first': 'one',
  'second': 'two',
  'third': 'three' 
}
 
dotenvM.bulkAdd(obj, (exist) => {
    if(exist)
        // ['FIRST', 'SECOND']
})

bulkDel(arr, callback)

index.js, line 131
  • Deletes keys that are defined in the env.
  • If one or more keys aren't defined
    • The key are skipped
    • callback returns an array containing the skipped key(s)
  • Else
    • callback's returned variable is undefined
Name Type Description
arr Array. Array containing keys to delete
callback Callback

Example :

const dotenvM = require('dotenv-manipulator')
 
let arr = ['first', 'second', 'third']
 
dotenvM.bulkDel(arr, (notDefined) => {
    if(notDefined)
      // ['SECOND', 'THIRD']
})

bulkUpdate(obj, callback)

index.js, line 161
  • Updates keys that are defined in the env.
  • If one or more key aren't defined
    • Acts as add()
    • callback returns an array containing the added key(s)
  • Else
    • callback's returned variable is undefined
Name Type Description
obj Object.<string, string> Object containing keys and values
callback Callback

Example :

const dotenvM = require('dotenv-manipulator')
 
let obj = {
  'first': 'one',
  'second': 'two',
  'third': 'three' 
}
 
dotenvM.bulkUpdate(obj, (added) => {
    if(added)
        // ['FIRST', 'THIRD']
})

install

npm i dotenv-manipulator

Downloadsweekly downloads

21

version

1.3.4

license

GPL-3.0

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability