manager.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Manager = void 0;
  4. const listr_1 = require("./listr");
  5. /**
  6. * Creates a new Listr2 task manager.
  7. *
  8. * Useful for creating a single instace of Listr2 with pre-set settings.
  9. */
  10. class Manager {
  11. constructor(options) {
  12. this.options = options;
  13. this.err = [];
  14. this.tasks = [];
  15. }
  16. set ctx(ctx) {
  17. this.options.ctx = ctx;
  18. }
  19. add(tasks, options) {
  20. options = { ...this.options, ...options };
  21. this.tasks = [...this.tasks, this.indent(tasks, options)];
  22. }
  23. async runAll(options) {
  24. options = { ...this.options, ...options };
  25. const ctx = await this.run(this.tasks, options);
  26. // clear out queues
  27. this.tasks = [];
  28. return ctx;
  29. }
  30. newListr(tasks, options) {
  31. return new listr_1.Listr(tasks, options);
  32. }
  33. indent(tasks, options, taskOptions) {
  34. options = { ...this.options, ...options };
  35. let newTask;
  36. // type function or directly
  37. if (typeof tasks === 'function') {
  38. newTask = {
  39. ...taskOptions,
  40. task: (ctx) => this.newListr(tasks(ctx), options)
  41. };
  42. }
  43. else {
  44. newTask = {
  45. ...taskOptions,
  46. task: () => this.newListr(tasks, options)
  47. };
  48. }
  49. return newTask;
  50. }
  51. async run(tasks, options) {
  52. options = { ...this.options, ...options };
  53. // create task
  54. const task = this.newListr(tasks, options);
  55. // run task
  56. const ctx = await task.run();
  57. // reset error queue
  58. this.err = task.err;
  59. return ctx;
  60. }
  61. // general utils
  62. /* istanbul ignore next */
  63. getRuntime(pipetime) {
  64. return `${Math.round(Date.now() - pipetime) / 1000}s`;
  65. }
  66. }
  67. exports.Manager = Manager;