Skip to content

Commit

Permalink
Automatically load .env files (#57)
Browse files Browse the repository at this point in the history
* Automatically load `.env` files

* Improved detection

* Even better
  • Loading branch information
leo committed Dec 8, 2017
1 parent f64245b commit b8ff577
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
12 changes: 10 additions & 2 deletions bin/micro-dev.js
Expand Up @@ -6,6 +6,7 @@ const path = require('path')

// Packages
const mri = require('mri')
const dotEnv = require('dotenv')

// Utilities
const generateHelp = require('../lib/help')
Expand All @@ -17,7 +18,8 @@ const flags = mri(process.argv.slice(2), {
default: {
host: '::',
port: 3000,
limit: '1mb'
limit: '1mb',
dotenv: '.env'
},
alias: {
p: 'port',
Expand All @@ -29,7 +31,8 @@ const flags = mri(process.argv.slice(2), {
h: 'help',
v: 'version',
i: 'ignore',
l: 'limit'
l: 'limit',
d: 'dotenv'
},
unknown(flag) {
console.log(`The option "${flag}" is unknown. Use one of these:`)
Expand All @@ -52,6 +55,11 @@ if (flags.version) {
process.exit()
}

// Load the `.env` file
dotEnv.config({
path: path.resolve(process.cwd(), flags.dotenv)
})

if (flags.cold && (flags.watch || flags.poll)) {
logError(
'The --cold flag is not compatible with --watch or --poll!',
Expand Down
1 change: 1 addition & 0 deletions lib/help.js
Expand Up @@ -8,6 +8,7 @@ module.exports = () => {
${g('-p, --port <n>')} Port to listen on (defaults to 3000)
${g('-H, --host')} The host on which micro will run
${g('-d, --dotenv')} Custom path for a .env file (relative to cwd)
${g('-c, --cold')} Disable hot reloading
${g('-w, --watch <dir>')} A directory to watch in addition to [path]
${g('-L, --poll')} Poll for code changes rather than using events
Expand Down
35 changes: 20 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -44,6 +44,7 @@
"chokidar": "1.7.0",
"clipboardy": "1.2.2",
"debounce": "1.1.0",
"dotenv": "4.0.0",
"get-port": "3.2.0",
"ip": "1.1.5",
"jsome": "2.3.26",
Expand All @@ -57,7 +58,7 @@
"eslint-config-prettier": "2.9.0",
"husky": "0.14.3",
"lint-staged": "6.0.0",
"prettier": "1.8.2",
"prettier": "1.9.1",
"xo": "0.18.2"
}
}

0 comments on commit b8ff577

Please sign in to comment.