From 1dff4fa2a382cc5ee49d6b8fe512178e3e1ecb60 Mon Sep 17 00:00:00 2001 From: Alexander Praetorius Date: Tue, 21 Feb 2017 09:51:36 +0100 Subject: [PATCH] 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` --- lib/index.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index e43a560..413cd6e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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()