Skip to content

Commit

Permalink
Split file
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 15, 2019
1 parent 545682f commit 1263625
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 52 deletions.
26 changes: 26 additions & 0 deletions src/options/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Validation that cannot be handled by `jest-validate`
export const validateCustom = function({ opts }) {
Object.entries(ENUM_OPTS).forEach(([attrName, allowed]) => {
validateEnum({ attrName, allowed, opts })
})
}

const ENUM_OPTS = {
result: ['save', 'replace'],
from: ['stdout', 'stderr', 'all'],
}

// Validate an enum option
const validateEnum = function({
attrName,
allowed,
opts: { [attrName]: value },
}) {
if (value === undefined || allowed.includes(value)) {
return
}

throw new Error(
`option '${attrName}' '${value}' must be one of: ${allowed.join(', ')}`,
)
}
56 changes: 4 additions & 52 deletions src/options/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import isCi from 'is-ci'

import { isPlainObject, pickBy } from '../utils.js'

import { CHILD_PROCESS_OPTS, EXECA_OPTS } from './upstream.js'
import { validateCustom } from './custom.js'
import { addVerbose } from './verbose.js'

// Parse options
export const parseOpts = function({
opts = {},
Expand Down Expand Up @@ -36,61 +40,9 @@ const DEFAULT_OPTS = {
verbose: isCi,
}

// Examples for the core `child_process.spawn()` options
const CHILD_PROCESS_OPTS = {
windowsHide: true,
}

// Examples for the `execa` options
const EXECA_OPTS = {

}

const EXAMPLE_OPTS = {
...CHILD_PROCESS_OPTS,
...EXECA_OPTS,
...DEFAULT_OPTS,
echo: true,
}

// Validation that cannot be handled by `jest-validate`
const validateCustom = function({ opts }) {
Object.entries(ENUM_OPTS).forEach(([attrName, allowed]) => {
validateEnum({ attrName, allowed, opts })
})
}

const ENUM_OPTS = {
result: ['save', 'replace'],
from: ['stdout', 'stderr', 'all'],
}

// Validate an enum option
const validateEnum = function({
attrName,
allowed,
opts: { [attrName]: value },
}) {
if (value === undefined || allowed.includes(value)) {
return
}

throw new Error(
`option '${attrName}' '${value}' must be one of: ${allowed.join(', ')}`,
)
}

// Translate `verbose` option into `stdout|stderr|echo` options
const addVerbose = function({ opts: { verbose, ...opts }, opts: { stdio } }) {
if (!verbose) {
// The default value for `echo` must be added here, not in `DEFAULT_OPTS`
return { echo: false, ...opts }
}

// `execa` does not allow mixing `stdio` and `stdout|stderr` options
if (stdio !== undefined) {
return { echo: true, ...opts }
}

return { stdout: 'inherit', stderr: 'inherit', echo: true, ...opts }
}
9 changes: 9 additions & 0 deletions src/options/upstream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Examples for the core `child_process.spawn()` options
export const CHILD_PROCESS_OPTS = {
windowsHide: true,
}

// Examples for the `execa` options
export const EXECA_OPTS = {

}
14 changes: 14 additions & 0 deletions src/options/verbose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Translate `verbose` option into `stdout|stderr|echo` options
export const addVerbose = function({ opts: { verbose, ...opts }, opts: { stdio } }) {
if (!verbose) {
// The default value for `echo` must be added here, not in `DEFAULT_OPTS`
return { echo: false, ...opts }
}

// `execa` does not allow mixing `stdio` and `stdout|stderr` options
if (stdio !== undefined) {
return { echo: true, ...opts }
}

return { stdout: 'inherit', stderr: 'inherit', echo: true, ...opts }
}

0 comments on commit 1263625

Please sign in to comment.