Skip to content

Commit

Permalink
Allow environment variables for configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Aug 18, 2019
1 parent f8cd849 commit 0c256c7
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 6 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
"strip-ansi": "^5.2.0",
"uuid": "^3.3.2",
"write-file-atomic": "^3.0.0",
"yargs": "^13.3.0"
"yargs": "^13.3.0",
"yn": "^3.1.1"
},
"devDependencies": {
"ava": "^2.3.0",
Expand Down
82 changes: 82 additions & 0 deletions src/options/env.js
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',
}
12 changes: 7 additions & 5 deletions src/options/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import isInteractive from 'is-interactive'
import { omitBy } from '../utils/main.js'

import { getConfig } from './config.js'
import { addEnvVars } from './env.js'
import { normalizeOpts } from './normalize.js'
import { loadAllPlugins } from './plugins.js'

Expand All @@ -16,13 +17,14 @@ export const getOpts = async function(opts = {}) {
validateOpts(optsA)

const optsB = await getConfig({ opts: optsA })
const optsC = addEnvVars(optsB)

validateOpts(optsB)
const optsC = { ...DEFAULT_OPTS, ...optsB }
validateOpts(optsC)
const optsD = { ...DEFAULT_OPTS, ...optsC }

const optsD = await normalizeOpts(optsC)
const optsE = await loadAllPlugins(optsD)
return optsE
const optsE = await normalizeOpts(optsD)
const optsF = await loadAllPlugins(optsE)
return optsF
}

const isUndefined = function(value) {
Expand Down

0 comments on commit 0c256c7

Please sign in to comment.