Skip to content

Commit 35ae48c

Browse files
committed
Merge branch 'release/2.0.0'
2 parents b013a05 + b3aafc8 commit 35ae48c

File tree

6 files changed

+158
-14
lines changed

6 files changed

+158
-14
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
language: node_js
22
node_js:
3+
- "7"
34
- "6.0"
45
- "5.0"
56
- "4.4"

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,42 @@ Or to specify load options:
8989
node -r dotenv-extended/config your_script.js dotenv_config_path=./env/.env dotenv_config_defaults=./env/.env.defaults
9090
```
9191

92+
### Load Environment Variables and pass to non-NodeJS script
93+
94+
New in 2.0.0, is a feature inspired by [cross-env](https://www.npmjs.com/package/cross-env) to allow you to load environment variables from your `.env` files and then pass them into a non-NodeJS script such as a shell script. This can simplify the process of maintaining variables used in both your Node app and other scripts. To use this command line executable, you will either need to install globally with the `-g` flag, or install `dotenv-extended` in your project and reference it from your npm scripts.
95+
96+
Install Globally:
97+
98+
```
99+
npm install -g dotenv-extended
100+
```
101+
102+
Now call your shell scripts through `dotenv-extended` (this uses the defaults):
103+
104+
```
105+
dotenv-extended myshellscript.sh --whatever-flags-my-script-takes
106+
```
107+
108+
Configure `dotenv-extended` by passing any of the dotenv-extended options before your command. Preceed each option with two dashes `--`:
109+
110+
```
111+
dotenv-extended --path=/path/to/.env --defaults=/path/to/.env.defaults --errorOnMissing=true myshellscript.sh --whatever-flags-my-script-takes
112+
```
113+
114+
The following are the flags you can pass to the `dotenv-extended` cli with their default values. these options detailed later in this document:
115+
116+
```bash
117+
--encoding=utf8
118+
--silent=true
119+
--path=.env
120+
--defaults=.env.defaults
121+
--schema=.env.schema
122+
--errorOnMissing=false # or --error-on-missing=false
123+
--errorOnExtra=false # or --error-on-extra=false
124+
--assignToProcessEnv=true # or --assign-to-process-env=true
125+
--overrideProcessEnv=false # or --override-process-env=true
126+
```
127+
92128
## Options
93129

94130
Defaults are shown below:

package.json

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"name": "dotenv-extended",
3-
"version": "1.0.4",
3+
"version": "2.0.0",
44
"description": "A module for loading .env files and optionally loading defaults and a schema for validating all values are present.",
55
"repository": "[email protected]:keithmorris/node-dotenv-extended.git",
66
"main": "lib/index.js",
7+
"bin": "lib/bin/index.js",
78
"scripts": {
8-
"test": "./node_modules/.bin/gulp unittest",
9-
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
9+
"test": "gulp unittest",
10+
"coveralls": "cat ./coverage/lcov.info | coveralls",
1011
"lint": "gulp lint",
11-
"prepublish": "./node_modules/.bin/gulp build"
12+
"prepublish": "gulp build"
1213
},
1314
"keywords": [
1415
"dotenv",
@@ -17,24 +18,26 @@
1718
"author": "Keith Morris <[email protected]> (http://standupbass.wordpress.com/)",
1819
"license": "MIT",
1920
"devDependencies": {
20-
"babel-eslint": "^6.0.4",
21+
"babel-eslint": "^7.2.3",
2122
"babel-plugin-transform-object-rest-spread": "^6.8.0",
2223
"babel-preset-es2015": "^6.5.0",
2324
"chai": "^3.5.0",
2425
"coveralls": "^2.11.6",
2526
"del": "^2.2.0",
2627
"eslint": "^3.2.2",
27-
"eslint-config-standard": "^5.1.0",
28-
"eslint-plugin-promise": "^2.0.0",
29-
"eslint-plugin-standard": "^2.0.0",
28+
"eslint-config-standard": "^10.2.1",
29+
"eslint-plugin-import": "^2.2.0",
30+
"eslint-plugin-node": "^4.2.2",
31+
"eslint-plugin-promise": "^3.5.0",
32+
"eslint-plugin-standard": "^3.0.1",
3033
"gulp": "^3.9.1",
3134
"gulp-babel": "^6.1.2",
3235
"gulp-eslint": "^3.0.1",
33-
"gulp-istanbul": "^1.0.0",
36+
"gulp-istanbul": "^1.1.1",
3437
"gulp-mocha": "^3.0.0",
35-
"mockery": "^1.4.0",
38+
"mockery": "^2.0.0",
3639
"run-sequence": "^1.1.5",
37-
"sinon": "^1.17.3",
40+
"sinon": "^2.1.0",
3841
"sinon-chai": "^2.8.0"
3942
},
4043
"babel": {
@@ -46,7 +49,11 @@
4649
]
4750
},
4851
"dependencies": {
49-
"dotenv": "^2.0.0",
52+
"auto-parse": "^1.3.0",
53+
"camelcase": "^4.1.0",
54+
"cross-spawn": "^5.1.0",
55+
"dotenv": "^4.0.0",
56+
"is-windows": "^1.0.0",
5057
"lodash": "^4.3.0"
5158
}
5259
}

src/bin/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Created by Keith Morris on 4/26/17.
4+
*
5+
* This bin script is inspired by and borrows heavily from CrossEnv
6+
* https://github.com/kentcdodds/cross-env
7+
*/
8+
9+
import {config} from '..';
10+
import {parseCommand} from './parse-command';
11+
import {spawn} from 'cross-spawn';
12+
13+
function loadAndExecute(args) {
14+
const [dotEnvConfig, command, commandArgs] = parseCommand(args);
15+
if (command) {
16+
const proc = spawn(command, commandArgs, {
17+
stdio: 'inherit',
18+
shell: true,
19+
env: config(dotEnvConfig),
20+
});
21+
22+
process.on('SIGTERM', () => proc.kill('SIGTERM'));
23+
process.on('SIGINT', () => proc.kill('SIGINT'));
24+
process.on('SIGBREAK', () => proc.kill('SIGBREAK'));
25+
process.on('SIGHUP', () => proc.kill('SIGHUP'));
26+
proc.on('exit', process.exit);
27+
28+
return proc;
29+
}
30+
}
31+
32+
loadAndExecute(process.argv.slice(2));

src/bin/parse-command.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Created by Keith Morris on 4/26/17.
3+
*/
4+
import autoParse from 'auto-parse';
5+
import camelcase from 'camelcase';
6+
7+
const dotEnvFlagRegex = /^--(.+)=(.+)/;
8+
9+
/**
10+
* First parses config variables for dotenv-extended then selects the next item as the command and everything after that
11+
* are considered arguments for the command
12+
*
13+
* @param args
14+
* @returns {[Object,String,Array]}
15+
*/
16+
export const parseCommand = (args) => {
17+
const config = {};
18+
let command = null;
19+
let commandArgs = [];
20+
for (let i = 0; i < args.length; i++) {
21+
const match = dotEnvFlagRegex.exec(args[i]);
22+
if (match) {
23+
config[camelcase(match[1])] = autoParse(match[2]);
24+
} else {
25+
// No more env setters, the rest of the line must be the command and args
26+
command = args[i];
27+
commandArgs = args.slice(i + 1);
28+
break;
29+
}
30+
}
31+
return [config, command, commandArgs];
32+
};

test/test.spec.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('dotenv-extended tests', function () {
4141

4242
it('Should load .env file into process.env and override process.env properties with overrideProcessEnv set to true', function () {
4343
process.env.TEST_ONE = 'original';
44-
dotenvex.load({ overrideProcessEnv: true });
44+
dotenvex.load({overrideProcessEnv: true});
4545
expect(process.env.TEST_ONE).to.equal('overridden');
4646
});
4747

@@ -103,7 +103,43 @@ describe('dotenv-extended tests', function () {
103103
});
104104

105105
it('Should log an error when silent is set to false and .env.defaults is missing', function () {
106-
dotenvex.load({ silent: false });
106+
dotenvex.load({silent: false});
107107
expect(console.error).to.have.been.calledOnce;
108108
});
109109
});
110+
111+
describe('CLI supporting libraries tests', function () {
112+
var parseCommand = require('../lib/bin/parse-command').parseCommand;
113+
var cliArgs = [
114+
'--encoding=utf8',
115+
'--silent=true',
116+
'--path=test/.env.override',
117+
'--defaults=test/.env.defaults.example',
118+
'--schema=test/.env.schema.example',
119+
'--error-on-missing=false',
120+
'--error-on-extra=false',
121+
'--assignToProcessEnv=true',
122+
'--overrideProcessEnv=false',
123+
'testing.sh',
124+
'--jump',
125+
'--dive=true',
126+
'I was here'
127+
];
128+
129+
it('Should parse command line arguments correctly', function () {
130+
var parsed = parseCommand(cliArgs);
131+
expect(parsed[0]).to.eql({
132+
encoding: 'utf8',
133+
silent: true,
134+
path: 'test/.env.override',
135+
defaults: 'test/.env.defaults.example',
136+
schema: 'test/.env.schema.example',
137+
errorOnMissing: false,
138+
errorOnExtra: false,
139+
assignToProcessEnv: true,
140+
overrideProcessEnv: false
141+
});
142+
expect(parsed[1]).to.eql('testing.sh');
143+
expect(parsed[2]).to.eql(['--jump', '--dive=true', 'I was here'])
144+
});
145+
});

0 commit comments

Comments
 (0)