|
1 | 1 | import Container from '../container.js' |
| 2 | +import output from '../output.js' |
2 | 3 |
|
3 | 4 | const supportedHelpers = Container.STANDARD_ACTING_HELPERS |
4 | 5 |
|
5 | 6 | const RESERVED_KEYS = new Set(['on', 'path', 'line', 'pattern']) |
| 7 | +const ALL_MODES = ['fail', 'test', 'step', 'file', 'url'] |
6 | 8 |
|
7 | 9 | /** |
8 | 10 | * Parse a plugin's _args (from CLI `-p plugin:key=value:key=value`) into a flat dict. |
@@ -44,12 +46,37 @@ function coerce(v) { |
44 | 46 | } |
45 | 47 |
|
46 | 48 | /** |
47 | | - * Compose CLI args > config > defaults into a normalized trigger spec. |
48 | | - * Returns `{ on, path, line, pattern, ...rest }`. `line` is coerced to a number. |
| 49 | + * Compose CLI args > config > defaults into a normalized trigger spec, then |
| 50 | + * validate it. Returns `{ on, path, line, pattern, ...rest }` with `line` |
| 51 | + * coerced to a number, or `null` if validation failed (an error is printed). |
| 52 | + * |
| 53 | + * @param {object} cliArgs — output of parsePluginArgs(config._args) |
| 54 | + * @param {object} config — full plugin config object |
| 55 | + * @param {object} defaults — fallback values, e.g. `{ on: 'fail' }` |
| 56 | + * @param {object} options |
| 57 | + * @param {string} options.name — plugin name, used in error messages |
| 58 | + * @param {string[]} [options.validModes] — accepted values for `on` |
| 59 | + * (default: fail, test, step, file, url) |
49 | 60 | */ |
50 | | -export function resolveTrigger(cliArgs = {}, config = {}, defaults = {}) { |
| 61 | +export function resolveTrigger(cliArgs = {}, config = {}, defaults = {}, options = {}) { |
| 62 | + const { name = 'plugin', validModes = ALL_MODES } = options |
51 | 63 | const merged = { ...defaults, ...pickKnown(config), ...cliArgs } |
52 | 64 | if (merged.line != null) merged.line = parseInt(merged.line, 10) |
| 65 | + |
| 66 | + const valid = new Set(validModes) |
| 67 | + if (!valid.has(merged.on)) { |
| 68 | + output.error(`${name}: unknown on="${merged.on}". Valid: ${validModes.join(', ')}`) |
| 69 | + return null |
| 70 | + } |
| 71 | + if (merged.on === 'file' && !merged.path) { |
| 72 | + output.error(`${name}:on=file requires path=. Example: -p ${name}:on=file:path=tests/foo.js`) |
| 73 | + return null |
| 74 | + } |
| 75 | + if (merged.on === 'url' && !merged.pattern) { |
| 76 | + output.error(`${name}:on=url requires pattern=. Example: -p ${name}:on=url:pattern=/users/*`) |
| 77 | + return null |
| 78 | + } |
| 79 | + |
53 | 80 | return merged |
54 | 81 | } |
55 | 82 |
|
|
0 commit comments