index.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use strict';
  2. const logUpdate = require('log-update');
  3. const chalk = require('chalk');
  4. const figures = require('figures');
  5. const indentString = require('indent-string');
  6. const cliTruncate = require('cli-truncate');
  7. const stripAnsi = require('strip-ansi');
  8. const utils = require('./lib/utils');
  9. const renderHelper = (tasks, options, level) => {
  10. level = level || 0;
  11. let output = [];
  12. for (const task of tasks) {
  13. if (task.isEnabled()) {
  14. const skipped = task.isSkipped() ? ` ${chalk.dim('[skipped]')}` : '';
  15. output.push(indentString(` ${utils.getSymbol(task, options)} ${task.title}${skipped}`, level, ' '));
  16. if ((task.isPending() || task.isSkipped() || task.hasFailed()) && utils.isDefined(task.output)) {
  17. let data = task.output;
  18. if (typeof data === 'string') {
  19. data = stripAnsi(data.trim().split('\n').filter(Boolean).pop());
  20. if (data === '') {
  21. data = undefined;
  22. }
  23. }
  24. if (utils.isDefined(data)) {
  25. const out = indentString(`${figures.arrowRight} ${data}`, level, ' ');
  26. output.push(` ${chalk.gray(cliTruncate(out, process.stdout.columns - 3))}`);
  27. }
  28. }
  29. if ((task.isPending() || task.hasFailed() || options.collapse === false) && (task.hasFailed() || options.showSubtasks !== false) && task.subtasks.length > 0) {
  30. output = output.concat(renderHelper(task.subtasks, options, level + 1));
  31. }
  32. }
  33. }
  34. return output.join('\n');
  35. };
  36. const render = (tasks, options) => {
  37. logUpdate(renderHelper(tasks, options));
  38. };
  39. class UpdateRenderer {
  40. constructor(tasks, options) {
  41. this._tasks = tasks;
  42. this._options = Object.assign({
  43. showSubtasks: true,
  44. collapse: true,
  45. clearOutput: false
  46. }, options);
  47. }
  48. render() {
  49. if (this._id) {
  50. // Do not render if we are already rendering
  51. return;
  52. }
  53. this._id = setInterval(() => {
  54. render(this._tasks, this._options);
  55. }, 100);
  56. }
  57. end(err) {
  58. if (this._id) {
  59. clearInterval(this._id);
  60. this._id = undefined;
  61. }
  62. render(this._tasks, this._options);
  63. if (this._options.clearOutput && err === undefined) {
  64. logUpdate.clear();
  65. } else {
  66. logUpdate.done();
  67. }
  68. }
  69. }
  70. module.exports = UpdateRenderer;