Skip to content
This repository was archived by the owner on Feb 1, 2022. It is now read-only.

Commit 7ecdf46

Browse files
committed
feat: use @oclif/errors for errors
BREAKING CHANGE: all the error logging functions have been moved to @oclif/errors
1 parent 97ecfca commit 7ecdf46

File tree

13 files changed

+54
-458
lines changed

13 files changed

+54
-458
lines changed

examples/action.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@ async function main() {
5353
cli.error('oh no')
5454
}
5555
main()
56+
.catch(require('@oclif/errors/handle'))

examples/errors.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import cli from '../src'
22

33
cli.warn('uh oh!')
4-
cli.warn('uh oh!', 'myscope')
54
cli.error('uh oh!')

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"supports-color": "^5.4.0"
2222
},
2323
"devDependencies": {
24-
"@oclif/tslint": "^1.1.0",
24+
"@oclif/errors": "^1.0.9",
25+
"@oclif/tslint": "^1.1.1",
2526
"@types/ansi-styles": "^2.0.30",
2627
"@types/chai": "^4.1.3",
2728
"@types/clean-stack": "^1.3.0",

src/config.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const actionType = (
2525
const Action = actionType === 'spinner' ? require('./action/spinner').default : require('./action/simple').default
2626

2727
export class Config {
28-
logLevel: Levels = 'warn'
2928
outputLevel: Levels = 'info'
3029
action: ActionBase = new Action()
3130
errorsHandled = false
@@ -35,8 +34,6 @@ export class Config {
3534
set debug(v: boolean) { globals.debug = v }
3635
get context(): any { return globals.context || {} }
3736
set context(v: any) { globals.context = v }
38-
get errlog(): string | undefined { return globals.errlog }
39-
set errlog(v: string | undefined) { globals.errlog = v }
4037
}
4138

4239
function fetch() {

src/errors.ts

Lines changed: 0 additions & 197 deletions
This file was deleted.

src/events.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/index.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,19 @@
1-
import * as EventEmitter from 'events'
1+
import * as Errors from '@oclif/errors'
2+
import * as util from 'util'
23

34
import {ActionBase} from './action/base'
5+
import {config, Config} from './config'
46
import deps from './deps'
5-
import Errors, {CLIError, Options as ErrorOptions} from './errors'
67
import {ExitError} from './exit'
7-
import * as Logger from './logger'
8-
import Output from './output'
98
import {IPromptOptions} from './prompt'
109
import * as Table from './styled/table'
1110

12-
import {config, Config} from './config'
13-
14-
const e = new EventEmitter()
15-
const output = Output(e)
16-
const errors = Errors(e)
17-
const logger = Logger.default(e)
18-
1911
export const cli = {
2012
config,
21-
trace: output('trace'),
22-
debug: output('debug'),
23-
info: output('info'),
24-
log: output('info'),
25-
26-
warn: errors('warn'),
27-
error: errors('error'),
28-
fatal: errors('fatal'),
2913

30-
exit(code = 1, error?: Error) { throw new ExitError(code, error) },
14+
warn: Errors.warn,
15+
error: Errors.error,
16+
exit: Errors.exit,
3117

3218
get prompt() { return deps.prompt.prompt },
3319
get confirm() { return deps.prompt.confirm },
@@ -39,19 +25,35 @@ export const cli = {
3925

4026
async done() {
4127
config.action.stop()
42-
await logger.flush()
4328
// await flushStdout()
44-
}
29+
},
30+
31+
trace(format: string, ...args: string[]) {
32+
if (this.config.outputLevel === 'trace') {
33+
process.stdout.write(util.format(format, ...args) + '\n')
34+
}
35+
},
36+
37+
debug(format: string, ...args: string[]) {
38+
if (['trace', 'debug'].includes(this.config.outputLevel)) {
39+
process.stdout.write(util.format(format, ...args) + '\n')
40+
}
41+
},
42+
43+
info(format: string, ...args: string[]) {
44+
process.stdout.write(util.format(format, ...args) + '\n')
45+
},
46+
47+
log(format: string, ...args: string[]) {
48+
this.info(format, ...args)
49+
},
4550
}
4651
export default cli
4752

4853
export {
4954
config,
5055
ActionBase,
51-
CLIError,
5256
Config,
53-
ErrorOptions,
54-
Errors,
5557
ExitError,
5658
IPromptOptions,
5759
Table,

src/logger.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)