generated from ehmicky/template-javascript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
57 lines (50 loc) · 1.25 KB
/
main.ts
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
import handleCliError, { validateOptions, type Options } from 'handle-cli-error'
import type { Info, Plugin } from 'modern-errors'
/**
* Options of `modern-errors-cli`
*/
export type { Options }
const getOptions = (options: Options = {}) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
validateOptions(options)
if (options.classes !== undefined) {
throw new TypeError('"classes" must not be defined.')
}
return options
}
/**
* Logs `error` on the console (`stderr`) then exits the process.
*
* This never throws. Invalid errors are silently
* [normalized](https://github.com/ehmicky/normalize-exception).
*
* @example
* ```js
* const cliMain = () => {
* try {
* // ...
* } catch (error) {
* // Logs `error` then exits the process
* BaseError.exit(error)
* }
* }
*
* cliMain()
* ```
*/
// Stack traces and error properties are displayed by default.
const exit = ({
error,
options: { stack = true, ...options },
}: Info<Options>['instanceMethods']) => {
handleCliError(error, { ...options, stack })
}
/**
* `modern-errors-cli` plugin
*/
const modernErrorsCli = {
name: 'cli' as const,
getOptions,
instanceMethods: { exit },
} satisfies Plugin
export default modernErrorsCli