ADD default .env file as a fallback

If a repository contains a `.env` example file or - in case of a private repository - a `.env` file with production values,
a developer can add a local (e.g. `.env.local` file to `.gitignore`) and feed it to `env-cmd`.
So in development a custom local configuration can be used, but in production, `env-cmd` falls back to `.env`
This commit is contained in:
Alexander Praetorius 2017-02-21 09:51:36 +01:00 committed by GitHub
parent 104475c11d
commit 1dff4fa2a3

View File

@ -4,7 +4,8 @@ const spawn = require('cross-spawn').spawn
const path = require('path')
const fs = require('fs')
const rcFileLocation = path.join(process.cwd(), '.env-cmdrc')
const envFilePathDefault = path.join(process.cwd(), '.env')
function EnvCmd (args) {
// First Parse the args from the command line
const parsedArgs = ParseArgs(args)
@ -133,8 +134,13 @@ function UseCmdLine (parsedArgs) {
let file
try {
file = fs.readFileSync(envFilePath, { encoding: 'utf8' })
} catch (e) {
throw new Error(`Error! Could not find or read file at ${envFilePath}`)
} catch (err) {
console.error(`Error! Could not find or read file at ${envFilePath}`)
try {
file = fs.readFileSync(envFilePathDefault)
} catch (e) {
throw new Error(`Error! Could not fallback to find or read file at ${envFilePathDefault}`)
}
}
const ext = path.extname(envFilePath).toLowerCase()