import * as readline from 'readline'; import * as Env from '../util/Env'; interface ResultData { readonly done: boolean; readonly id: string; readonly numFailed: number; readonly numSkipped: number; readonly numPassed: number; readonly test?: string; readonly totalFiles?: number; readonly totalTests?: number; } export interface Hud { readonly update: (data: ResultData) => Promise; readonly warn: (...messages: any[]) => void; readonly complete: () => Promise; } export const create = (testfiles: string[], loglevel: 'simple' | 'advanced'): Hud => { let started = false; const warn = (...messages: any[]): void => { // disable the next cursor movement so the message isn't overwritten started = false; console.warn(...messages); }; const stream = process.stdout; const numFiles = testfiles.length > 0 ? testfiles.length : '?'; const writeProgress = (id: string, stopped: boolean, numPassed: number, numSkipped: number, numFailed: number, total: number | '?') => { const numRun = numPassed + numFailed + numSkipped; total = total === '?' ? -Infinity : total; const status = stopped ? (numRun < total ? 'STOPPED' : 'COMPLETE') : 'RUNNING'; stream.write( 'Session: ' + id + ', Status: ' + status + ', Progress: ' + numRun + '/' + total + ', Failed: ' + numFailed + ', Skipped: ' + numSkipped + ' ... ' + '\n' ); if (!Env.IS_CI) { readline.clearLine(stream, 1); } return Promise.resolve(); }; const advUpdate = (data: ResultData) => { if (started && !Env.IS_CI) { // Note, this writes over the above line, which is why we only do this after the first update. readline.moveCursor(stream, 0, -2); } else { started = true; } if (!Env.IS_CI) { readline.clearLine(stream, 0); readline.cursorTo(stream, 0); } const currentTestMessage = 'Current test: ' + (data.test !== undefined ? data.test : 'Unknown') + '\n'; stream.write(currentTestMessage.substring(0, Env.IS_CI ? undefined : process.stdout.columns)); const totalFiles = data.totalFiles !== undefined ? data.totalFiles : numFiles; const totalTests = data.totalTests !== undefined ? data.totalTests : totalFiles; return writeProgress(data.id, data.done, data.numPassed, data.numSkipped, data.numFailed, totalTests); }; const complete = () => { return Promise.resolve(); }; const basicUpdate = (_data: ResultData) => { stream.write('.'); return Promise.resolve(); }; const supportsAdvanced = (() => { return readline.clearLine !== undefined && readline.moveCursor !== undefined && readline.cursorTo !== undefined; })(); return { update: loglevel === 'advanced' && supportsAdvanced ? advUpdate : basicUpdate, warn, complete }; };