UNPKG

@xarc/run

Version:

concurrent or serial run npm scripts, javascript tasks, and more

136 lines (119 loc) 4 kB
"use strict"; const chalk = require("chalk"); const logger = require("../logger"); const stringify = require("../stringify"); const assert = require("assert"); const defaults = require("../defaults"); const xsh = require("xsh"); class XReporterConsole { constructor(xrun) { this._xrun = xrun; xrun.on("execute", data => this._onExecute(data)); xrun.on("done-item", data => this._onDoneItem(data)); xrun.on("run", () => (this._sep = "")); xrun.on("not-found", err => { this._logger.log(err.message); }); xrun.once("warn-finally", () => { logger.log(chalk.yellow("NOTE: finally hook is unreliable when stopOnError is set to full")); }); this._tags = {}; this._logger = logger; } _log(qItem, msg) { this._logger.log(`${this._indent(qItem)}${msg}`); this._tags[qItem.id] = { sep: this._sep, msg }; } _indent(qItem, sep) { if (sep === undefined) { this._sep = this._sep === "." ? "-" : "."; sep = this._sep; } if (qItem.level) { return chalk.magenta(new Array(qItem.level + 1).join(sep)); } else { return ""; } } _onExecute(data) { const m = `_onExeType_${data.type.replace(/-/g, "_")}`; this[m](data); } _depMsg(qItem) { const depDee = qItem.value().depDee; const dep = depDee ? "'s dependency" : ""; return dep; } _onExeType_env(data) { const qItem = data.qItem; const name = this._itemDisplayName(qItem); const str = data.cmdVal.toString(); const msg = `Execute ${name} setting ${chalk.blue(str)}`; this._log(qItem, msg); } _onExeType_shell(data) { const qItem = data.qItem; const name = this._itemDisplayName(qItem); let cmd; if (data.cmdVal.constructor.name === "XTaskSpec") { cmd = xsh.pathCwd.replace(data.cmdVal.toString(data.cmd2), ".", "g"); } else { cmd = xsh.pathCwd.replace(data.cmd, ".", "g"); } const msg = data.anon ? `Execute ${chalk.cyan(cmd)}` : `Execute ${name}${this._depMsg(qItem)} ${chalk.blue(cmd)}`; this._log(qItem, msg); } _onExeType_lookup(_data) { // do nothing } _onExeType_serial_arr(data) { const qItem = data.qItem; const name = this._itemDisplayName(qItem); const ts = chalk.blue(stringify(data.array)); const msg = `Process ${name}${this._depMsg(qItem)} serial array ${ts}`; this._log(qItem, msg); } _onExeType_concurrent_arr(data) { const qItem = data.qItem; const name = this._itemDisplayName(qItem); const ts = chalk.blue(xsh.pathCwd.replace(stringify(data.array), ".", "g")); const msg = `Process ${name}${this._depMsg(qItem)} concurrent array ${ts}`; this._log(qItem, msg); } _onExeType_function(data) { const qItem = data.qItem; const name = this._itemDisplayName(qItem); const anon = qItem.anon ? " anonymous " : " as "; const msg = `Execute ${name}${this._depMsg(qItem)}${anon}function`; this._log(qItem, msg); } _itemDisplayName(qItem) { let ns = ""; const ix = qItem.name.indexOf(defaults.NS_SEP); if (ix < 0) { if (qItem.ns === defaults.NAMESPACE) { ns = chalk.dim.cyan(qItem.ns); } else if (qItem.ns) { ns = chalk.dim.cyan(`${qItem.ns}${defaults.NS_SEP}`); } } const finallyMsg = qItem.isFinally ? chalk.magenta(":finally") : ""; return `${ns}${chalk.cyan(qItem.name)}${finallyMsg}`; } _onDoneItem(data) { // const qItem = data.qItem; const xqItem = data.xqItem; const elapseStr = `(${logger.formatElapse(data.elapse)})`; const failed = !!this._xrun.failed || !!xqItem.err; const result = xqItem.err ? "Failed" : "Done"; const status = failed ? chalk.red(result) : chalk.green(result); const tag = this._tags[xqItem.id]; assert(tag, `console reporter no tag found for ${xqItem.name}`); this._logger.log( `${this._indent(xqItem, ">")}${status} ${tag.msg} ${chalk.magenta(elapseStr)}` ); } } module.exports = XReporterConsole;