Created
February 7, 2017 10:30
-
-
Save ooooak/347217a75fa41e4ad2bb21fbc10a5ebe to your computer and use it in GitHub Desktop.
read laravel .env in node
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
var fs = require('fs'); | |
var path = require('path'); | |
function readEnv() {} | |
readEnv.prototype.get = function(index, other) { | |
var root = __dirname.replace('test' + path.sep + 'Libs', ''); | |
var contents = fs.readFileSync(root + '.env', 'utf8').split("\n"); | |
var obj = {}; | |
for (var i = 0; i < contents.length; i++) { | |
var code = contents[i].replace('\r', ''); | |
if (code.length) { | |
var chunk = code.split('='); | |
obj[chunk[0]] = chunk[1]; | |
} | |
} | |
if (obj[index]) | |
return obj[index]; | |
else | |
return other; | |
} | |
module.exports = new readEnv(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment