Comparing version 1.1.1 to 1.1.2
import { Base } from './base'; | ||
export interface IWarnOptions { | ||
prefix?: string; | ||
export interface IErrorOptions { | ||
exitCode?: number | false; | ||
severity: 'warn' | 'fatal' | 'error'; | ||
context?: string; | ||
} | ||
export declare class Errors extends Base { | ||
error(err: Error | string, exitCode?: number | false): void; | ||
warn(err: Error | string, options?: IWarnOptions): void; | ||
handleUnhandleds(): void; | ||
error(err: Error | string, options: Partial<IErrorOptions> & { | ||
exitCode: false; | ||
}): void; | ||
error(err: Error | string, options?: Partial<IErrorOptions>): never; | ||
fatal(err: Error | string, options?: Partial<IErrorOptions>): void; | ||
warn(err: Error | string, options?: Partial<IErrorOptions>): void; | ||
exit(code?: number): void; | ||
private logError(err); | ||
} |
@@ -8,3 +8,3 @@ "use strict"; | ||
const stream_1 = require("./stream"); | ||
const arrow = process.platform === 'win32' ? '!' : '▸'; | ||
const arrow = process.platform === 'win32' ? ' !' : ' ▸'; | ||
function bangify(msg, c) { | ||
@@ -14,3 +14,3 @@ const lines = msg.split('\n'); | ||
const line = lines[i]; | ||
lines[i] = ' ' + c + line.substr(2, line.length); | ||
lines[i] = c + line.substr(2, line.length); | ||
} | ||
@@ -48,15 +48,32 @@ return lines.join('\n'); | ||
class Errors extends base_1.Base { | ||
error(err, exitCode = 1) { | ||
if (this.options.mock && typeof err !== 'string' && exitCode !== false) | ||
handleUnhandleds() { | ||
process.on('unhandledRejection', (reason, p) => { | ||
this.fatal(reason, { context: 'Promise unhandledRejection' }); | ||
}); | ||
process.on('uncaughtException', error => { | ||
this.fatal(error, { context: 'Error uncaughtException' }); | ||
}); | ||
} | ||
error(err, options) { | ||
options = options || {}; | ||
if (options.exitCode === undefined) | ||
options.exitCode = 1; | ||
if (options.severity !== 'warn' && this.options.mock && typeof err !== 'string' && options.exitCode !== false) | ||
throw err; | ||
try { | ||
if (typeof err === 'string') { | ||
if (typeof err === 'string') | ||
err = new Error(err); | ||
} | ||
const prefix = options.context ? `${options.context}: ` : ''; | ||
this.logError(err); | ||
if (this.options.debug) { | ||
this.stderr.write(`${options.severity.toUpperCase()}: ${prefix}`); | ||
this.stderr.log(err.stack || util.inspect(err)); | ||
} | ||
else { | ||
this.stderr.log(bangify(wrap(getErrorMessage(err)), deps_1.deps.chalk.red(arrow))); | ||
let bang = deps_1.deps.chalk.red(arrow); | ||
if (options.severity === 'fatal') | ||
bang = deps_1.deps.chalk.bgRed.bold.white(' FATAL '); | ||
if (options.severity === 'warn') | ||
bang = deps_1.deps.chalk.yellow(arrow); | ||
this.stderr.log(bangify(wrap(prefix + getErrorMessage(err)), bang)); | ||
} | ||
@@ -69,24 +86,13 @@ } | ||
} | ||
if (exitCode !== false) { | ||
this.exit(exitCode); | ||
} | ||
if (options.exitCode !== false) | ||
this.exit(options.exitCode); | ||
} | ||
fatal(err, options = {}) { | ||
options.severity = 'fatal'; | ||
this.error(err, options); | ||
} | ||
warn(err, options = {}) { | ||
try { | ||
const prefix = options.prefix ? `${options.prefix} ` : ''; | ||
err = typeof err === 'string' ? new Error(err) : err; | ||
this.logError(err); | ||
if (this.options.debug) { | ||
this.stderr.write(`WARNING: ${prefix}`); | ||
this.stderr.log(err.stack || util.inspect(err)); | ||
} | ||
else { | ||
this.stderr.log(bangify(wrap(prefix + getErrorMessage(err)), deps_1.deps.chalk.yellow(arrow))); | ||
} | ||
} | ||
catch (e) { | ||
console.error('error displaying warning'); | ||
console.error(e); | ||
console.error(err); | ||
} | ||
options.exitCode = false; | ||
options.severity = 'warn'; | ||
this.error(err, options); | ||
} | ||
@@ -101,9 +107,8 @@ exit(code = 0) { | ||
else { | ||
process.exit(code); | ||
// process.exit(code) | ||
} | ||
} | ||
logError(err) { | ||
if (!this.options.errlog) { | ||
if (!this.options.errlog) | ||
return; | ||
} | ||
stream_1.StreamOutput.logToFile(util.inspect(err) + '\n', this.options.errlog); | ||
@@ -110,0 +115,0 @@ } |
import { ActionBase } from './action/base'; | ||
import { IWarnOptions } from './errors'; | ||
import { IErrorOptions } from './errors'; | ||
import { IPromptOptions } from './prompt'; | ||
@@ -16,9 +16,9 @@ import { StreamOutput } from './stream'; | ||
stderr: StreamOutput; | ||
private errorsDep; | ||
private promptDep; | ||
private _errors; | ||
private _prompt; | ||
constructor(options?: IOptions); | ||
prompt(name: string, options?: IPromptOptions): Promise<any>; | ||
log(data: string, ...args: any[]): void; | ||
warn(err: Error | string, options?: IWarnOptions): void; | ||
error(err: Error | string, exitCode?: number | false): void; | ||
warn(err: Error | string, options?: Partial<IErrorOptions>): void; | ||
error(err: Error | string, options?: Partial<IErrorOptions>): void; | ||
exit(code?: number): void; | ||
@@ -29,4 +29,11 @@ table(data: any[], options: Partial<TableOptions>): any; | ||
styledObject(obj: any, keys: string[]): void; | ||
/** | ||
* puts in a handler for process.on('uncaughtException') and process.on('unhandledRejection') | ||
*/ | ||
handleUnhandleds(): void; | ||
/** | ||
* cleanup any outstanding output like actions that need to be stopped | ||
*/ | ||
done(): void; | ||
} | ||
export declare const cli: CLI; |
@@ -10,2 +10,3 @@ "use strict"; | ||
const deps_1 = require("./deps"); | ||
const debug = require('debug')('cli-ux'); | ||
class CLI { | ||
@@ -17,3 +18,3 @@ constructor(options = {}) { | ||
const depOpts = { | ||
debug: !!options.debug, | ||
debug: options.debug || (options.debug === undefined && debug.enabled), | ||
mock: !!options.mock, | ||
@@ -23,4 +24,4 @@ stderr: this.stderr, | ||
}; | ||
this.errorsDep = new errors_1.Errors(depOpts); | ||
this.promptDep = new prompt_1.Prompt(depOpts); | ||
this._errors = new errors_1.Errors(depOpts); | ||
this._prompt = new prompt_1.Prompt(depOpts); | ||
this.action = base_1.shouldDisplaySpinner(depOpts) ? new spinner_1.SpinnerAction(depOpts) : new simple_1.SimpleAction(depOpts); | ||
@@ -32,3 +33,3 @@ if (this.options.mock || !process.stderr.isTTY || !process.stdout.isTTY) | ||
return this.action.pauseAsync(() => { | ||
return this.promptDep.prompt(name, options); | ||
return this._prompt.prompt(name, options); | ||
}, deps_1.deps.chalk.cyan('?')); | ||
@@ -43,12 +44,12 @@ } | ||
this.action.pause(() => { | ||
return this.errorsDep.warn(err, options); | ||
return this._errors.warn(err, options); | ||
}, deps_1.deps.chalk.bold.yellow('!')); | ||
} | ||
error(err, exitCode = 1) { | ||
error(err, options = {}) { | ||
this.action.pause(() => { | ||
return this.errorsDep.error(err, exitCode); | ||
return this._errors.error(err, options); | ||
}, deps_1.deps.chalk.bold.red('!')); | ||
} | ||
exit(code = 1) { | ||
this.errorsDep.exit(code); | ||
this._errors.exit(code); | ||
} | ||
@@ -108,2 +109,11 @@ table(data, options) { | ||
} | ||
/** | ||
* puts in a handler for process.on('uncaughtException') and process.on('unhandledRejection') | ||
*/ | ||
handleUnhandleds() { | ||
this._errors.handleUnhandleds(); | ||
} | ||
/** | ||
* cleanup any outstanding output like actions that need to be stopped | ||
*/ | ||
done() { | ||
@@ -110,0 +120,0 @@ this.action.stop(); |
@@ -23,4 +23,5 @@ "use strict"; | ||
write(msg, options = {}) { | ||
// const log = options.log !== false | ||
// if (log) this.writeLogFile(msg, this.constructor.startOfLine) | ||
const log = options.log !== false; | ||
if (log) | ||
this.writeLogFile(msg, StreamOutput.startOfLine); | ||
// conditionally show timestamp if configured to display | ||
@@ -27,0 +28,0 @@ if (StreamOutput.startOfLine && this.displayTimestamps) { |
{ | ||
"name": "cli-ux", | ||
"description": "set of CLI output utilities", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"author": "Jeff Dickey", | ||
@@ -6,0 +6,0 @@ "bugs": "https://github.com/jdxcode/cli-ux/issues", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46977
1410