@runcapsule/get-env-config
Hierarchical Node.js configuration with files, environment variables. Configs have the following priority (from lowest to highest):
- Default config
env.ts
- Local config
env.local.ts
- Environment config
env.development.ts
- Local Environment config
env.development.local.ts
- All above rules for configs found in the rest of directories from
configsDir
. - Environment variables
Installation
npm install -D @runcapsule/get-env-config@latest
or
yarn add -D @runcapsule/get-env-config@latest
Usage example
import { getConfig } from '@runcapsule/get-env-config';
const envConfig = getConfig(CONFIG_NAME, {
configsDir: BASE_CONFIGS_DIR,
environment: ENVIRONMENT_NAME,
});
// Now you can refer to this config from anywhere
API
getConfig(options)
Options object itself is required.
options.configsDir(String | String[])
Base configs directory or directories. Will use it to search for configs.
options.environment(String?)
Optional environment name. Will be added to base config name. For example for environment produciton
will be required
following configs:
CONFIGS_DIR/env.ts
CONFIGS_DIR/env.local.ts
CONFIGS_DIR/env.produciton.ts
CONFIGS_DIR/env.produciton.local.ts
loadConfigIntoProcessEnv(options)
Will follow all rules of getConfig
but instead of return resulting config it will load into process.env
.
getHelpers(config, properties)
getHelpers(config, [
'SECURE',
'PRIVATE',
{
dev: config.NODE_ENV === 'development',
prod: config.NODE_ENV === 'production',
test: config.NODE_ENV === 'test',
},
])
Result will have:
- properties with boolean values:
['isSecure', 'isPrivate', 'isDev', 'isProd', 'isTest']
- functions, that can accept up to 2 arguments and return 1st arg if flag is truthy and 2nd arg if flag is falsy:
['ifSecure', 'ifPrivate', 'ifDev', 'ifProd', 'ifTest]
- functions, that can accept up to 2 arguments and return 1st arg if flag is falsy and 2nd arg if flag is truthy:
['ifNotSecure', 'ifNotPrivate', 'ifNotDev', 'ifNotProd', 'ifNotTest]
printConfig(config)
Pretty print sorted config values into console.