default.renderer.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.DefaultRenderer = void 0;
  7. const cli_truncate_1 = __importDefault(require("cli-truncate"));
  8. const figures_1 = __importDefault(require("figures"));
  9. const indent_string_1 = __importDefault(require("indent-string"));
  10. const log_update_1 = __importDefault(require("log-update"));
  11. const chalk_1 = __importDefault(require("../utils/chalk"));
  12. class DefaultRenderer {
  13. constructor(tasks, options, renderHook$) {
  14. this.tasks = tasks;
  15. this.options = options;
  16. this.renderHook$ = renderHook$;
  17. this.bottomBar = {};
  18. this.spinner = process.platform === 'win32' && !process.env.WT_SESSION ? ['-', '\\', '|', '/'] : ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
  19. this.spinnerPosition = 0;
  20. this.options = { ...DefaultRenderer.rendererOptions, ...this.options };
  21. }
  22. getTaskOptions(task) {
  23. return { ...DefaultRenderer.rendererTaskOptions, ...task.rendererTaskOptions };
  24. }
  25. isBottomBar(task) {
  26. const bottomBar = this.getTaskOptions(task).bottomBar;
  27. return typeof bottomBar === 'number' && bottomBar !== 0 || typeof bottomBar === 'boolean' && bottomBar !== false;
  28. }
  29. hasPersistentOutput(task) {
  30. return this.getTaskOptions(task).persistentOutput === true;
  31. }
  32. hasTimer(task) {
  33. return this.getTaskOptions(task).showTimer === true;
  34. }
  35. getTaskTime(task) {
  36. const seconds = Math.floor(task.message.duration / 1000);
  37. const minutes = Math.floor(seconds / 60);
  38. let parsedTime;
  39. if (seconds === 0 && minutes === 0) {
  40. parsedTime = `0.${Math.floor(task.message.duration / 100)}s`;
  41. }
  42. if (seconds > 0) {
  43. parsedTime = `${seconds % 60}s`;
  44. }
  45. if (minutes > 0) {
  46. parsedTime = `${minutes}m${parsedTime}`;
  47. }
  48. return chalk_1.default.dim(`[${parsedTime}]`);
  49. }
  50. render() {
  51. var _a;
  52. if (this.id) {
  53. return;
  54. }
  55. const updateRender = () => log_update_1.default(this.multiLineRenderer(this.tasks), this.renderBottomBar(), this.renderPrompt());
  56. if (!((_a = this.options) === null || _a === void 0 ? void 0 : _a.lazy)) {
  57. this.id = setInterval(() => {
  58. this.spinnerPosition = ++this.spinnerPosition % this.spinner.length;
  59. updateRender();
  60. }, 100);
  61. }
  62. this.renderHook$.subscribe(() => {
  63. updateRender();
  64. });
  65. }
  66. end() {
  67. clearInterval(this.id);
  68. if (this.id) {
  69. this.id = undefined;
  70. }
  71. log_update_1.default(this.multiLineRenderer(this.tasks), this.renderBottomBar());
  72. if (this.options.clearOutput) {
  73. log_update_1.default.clear();
  74. }
  75. else {
  76. log_update_1.default.done();
  77. }
  78. }
  79. multiLineRenderer(tasks, level = 0) {
  80. let output = [];
  81. for (const task of tasks) {
  82. if (task.isEnabled()) {
  83. if (task.hasTitle()) {
  84. if (!(tasks.some((task) => task.hasFailed()) && !task.hasFailed() && task.options.exitOnError !== false && !(task.isCompleted() || task.isSkipped()))) {
  85. output = [...output, this.formatString(task.title, this.getSymbol(task), level)];
  86. }
  87. else {
  88. output = [...output, this.formatString(task.title, chalk_1.default.red(figures_1.default.main.squareSmallFilled), level)];
  89. }
  90. if (task.isSkipped() && this.options.collapseSkips) {
  91. task.title = !task.message.skip ? `${task.cleanTitle} ` : `${task.message.skip} ` + chalk_1.default.dim('[SKIPPED]');
  92. }
  93. else if (task.isSkipped() && this.options.collapseSkips === false) {
  94. output = [...output, ...this.dumpData(task, level, 'skip')];
  95. }
  96. }
  97. if (task === null || task === void 0 ? void 0 : task.output) {
  98. if (task.isPending() && task.isPrompt()) {
  99. this.promptBar = task.output;
  100. }
  101. else if (this.isBottomBar(task) || !task.hasTitle()) {
  102. const data = this.dumpData(task, -1);
  103. if (!this.bottomBar[task.id]) {
  104. this.bottomBar[task.id] = {};
  105. this.bottomBar[task.id].data = [];
  106. const bottomBar = this.getTaskOptions(task).bottomBar;
  107. if (typeof bottomBar === 'boolean') {
  108. this.bottomBar[task.id].items = 1;
  109. }
  110. else {
  111. this.bottomBar[task.id].items = bottomBar;
  112. }
  113. }
  114. if (!(data === null || data === void 0 ? void 0 : data.some((element) => this.bottomBar[task.id].data.includes(element))) && !task.isSkipped()) {
  115. this.bottomBar[task.id].data = [...this.bottomBar[task.id].data, ...data];
  116. }
  117. }
  118. else if (task.isPending() || this.hasPersistentOutput(task)) {
  119. output = [...output, ...this.dumpData(task, level)];
  120. }
  121. }
  122. if (this.options.showSubtasks !== false &&
  123. task.hasSubtasks() &&
  124. (task.isPending() ||
  125. task.hasFailed() ||
  126. task.isCompleted() && !task.hasTitle() ||
  127. task.isCompleted() && this.options.collapse === false && !task.subtasks.some((subtask) => subtask.rendererOptions.collapse === true) ||
  128. task.subtasks.some((subtask) => subtask.rendererOptions.collapse === false) ||
  129. task.subtasks.some((subtask) => subtask.hasFailed()))) {
  130. const subtaskLevel = !task.hasTitle() ? level : level + 1;
  131. const subtaskRender = this.multiLineRenderer(task.subtasks, subtaskLevel);
  132. if ((subtaskRender === null || subtaskRender === void 0 ? void 0 : subtaskRender.trim()) !== '' && !task.subtasks.every((subtask) => !subtask.hasTitle())) {
  133. output = [...output, subtaskRender];
  134. }
  135. }
  136. if (task.isCompleted() || task.hasFailed() || task.isSkipped()) {
  137. this.promptBar = null;
  138. if (!this.hasPersistentOutput(task)) {
  139. delete this.bottomBar[task.id];
  140. }
  141. if (task.isCompleted() && task.hasTitle() && (this.options.showTimer || this.hasTimer(task))) {
  142. task.title = `${task === null || task === void 0 ? void 0 : task.cleanTitle} ${this.getTaskTime(task)}`;
  143. }
  144. }
  145. }
  146. }
  147. if (output.length > 0) {
  148. return output.join('\n');
  149. }
  150. else {
  151. return;
  152. }
  153. }
  154. renderBottomBar() {
  155. if (Object.keys(this.bottomBar).length > 0) {
  156. this.bottomBar = Object.keys(this.bottomBar).reduce((o, key) => {
  157. if (!(o === null || o === void 0 ? void 0 : o[key])) {
  158. o[key] = {};
  159. }
  160. o[key] = this.bottomBar[key];
  161. this.bottomBar[key].data = this.bottomBar[key].data.slice(-this.bottomBar[key].items);
  162. o[key].data = this.bottomBar[key].data;
  163. return o;
  164. }, {});
  165. const returnRender = Object.values(this.bottomBar).reduce((o, value) => o = [...o, ...value.data], []);
  166. return ['\n', ...returnRender].join('\n');
  167. }
  168. }
  169. renderPrompt() {
  170. if (this.promptBar) {
  171. return `\n\n${this.promptBar}`;
  172. }
  173. }
  174. dumpData(task, level, source = 'output') {
  175. const output = [];
  176. let data;
  177. switch (source) {
  178. case 'output':
  179. data = task.output;
  180. break;
  181. case 'skip':
  182. data = task.message.skip;
  183. break;
  184. case 'error':
  185. data = task.message.error;
  186. break;
  187. }
  188. if (typeof data === 'string') {
  189. data
  190. .split('\n')
  191. .filter(Boolean)
  192. .forEach((line, i) => {
  193. const icon = i === 0 ? this.getSymbol(task, true) : ' ';
  194. output.push(this.formatString(line, icon, level + 1));
  195. });
  196. }
  197. return output;
  198. }
  199. formatString(string, icon, level) {
  200. var _a;
  201. return `${cli_truncate_1.default(indent_string_1.default(`${icon} ${string}`, level * this.options.indentation), (_a = process.stdout.columns) !== null && _a !== void 0 ? _a : 80)}`;
  202. }
  203. getSymbol(task, data = false) {
  204. var _a;
  205. if (task.isPending() && !data) {
  206. return ((_a = this.options) === null || _a === void 0 ? void 0 : _a.lazy) || this.options.showSubtasks !== false && task.hasSubtasks() && !task.subtasks.every((subtask) => !subtask.hasTitle())
  207. ? chalk_1.default.yellow(figures_1.default.main.pointer)
  208. : chalk_1.default.yellowBright(this.spinner[this.spinnerPosition]);
  209. }
  210. if (task.isCompleted() && !data) {
  211. if (task.hasSubtasks() && task.subtasks.some((subtask) => subtask.hasFailed())) {
  212. return chalk_1.default.yellow(figures_1.default.main.warning);
  213. }
  214. return chalk_1.default.green(figures_1.default.main.tick);
  215. }
  216. if (task.hasFailed() && !data) {
  217. return task.hasSubtasks() ? chalk_1.default.red(figures_1.default.main.pointer) : chalk_1.default.red(figures_1.default.main.cross);
  218. }
  219. if (task.isSkipped() && !data && this.options.collapseSkips === false) {
  220. return chalk_1.default.yellow(figures_1.default.main.warning);
  221. }
  222. else if (task.isSkipped() && (data || this.options.collapseSkips)) {
  223. return chalk_1.default.yellow(figures_1.default.main.arrowDown);
  224. }
  225. if (!data) {
  226. return chalk_1.default.dim(figures_1.default.main.squareSmallFilled);
  227. }
  228. else {
  229. return figures_1.default.main.pointerSmall;
  230. }
  231. }
  232. }
  233. exports.DefaultRenderer = DefaultRenderer;
  234. DefaultRenderer.nonTTY = false;
  235. DefaultRenderer.rendererOptions = {
  236. indentation: 2,
  237. clearOutput: false,
  238. showSubtasks: true,
  239. collapse: true,
  240. collapseSkips: true,
  241. lazy: false,
  242. showTimer: false
  243. };
  244. //# sourceMappingURL=default.renderer.js.map