|
| 1 | +import { validateOptions as validateBeautifulOptions } from 'beautiful-error' |
1 | 2 | import isPlainObj from 'is-plain-obj' |
2 | 3 |
|
3 | 4 | import { validateExitCode } from '../exit.js' |
4 | 5 | import { validateTimeout } from '../timeout.js' |
5 | 6 |
|
6 | | -import { validateClasses } from './classes.js' |
7 | | -import { handleInvalidOpts } from './invalid.js' |
| 7 | +import { applyClassesOpts } from './classes.js' |
8 | 8 |
|
9 | 9 | // Validate option values. |
10 | 10 | // This is exported, although not documented. |
11 | 11 | export const validateOptions = (opts) => { |
12 | | - validateAllOpts(opts, []) |
13 | | -} |
14 | | - |
15 | | -const validateAllOpts = (opts, optName) => { |
16 | | - if (opts === undefined) { |
17 | | - return |
18 | | - } |
19 | | - |
20 | 12 | if (!isPlainObj(opts)) { |
21 | | - handleInvalidOpts('must be a plain object', opts, optName) |
| 13 | + return |
22 | 14 | } |
23 | 15 |
|
24 | | - Object.entries(opts).forEach(([key, optValue]) => { |
25 | | - validateOpt(optValue, [...optName, key]) |
| 16 | + const { classes } = opts |
| 17 | + const names = |
| 18 | + isPlainObj(classes) && Object.keys(classes).length !== 0 |
| 19 | + ? Object.keys(classes) |
| 20 | + : ['default'] |
| 21 | + names.forEach((name) => { |
| 22 | + normalizeOptions(name, opts) |
26 | 23 | }) |
27 | 24 | } |
28 | 25 |
|
29 | | -const validateOpt = (optValue, optName) => { |
30 | | - if (optValue === undefined || BEAUTIFUL_ERROR_OPTS.has(optName)) { |
31 | | - return |
32 | | - } |
33 | | - |
34 | | - const validator = VALIDATORS[optName.at(-1)] |
| 26 | +export const normalizeOptions = (name, opts) => { |
| 27 | + const { silent, exitCode, timeout, ...beautifulErrorOpts } = applyClassesOpts( |
| 28 | + name, |
| 29 | + opts, |
| 30 | + ) |
| 31 | + const optsA = { silent, exitCode, timeout } |
| 32 | + Object.entries(optsA).forEach(validateOpt) |
| 33 | + validateBeautifulOptions(beautifulErrorOpts) |
| 34 | + return { opts: optsA, beautifulErrorOpts } |
| 35 | +} |
35 | 36 |
|
36 | | - if (validator === undefined) { |
37 | | - handleInvalidOpts('is an unknown option', '', optName) |
| 37 | +const validateOpt = ([optName, optValue]) => { |
| 38 | + if (optValue !== undefined) { |
| 39 | + VALIDATORS[optName](optValue, optName) |
38 | 40 | } |
39 | | - |
40 | | - validator(optValue, optName, validateAllOpts) |
41 | 41 | } |
42 | 42 |
|
43 | 43 | const validateBooleanOpt = (value, optName) => { |
44 | 44 | if (typeof value !== 'boolean') { |
45 | | - handleInvalidOpts('must be a boolean', value, optName) |
| 45 | + throw new TypeError(`"${optName}" must be a boolean: ${value}`) |
46 | 46 | } |
47 | 47 | } |
48 | 48 |
|
49 | 49 | const VALIDATORS = { |
50 | 50 | silent: validateBooleanOpt, |
51 | 51 | exitCode: validateExitCode, |
52 | 52 | timeout: validateTimeout, |
53 | | - classes: validateClasses, |
54 | 53 | } |
55 | | - |
56 | | -const BEAUTIFUL_ERROR_OPTS = new Set([ |
57 | | - 'stack', |
58 | | - 'props', |
59 | | - 'colors', |
60 | | - 'icon', |
61 | | - 'header', |
62 | | -]) |
0 commit comments