Skip to content

Commit

Permalink
Improve cli runner validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jan 30, 2022
1 parent aff8126 commit e2cff77
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 105 deletions.
1 change: 0 additions & 1 deletion package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"is-interactive": "^2.0.0",
"is-plain-obj": "^4.0.0",
"is-unicode-supported": "^1.1.0",
"jest-validate": "^27.4.2",
"js-yaml": "^4.0.0",
"junk": "^4.0.0",
"map-obj": "^5.0.0",
Expand Down
17 changes: 16 additions & 1 deletion src/config/normalize/validate.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import { isDeepStrictEqual } from 'util'
import { isDeepStrictEqual, inspect } from 'util'

import isPlainObj from 'is-plain-obj'
import { pathExists } from 'path-exists'
import { isFile, isDirectory } from 'path-type'

export const validateEnum = function (value, set) {
if (set.has(value)) {
return
}

const setItems = [...set].map(inspect)
const separator = setItems.some(hasNewline) ? '\n' : ', '
const setItemsStr = setItems.join(separator)
throw new Error(`must be one of: ${setItemsStr}.`)
}

const hasNewline = function (string) {
return string.includes('\n')
}

export const validateBoolean = function (value) {
if (typeof value !== 'boolean') {
throw new TypeError('must be true or false.')
Expand Down
18 changes: 18 additions & 0 deletions src/runners/cli/launch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { fileURLToPath } from 'url'

const HANDLER_MAIN_PATH = fileURLToPath(
new URL('./handler/main.js', import.meta.url),
)

export const launch = function ({ shell }) {
return {
spawn: ['node', HANDLER_MAIN_PATH],
versions: SHELL_VERSIONS[shell],
}
}

const SHELL_VERSIONS = {
none: {},
sh: {},
bash: { Bash: ['bash', '-c', 'echo $BASH_VERSION'] },
}
23 changes: 0 additions & 23 deletions src/runners/cli/launch/main.js

This file was deleted.

29 changes: 0 additions & 29 deletions src/runners/cli/launch/validate.js

This file was deleted.

6 changes: 5 additions & 1 deletion src/runners/cli/main.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export { launch } from './launch/main.js'
import { shellDefinition } from './shell.js'

export { launch } from './launch.js'

export const config = [shellDefinition]
18 changes: 18 additions & 0 deletions src/runners/cli/shell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
validateDefinedString,
validateEnum,
} from '../../config/normalize/validate.js'

// We only allow shells that are cross-platform
const SHELL_VALUES = new Set(['none', 'sh', 'bash'])
// Shells have a performance impact and are less portable, so they are opt-in
const DEFAULT_SHELL = 'none'

export const shellDefinition = {
name: 'shell',
default: DEFAULT_SHELL,
validate(value) {
validateDefinedString(value)
validateEnum(value, SHELL_VALUES)
},
}
18 changes: 2 additions & 16 deletions src/runners/node/main.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
import { normalizeNumberString } from '../../config/normalize/transform.js'
import { validateNumberString } from '../../config/normalize/validate.js'

import { transformVersion, validateVersion } from './version.js'
import { versionDefinitions } from './version.js'

export { launch } from './launch.js'

export const config = [
{
name: 'version',
validate: validateNumberString,
transform: normalizeNumberString,
},
{
name: 'version',
validate: validateVersion,
transform: transformVersion,
},
]
export const config = [...versionDefinitions]
15 changes: 15 additions & 0 deletions src/runners/node/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { fileURLToPath } from 'url'
import { readPackageUp } from 'read-pkg-up'
import semver from 'semver'

import { normalizeNumberString } from '../../config/normalize/transform.js'
import { validateNumberString } from '../../config/normalize/validate.js'
import { wrapError } from '../../error/wrap.js'

// Validate the Node.js version
Expand Down Expand Up @@ -42,3 +44,16 @@ export const transformVersion = async function (version) {
throw wrapError(error, 'must be a valid Node.js version:')
}
}

export const versionDefinitions = [
{
name: 'version',
validate: validateNumberString,
transform: normalizeNumberString,
},
{
name: 'version',
validate: validateVersion,
transform: transformVersion,
},
]
33 changes: 0 additions & 33 deletions src/utils/validate.js

This file was deleted.

0 comments on commit e2cff77

Please sign in to comment.