Added Env default locations (#81)

* Added env default locations to match rc file locations

* Updated README to clarify all supported files and their formats

* Added new test to verify all env files are searched for
This commit is contained in:
Gregory Waxman 2019-08-28 03:25:15 -04:00 committed by Todd Bluhm
parent 29dcda819a
commit ae7981691e
4 changed files with 55 additions and 35 deletions

View File

@ -134,10 +134,11 @@ the examples repo [env-cmd-examples](https://github.com/toddbluhm/env-cmd-exampl
## Environment File Formats ## Environment File Formats
These are the currently accepted environment file formats. If any other formats are desired please create an issue. These are the currently accepted environment file formats. If any other formats are desired please create an issue.
- `key=value` - `.env` as `key=value`
- Key/value pairs as JSON - `.env.json` Key/value pairs as JSON
- JavaScript file exporting an `object` or a `Promise` that resolves to an `object` - `.env.js` JavaScript file exporting an `object` or a `Promise` that resolves to an `object`
- `.env-cmdrc` file (as valid json) in execution directory - `.env-cmdrc` as valid json or `.env-cmdrc.json` in execution directory with at least one environment `{ "dev": { "key1": "val1" } }`
- `.env-cmdrc.js` JavaScript file exporting an `object` or a `Promise` that resolves to an `object` that contains at least one environment
## Path Rules ## Path Rules

21
dist/get-env-vars.js vendored
View File

@ -3,8 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
const parse_rc_file_1 = require("./parse-rc-file"); const parse_rc_file_1 = require("./parse-rc-file");
const parse_env_file_1 = require("./parse-env-file"); const parse_env_file_1 = require("./parse-env-file");
const RC_FILE_DEFAULT_LOCATIONS = ['./.env-cmdrc', './.env-cmdrc.js', './.env-cmdrc.json']; const RC_FILE_DEFAULT_LOCATIONS = ['./.env-cmdrc', './.env-cmdrc.js', './.env-cmdrc.json'];
const ENV_FILE_DEFAULT_LOCATION = './.env'; const ENV_FILE_DEFAULT_LOCATIONS = ['./.env', './.env.js', './.env.json'];
async function getEnvVars(options) { function getEnvVars(options) {
return __awaiter(this, void 0, void 0, function* () {
options = options || {}; options = options || {};
options.envFile = options.envFile || {}; options.envFile = options.envFile || {};
// Check for rc file usage // Check for rc file usage
@ -12,26 +13,30 @@ async function getEnvVars(options) {
return getRCFile({ environments: options.rc.environments, filePath: options.rc.filePath }); return getRCFile({ environments: options.rc.environments, filePath: options.rc.filePath });
} }
return getEnvFile({ filePath: options.envFile.filePath, fallback: options.envFile.fallback }); return getEnvFile({ filePath: options.envFile.filePath, fallback: options.envFile.fallback });
});
} }
exports.getEnvVars = getEnvVars; exports.getEnvVars = getEnvVars;
async function getEnvFile({ filePath, fallback }) { function getEnvFile({ filePath, fallback }) {
return __awaiter(this, void 0, void 0, function* () {
// Use env file // Use env file
if (filePath) { if (filePath) {
try { try {
return await parse_env_file_1.getEnvFileVars(filePath); return yield parse_env_file_1.getEnvFileVars(filePath);
} }
catch (e) { } catch (e) { }
if (!fallback) { if (!fallback) {
throw new Error(`Unable to locate env file at location (${filePath})`); throw new Error(`Unable to locate env file at location (${filePath})`);
} }
} }
// Use the default env file location // Use the default env file locations
for (const path of ENV_FILE_DEFAULT_LOCATIONS) {
try { try {
return await parse_env_file_1.getEnvFileVars(ENV_FILE_DEFAULT_LOCATION); return yield parse_env_file_1.getEnvFileVars(path);
} }
catch (e) { catch (e) { }
throw new Error(`Unable to locate env file at default location (${ENV_FILE_DEFAULT_LOCATION})`);
} }
throw new Error(`Unable to locate env file at default locations (${ENV_FILE_DEFAULT_LOCATIONS})`);
});
} }
exports.getEnvFile = getEnvFile; exports.getEnvFile = getEnvFile;
async function getRCFile({ environments, filePath }) { async function getRCFile({ environments, filePath }) {

View File

@ -3,7 +3,7 @@ import { getRCFileVars } from './parse-rc-file'
import { getEnvFileVars } from './parse-env-file' import { getEnvFileVars } from './parse-env-file'
const RC_FILE_DEFAULT_LOCATIONS = ['./.env-cmdrc', './.env-cmdrc.js', './.env-cmdrc.json'] const RC_FILE_DEFAULT_LOCATIONS = ['./.env-cmdrc', './.env-cmdrc.js', './.env-cmdrc.json']
const ENV_FILE_DEFAULT_LOCATION = './.env' const ENV_FILE_DEFAULT_LOCATIONS = ['./.env', './.env.js', './.env.json']
export async function getEnvVars (options?: GetEnvVarOptions): Promise<{ [key: string]: any }> { export async function getEnvVars (options?: GetEnvVarOptions): Promise<{ [key: string]: any }> {
options = options || {} options = options || {}
@ -28,12 +28,14 @@ export async function getEnvFile (
} }
} }
// Use the default env file location // Use the default env file locations
for (const path of ENV_FILE_DEFAULT_LOCATIONS) {
try { try {
return await getEnvFileVars(ENV_FILE_DEFAULT_LOCATION) return await getEnvFileVars(path)
} catch (e) { } catch (e) { }
throw new Error(`Unable to locate env file at default location (${ENV_FILE_DEFAULT_LOCATION})`)
} }
throw new Error(`Unable to locate env file at default locations (${ENV_FILE_DEFAULT_LOCATIONS})`)
} }
export async function getRCFile ( export async function getRCFile (

View File

@ -133,6 +133,18 @@ describe('getEnvVars', (): void => {
assert.equal(getEnvFileVarsStub.args[0][0], './.env') assert.equal(getEnvFileVarsStub.args[0][0], './.env')
}) })
it('should search all default env file locations', async (): Promise<void> => {
getEnvFileVarsStub.throws('Not found.')
getEnvFileVarsStub.onThirdCall().returns({ THANKS: 'FORALLTHEFISH' })
const envs = await getEnvVars()
assert.isOk(envs)
assert.lengthOf(Object.keys(envs), 1)
assert.equal(envs.THANKS, 'FORALLTHEFISH')
assert.equal(getEnvFileVarsStub.callCount, 3)
assert.isTrue(getEnvFileVarsStub.calledWithExactly('./.env.json'))
assert.isTrue(getEnvFileVarsStub.calledWithExactly('./.env.json'))
})
it('should fail to find env file at default location', async (): Promise<void> => { it('should fail to find env file at default location', async (): Promise<void> => {
getEnvFileVarsStub.rejects('Not found.') getEnvFileVarsStub.rejects('Not found.')
try { try {