diff --git a/lib/index.js b/lib/index.js index 413cd6e..426d5a4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,7 +5,7 @@ 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) @@ -99,7 +99,7 @@ function ParseEnvVars (envString) { // Parse out all env vars from a given env file string and return an object function ParseEnvString (envFileString) { // First thing we do is stripe out all comments - envFileString = StripComments(envFileString) + envFileString = StripComments(envFileString.toString()) // Next we stripe out all the empty lines envFileString = StripEmptyLines(envFileString) @@ -135,7 +135,12 @@ function UseCmdLine (parsedArgs) { try { file = fs.readFileSync(envFilePath, { encoding: 'utf8' }) } catch (err) { - console.error(`Error! Could not find or read file at ${envFilePath}`) + console.error(`WARNING: + Could not find or read file at: + ${envFilePath} + Trying to fallback to read: + ${envFilePathDefault} + `) try { file = fs.readFileSync(envFilePathDefault) } catch (e) { diff --git a/package.json b/package.json index dfa1887..f015b35 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "author": "Todd Bluhm", "contributors": [ "Eric Lanehart ", - "Jon Scheiding " + "Jon Scheiding ", + "serapath (Alexander Praetorius) " ], "license": "MIT", "bugs": { diff --git a/test/test.js b/test/test.js index 7769d43..c68d2bf 100644 --- a/test/test.js +++ b/test/test.js @@ -250,13 +250,14 @@ describe('env-cmd', function () { assert(spawnStub.args[0][2].env.ANSWER === '42') }) - it('should throw error if file does not exist', function () { + it('should throw error if file and fallback does not exist', function () { this.readFileStub.restore() + try { EnvCmd(['./test/.non-existent-file', 'echo', '$BOB']) } catch (e) { - const resolvedPath = path.join(process.cwd(), 'test/.non-existent-file') - assert(e.message === `Error! Could not find or read file at ${resolvedPath}`) + const resolvedPath = path.join(process.cwd(), '.env') + assert(e.message ===`Error! Could not fallback to find or read file at ${resolvedPath}`) return } assert(!'No exception thrown')