Skip to content

Instantly share code, notes, and snippets.

@koyanloshe
Last active April 17, 2020 08:28
Show Gist options
  • Save koyanloshe/424013b152d444983de72c855ba51cdb to your computer and use it in GitHub Desktop.
Save koyanloshe/424013b152d444983de72c855ba51cdb to your computer and use it in GitHub Desktop.
process.env setter #Javascript
const fs = require('fs')
const rootDir = require('../utils/rootDir')
const env = {
// accepts a filename and if no filename, it defaults to .env
config: filename => {
filename === undefined ? (filename = '.env') : filename
fs.readFileSync(`${rootDir}/${filename}`, 'utf-8')
.split('\n')
.filter(each => {
return each.includes('=') && (!each.includes('# ') || !each === '')
})
.forEach(each => {
let envKeyValue = each.split('=')
process.env[envKeyValue[0]] = envKeyValue[1]
return
})
},
}
module.exports = env
const path = require('path')
// the second argument to be root directory actual path. This is a helper file
module.exports = path.resolve(__dirname, '../../')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment