-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathcmd-oops.mts
More file actions
116 lines (98 loc) · 2.94 KB
/
Copy pathcmd-oops.mts
File metadata and controls
116 lines (98 loc) · 2.94 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { getDefaultLogger } from '@socketsecurity/lib-stable/logger/default'
import { DRY_RUN_LABEL } from '../../constants/cli.mts'
import { defineFlags } from '../../meow.mts'
import { commonFlags, outputFlags } from '../../flags.mts'
import { meowOrExit } from '../../util/cli/with-subcommands.mjs'
import { failMsgWithBadge } from '../../util/error/fail-msg-with-badge.mts'
import { serializeResultJson } from '../../util/output/result-json.mjs'
import type { CliCommandContext } from '../../util/cli/with-subcommands.mjs'
import type { MeowFlags } from '../../flags.mts'
const logger = getDefaultLogger()
export const CMD_NAME = 'oops'
const description = 'Trigger an intentional error (for development)'
const hidden = true
// Command handler.
export async function run(
argv: string[] | readonly string[],
importMeta: ImportMeta,
{ parentName }: CliCommandContext,
): Promise<void> {
const config = {
commandName: CMD_NAME,
description,
hidden,
flags: defineFlags({
...commonFlags,
...outputFlags,
throw: {
type: 'boolean',
default: false,
description:
'Throw an explicit error even if --json or --markdown are set',
},
}),
help: (
helpParentName: string,
helpConfig: { commandName: string; flags: MeowFlags },
) => `
Usage
$ ${helpParentName} ${helpConfig.commandName}
Don't run me.
`,
}
const cli = meowOrExit({
argv,
config,
importMeta,
parentName,
})
const { json, markdown, throw: justThrow } = cli.flags
const dryRun = cli.flags['dryRun']
if (dryRun) {
// Dry-run previews are contextual output; route to stderr per the
// stream discipline rule so stdout stays payload-only.
logger.error('')
logger.error(`${DRY_RUN_LABEL}: Would trigger an intentional error`)
logger.error('')
logger.error(
' This command throws an error for development/testing purposes.',
)
logger.error(` Error message: "This error was intentionally left blank."`)
logger.error('')
if (json && !justThrow) {
logger.error(' Output format: JSON error response')
} else if (markdown && !justThrow) {
logger.error(' Output format: Markdown error message')
} else {
logger.error(' Output format: Thrown Error exception')
}
logger.error('')
logger.error(' Run without --dry-run to trigger the error.')
logger.error('')
return
}
if (json && !justThrow) {
process.exitCode = 1
logger.log(
serializeResultJson({
ok: false,
message: 'Oops',
cause: 'This error was intentionally left blank',
}),
)
}
if (markdown && !justThrow) {
process.exitCode = 1
logger.fail(
failMsgWithBadge('Oops', 'This error was intentionally left blank'),
)
return
}
throw new Error('This error was intentionally left blank.')
}
// Exported command.
export const cmdOops = {
description,
hidden,
run,
}