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

Commit

Permalink
feat: use @oclif/errors for errors
Browse files Browse the repository at this point in the history
BREAKING CHANGE: all the error logging functions have been moved to @oclif/errors
  • Loading branch information
jdx committed May 6, 2018
1 parent 97ecfca commit 7ecdf46
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 458 deletions.
1 change: 1 addition & 0 deletions examples/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ async function main() {
cli.error('oh no')
}
main()
.catch(require('@oclif/errors/handle'))
1 change: 0 additions & 1 deletion examples/errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import cli from '../src'

cli.warn('uh oh!')
cli.warn('uh oh!', 'myscope')
cli.error('uh oh!')
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"supports-color": "^5.4.0"
},
"devDependencies": {
"@oclif/tslint": "^1.1.0",
"@oclif/errors": "^1.0.9",
"@oclif/tslint": "^1.1.1",
"@types/ansi-styles": "^2.0.30",
"@types/chai": "^4.1.3",
"@types/clean-stack": "^1.3.0",
Expand Down
3 changes: 0 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const actionType = (
const Action = actionType === 'spinner' ? require('./action/spinner').default : require('./action/simple').default

export class Config {
logLevel: Levels = 'warn'
outputLevel: Levels = 'info'
action: ActionBase = new Action()
errorsHandled = false
Expand All @@ -35,8 +34,6 @@ export class Config {
set debug(v: boolean) { globals.debug = v }
get context(): any { return globals.context || {} }
set context(v: any) { globals.context = v }
get errlog(): string | undefined { return globals.errlog }
set errlog(v: string | undefined) { globals.errlog = v }
}

function fetch() {
Expand Down
197 changes: 0 additions & 197 deletions src/errors.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/events.ts

This file was deleted.

52 changes: 27 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
import * as EventEmitter from 'events'
import * as Errors from '@oclif/errors'
import * as util from 'util'

import {ActionBase} from './action/base'
import {config, Config} from './config'
import deps from './deps'
import Errors, {CLIError, Options as ErrorOptions} from './errors'
import {ExitError} from './exit'
import * as Logger from './logger'
import Output from './output'
import {IPromptOptions} from './prompt'
import * as Table from './styled/table'

import {config, Config} from './config'

const e = new EventEmitter()
const output = Output(e)
const errors = Errors(e)
const logger = Logger.default(e)

export const cli = {
config,
trace: output('trace'),
debug: output('debug'),
info: output('info'),
log: output('info'),

warn: errors('warn'),
error: errors('error'),
fatal: errors('fatal'),

exit(code = 1, error?: Error) { throw new ExitError(code, error) },
warn: Errors.warn,
error: Errors.error,
exit: Errors.exit,

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

async done() {
config.action.stop()
await logger.flush()
// await flushStdout()
}
},

trace(format: string, ...args: string[]) {
if (this.config.outputLevel === 'trace') {
process.stdout.write(util.format(format, ...args) + '\n')
}
},

debug(format: string, ...args: string[]) {
if (['trace', 'debug'].includes(this.config.outputLevel)) {
process.stdout.write(util.format(format, ...args) + '\n')
}
},

info(format: string, ...args: string[]) {
process.stdout.write(util.format(format, ...args) + '\n')
},

log(format: string, ...args: string[]) {
this.info(format, ...args)
},
}
export default cli

export {
config,
ActionBase,
CLIError,
Config,
ErrorOptions,
Errors,
ExitError,
IPromptOptions,
Table,
Expand Down
67 changes: 0 additions & 67 deletions src/logger.ts

This file was deleted.

Loading

0 comments on commit 7ecdf46

Please sign in to comment.