-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow environment variables for configuration
- Loading branch information
Showing
4 changed files
with
96 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { env } from 'process' | ||
|
||
import yn from 'yn' | ||
|
||
import { set } from '../utils/get_set.js' | ||
|
||
// All the options can be set using environment variables. | ||
// This is especially handy in CI, including for the `job` and `env` options. | ||
export const addEnvVars = function(opts) { | ||
const envVars = Object.entries(env) | ||
.filter(isSpydEnvVar) | ||
.map(getEnvVar) | ||
return Object.assign({}, opts, ...envVars) | ||
} | ||
|
||
const isSpydEnvVar = function([key]) { | ||
return key.startsWith(SPYD_NAMESPACE) | ||
} | ||
|
||
const getEnvVar = function([key, value]) { | ||
const keyA = key.replace(SPYD_NAMESPACE, '').toLowerCase() | ||
|
||
if (keyA.includes('_')) { | ||
return set({}, keyA.split('_'), value) | ||
} | ||
|
||
const type = OPTS[keyA] | ||
|
||
if (type === undefined) { | ||
return {} | ||
} | ||
|
||
const valueA = TYPES[type](value) | ||
return { [keyA]: valueA } | ||
} | ||
|
||
const SPYD_NAMESPACE = 'SPYD_' | ||
|
||
const getBoolean = function(value) { | ||
return yn(value, { default: true }) | ||
} | ||
|
||
const getString = function(value) { | ||
return value | ||
} | ||
|
||
const getStringArray = function(value) { | ||
return [value] | ||
} | ||
|
||
const TYPES = { | ||
boolean: getBoolean, | ||
string: getString, | ||
number: Number, | ||
stringArray: getStringArray, | ||
} | ||
|
||
const OPTS = { | ||
colors: 'boolean', | ||
diff: 'boolean', | ||
link: 'boolean', | ||
remove: 'boolean', | ||
save: 'boolean', | ||
show: 'boolean', | ||
system: 'boolean', | ||
verbose: 'boolean', | ||
config: 'string', | ||
cwd: 'string', | ||
data: 'string', | ||
env: 'string', | ||
insert: 'string', | ||
job: 'string', | ||
output: 'string', | ||
duration: 'number', | ||
files: 'stringArray', | ||
tasks: 'stringArray', | ||
variations: 'stringArray', | ||
progress: 'object', | ||
report: 'object', | ||
run: 'object', | ||
store: 'object', | ||
} |
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