执行过npm install命令的vue-element-admin源码
康凯
2022-05-20 aa4c235a8ca67ea8b731f90c951a465e92c0a865
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
'use strict';
const isPromise = require('is-promise');
const streamToObservable = require('@samverschueren/stream-to-observable');
const Subject = require('rxjs').Subject;
const renderer = require('./renderer');
const state = require('./state');
const utils = require('./utils');
const ListrError = require('./listr-error');
 
const defaultSkipFn = () => false;
 
class Task extends Subject {
    constructor(listr, task, options) {
        super();
 
        if (!task) {
            throw new TypeError('Expected a task');
        }
 
        if (typeof task.title !== 'string') {
            throw new TypeError(`Expected property \`title\` to be of type \`string\`, got \`${typeof task.title}\``);
        }
 
        if (typeof task.task !== 'function') {
            throw new TypeError(`Expected property \`task\` to be of type \`function\`, got \`${typeof task.task}\``);
        }
 
        if (task.skip && typeof task.skip !== 'function') {
            throw new TypeError(`Expected property \`skip\` to be of type \`function\`, got \`${typeof task.skip}\``);
        }
 
        if (task.enabled && typeof task.enabled !== 'function') {
            throw new TypeError(`Expected property \`enabled\` to be of type \`function\`, got \`${typeof task.enabled}\``);
        }
 
        this._listr = listr;
        this._options = options || {};
        this._subtasks = [];
        this._enabledFn = task.enabled;
        this._isEnabled = true;
 
        this.output = undefined;
        this.title = task.title;
        this.skip = task.skip || defaultSkipFn;
        this.task = task.task;
    }
 
    get subtasks() {
        return this._subtasks;
    }
 
    set state(state) {
        this._state = state;
 
        this.next({
            type: 'STATE'
        });
    }
 
    get state() {
        return state.toString(this._state);
    }
 
    check(ctx) {
        // Check if a task is enabled or disabled
        if (this._state === undefined && this._enabledFn) {
            const isEnabled = this._enabledFn(ctx);
 
            if (this._isEnabled !== isEnabled) {
                this._isEnabled = isEnabled;
 
                this.next({
                    type: 'ENABLED',
                    data: isEnabled
                });
            }
        }
    }
 
    hasSubtasks() {
        return this._subtasks.length > 0;
    }
 
    isPending() {
        return this._state === state.PENDING;
    }
 
    isSkipped() {
        return this._state === state.SKIPPED;
    }
 
    isCompleted() {
        return this._state === state.COMPLETED;
    }
 
    isEnabled() {
        return this._isEnabled;
    }
 
    hasFailed() {
        return this._state === state.FAILED;
    }
 
    run(context, wrapper) {
        const handleResult = result => {
            // Detect the subtask
            if (utils.isListr(result)) {
                result._options = Object.assign(this._options, result._options);
 
                result.exitOnError = result._options.exitOnError;
 
                result.setRenderer(renderer.getRenderer('silent'));
                this._subtasks = result.tasks;
 
                this.next({
                    type: 'SUBTASKS'
                });
 
                return result.run(context);
            }
 
            // Detect stream
            if (utils.isStream(result)) {
                result = streamToObservable(result);
            }
 
            // Detect Observable
            if (utils.isObservable(result)) {
                result = new Promise((resolve, reject) => {
                    result.subscribe({
                        next: data => {
                            this.output = data;
 
                            this.next({
                                type: 'DATA',
                                data
                            });
                        },
                        error: reject,
                        complete: resolve
                    });
                });
            }
 
            // Detect promise
            if (isPromise(result)) {
                return result.then(handleResult);
            }
 
            return result;
        };
 
        return Promise.resolve()
            .then(() => {
                this.state = state.PENDING;
                return this.skip(context);
            })
            .then(skipped => {
                if (skipped) {
                    if (typeof skipped === 'string') {
                        this.output = skipped;
                    }
                    this.state = state.SKIPPED;
                    return;
                }
 
                return handleResult(this.task(context, wrapper));
            })
            .then(() => {
                if (this.isPending()) {
                    this.state = state.COMPLETED;
                }
            })
            .catch(error => {
                this.state = state.FAILED;
 
                if (error instanceof ListrError) {
                    wrapper.report(error);
                    return;
                }
 
                if (!this.hasSubtasks()) {
                    // Do not show the message if we have subtasks as the error is already shown in the subtask
                    this.output = error.message;
                }
 
                this.next({
                    type: 'DATA',
                    data: error.message
                });
 
                wrapper.report(error);
 
                if (this._listr.exitOnError !== false) {
                    // Do not exit when explicitely set to `false`
                    throw error;
                }
            })
            .then(() => {
                // Mark the Observable as completed
                this.complete();
            });
    }
}
 
module.exports = Task;