Browse files

[bump] Add .env file loading.

  • Loading branch information...
1 parent b65bff5 commit be94c3d5f058e89fa1a36dc5965ab9666a1c79e0 @brianloveswords committed Oct 19, 2012
Showing with 72 additions and 6 deletions.
  1. +30 −0 README.md
  2. +31 −5 index.js
  3. +1 −1 package.json
  4. +2 −0 test/.env
  5. +8 −0 test/habitat.test.js
View
30 README.md
@@ -105,6 +105,36 @@ var env = new habitat('app');
console.log(env.get('someLongKey')) // 'great'
```
+## habitat.get(key)
+
+You can also use `get` directly from the habitat objects to get unprefixed things from the environment.
+
+```js
+var path = habitat.get('path');
+var nodeEnv = habitat.get('nodeEnv');
+```
+
+## habitat.load([*pathToEnvFile*])
+Try to load a set of environment variables from a file. Will **not**
+overwrite existing environment variables if there is a conflict.
+
+```bash
+# /some/directory/.env
+# The leading `export` is optional.
+# Useful if you want to be able to also `source /some/directory/.env`
+
+export PARAMETER_ONE=one
+export PARAMETER_TWO=two
+```
+
+```
+habitat.load('/some/directory/.env'); // returns true on success
+console.dir(habitat.get('parameter')); // { one: 'one', two: 'two' }
+```
+
+`pathToEnvFile` defaults to `'.env'`, which will just look for a .env
+file in the current working directory.
+
## habitat#set(key, value)
Sets an environment variable, with prefix if passed.
View
36 index.js
@@ -1,3 +1,5 @@
+var fs = require('fs');
+
function habitat(prefix, defaults) {
if (!(this instanceof habitat))
return new habitat(prefix);
@@ -147,7 +149,6 @@ habitat.prototype.all = function all() {
}, {});
};
-
habitat.prototype.rawKeys = function rawKeys() {
var prefix = this.prefix;
var keys = Object.keys(process.env);
@@ -172,16 +173,41 @@ habitat.prototype.getAsObject = function getAsObject(keyPrefix) {
return env.all();
};
-
-
/**
* Get a key from the environment without a prefix.
*
* @see habitat#get
*/
-habitat.get = function get(value) {
- return (new habitat().get(value))
+habitat.get = function get() {
+ var env = new habitat();
+ return env.get.apply(env, arguments)
+};
+
+/**
+ * Load some things from an env file into the environment.
+ *
+ * @param {String} path The path to the environment file.
+ * @return {Boolean} true if able to load, false otherwise.
+ */
+
+habitat.load = function load(path) {
+ path = path || '.env';
+ if (!fs.existsSync(path))
+ return false;
+ var exports = fs.readFileSync(path).toString().split('\n');
+ exports.map(function (param) {
+ var match = param.replace(/^export /i, '').match(/(.+?)=(.*)/);
+ var key = match[1];
+ var value = match[2];
+ if ((match = value.match(/^(?:'|")(.*)(?:'|")$/)))
+ value = match[1];
+ return { key: key, value: value };
+ }).forEach(function (param) {
+ if (process.env[param.key]) return;
+ process.env[param.key] = param.value;
+ });
+ return true;
};
/**
View
2 package.json
@@ -1,6 +1,6 @@
{
"name": "habitat",
- "version": "0.3.0",
+ "version": "0.3.1",
"description": "Small library for managing environment variables",
"main": "index.js",
"directories": {
View
2 test/.env
@@ -0,0 +1,2 @@
+export PARAMETER_ONE='one=1'
+export PARAMETER_TWO='two'
View
8 test/habitat.test.js
@@ -169,3 +169,11 @@ test('habitat#get: should try to expand camelcase', function (t) {
t.same(port, 3000);
t.end();
});
+
+test('habitat.load: load some shit in from a file', function (t) {
+ var path = require('path').join(__dirname, '.env');
+ habitat.load(path);
+ t.same(process.env['PARAMETER_ONE'], 'one=1');
+ t.same(process.env['PARAMETER_TWO'], 'two');
+ t.end();
+});

0 comments on commit be94c3d

Please sign in to comment.