simple.renderer.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SimpleRenderer = void 0;
  4. const log_update_1 = require("log-update");
  5. const os_1 = require("os");
  6. const event_constants_1 = require("../constants/event.constants");
  7. const colorette_1 = require("../utils/colorette");
  8. const figures_1 = require("../utils/figures");
  9. /**
  10. * This is the default renderer which is neither verbose or updating.
  11. * It provides short output like update renderer, but does not disturb
  12. * stdin during execution of listr tasks
  13. */
  14. class SimpleRenderer {
  15. constructor(tasks, options) {
  16. this.tasks = tasks;
  17. this.options = options;
  18. /**
  19. * Event type renderer map contains functions to process different task events
  20. */
  21. this.eventTypeRendererMap = {
  22. [event_constants_1.ListrEventType.SUBTASK]: (task) => {
  23. if (task.hasTitle()) {
  24. // if Task has subtasks where we want to log the group indication
  25. this.log(`${colorette_1.default.blue(figures_1.figures.pointer)} ${task.title}`);
  26. }
  27. if (task.hasSubtasks()) {
  28. this.render(task.subtasks);
  29. }
  30. },
  31. [event_constants_1.ListrEventType.STATE]: (task) => {
  32. if (task.isCompleted() && task.hasTitle()) {
  33. // The title is only logged at the end of the task execution
  34. this.log(`${colorette_1.default.green(figures_1.figures.tick)} ${task.title}`);
  35. }
  36. },
  37. [event_constants_1.ListrEventType.DATA]: (task, event) => {
  38. // ! This is where it gets dirty
  39. // * We want the prompt to stay visible after confirmation
  40. if (task.isPrompt() && !String(event.data).match(/^\n$/)) {
  41. (0, log_update_1.stderr)(`${event.data}`);
  42. }
  43. else {
  44. this.log(`${figures_1.figures.pointerSmall} ${event.data}`);
  45. }
  46. },
  47. [event_constants_1.ListrEventType.MESSAGE]: (task, event) => {
  48. if (event.data.error) {
  49. // error message
  50. const title = SimpleRenderer.formatTitle(task);
  51. this.log(`${colorette_1.default.red(figures_1.figures.cross)}${title}: ${event.data.error}`);
  52. }
  53. else if (event.data.skip) {
  54. // Skip message
  55. const title = SimpleRenderer.formatTitle(task);
  56. const skip = task.title !== event.data.skip ? `: ${event.data.skip}` : '';
  57. this.log(`${colorette_1.default.yellow(figures_1.figures.arrowDown)}${title} [${colorette_1.default.yellow(`skipped${skip}`)}]`);
  58. }
  59. else if (event.data.rollback) {
  60. // rollback message
  61. const title = SimpleRenderer.formatTitle(task);
  62. this.log(`${colorette_1.default.red(figures_1.figures.arrowLeft)}${title}: ${event.data.rollback}`);
  63. }
  64. else if (event.data.retry) {
  65. // Retry Message
  66. const title = SimpleRenderer.formatTitle(task);
  67. this.log(`[${colorette_1.default.yellow(`${event.data.retry.count}`)}]${title}`);
  68. }
  69. }
  70. // * We do not log out initial title. Only the final one.
  71. // [ListrEventType.TITLE]: (t, e) => this.renderTitle(t, e),
  72. };
  73. this.options = { ...SimpleRenderer.rendererOptions, ...options };
  74. }
  75. // This is used for mocks, since mocking Date is cumbesome
  76. static now() {
  77. return new Date();
  78. }
  79. // Used to sanitize title output
  80. static formatTitle(task) {
  81. return (task === null || task === void 0 ? void 0 : task.title) ? ` ${task.title}` : '';
  82. }
  83. // Writes sanitized output
  84. log(output) {
  85. const logOut = (msg) => {
  86. // Need appent \n to mimic console.log
  87. process[this.options.output].write(msg.endsWith(os_1.EOL) ? msg : `${msg}${os_1.EOL}`);
  88. };
  89. if (!this.options.prefixWithTimestamp) {
  90. logOut(`${output}`);
  91. return;
  92. }
  93. const now = SimpleRenderer.now();
  94. const timestamp = String(now.getHours()).padStart(2, '0') + ':' + String(now.getMinutes()).padStart(2, '0') + ':' + String(now.getSeconds()).padStart(2, '0');
  95. logOut(`${colorette_1.default.dim(`[${timestamp}]`)} ${output}`);
  96. }
  97. // eslint-disable-next-line
  98. end() { }
  99. // yes this is a misuse :)
  100. render(tasks) {
  101. if (tasks === null || tasks === void 0 ? void 0 : tasks.length) {
  102. tasks.forEach((task) => {
  103. task.subscribe((event) => {
  104. var _a, _b;
  105. // Here event type will match event.type anyway
  106. (_b = (_a = this.eventTypeRendererMap)[event.type]) === null || _b === void 0 ? void 0 : _b.call(_a, task, event);
  107. }, this.log);
  108. });
  109. }
  110. else {
  111. this.render(this.tasks);
  112. }
  113. }
  114. }
  115. exports.SimpleRenderer = SimpleRenderer;
  116. // Designate this renderer as tty or nonTTY
  117. SimpleRenderer.nonTTY = true;
  118. // designate your renderer options that will be showed inside the `ListrOptions` as rendererOptions
  119. SimpleRenderer.rendererOptions = { prefixWithTimestamp: false, output: 'stdout' };