forked from bloomtime/require-env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (41 loc) · 1.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*jshint node:true globalstrict:true*/
"use strict";
var fs = require('fs'),
url = require('url');
module.exports = {
contains: function(name) {
return (process.env[name] !== undefined);
},
inherit: function(filename) {
try {
var env = fs.readFileSync(filename, "utf8");
env.split("\n").forEach(function(line) {
var parts = line.split("=");
// override environment vars with test.env
if (parts.length == 2 &&
!parts[0].match(/^#/) &&
process.env[parts[0]] === undefined) {
process.env[parts[0]] = parts[1];
}
});
} catch (e) {
console.warn(filename + " is absent; your environment may be incomplete.");
}
},
require: function(name) {
if (!this.contains(name)) {
throw new Error("process.env." + name + " is undefined");
}
return process.env[name];
},
requireUrl: function(name) {
var option = url.parse(this.require(name));
option.port = parseInt(option.port, 10);
if (option.auth) {
var auth = option.auth.split(":", 2);
option.user = auth[0];
option.pass = auth[1];
}
return option;
}
};