diff --git a/README.md b/README.md index bdb003d..c0962b2 100644 --- a/README.md +++ b/README.md @@ -39,9 +39,11 @@ npm install configuru } ``` -3. _(optional)_ As a developer (or environment), create a custom override file (e.g. `~/.env/my-project.jsonc`) and save the path in your `CFG_JSON_PATH`. +3. _(optional)_ As a developer (or environment), create a custom override file (e.g. `~/.env/my-project.jsonc`) and save the path in your `CONFIGURU_CONFIG`. - Tip: Use inline secrets like `CFG_JSON_PATH='{"mysecret":"Sssshhh..."}'` or load from GCP Secret Manager: `CFG_JSON_PATH=$(gcloud secrets versions access latest --project=myproject --secret=mysecret)` + Tip: Use inline secrets like `CONFIGURU_CONFIG='{"mysecret":"Sssshhh..."}'` or load from GCP Secret Manager: `CONFIGURU_CONFIG=$(gcloud secrets versions access latest --project=myproject --secret=mysecret)` + + **Note:** The legacy `CFG_JSON_PATH` variable is still supported but deprecated and will be removed in the next major version. 4. Create a configuration module (e.g. `config.ts`) diff --git a/src/lib/loader.ts b/src/lib/loader.ts index 9451c4f..890638a 100644 --- a/src/lib/loader.ts +++ b/src/lib/loader.ts @@ -9,9 +9,22 @@ export interface ConfigLoaderOptions { envMode?: 'all' | 'default' | 'merged' | 'none' } +const resolveUserConfigPath = () => { + if (process.env.CONFIGURU_CONFIG) { + return process.env.CONFIGURU_CONFIG + } + if (process.env.CFG_JSON_PATH) { + console.warn( + 'Configuru: CFG_JSON_PATH is deprecated, use CONFIGURU_CONFIG instead. It will be removed in the next major version.' + ) + return process.env.CFG_JSON_PATH + } + return undefined +} + const defaultOpts: ConfigLoaderOptions = { defaultConfigPath: '.env', - userConfigPath: process.env.CFG_JSON_PATH, + userConfigPath: resolveUserConfigPath(), envMode: 'default', } diff --git a/src/test/loader.test.ts b/src/test/loader.test.ts index e30ab03..1198f49 100644 --- a/src/test/loader.test.ts +++ b/src/test/loader.test.ts @@ -157,3 +157,31 @@ describe('simple loads', () => { }) }) }) + +describe('User config variable', () => { + const defaultConfig = resolve(__dirname, './sandbox/default.json') + + afterEach(() => { + delete process.env.CONFIGURU_CONFIG + delete process.env.CFG_JSON_PATH + }) + + test('CONFIGURU_CONFIG inline JSON', () => { + process.env.CONFIGURU_CONFIG = JSON.stringify({ foo: 'from_configuru' }) + jest.resetModules() // loader reads env at import time, so we must re-import + const { createLoader } = require('../lib/loader') + const loader = createLoader({ defaultConfigPath: defaultConfig }) + const config = loader({ foo: schema.string('foo') }).values() + expect(config.foo).toBe('from_configuru') + }) + + test('CONFIGURU_CONFIG over CFG_JSON_PATH', () => { + process.env.CONFIGURU_CONFIG = JSON.stringify({ foo: 'new' }) + process.env.CFG_JSON_PATH = JSON.stringify({ foo: 'old' }) + jest.resetModules() // loader reads env at import time, so we must re-import + const { createLoader } = require('../lib/loader') + const loader = createLoader({ defaultConfigPath: defaultConfig }) + const config = loader({ foo: schema.string('foo') }).values() + expect(config.foo).toBe('new') + }) +}) diff --git a/wiki/advanced-usage.md b/wiki/advanced-usage.md index fedecfa..8e67fcc 100644 --- a/wiki/advanced-usage.md +++ b/wiki/advanced-usage.md @@ -100,13 +100,13 @@ maskedConfig.apiKey // [redacted] import { createLoader } from 'configuru' const loader = createLoader({ defaultConfigPath: 'default-config', // defaults to ".env" - userConfigPath: process.env.USER_CONFIG, // defaults to process.env.CFG_JSON_PATH + userConfigPath: process.env.USER_CONFIG, // defaults to process.env.CONFIGURU_CONFIG envMode: 'all', // defaults to "default" }) ``` 1. `defaultConfigPath`: Where to look for your default config JSON file (provide null to skip) -2. `userConfigPath`: Where to look for your user config JSON file (provide null to skip) +2. `userConfigPath`: Where to look for your user config JSON file. Defaults to `process.env.CONFIGURU_CONFIG`, falling back to `process.env.CFG_JSON_PATH` (deprecated). Provide null to skip 3. `envMode`: How to handle process.env variables 1. `all` - Load (override) all vars available in process.env to the store 2. `default` - Load (override) only vars with keys from default config