listr.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.Listr = void 0;
  7. const p_map_1 = __importDefault(require("p-map"));
  8. const rxjs_1 = require("rxjs");
  9. const listr_interface_1 = require("./interfaces/listr.interface");
  10. const state_constants_1 = require("./interfaces/state.constants");
  11. const task_1 = require("./lib/task");
  12. const task_wrapper_1 = require("./lib/task-wrapper");
  13. const renderer_1 = require("./utils/renderer");
  14. class Listr {
  15. constructor(task, options) {
  16. var _a, _b, _c;
  17. this.task = task;
  18. this.options = options;
  19. this.tasks = [];
  20. this.err = [];
  21. this.renderHook$ = new rxjs_1.Subject();
  22. this.options = Object.assign({
  23. concurrent: false,
  24. renderer: 'default',
  25. nonTTYRenderer: 'verbose',
  26. exitOnError: true,
  27. registerSignalListeners: true
  28. }, options);
  29. this.concurrency = 1;
  30. if (this.options.concurrent === true) {
  31. this.concurrency = Infinity;
  32. }
  33. else if (typeof this.options.concurrent === 'number') {
  34. this.concurrency = this.options.concurrent;
  35. }
  36. const renderer = 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);
  37. this.rendererClass = renderer.renderer;
  38. if (!renderer.nonTTY) {
  39. this.rendererClassOptions = this.options.rendererOptions;
  40. }
  41. else {
  42. this.rendererClassOptions = this.options.nonTTYRendererOptions;
  43. }
  44. this.add(task || []);
  45. if (this.options.registerSignalListeners) {
  46. process
  47. .on('SIGINT', async () => {
  48. await Promise.all(this.tasks.map(async (task) => {
  49. if (task.isPending()) {
  50. task.state$ = state_constants_1.stateConstants.FAILED;
  51. }
  52. }));
  53. this.renderer.end(new Error('Interrupted.'));
  54. process.exit(127);
  55. })
  56. .setMaxListeners(0);
  57. }
  58. if ((_c = this.options) === null || _c === void 0 ? void 0 : _c.disableColor) {
  59. process.env.LISTR_DISABLE_COLOR = '1';
  60. }
  61. }
  62. add(task) {
  63. const tasks = Array.isArray(task) ? task : [task];
  64. tasks.forEach((task) => {
  65. this.tasks.push(new task_1.Task(this, task, this.options, { ...this.rendererClassOptions, ...task.options }));
  66. });
  67. }
  68. async run(context) {
  69. var _a;
  70. if (!this.renderer) {
  71. this.renderer = new this.rendererClass(this.tasks, this.rendererClassOptions, this.renderHook$);
  72. }
  73. this.renderer.render();
  74. context = context || ((_a = this.options) === null || _a === void 0 ? void 0 : _a.ctx) || Object.create({});
  75. const errors = [];
  76. await this.checkAll(context);
  77. try {
  78. await p_map_1.default(this.tasks, async (task) => {
  79. await this.checkAll(context);
  80. return this.runTask(task, context, errors);
  81. }, { concurrency: this.concurrency });
  82. this.renderer.end();
  83. }
  84. catch (error) {
  85. this.err.push(new listr_interface_1.ListrError(error, [error], context));
  86. if (this.options.exitOnError !== false) {
  87. this.renderer.end(error);
  88. throw error;
  89. }
  90. }
  91. finally {
  92. if (errors.length > 0) {
  93. this.err.push(new listr_interface_1.ListrError('Task failed without crashing.', errors, context));
  94. }
  95. }
  96. return context;
  97. }
  98. checkAll(context) {
  99. return Promise.all(this.tasks.map((task) => {
  100. task.check(context);
  101. }));
  102. }
  103. runTask(task, context, errors) {
  104. if (!task.isEnabled()) {
  105. return Promise.resolve();
  106. }
  107. return new task_wrapper_1.TaskWrapper(task, errors, this.options).run(context);
  108. }
  109. }
  110. exports.Listr = Listr;
  111. //# sourceMappingURL=listr.js.map