Last active
April 17, 2020 08:28
-
-
Save koyanloshe/424013b152d444983de72c855ba51cdb to your computer and use it in GitHub Desktop.
process.env setter #Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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