forked from TestSprite/testsprite-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender-error.ts
More file actions
85 lines (82 loc) · 2.86 KB
/
Copy pathrender-error.ts
File metadata and controls
85 lines (82 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Shared error rendering helpers for `src/index.ts`.
*
* Extracted so the output-interceptor rephrasing logic (P10) is
* unit-testable without spawning a full CLI process.
*/
import type { OutputMode } from './output.js';
/**
* Global flags that belong before the subcommand, not after it.
*
* `boolean` flags (--dry-run, --debug, --verbose) take no value; emit
* example without a placeholder. `value` flags (--output, --profile,
* --endpoint-url) take a single argument; emit example with `<value>`.
*/
const GLOBAL_FLAG_ARITY: Record<string, 'boolean' | 'value'> = {
'dry-run': 'boolean',
output: 'value',
profile: 'value',
'endpoint-url': 'value',
debug: 'boolean',
verbose: 'boolean',
};
/**
* Rephrase Commander's "unknown option '--foo'" error when `--foo` is
* a known global flag that was placed after the subcommand.
*
* Returns the rephrased string when the pattern matches, or `null` when
* it is an ordinary unknown flag that should fall through to the original
* Commander output.
*/
/**
* Format a Commander parse-error message for the requested output mode.
* Returns the string to write to stderr; the caller writes it.
*
* pendingMsg: message captured by configureOutput.outputError before the
* CommanderError was thrown (may already be rephrased by
* rephraseUnknownOption). Null only when Commander threw without
* first calling outputError (should not happen for parse errors).
* fallbackMsg: err.message from CommanderError, used when pendingMsg is null.
* mode: the output mode resolved from --output (or argv fallback).
*/
export function renderCommanderError(
pendingMsg: string | null,
fallbackMsg: string,
mode: OutputMode,
): string {
const rawMsg = (pendingMsg ?? fallbackMsg).trim();
if (mode === 'json') {
return (
JSON.stringify(
{
error: {
code: 'VALIDATION_ERROR',
message: rawMsg,
nextAction: 'Run testsprite --help or testsprite <command> --help for usage.',
requestId: 'local',
},
},
null,
2,
) + '\n'
);
}
// Text mode: emit the buffered message as-is (already formatted by Commander
// or rephrased by rephraseUnknownOption). Synthesize when null.
return pendingMsg ?? `${rawMsg}\n`;
}
export function rephraseUnknownOption(raw: string): string | null {
// Commander emits: "error: unknown option '--foo'"
const match = /unknown option\s+'--([^']+)'/.exec(raw);
if (!match) return null;
const name = match[1]!;
const arity = GLOBAL_FLAG_ARITY[name];
if (arity === undefined) return null;
const example =
arity === 'value'
? `testsprite --${name} <value> <subcommand> ...`
: `testsprite --${name} <subcommand> ...`;
return (
`error: '--${name}' is a global flag; place it before the subcommand.\n` + `Example: ${example}`
);
}