Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow environment variables to override config values
- Loading branch information
Showing
2 changed files
with
15 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
const Configstore = require('configstore'); | ||
const pkg = require(__dirname + '/../../package.json'); | ||
export const config = new Configstore(pkg.name); | ||
|
||
class ConfigStoreWithEnvironmentVariables extends Configstore { | ||
constructor(id, defaults = undefined, options = {}) { | ||
super(id, defaults, options); | ||
} | ||
|
||
public get(key: string): string | undefined { | ||
const envKey = `SNYK_CFG_${key.replace(/-/g, '_').toUpperCase()}`; | ||
const envValue = process.env[envKey]; | ||
return super.has(key) && !envValue ? String(super.get(key)) : envValue; | ||
} | ||
} | ||
|
||
export const config = new ConfigStoreWithEnvironmentVariables(pkg.name); |