@ms-cloudpack/task-reporter
Advanced tools
Comparing version 0.9.0 to 0.10.0
export declare const ansiEscape = "\u001B["; | ||
export declare function setAnsiDisabled(disabled: boolean): void; | ||
export declare function stripAnsi(text: string): string; | ||
export declare function moveUpLines(count?: number): void; | ||
@@ -4,0 +5,0 @@ export declare function moveDownLines(count?: number): void; |
@@ -66,6 +66,2 @@ import { TaskReporterTask, type TaskReporterTaskResult } from './TaskReporterTask.js'; | ||
/** | ||
* Show tasks that are started above the sticky summary, as they start. | ||
*/ | ||
showStarted?: boolean; | ||
/** | ||
* Show tasks that are completed above the sticky summary, as they complete. | ||
@@ -113,2 +109,3 @@ */ | ||
setOptions(options: TaskReporterOptions): void; | ||
getOptions(): Readonly<TaskReporterOptions>; | ||
/** | ||
@@ -115,0 +112,0 @@ * Add a task to the reporter. If `pending` is true, the task will be initially |
@@ -24,4 +24,4 @@ /// <reference types="node" /> | ||
start(): void; | ||
complete({ status, message, details, extended, forceShow }?: TaskReporterTaskResult): void; | ||
complete(result?: TaskReporterTaskResult): void; | ||
} | ||
//# sourceMappingURL=TaskReporterTask.d.ts.map |
export declare const ansiEscape = "\u001B["; | ||
export declare function setAnsiDisabled(disabled: boolean): void; | ||
export declare function stripAnsi(text: string): string; | ||
export declare function moveUpLines(count?: number): void; | ||
@@ -4,0 +5,0 @@ export declare function moveDownLines(count?: number): void; |
import { write } from './write.js'; | ||
export const ansiEscape = '\x1b['; | ||
let isAnsiDisabled = process.argv.includes('--no-color') || process.argv.includes('-n') || process.env.NODE_ENV === 'test'; | ||
let isAnsiDisabled = process.argv.includes('--no-color') || process.argv.includes('-n') || !!process.env.JEST_WORKER_ID; | ||
const ansiCodes = { | ||
@@ -33,2 +33,6 @@ bold: `${ansiEscape}1m`, | ||
} | ||
export function stripAnsi(text) { | ||
// eslint-disable-next-line no-control-regex -- supposed to match a control character | ||
return text.replace(/\x1b\[[\d;]+(m|[AKB]\b)/g, ''); | ||
} | ||
/* | ||
@@ -35,0 +39,0 @@ * Moves the cursor up N lines. |
@@ -15,3 +15,2 @@ /** | ||
showProductInfo: false, | ||
showStarted: false, | ||
showSummary: false, | ||
@@ -40,5 +39,3 @@ showTaskDetails: false, | ||
showCompleted: true, | ||
showConsoleError: true, | ||
showConsoleInfo: true, | ||
showStarted: true, | ||
showTaskDetails: true, | ||
@@ -45,0 +42,0 @@ }; |
@@ -66,6 +66,2 @@ import { TaskReporterTask, type TaskReporterTaskResult } from './TaskReporterTask.js'; | ||
/** | ||
* Show tasks that are started above the sticky summary, as they start. | ||
*/ | ||
showStarted?: boolean; | ||
/** | ||
* Show tasks that are completed above the sticky summary, as they complete. | ||
@@ -113,2 +109,3 @@ */ | ||
setOptions(options: TaskReporterOptions): void; | ||
getOptions(): Readonly<TaskReporterOptions>; | ||
/** | ||
@@ -115,0 +112,0 @@ * Add a task to the reporter. If `pending` is true, the task will be initially |
@@ -44,2 +44,9 @@ import { Writer } from './Writer.js'; | ||
}; | ||
const consoleOverrides = { | ||
log: 'showConsoleLog', | ||
warn: 'showConsoleWarn', | ||
info: 'showConsoleInfo', | ||
error: 'showConsoleError', | ||
debug: 'showConsoleDebug', | ||
}; | ||
/** | ||
@@ -51,3 +58,5 @@ * A reporter for tasks. This is used to report the status of tasks to the console. | ||
this._ignoredLogMessages = []; | ||
this._options = options; | ||
// This must be set to an empty object initially so that if initial options are provided, | ||
// setOptions will print the product info. | ||
this._options = {}; | ||
this._writer = new Writer(); | ||
@@ -74,6 +83,9 @@ this._pendingTasks = new Set(); | ||
setOptions(options) { | ||
if (this._hasCompleted) { | ||
throw new Error(`TaskReporter has already completed.`); | ||
} | ||
const previousOptions = this._options; | ||
this._options = { ...options }; | ||
if (options.plainTextMode !== undefined) { | ||
setAnsiDisabled(!!options.plainTextMode); | ||
setAnsiDisabled(options.plainTextMode); | ||
} | ||
@@ -84,2 +96,5 @@ if (previousOptions.productName !== options.productName) { | ||
} | ||
getOptions() { | ||
return this._options; | ||
} | ||
/** | ||
@@ -90,4 +105,9 @@ * Add a task to the reporter. If `pending` is true, the task will be initially | ||
addTask(name, pending) { | ||
if (this._hasCompleted) { | ||
throw new Error(`TaskReporter has already completed.`); | ||
} | ||
const task = new TaskReporterTask(name); | ||
task.on('start', () => { | ||
if (this._hasCompleted) | ||
return; | ||
this._pendingTasks.delete(task); | ||
@@ -98,2 +118,4 @@ this._runningTasks.add(task); | ||
task.on('complete', () => { | ||
if (this._hasCompleted) | ||
return; | ||
this._runningTasks.delete(task); | ||
@@ -112,2 +134,3 @@ this._completedTasks.push(task); | ||
this._pendingTasks.add(task); | ||
this._updateStickies(); | ||
} | ||
@@ -117,7 +140,2 @@ else { | ||
this._runningTasks.add(task); | ||
} | ||
if (pending) { | ||
this._updateStickies(); | ||
} | ||
else { | ||
task.start(); | ||
@@ -144,2 +162,5 @@ } | ||
async runTask(name, execute) { | ||
if (this._hasCompleted) { | ||
throw new Error(`TaskReporter has already completed.`); | ||
} | ||
const task = this.addTask(name); | ||
@@ -149,13 +170,10 @@ let result; | ||
result = await execute(); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
task.complete(result); | ||
} | ||
catch (err) { | ||
const error = err; | ||
const message = typeof error.message === 'string' | ||
? error.message | ||
: typeof error.toString === 'function' | ||
? error.toString() | ||
: 'Unexpected exception occurred.'; | ||
task.complete({ status: 'fail', message }); | ||
task.complete({ | ||
status: 'fail', | ||
message: err.message || String(err), | ||
}); | ||
throw err; | ||
@@ -166,10 +184,15 @@ } | ||
complete(completeReason) { | ||
if (this._hasCompleted) { | ||
throw new Error(`TaskReporter has already completed.`); | ||
} | ||
// Complete all the pending and running tasks. The complete handler for each task (defined | ||
// in addTask) will add these to the array of completed tasks. | ||
this._pendingTasks.forEach((task) => task.complete({ status: 'skip', message: completeReason })); | ||
this._runningTasks.forEach((task) => task.complete({ status: 'abort', message: completeReason })); | ||
this._completedTasks = [...this._completedTasks, ...this._pendingTasks, ...this._runningTasks]; | ||
this._pendingTasks = new Set(); | ||
this._runningTasks = new Set(); | ||
this._pendingTasks.clear(); | ||
this._runningTasks.clear(); | ||
this._writer.stickies = []; | ||
this._completeReason = completeReason; | ||
this._reportSummary(); | ||
this._hasCompleted = true; | ||
} | ||
@@ -193,7 +216,6 @@ hasErrors() { | ||
}; | ||
console.log = createOverride('showConsoleLog', 'log'); | ||
console.warn = createOverride('showConsoleWarn', 'warn'); | ||
console.info = createOverride('showConsoleInfo', 'info'); | ||
console.error = createOverride('showConsoleError', 'error'); | ||
console.debug = createOverride('showConsoleDebug', 'debug'); | ||
for (const [key, overrideName] of Object.entries(consoleOverrides)) { | ||
const method = key; | ||
console[method] = createOverride(overrideName, method); | ||
} | ||
} | ||
@@ -249,29 +271,23 @@ _reportProductInfo() { | ||
// Report the final summary of pass/fail/skip/abort | ||
const summary = Array.from(this._completedTasks).reduce((prev, { status }) => { | ||
if (status) { | ||
prev[status] = (prev[status] || 0) + 1; | ||
} | ||
return prev; | ||
}, {}); | ||
const summary = {}; | ||
for (const { status } of this._completedTasks) { | ||
summary[status] = (summary[status] || 0) + 1; | ||
} | ||
const summaryMessage = [ | ||
summary.complete !== undefined && `${green(summary.complete)} tasks completed`, | ||
summary.pass !== undefined && `${green(summary.pass)} passed`, | ||
summary.fail !== undefined && `${red(summary.fail)} failed`, | ||
summary.abort !== undefined && `${yellow(summary.abort)} aborted`, | ||
summary.skip !== undefined && `${darkGrey(summary.skip)} skipped`, | ||
summary.complete && `${green(summary.complete)} tasks completed`, | ||
summary.pass && `${green(summary.pass)} passed`, | ||
summary.fail && `${red(summary.fail)} failed`, | ||
summary.abort && `${yellow(summary.abort)} aborted`, | ||
summary.skip && `${darkGrey(summary.skip)} skipped`, | ||
] | ||
.filter(Boolean) | ||
.join(', '); | ||
// Add a blank line before the summary | ||
this._writer.write(''); // Add a blank line before the summary | ||
if (this._completeReason) { | ||
this._writer.write(''); | ||
this._writer.write(this._completeReason); | ||
} | ||
else if (summaryMessage) { | ||
this._writer.write(''); | ||
this._writer.write(`Summary:\n\n${summaryMessage}`); | ||
} | ||
if (this._hasCompleted) { | ||
throw new Error(`TaskReporter has already completed.`); | ||
} | ||
this._hasCompleted = true; | ||
} | ||
@@ -278,0 +294,0 @@ _updateStickies() { |
@@ -24,4 +24,4 @@ /// <reference types="node" resolution-mode="require"/> | ||
start(): void; | ||
complete({ status, message, details, extended, forceShow }?: TaskReporterTaskResult): void; | ||
complete(result?: TaskReporterTaskResult): void; | ||
} | ||
//# sourceMappingURL=TaskReporterTask.d.ts.map |
@@ -14,3 +14,4 @@ import { EventEmitter } from 'events'; | ||
} | ||
complete({ status = 'complete', message, details, extended, forceShow } = {}) { | ||
complete(result = {}) { | ||
const { status = 'complete', message, details, extended, forceShow } = result; | ||
this.timeEnded = performance.now(); | ||
@@ -17,0 +18,0 @@ if (this.timeStarted) { |
{ | ||
"name": "@ms-cloudpack/task-reporter", | ||
"version": "0.9.0", | ||
"version": "0.10.0", | ||
"description": "Helpers for logging tasks to the console.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
244334
2335