diff --git a/packages/core/src/Site/index.js b/packages/core/src/Site/index.js index 8530a1f7f2..356a8ccb49 100644 --- a/packages/core/src/Site/index.js +++ b/packages/core/src/Site/index.js @@ -1096,6 +1096,7 @@ class Site { const pagesCount = pageGenerationTasks.reduce((acc, task) => acc + task.pages.length, 0); const progressBar = new ProgressBar(`[:bar] :current / ${pagesCount} pages built`, { total: pagesCount }); progressBar.render(); + logger.setProgressBar(progressBar); const startTime = new Date(); let isCompleted = true; @@ -1113,6 +1114,7 @@ class Site { isCompleted = await this.generatePagesAsyncThrottled(task.pages, progressBar); } + logger.removeProgressBar(); this.siteLinkManager.validateAllIntralinks(); }); return isCompleted; diff --git a/packages/core/src/lib/progress/node-progress.js b/packages/core/src/lib/progress/node-progress.js index 3178c0eeed..5e6a726686 100644 --- a/packages/core/src/lib/progress/node-progress.js +++ b/packages/core/src/lib/progress/node-progress.js @@ -1,6 +1,6 @@ /* eslint-disable */ /* - * Patch for node-progress to fix display issue in certain terminals. + * Patch #1 for node-progress to fix display issue in certain terminals. * Issue related: https://github.com/MarkBind/markbind/issues/416 * The **only** changes are based on the following PR and its comments: * https://github.com/visionmedia/node-progress/pull/168 @@ -8,6 +8,12 @@ * to fix the issue for markbind's usecase. */ +/* + * Patch #2 for node-progress to allow logging without disturbing the progress bar. + * The changes are based on the following PR: + * https://github.com/visionmedia/node-progress/pull/155 + */ + /*! * node-progress * Copyright(c) 2011 TJ Holowaychuk @@ -57,7 +63,7 @@ exports = module.exports = ProgressBar; function ProgressBar(fmt, options) { this.stream = options.stream || process.stderr; - // patch + // patch #1 // options.forceTTY is undefined in git-bash on mintty Windows if (!process.stderr.isTTY) { var tty = require('tty').WriteStream.prototype; @@ -256,3 +262,25 @@ ProgressBar.prototype.terminate = function () { this.stream.write('\n'); } }; + +// patch #2 Add interruptBegin & interruptEnd +/** + * Begin a interrupt so the user can manually write messages above the bar. + * + * @api public + */ + +ProgressBar.prototype.interruptBegin = function () { + this.stream.clearLine(); + this.stream.cursorTo(0); +}; + +/** + * End a interrupt, rendering the last draw again. + * + * @api public + */ + +ProgressBar.prototype.interruptEnd = function () { + this.stream.write(this.lastDraw); +}; // end patch diff --git a/packages/core/src/utils/logger.ts b/packages/core/src/utils/logger.ts index f8f2dfd8b5..1d3a0be65a 100644 --- a/packages/core/src/utils/logger.ts +++ b/packages/core/src/utils/logger.ts @@ -1,6 +1,15 @@ import winston from 'winston'; -const consoleTransport = new (winston.transports.Console)({ +let progressBar: any; + +const setProgressBar = (bar: any) => { + progressBar = bar; +}; +const removeProgressBar = () => { + progressBar = null; +}; + +const consoleTransport = new winston.transports.Console({ colorize: true, handleExceptions: true, humanReadableUnhandledException: true, @@ -13,10 +22,48 @@ winston.configure({ transports: [consoleTransport], }); -export = { - error: winston.error, - warn: winston.warn, - info: winston.info, - verbose: winston.verbose, - debug: winston.debug, +// create a wrapper for error messages +const errorWrap = (input: any) => { + if (progressBar) { + progressBar.interruptBegin(); + winston.error(input); + progressBar.interruptEnd(); + } else { + winston.error(input); + } +}; + +// create a wrapper for warning messages +const warnWarp = (input: any) => { + if (progressBar) { + progressBar.interruptBegin(); + winston.warn(input); + progressBar.interruptEnd(); + } else { + winston.warn(input); + } +}; + +// create a wrapper for info messages +const infoWarp = (input: any) => { + if (progressBar) { + progressBar.interruptBegin(); + winston.info(input); + progressBar.interruptEnd(); + } else { + winston.info(input); + } +}; + +const { debug } = winston; +const { verbose } = winston; + +export { + errorWrap as error, + warnWarp as warn, + infoWarp as info, + verbose, + debug, + setProgressBar, + removeProgressBar, };