Allow for using absolute pathing, ~ pathing, and relative pathing (#30)

* Allow for using absolute pathing, ~ pathing, and relative pathing

* Add test cases for `./` and `../` pathing

* Update Readme file with new path rules
This commit is contained in:
Todd Bluhm 2017-09-25 13:39:06 -05:00 committed by GitHub
parent 26d8eaad9e
commit 28bd3845e7
3 changed files with 77 additions and 2 deletions

View File

@ -122,6 +122,21 @@ These are the currently accepted environment file formats. If any other formats
- JavaScript file exporting an object - JavaScript file exporting an object
- `.env-cmdrc` file (as valid json) in execution directory - `.env-cmdrc` file (as valid json) in execution directory
## Path Rules
This lib attempts to follow standard `bash` path rules. The rules are as followed:
Home Directory = `/Users/test`
Working Directory = `/Users/test/Development/app`
| Type | Input Path | Expanded Path |
| -- | -- | ------------- |
| Absolute | `/some/absolute/path.env` | `/some/absolute/path.env` |
| Home Directory with `~` | `~/starts/on/homedir/path.env` | `/Users/test/starts/on/homedir/path.env` |
| Relative | `./some/relative/path.env` or `some/relative/path.env` | `/Users/test/Development/app/some/relative/path.env` |
| Relative with parent dir | `../some/relative/path.env` | `/Users/test/Development/some/relative/path.env` |
## Why ## Why
Because sometimes its just too cumbersome passing lots of environment variables to scripts. Its usually just easier to have a file with all the vars in them, especially for development and testing. Because sometimes its just too cumbersome passing lots of environment variables to scripts. Its usually just easier to have a file with all the vars in them, especially for development and testing.

View File

@ -3,6 +3,7 @@
const spawn = require('cross-spawn').spawn const spawn = require('cross-spawn').spawn
const path = require('path') const path = require('path')
const fs = require('fs') const fs = require('fs')
const os = require('os')
const rcFileLocation = path.join(process.cwd(), '.env-cmdrc') const rcFileLocation = path.join(process.cwd(), '.env-cmdrc')
const envFilePathDefault = path.join(process.cwd(), '.env') const envFilePathDefault = path.join(process.cwd(), '.env')
@ -209,7 +210,7 @@ function UseRCFile (options) {
* @return {Object} Key/Value pairing of env vars found in .env file * @return {Object} Key/Value pairing of env vars found in .env file
*/ */
function UseCmdLine (options) { function UseCmdLine (options) {
const envFilePath = path.join(process.cwd(), options.envFile) const envFilePath = ResolveEnvFilePath(options.envFile)
// Attempt to open the provided file // Attempt to open the provided file
let file let file
@ -272,6 +273,20 @@ function HandleUncaughtExceptions (e) {
process.exit(1) process.exit(1)
} }
/**
* A simple function for resolving the path the user entered
* @param {String} userPath A path
* @return {String} The fully qualified absolute path
*/
function ResolveEnvFilePath (userPath) {
// Make sure a home directory exist
const home = os.homedir()
if (home) {
userPath = userPath.replace(/^~($|\/|\\)/, `${home}$1`)
}
return path.resolve(process.cwd(), userPath)
}
process.on('uncaughtException', HandleUncaughtExceptions) process.on('uncaughtException', HandleUncaughtExceptions)
module.exports = { module.exports = {
@ -285,5 +300,6 @@ module.exports = {
ParseEnvVars, ParseEnvVars,
ParseRCFile, ParseRCFile,
UseRCFile, UseRCFile,
UseCmdLine UseCmdLine,
ResolveEnvFilePath
} }

View File

@ -11,6 +11,7 @@ const path = require('path')
const proxyquire = require('proxyquire') const proxyquire = require('proxyquire')
const sinon = require('sinon') const sinon = require('sinon')
const fs = require('fs') const fs = require('fs')
let userHomeDir = '/Users/hitchhikers-guide-to-the-galaxy'
const spawnStub = sinon.spy(() => ({ const spawnStub = sinon.spy(() => ({
on: sinon.stub(), on: sinon.stub(),
@ -31,6 +32,9 @@ const lib = proxyquire('../lib', {
BOB: 'COOL', BOB: 'COOL',
NODE_ENV: 'dev', NODE_ENV: 'dev',
ANSWER: '42' ANSWER: '42'
},
'os': {
homedir: () => userHomeDir
} }
}) })
const EnvCmd = lib.EnvCmd const EnvCmd = lib.EnvCmd
@ -41,6 +45,7 @@ const HandleUncaughtExceptions = lib.HandleUncaughtExceptions
const StripComments = lib.StripComments const StripComments = lib.StripComments
const StripEmptyLines = lib.StripEmptyLines const StripEmptyLines = lib.StripEmptyLines
const ParseEnvVars = lib.ParseEnvVars const ParseEnvVars = lib.ParseEnvVars
const ResolveEnvFilePath = lib.ResolveEnvFilePath
describe('env-cmd', function () { describe('env-cmd', function () {
describe('ParseArgs', function () { describe('ParseArgs', function () {
@ -357,4 +362,43 @@ describe('env-cmd', function () {
this.logStub.restore() // restore here so test success logs get printed this.logStub.restore() // restore here so test success logs get printed
}) })
}) })
describe('ResolveEnvFilePath', function () {
beforeEach(function () {
this.cwdStub = sinon.stub(process, 'cwd')
this.cwdStub.returns('/Users/hitchhikers-guide-to-the-galaxy/Thanks')
})
afterEach(function () {
this.cwdStub.restore()
})
it('should add "fish.env" to the end of the current directory', function () {
const abPath = ResolveEnvFilePath('fish.env')
assert(abPath === '/Users/hitchhikers-guide-to-the-galaxy/Thanks/fish.env')
})
it('should add "./fish.env" to the end of the current directory', function () {
const abPath = ResolveEnvFilePath('./fish.env')
assert(abPath === '/Users/hitchhikers-guide-to-the-galaxy/Thanks/fish.env')
})
it('should add "../fish.env" to the end of the current directory', function () {
const abPath = ResolveEnvFilePath('../fish.env')
assert(abPath === '/Users/hitchhikers-guide-to-the-galaxy/fish.env')
})
it('should add "for-all-the/fish.env" to the end of the current directory', function () {
const abPath = ResolveEnvFilePath('for-all-the/fish.env')
assert(abPath === '/Users/hitchhikers-guide-to-the-galaxy/Thanks/for-all-the/fish.env')
})
it('should set the absolute path to "/thanks/for-all-the/fish.env"', function () {
const abPath = ResolveEnvFilePath('/thanks/for-all-the/fish.env')
assert(abPath === '/thanks/for-all-the/fish.env')
})
it('should use "~" to add "fish.env" to the end of user directory', function () {
const abPath = ResolveEnvFilePath('~/fish.env')
assert(abPath === '/Users/hitchhikers-guide-to-the-galaxy/fish.env')
})
it('should leave "~" in path if no user home directory found', function () {
userHomeDir = ''
const abPath = ResolveEnvFilePath('~/fish.env')
assert(abPath === '/Users/hitchhikers-guide-to-the-galaxy/Thanks/~/fish.env')
})
})
}) })