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

Commit

Permalink
feat: allow setting scope in warn/error/fatal call
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 19, 2018
1 parent 315ef62 commit b6e1933
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,45 @@ let children: Rx.Observable<any>

config.subscribe(subject)

export class CLI {
export interface ICLI {
fatal(input: Error | string, scope: string | string[], options?: IErrorOptions): void
fatal(input: Error | string, options?: IErrorOptions): void
error(input: Error | string, scope: string | string[], options?: IErrorOptions): void
error(input: Error | string, options?: IErrorOptions): void
warn(input: Error | string, scope?: string | string[]): void
}

function getScopeAndOpts(scopeOrOpts: string | string[] | undefined | IErrorOptions, options: IErrorOptions | undefined): {scope?: string, options: IErrorOptions} {
options = options || {}
if (typeof scopeOrOpts === 'string') return {scope: scopeOrOpts, options}
if (Array.isArray(scopeOrOpts)) return {scope: scopeOrOpts.join(':'), options}
if (scopeOrOpts) return {options: scopeOrOpts}
return {options}
}

export class CLI implements ICLI {
config: Config = config

get action(): ActionBase { return config.action }

constructor(public scope?: string) {}

error(input: Error | string, options: IErrorOptions = {}) {
error(input: Error | string, a?: string | string[] | IErrorOptions, b: IErrorOptions = {}) {
const {scope, options} = getScopeAndOpts(a, b)
const error = input instanceof Error ? input : new Error(input)
subject.next({type: 'error', scope: this.scope, severity: options.severity || 'error', error} as ErrorMessage)
subject.next({type: 'error', scope: scope || this.scope, severity: options.severity || 'error', error} as ErrorMessage)
const code = getExitCode(options)
if (code === false) return
let exitErr: ExitError = error as any
exitErr['cli-ux'] = exitErr['cli-ux'] || {exitCode: code}
throw exitErr
}

fatal(input: Error | string, options: IErrorOptions = {}) { this.error(input, {...options, severity: 'fatal'}) }
warn(input: Error | string) { this.error(input, {severity: 'warn', exit: false}) }
fatal(input: Error | string, a?: string | string[] | IErrorOptions, b: IErrorOptions = {}) {
const {scope, options} = getScopeAndOpts(a, b)
this.error(input, scope, {...options, severity: 'fatal'})
}
warn(input: Error | string, scope?: string | string[]) { this.error(input, scope, {severity: 'warn', exit: false}) }

log(...input: any[]) { this.info(...input) }
info(...input: any[]) { subject.next({type: 'output', scope: this.scope, severity: 'info', input}) }
Expand Down
2 changes: 2 additions & 0 deletions test/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ describe.stdout.stderr('logger', () => {
let cli = new CLI('mynewscope')
cli.warn('showwarning')
cli.info('hideme')
cli.warn('showotherwarning', ['myotherscope'])
cli.error('showerror', {exit: false})
await cli.done()
expect(fs.readFileSync(log, 'utf8')).to.contain(' WARN mynewscope showwarning')
expect(fs.readFileSync(log, 'utf8')).to.contain(' WARN myotherscope showotherwarning')
})

it('does not create file if no output', async () => {
Expand Down

0 comments on commit b6e1933

Please sign in to comment.