1.0.2 • Published 8 days ago

@zaeny/env v1.0.2

Weekly downloads
-
License
GPL-3.0
Repository
github
Last release
8 days ago

@zaeny/env

npm version npm downloads

Reading .env files

Getting Started

npm install @zaeny/env

Example create .env files key=value =" and ='' or =

touch .env
cat > .env <<'EOL'
FOO="Bar"
TEST=DATA
BAR='apapa'
EOL

usage:

var {env} = require('@zaeny/env');
env()

/*
  { FOO: 'Bar', TEST: 'DATA', BAR: 'apapa'}  
  added to process.env[]
*/

// select different files
env('./environment'); 

Implementation

this utility function free to copy

/* author: https://github.com/azizzaeny/env */

function env(file=".env", state=process.env){
  var {readFileSync, existsSync} = require('fs');
  if(!existsSync(file)) return console.log('no file', file);
  let contents = readFileSync(file, 'utf8');  
  let params= contents.split('\n').map((line)=>line.split('=')).reduce((k, [key, value])=>{
    if(value && (value.startsWith('"') || value.startsWith("'"))) (value = value.slice(1, -1));
    return (value) ? ((state[key]=value), Object.assign(k,{[key] : value.trim()})) : k ;
  },{});
  return params
}

module.exports = { env }

Development and building codes

npm i @zaeny/literate --save-dev

outputing files from readme.md

start repl node and evaluating this block code

var {tangle} = require('@zaeny/literate/tangle');
tangle( require('fs').readFileSync('./readme.md', 'utf8'), (file) => file.path );

node evaluate

node -e "var {tangle} = require('@zaeny/literate/tangle');console.log(tangle( require('fs').readFileSync('./readme.md', 'utf8'), (file) => file.path ));"

Changes

  • 1.0.0 initial release, add read double quote
  • 1.0.1 add information update repositry, issues, keywords and so on
  • 1.0.2 fix cannot get value from env that contain = like uri ?auth=admin&foo=bar
1.0.2

8 days ago

1.0.1

2 months ago

1.0.0

2 months ago