This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.ts
56 lines (45 loc) · 1.86 KB
/
index.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
// tslint:disable no-console
export {handle} from './handle'
export {ExitError} from './errors/exit'
export {CLIError} from './errors/cli'
export {Logger} from './logger'
export {config} from './config'
import {config} from './config'
import {CLIError, addOclifExitCode, OclifError} from './errors/cli'
import {ExitError} from './errors/exit'
import prettyPrint, {PrettyPrintableError, applyPrettyPrintOptions} from './errors/pretty-print'
export {PrettyPrintableError}
export function exit(code = 0): never {
throw new ExitError(code)
}
export function error(input: string | Error, options: {exit: false} & PrettyPrintableError): void
export function error(input: string | Error, options?: {exit?: number} & PrettyPrintableError): never
export function error(input: string | Error, options: {exit?: number | false} & PrettyPrintableError = {}) {
let err: Error & OclifError
if (typeof input === 'string') {
err = new CLIError(input, options)
} else if (input instanceof Error) {
err = addOclifExitCode(input, options) as Error & OclifError
} else {
throw new TypeError('first argument must be a string or instance of Error')
}
err = applyPrettyPrintOptions(err, options) as Error & OclifError & PrettyPrintableError
if (options.exit === false) {
const message = prettyPrint(err)
console.error(message)
if (config.errorLogger) config.errorLogger.log(err?.stack ?? '')
} else throw err
}
export function warn(input: string | Error) {
let err: Error & OclifError
if (typeof input === 'string') {
err = new CLIError.Warn(input)
} else if (input instanceof Error) {
err = addOclifExitCode(input) as Error & OclifError
} else {
throw new TypeError('first argument must be a string or instance of Error')
}
const message = prettyPrint(err)
console.error(message)
if (config.errorLogger) config.errorLogger.log(err?.stack ?? '')
}