listr.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Listr = void 0;
  4. const pMap = require("p-map");
  5. const rxjs_1 = require("rxjs");
  6. const state_constants_1 = require("./constants/state.constants");
  7. const task_1 = require("./lib/task");
  8. const task_wrapper_1 = require("./lib/task-wrapper");
  9. const renderer_1 = require("./utils/renderer");
  10. /**
  11. * Creates a new set of Listr2 task list.
  12. */
  13. class Listr {
  14. constructor(task, options) {
  15. var _a, _b, _c;
  16. this.task = task;
  17. this.options = options;
  18. this.tasks = [];
  19. this.err = [];
  20. this.renderHook$ = new rxjs_1.Subject();
  21. // assign over default options
  22. this.options = {
  23. ...{
  24. concurrent: false,
  25. renderer: 'default',
  26. nonTTYRenderer: 'verbose',
  27. exitOnError: true,
  28. exitAfterRollback: true,
  29. registerSignalListeners: true
  30. },
  31. ...options
  32. };
  33. // define parallel options
  34. if (this.options.concurrent === true) {
  35. this.concurrency = Infinity;
  36. }
  37. else if (typeof this.options.concurrent === 'number') {
  38. this.concurrency = this.options.concurrent;
  39. }
  40. else {
  41. this.concurrency = 1;
  42. }
  43. // get renderer class
  44. const renderer = (0, renderer_1.getRenderer)(this.options.renderer, this.options.nonTTYRenderer, (_a = this.options) === null || _a === void 0 ? void 0 : _a.rendererFallback, (_b = this.options) === null || _b === void 0 ? void 0 : _b.rendererSilent);
  45. this.rendererClass = renderer.renderer;
  46. // depending on the result pass the given options in
  47. if (!renderer.nonTTY) {
  48. this.rendererClassOptions = this.options.rendererOptions;
  49. }
  50. else {
  51. this.rendererClassOptions = this.options.nonTTYRendererOptions;
  52. }
  53. // parse and add tasks
  54. /* istanbul ignore next */
  55. this.add(task !== null && task !== void 0 ? task : []);
  56. // Graceful interrupt for render cleanup
  57. /* istanbul ignore if */
  58. if (this.options.registerSignalListeners) {
  59. process
  60. .once('SIGINT', () => {
  61. this.tasks.forEach(async (task) => {
  62. if (task.isPending()) {
  63. task.state$ = state_constants_1.ListrTaskState.FAILED;
  64. }
  65. });
  66. this.renderer.end(new Error('Interrupted.'));
  67. process.exit(127);
  68. })
  69. .setMaxListeners(0);
  70. }
  71. // disable color programatically for CI purposes
  72. /* istanbul ignore if */
  73. if ((_c = this.options) === null || _c === void 0 ? void 0 : _c.disableColor) {
  74. process.env.LISTR_DISABLE_COLOR = '1';
  75. }
  76. }
  77. add(task) {
  78. const tasks = Array.isArray(task) ? task : [task];
  79. tasks.forEach((task) => {
  80. this.tasks.push(new task_1.Task(this, task, this.options, { ...this.rendererClassOptions, ...task.options }));
  81. });
  82. }
  83. async run(context) {
  84. var _a, _b, _c;
  85. // start the renderer
  86. if (!this.renderer) {
  87. this.renderer = new this.rendererClass(this.tasks, this.rendererClassOptions, this.renderHook$);
  88. }
  89. this.renderer.render();
  90. // create a new context
  91. this.ctx = (_c = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.ctx) !== null && _b !== void 0 ? _b : context) !== null && _c !== void 0 ? _c : {};
  92. // check if the items are enabled
  93. await this.checkAll(this.ctx);
  94. // run tasks
  95. try {
  96. await pMap(this.tasks, async (task) => {
  97. // check this item is enabled, conditions may change depending on context
  98. await task.check(this.ctx);
  99. return this.runTask(task, this.ctx, this.err);
  100. }, { concurrency: this.concurrency });
  101. this.renderer.end();
  102. }
  103. catch (err) {
  104. if (this.options.exitOnError !== false) {
  105. this.renderer.end(err);
  106. // Do not exit when explicitly set to `false`
  107. throw err;
  108. }
  109. }
  110. return this.ctx;
  111. }
  112. checkAll(context) {
  113. return Promise.all(this.tasks.map((task) => task.check(context)));
  114. }
  115. runTask(task, context, errors) {
  116. if (!task.isEnabled()) {
  117. return Promise.resolve();
  118. }
  119. return new task_wrapper_1.TaskWrapper(task, errors, this.options).run(context);
  120. }
  121. }
  122. exports.Listr = Listr;