Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

Expand Down
15 changes: 14 additions & 1 deletion src/lib/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}

Expand Down
28 changes: 28 additions & 0 deletions src/test/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
4 changes: 2 additions & 2 deletions wiki/advanced-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading