From b6e193302e266a2754dc87a671674920a34e11c5 Mon Sep 17 00:00:00 2001 From: Jeff Dickey <216188+jdxcode@users.noreply.github.com> Date: Fri, 19 Jan 2018 14:28:39 -0800 Subject: [PATCH] feat: allow setting scope in warn/error/fatal call --- src/index.ts | 30 +++++++++++++++++++++++++----- test/logger.test.ts | 2 ++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 34c2fc12..83857783 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,16 +24,33 @@ let children: Rx.Observable 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 @@ -41,8 +58,11 @@ export class CLI { 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}) } diff --git a/test/logger.test.ts b/test/logger.test.ts index 094309c1..3c642a08 100644 --- a/test/logger.test.ts +++ b/test/logger.test.ts @@ -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 () => {