diff --git a/README.md b/README.md index 5c16352..16c0775 100644 --- a/README.md +++ b/README.md @@ -134,10 +134,11 @@ the examples repo [env-cmd-examples](https://github.com/toddbluhm/env-cmd-exampl ## Environment File Formats These are the currently accepted environment file formats. If any other formats are desired please create an issue. -- `key=value` -- Key/value pairs as JSON -- JavaScript file exporting an `object` or a `Promise` that resolves to an `object` -- `.env-cmdrc` file (as valid json) in execution directory +- `.env` as `key=value` +- `.env.json` Key/value pairs as JSON +- `.env.js` JavaScript file exporting an `object` or a `Promise` that resolves to an `object` +- `.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 diff --git a/dist/get-env-vars.js b/dist/get-env-vars.js index a5f7d3d..99f8a99 100644 --- a/dist/get-env-vars.js +++ b/dist/get-env-vars.js @@ -3,35 +3,40 @@ Object.defineProperty(exports, "__esModule", { value: true }); const parse_rc_file_1 = require("./parse-rc-file"); const parse_env_file_1 = require("./parse-env-file"); const RC_FILE_DEFAULT_LOCATIONS = ['./.env-cmdrc', './.env-cmdrc.js', './.env-cmdrc.json']; -const ENV_FILE_DEFAULT_LOCATION = './.env'; -async function getEnvVars(options) { - options = options || {}; - options.envFile = options.envFile || {}; - // Check for rc file usage - if (options.rc) { - return getRCFile({ environments: options.rc.environments, filePath: options.rc.filePath }); - } - return getEnvFile({ filePath: options.envFile.filePath, fallback: options.envFile.fallback }); +const ENV_FILE_DEFAULT_LOCATIONS = ['./.env', './.env.js', './.env.json']; +function getEnvVars(options) { + return __awaiter(this, void 0, void 0, function* () { + options = options || {}; + options.envFile = options.envFile || {}; + // Check for rc file usage + if (options.rc) { + return getRCFile({ environments: options.rc.environments, filePath: options.rc.filePath }); + } + return getEnvFile({ filePath: options.envFile.filePath, fallback: options.envFile.fallback }); + }); } exports.getEnvVars = getEnvVars; -async function getEnvFile({ filePath, fallback }) { - // Use env file - if (filePath) { - try { - return await parse_env_file_1.getEnvFileVars(filePath); +function getEnvFile({ filePath, fallback }) { + return __awaiter(this, void 0, void 0, function* () { + // Use env file + if (filePath) { + try { + return yield parse_env_file_1.getEnvFileVars(filePath); + } + catch (e) { } + if (!fallback) { + throw new Error(`Unable to locate env file at location (${filePath})`); + } } - catch (e) { } - if (!fallback) { - throw new Error(`Unable to locate env file at location (${filePath})`); + // Use the default env file locations + for (const path of ENV_FILE_DEFAULT_LOCATIONS) { + try { + return yield parse_env_file_1.getEnvFileVars(path); + } + catch (e) { } } - } - // Use the default env file location - try { - return await parse_env_file_1.getEnvFileVars(ENV_FILE_DEFAULT_LOCATION); - } - 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; async function getRCFile({ environments, filePath }) { diff --git a/src/get-env-vars.ts b/src/get-env-vars.ts index 5887ca5..2dc662e 100644 --- a/src/get-env-vars.ts +++ b/src/get-env-vars.ts @@ -3,7 +3,7 @@ import { getRCFileVars } from './parse-rc-file' import { getEnvFileVars } from './parse-env-file' 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 }> { options = options || {} @@ -28,12 +28,14 @@ export async function getEnvFile ( } } - // Use the default env file location - try { - return await getEnvFileVars(ENV_FILE_DEFAULT_LOCATION) - } catch (e) { - throw new Error(`Unable to locate env file at default location (${ENV_FILE_DEFAULT_LOCATION})`) + // Use the default env file locations + for (const path of ENV_FILE_DEFAULT_LOCATIONS) { + try { + return await getEnvFileVars(path) + } catch (e) { } } + + throw new Error(`Unable to locate env file at default locations (${ENV_FILE_DEFAULT_LOCATIONS})`) } export async function getRCFile ( diff --git a/test/get-env-vars.spec.ts b/test/get-env-vars.spec.ts index f46b4d7..a51f24a 100644 --- a/test/get-env-vars.spec.ts +++ b/test/get-env-vars.spec.ts @@ -133,6 +133,18 @@ describe('getEnvVars', (): void => { assert.equal(getEnvFileVarsStub.args[0][0], './.env') }) + it('should search all default env file locations', async (): Promise => { + 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 => { getEnvFileVarsStub.rejects('Not found.') try {