Skip to content

Commit 71dabbf

Browse files
committed
Initial commit - does the very basics of loading .env files into ENV
0 parents  commit 71dabbf

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BASIC=basic

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# dotenv
2+
3+
Dotenv loads environment variables from .env into ENV (process.env).
4+
5+
## Installation
6+
7+
```javascript
8+
var dotenv = require('dotenv');
9+
dotenv.load();
10+
```
11+
12+
## Contributions
13+
14+
Contributions are very welcome. To run the tests, run:
15+
16+
```bash
17+
npm test
18+
```
19+

lib/main.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"use strict";
2+
3+
var fs = require('fs');
4+
5+
function dotenv() {
6+
dotenv = {
7+
environment: process.env.NODE_ENV || "development",
8+
9+
_loadEnv: function() {
10+
return dotenv._setKeysAndValuesFromEnvFilePath(".env");
11+
},
12+
_loadEnvDotEnvironment: function() {
13+
return dotenv._setKeysAndValuesFromEnvFilePath(".env."+dotenv.environment);
14+
},
15+
_setKeysAndValuesFromEnvFilePath: function(filepath) {
16+
try {
17+
var data = fs.readFileSync(filepath);
18+
var content = data.toString().trim();
19+
var lines = content.split('\n');
20+
21+
for (var i=0; i<lines.length; i++) {
22+
var key_value_array = lines[i].split("=");
23+
var key = key_value_array[0].trim();
24+
var value = key_value_array[1].trim();
25+
26+
process.env[key] = value;
27+
}
28+
} catch (e) {
29+
}
30+
31+
return true;
32+
},
33+
load: function() {
34+
dotenv._loadEnvDotEnvironment();
35+
dotenv._loadEnv();
36+
37+
return true;
38+
},
39+
};
40+
41+
return dotenv;
42+
}
43+
44+
module.exports = dotenv;

package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "dotenv",
3+
"version": "0.0.1",
4+
"description": "Loads environment variables from .env",
5+
"main": "lib/main.js",
6+
"directories": {
7+
"test": "test"
8+
},
9+
"scripts": {
10+
"test": "mocha test/*.js"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git://github.com/scottmotte/dotenv.git"
15+
},
16+
"keywords": [
17+
"dotenv",
18+
"env",
19+
".env",
20+
"environment",
21+
"variables",
22+
"config",
23+
"settings"
24+
],
25+
"readmeFilename": "README.md",
26+
"author": "scottmotte",
27+
"license": "BSD",
28+
"devDependencies": {
29+
"mocha": "",
30+
"should": ""
31+
}
32+
}

test/main.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var assert = require('assert'),
2+
should = require('should'),
3+
dotenv = require('../lib/main');
4+
5+
describe('dotenv', function() {
6+
describe('.load()', function() {
7+
it('sets the environment variables', function() {
8+
var result = dotenv();
9+
10+
result.load();
11+
12+
process.env.BASIC.should.eql("basic");
13+
});
14+
});
15+
});

0 commit comments

Comments
 (0)