task-wrapper.js 975 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. 'use strict';
  2. const state = require('./state');
  3. const ListrError = require('./listr-error');
  4. class TaskWrapper {
  5. constructor(task, errors) {
  6. this._task = task;
  7. this._errors = errors;
  8. }
  9. set title(title) {
  10. this._task.title = title;
  11. this._task.next({
  12. type: 'TITLE',
  13. data: title
  14. });
  15. }
  16. set output(data) {
  17. this._task.output = data;
  18. this._task.next({
  19. type: 'DATA',
  20. data
  21. });
  22. }
  23. get title() {
  24. return this._task.title;
  25. }
  26. report(error) {
  27. if (error instanceof ListrError) {
  28. for (const err of error.errors) {
  29. this._errors.push(err);
  30. }
  31. } else {
  32. this._errors.push(error);
  33. }
  34. }
  35. skip(message) {
  36. if (message && typeof message !== 'string') {
  37. throw new TypeError(`Expected \`message\` to be of type \`string\`, got \`${typeof message}\``);
  38. }
  39. if (message) {
  40. this._task.output = message;
  41. }
  42. this._task.state = state.SKIPPED;
  43. }
  44. run(ctx) {
  45. return this._task.run(ctx, this);
  46. }
  47. }
  48. module.exports = TaskWrapper;