index.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. const figures = require('figures');
  3. const cliCursor = require('cli-cursor');
  4. const utils = require('./lib/utils');
  5. const renderHelper = (task, event, options) => {
  6. const log = utils.log.bind(undefined, options);
  7. if (event.type === 'STATE') {
  8. const message = task.isPending() ? 'started' : task.state;
  9. log(`${task.title} [${message}]`);
  10. if (task.isSkipped() && task.output) {
  11. log(`${figures.arrowRight} ${task.output}`);
  12. }
  13. } else if (event.type === 'DATA') {
  14. // dont log anything on data events
  15. // log(`${figures.arrowRight} ${event.data}`);
  16. } else if (event.type === 'TITLE') {
  17. log(`${task.title} [title changed]`);
  18. }
  19. };
  20. const render = (tasks, options) => {
  21. for (const task of tasks) {
  22. task.subscribe(
  23. event => {
  24. if (event.type === 'SUBTASKS') {
  25. render(task.subtasks, options);
  26. return;
  27. }
  28. renderHelper(task, event, options);
  29. },
  30. err => {
  31. console.log(err);
  32. }
  33. );
  34. }
  35. };
  36. class VerboseRenderer {
  37. constructor(tasks, options) {
  38. this._tasks = tasks;
  39. this._options = Object.assign({
  40. dateFormat: 'HH:mm:ss'
  41. }, options);
  42. }
  43. static get nonTTY() {
  44. return true;
  45. }
  46. render() {
  47. cliCursor.hide();
  48. render(this._tasks, this._options);
  49. }
  50. end() {
  51. cliCursor.show();
  52. }
  53. }
  54. module.exports = VerboseRenderer;