-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathTaskContextImpl.ts
264 lines (230 loc) · 8.01 KB
/
TaskContextImpl.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// HACKHACK: copied from cli folder
import chalk from "chalk";
import { addPrefixToString } from "@fern-api/core-utils";
import { LogLevel, createLogger } from "@fern-api/logger";
import {
CreateInteractiveTaskParams,
FernCliError,
Finishable,
InteractiveTaskContext,
PosthogEvent,
Startable,
TaskContext,
TaskResult
} from "@fern-api/task-context";
import { Log } from "./Log";
import { logErrorMessage } from "./logErrorMessage";
export declare namespace TaskContextImpl {
export interface Init {
logImmediately: (logs: Log[]) => void;
takeOverTerminal: (run: () => void | Promise<void>) => Promise<void>;
logPrefix?: string;
/**
* called when this task or any subtask finishes
*/
onResult?: (result: TaskResult) => void;
shouldBufferLogs: boolean;
instrumentPostHogEvent: (event: PosthogEvent) => Promise<void>;
}
}
export class TaskContextImpl implements Startable<TaskContext>, Finishable, TaskContext {
protected result = TaskResult.Success;
protected logImmediately: (logs: Log[]) => void;
protected logPrefix: string;
protected subtasks: InteractiveTaskContextImpl[] = [];
private shouldBufferLogs: boolean;
private bufferedLogs: Log[] = [];
protected status: "notStarted" | "running" | "finished" = "notStarted";
private onResult: ((result: TaskResult) => void) | undefined;
private instrumentPostHogEventImpl: (event: PosthogEvent) => Promise<void>;
public constructor({
logImmediately,
logPrefix,
takeOverTerminal,
onResult,
shouldBufferLogs,
instrumentPostHogEvent
}: TaskContextImpl.Init) {
this.logImmediately = logImmediately;
this.logPrefix = logPrefix ?? "";
this.takeOverTerminal = takeOverTerminal;
this.onResult = onResult;
this.shouldBufferLogs = shouldBufferLogs;
this.instrumentPostHogEventImpl = instrumentPostHogEvent;
}
public start(): Finishable & TaskContext {
this.status = "running";
return this;
}
public isStarted(): boolean {
return this.status !== "notStarted";
}
public finish(): void {
this.status = "finished";
this.flushLogs();
this.onResult?.(this.getResult());
}
public isFinished(): boolean {
return this.status === "finished";
}
public takeOverTerminal: (run: () => void | Promise<void>) => Promise<void>;
public failAndThrow(message?: string, error?: unknown): never {
this.failWithoutThrowing(message, error);
this.finish();
throw new FernCliError();
}
public failWithoutThrowing(message?: string, error?: unknown): void {
logErrorMessage({ message, error, logger: this.logger });
this.result = TaskResult.Failure;
}
public getResult(): TaskResult {
return this.result;
}
public async instrumentPostHogEvent(event: PosthogEvent): Promise<void> {
this.instrumentPostHogEventImpl(event);
}
protected logAtLevel(level: LogLevel, ...parts: string[]): void {
this.logAtLevelWithOverrides(level, parts);
}
protected logAtLevelWithOverrides(level: LogLevel, parts: string[], overrides: Pick<Log, "omitOnTTY"> = {}): void {
this.log({
parts,
level,
time: new Date(),
...overrides
});
}
protected log(log: Log): void {
this.bufferedLogs.push({
...log,
prefix: this.logPrefix
});
if (!this.shouldBufferLogs) {
this.flushLogs();
}
}
protected flushLogs(): void {
this.logImmediately(this.bufferedLogs);
this.bufferedLogs = [];
}
public readonly logger = createLogger(this.logAtLevel.bind(this));
public addInteractiveTask({ name, subtitle }: CreateInteractiveTaskParams): Startable<InteractiveTaskContext> {
const subtask = new InteractiveTaskContextImpl({
name,
subtitle,
logImmediately: (content) => this.logImmediately(content),
logPrefix: `${this.logPrefix}${chalk.blackBright(name)} `,
takeOverTerminal: this.takeOverTerminal,
onResult: this.onResult,
shouldBufferLogs: this.shouldBufferLogs,
instrumentPostHogEvent: async (event) => await this.instrumentPostHogEventImpl(event)
});
this.subtasks.push(subtask);
return subtask;
}
public async runInteractiveTask(
params: CreateInteractiveTaskParams,
run: (context: InteractiveTaskContext) => void | Promise<void>
): Promise<boolean> {
const subtask = this.addInteractiveTask(params).start();
try {
await run(subtask);
} catch (error) {
subtask.failWithoutThrowing(undefined, error);
} finally {
subtask.finish();
}
return subtask.getResult() === TaskResult.Success;
}
public printInteractiveTasks({ spinner }: { spinner: string }): string {
return this.subtasks.map((subtask) => subtask.print({ spinner })).join("\n");
}
}
/**
* InteractiveTaskContextImpl is defined in this file because
* InteractiveTaskContextImpl and TaskContextImpl are co-dependent
*/
export declare namespace InteractiveTaskContextImpl {
export interface Init extends TaskContextImpl.Init {
name: string;
subtitle: string | undefined;
}
}
export class InteractiveTaskContextImpl
extends TaskContextImpl
implements Startable<InteractiveTaskContext>, Finishable, InteractiveTaskContext
{
private name: string;
private subtitle: string | undefined;
constructor({ name, subtitle, ...superArgs }: InteractiveTaskContextImpl.Init) {
super(superArgs);
this.name = name;
this.subtitle = subtitle;
}
public start(): Finishable & InteractiveTaskContext {
super.start();
this.logAtLevelWithOverrides(LogLevel.Info, ["Started."], {
omitOnTTY: true
});
this.flushLogs();
return this;
}
public isStarted(): boolean {
return this.status !== "notStarted";
}
public finish(): void {
if (this.result === TaskResult.Success) {
this.logAtLevelWithOverrides(LogLevel.Info, ["Finished."], {
omitOnTTY: true
});
} else {
this.logAtLevelWithOverrides(LogLevel.Error, ["Failed."], {
omitOnTTY: true
});
}
super.finish();
}
public setSubtitle(subtitle: string | undefined): void {
this.subtitle = subtitle;
}
public print({ spinner }: { spinner: string }): string {
const lines = [this.name];
if (this.subtitle != null) {
lines.push(chalk.dim(this.subtitle));
}
lines.push(...this.subtasks.map((subtask) => subtask.print({ spinner })));
return addPrefixToString({
prefix: `${this.getIcon({ spinner }).padEnd(spinner.length)} `,
content: lines.join("\n")
});
}
public printInteractiveTasks({ spinner }: { spinner: string }): string {
return this.print({ spinner });
}
private getIcon({ spinner }: { spinner: string }): string {
switch (this.status) {
case "notStarted":
return chalk.dim("◦");
case "running":
return spinner;
case "finished":
switch (this.getResult()) {
case TaskResult.Success:
return chalk.green("✓");
case TaskResult.Failure:
return chalk.red("x");
}
}
}
public getResult(): TaskResult {
if (this.result === TaskResult.Failure) {
return TaskResult.Failure;
}
for (const subtask of this.subtasks) {
if (subtask.getResult() === TaskResult.Failure) {
return TaskResult.Failure;
}
}
return TaskResult.Success;
}
}