zhangjian
2023-05-30 dabbcc356af21f9f2f88ac69ff07994e6e32e4fc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TaskWrapper = void 0;
const through = require("through");
const clearline_regex_constants_1 = require("../constants/clearline-regex.constants");
const state_constants_1 = require("../constants/state.constants");
const listr_error_interface_1 = require("../interfaces/listr-error.interface");
const listr_1 = require("../listr");
const general_1 = require("../utils/general");
const prompt_1 = require("../utils/prompt");
/**
 * Extend the task to have more functionality while accesing from the outside.
 */
class TaskWrapper {
    constructor(task, errors, options) {
        this.task = task;
        this.errors = errors;
        this.options = options;
    }
    /** Change the title of the current task. */
    set title(data) {
        this.task.title$ = data;
    }
    /** Get the title of the current task. */
    get title() {
        return this.task.title;
    }
    /** Send a output to the output channel. */
    set output(data) {
        this.task.output$ = data;
    }
    /** Get the output from the output channel. */
    get output() {
        return this.task.output;
    }
    /** Create a new subtask with given renderer selection from the parent task. */
    newListr(task, options) {
        let tasks;
        if (typeof task === 'function') {
            tasks = task(this);
        }
        else {
            tasks = task;
        }
        return new listr_1.Listr(tasks, options);
    }
    /** Report a error in process for error collection. */
    report(error, type) {
        var _a, _b, _c;
        this.errors.push(new listr_error_interface_1.ListrError(error, type, (0, general_1.cloneObject)(this.task.listr.ctx), (0, general_1.cloneObject)(this.task)));
        this.task.message$ = { error: (_c = (_a = error.message) !== null && _a !== void 0 ? _a : (_b = this.task) === null || _b === void 0 ? void 0 : _b.title) !== null && _c !== void 0 ? _c : 'Task with no title.' };
    }
    /** Skip current task. */
    skip(message) {
        var _a, _b;
        this.task.state$ = state_constants_1.ListrTaskState.SKIPPED;
        if (message) {
            this.task.message$ = { skip: (_b = message !== null && message !== void 0 ? message : (_a = this.task) === null || _a === void 0 ? void 0 : _a.title) !== null && _b !== void 0 ? _b : 'Task with no title.' };
        }
    }
    /** Get the number of retrying, else returns false */
    isRetrying() {
        return this.task.isRetrying() ? this.task.retry : { count: 0 };
    }
    /**
     * Create a new Enquirer prompt using prompt options.
     *
     * Since process.stdout is controlled by Listr, this will passthrough all Enquirer data through internal stdout.
     */
    async prompt(options) {
        var _a;
        return prompt_1.createPrompt.bind(this)(options, { ...(_a = this.options) === null || _a === void 0 ? void 0 : _a.injectWrapper });
    }
    /** Cancels the current prompt attach to this task. */
    cancelPrompt(throwError = false) {
        return prompt_1.destroyPrompt.bind(this)(throwError);
    }
    /**
     * Pass stream of data to internal stdout.
     *
     * Since Listr2 takes control of process.stdout utilizing the default renderer, any data outputted to process.stdout
     * will corupt its looks.
     *
     * This returns a fake stream to pass any stream inside Listr as task data.
     */
    stdout() {
        return through((chunk) => {
            chunk = chunk.toString();
            chunk = chunk.replace(new RegExp(clearline_regex_constants_1.CLEAR_LINE_REGEX, 'gmi'), '');
            chunk = chunk.replace(new RegExp(clearline_regex_constants_1.BELL_REGEX, 'gmi'), '');
            if (chunk !== '') {
                this.output = chunk;
            }
        });
    }
    /** Run this task. */
    run(ctx) {
        return this.task.run(ctx, this);
    }
}
exports.TaskWrapper = TaskWrapper;