index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. log(`${figures.arrowRight} ${event.data}`);
  15. } else if (event.type === 'TITLE') {
  16. log(`${task.title} [title changed]`);
  17. }
  18. };
  19. const render = (tasks, options) => {
  20. for (const task of tasks) {
  21. task.subscribe(
  22. event => {
  23. if (event.type === 'SUBTASKS') {
  24. render(task.subtasks, options);
  25. return;
  26. }
  27. renderHelper(task, event, options);
  28. },
  29. err => {
  30. console.log(err);
  31. }
  32. );
  33. }
  34. };
  35. class VerboseRenderer {
  36. constructor(tasks, options) {
  37. this._tasks = tasks;
  38. this._options = Object.assign({
  39. dateFormat: 'HH:mm:ss'
  40. }, options);
  41. }
  42. static get nonTTY() {
  43. return true;
  44. }
  45. render() {
  46. cliCursor.hide();
  47. render(this._tasks, this._options);
  48. }
  49. end() {
  50. cliCursor.show();
  51. }
  52. }
  53. module.exports = VerboseRenderer;