-
Notifications
You must be signed in to change notification settings - Fork 67
/
base.ts
201 lines (164 loc) · 4.7 KB
/
base.ts
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
import {inspect} from 'util'
import {castArray} from '../../util'
export interface ITask {
action: string;
status: string | undefined;
active: boolean;
}
export type ActionType = 'spinner' | 'simple' | 'debug'
export interface Options {
stdout?: boolean;
}
export class ActionBase {
type!: ActionType
std: 'stdout' | 'stderr' = 'stderr'
protected stdmocks?: ['stdout' | 'stderr', string[]][]
private stdmockOrigs = {
stdout: process.stdout.write,
stderr: process.stderr.write,
}
public start(action: string, status?: string, opts: Options = {}) {
this.std = opts.stdout ? 'stdout' : 'stderr'
const task = {action, status, active: Boolean(this.task && this.task.active)}
this.task = task
this._start()
task.active = true
this._stdout(true)
}
public stop(msg = 'done') {
const task = this.task
if (!task) {
return
}
this._stop(msg)
task.active = false
this.task = undefined
this._stdout(false)
}
private get globals(): { action: { task?: ITask }; output: string | undefined } {
(global as any)['cli-ux'] = (global as any)['cli-ux'] || {}
const globals = (global as any)['cli-ux']
globals.action = globals.action || {}
return globals
}
public get task(): ITask | undefined {
return this.globals.action.task
}
public set task(task: ITask | undefined) {
this.globals.action.task = task
}
protected get output(): string | undefined {
return this.globals.output
}
protected set output(output: string | undefined) {
this.globals.output = output
}
get running(): boolean {
return Boolean(this.task)
}
get status(): string | undefined {
return this.task ? this.task.status : undefined
}
set status(status: string | undefined) {
const task = this.task
if (!task) {
return
}
if (task.status === status) {
return
}
this._updateStatus(status, task.status)
task.status = status
}
public async pauseAsync<T extends any>(fn: () => Promise<T>, icon?: string): Promise<T> {
const task = this.task
const active = task && task.active
if (task && active) {
this._pause(icon)
this._stdout(false)
task.active = false
}
const ret = await fn()
if (task && active) {
this._resume()
}
return ret
}
public pause(fn: () => any, icon?: string) {
const task = this.task
const active = task && task.active
if (task && active) {
this._pause(icon)
this._stdout(false)
task.active = false
}
const ret = fn()
if (task && active) {
this._resume()
}
return ret
}
protected _start() {
throw new Error('not implemented')
}
protected _stop(_: string) {
throw new Error('not implemented')
}
protected _resume() {
if (this.task) this.start(this.task.action, this.task.status)
}
protected _pause(_?: string) {
throw new Error('not implemented')
}
protected _updateStatus(_: string | undefined, __?: string) {}
// mock out stdout/stderr so it doesn't screw up the rendering
protected _stdout(toggle: boolean) {
try {
const outputs: ['stdout', 'stderr'] = ['stdout', 'stderr']
if (toggle) {
if (this.stdmocks) return
this.stdmockOrigs = {
stdout: process.stdout.write,
stderr: process.stderr.write,
}
this.stdmocks = []
for (const std of outputs) {
(process[std] as any).write = (...args: any[]) => {
this.stdmocks!.push([std, args] as ['stdout' | 'stderr', string[]])
}
}
} else {
if (!this.stdmocks) return
// this._write('stderr', '\nresetstdmock\n\n\n')
delete this.stdmocks
for (const std of outputs) process[std].write = this.stdmockOrigs[std] as any
}
} catch (error) {
this._write('stderr', inspect(error))
}
}
// flush mocked stdout/stderr
protected _flushStdout() {
try {
let output = ''
let std: 'stdout' | 'stderr' | undefined
while (this.stdmocks && this.stdmocks.length > 0) {
const cur = this.stdmocks.shift() as ['stdout' | 'stderr', string[]]
std = cur[0]
this._write(std, cur[1])
output += (cur[1][0] as any).toString('utf8')
}
// add newline if there isn't one already
// otherwise we'll just overwrite it when we render
if (output && std && output[output.length - 1] !== '\n') {
this._write(std, '\n')
}
} catch (error) {
this._write('stderr', inspect(error))
}
}
// write to the real stdout/stderr
protected _write(std: 'stdout' | 'stderr', s: string | string[]) {
this.stdmockOrigs[std].apply(process[std], castArray(s) as [string])
}
}