mirror of
https://github.com/toddbluhm/env-cmd.git
synced 2025-12-08 18:23:33 +00:00
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:
parent
29dcda819a
commit
ae7981691e
@ -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
|
||||
|
||||
|
||||
55
dist/get-env-vars.js
vendored
55
dist/get-env-vars.js
vendored
@ -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 }) {
|
||||
|
||||
@ -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 (
|
||||
|
||||
@ -133,6 +133,18 @@ describe('getEnvVars', (): void => {
|
||||
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> => {
|
||||
getEnvFileVarsStub.rejects('Not found.')
|
||||
try {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user