Skip to content

Commit f6563de

Browse files
committed
fix: Fixed display functions to be async
1 parent d6eb8d4 commit f6563de

12 files changed

Lines changed: 49 additions & 41 deletions

File tree

src/common/base-command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export abstract class BaseCommand extends Command {
147147
protected async catch(err: Error): Promise<void> {
148148
if (err instanceof PluginError && this.reporter) {
149149
await this.reporter.hide();
150-
this.reporter.displayPluginError(err);
150+
await this.reporter.displayPluginError(err);
151151
process.exit(1);
152152
}
153153

src/orchestrators/destroy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class DestroyOrchestrator {
4242
plan.sortByEvalOrder(project.evaluationOrder);
4343
destroyProject.removeNoopFromEvaluationOrder(plan);
4444

45-
reporter.displayPlan(plan);
45+
await reporter.displayPlan(plan);
4646

4747
// Short circuit and exit if every change is NOOP
4848
if (plan.isEmpty()) {

src/orchestrators/import.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class ImportOrchestrator {
116116

117117
ctx.processFinished(ProcessName.IMPORT)
118118

119-
reporter.displayImportResult(importResult, false);
119+
await reporter.displayImportResult(importResult, false);
120120

121121
resourceInfoList.push(...(await pluginManager.getMultipleResourceInfo(
122122
project.resourceConfigs.map((r) => r.type)
@@ -194,7 +194,7 @@ export class ImportOrchestrator {
194194
}
195195

196196
// No writes
197-
reporter.displayImportResult(importResult, true);
197+
await reporter.displayImportResult(importResult, true);
198198
await reporter.displayMessage('\n🎉 Imported completed 🎉')
199199

200200
await sleep(100);
@@ -244,17 +244,17 @@ export class ImportOrchestrator {
244244

245245
// No changes to be made
246246
if (diffs.every((d) => d.modification.diff === '')) {
247-
reporter.displayMessage('\nNo changes are needed! Exiting...')
247+
await reporter.displayMessage('\nNo changes are needed! Exiting...')
248248

249249
// Wait for the message to display before we exit
250250
await sleep(100);
251251
return;
252252
}
253253

254-
reporter.displayFileModifications(diffs);
254+
await reporter.displayFileModifications(diffs);
255255
const shouldSave = await reporter.promptConfirmation('Save the changes?');
256256
if (!shouldSave) {
257-
reporter.displayMessage('\nSkipping save! Exiting...');
257+
await reporter.displayMessage('\nSkipping save! Exiting...');
258258

259259
// Wait for the message to display before we exit
260260
await sleep(100);
@@ -265,7 +265,7 @@ export class ImportOrchestrator {
265265
await FileUpdater.write(diff.file, diff.modification.newFile);
266266
}
267267

268-
reporter.displayMessage('\n🎉 Imported completed and saved to file 🎉');
268+
await reporter.displayMessage('\n🎉 Imported completed and saved to file 🎉');
269269

270270
// Wait for the message to display before we exit
271271
await sleep(100);
@@ -448,11 +448,11 @@ ${JSON.stringify(unsupportedTypeIds)}`);
448448
const newFile = JSON.stringify(importResult.result.map((r) => r.raw), null, 2);
449449
const diff = prettyFormatFileDiff('', newFile);
450450

451-
reporter.displayFileModifications([{ file: filePath, modification: { newFile, diff } }]);
451+
await reporter.displayFileModifications([{ file: filePath, modification: { newFile, diff } }]);
452452

453453
const shouldSave = await reporter.promptConfirmation(`Save the changes? (${filePath})`);
454454
if (!shouldSave) {
455-
reporter.displayMessage('\nSkipping save! Exiting...');
455+
await reporter.displayMessage('\nSkipping save! Exiting...');
456456

457457
// Wait for the message to display before we exit
458458
await sleep(100);
@@ -461,7 +461,7 @@ ${JSON.stringify(unsupportedTypeIds)}`);
461461

462462
await FileUpdater.write(filePath, newFile);
463463

464-
reporter.displayMessage('\n🎉 Imported completed and saved to file 🎉');
464+
await reporter.displayMessage('\n🎉 Imported completed and saved to file 🎉');
465465

466466
// Wait for the message to display before we exit
467467
await sleep(100);

src/orchestrators/plan.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class PlanOrchestrator {
4848
if (!args.noProgress) ctx.processFinished(ProcessName.PLAN)
4949

5050
await reporter.hide();
51-
reporter.displayPlan(plan);
51+
await reporter.displayPlan(plan);
5252

5353
return {
5454
plan,

src/orchestrators/refresh.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class RefreshOrchestrator {
3535

3636
ctx.processFinished(ProcessName.REFRESH);
3737

38-
reporter.displayImportResult(importResult, false);
38+
await reporter.displayImportResult(importResult, false);
3939

4040

4141
// Special handling for remote-file resources. Offer to save them remotely if any changes are detected on import.

src/orchestrators/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export const TestOrchestrator = {
130130

131131
// Short circuit and exit if every change is NOOP
132132
if (!planResult.plan.isEmpty()) {
133-
reporter.displayPlan(planResult.plan);
133+
await reporter.displayPlan(planResult.plan);
134134
const confirm = await reporter.promptConfirmation('The following resources will need to be installed (Tart VM - 25gb). Do you want to continue?')
135135
if (!confirm) {
136136
return process.exit(0);

src/ui/reporters/default-reporter.tsx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,11 @@ export class DefaultReporter implements Reporter {
207207
}
208208
}
209209

210-
displayImportResult(importResult: ImportResult, showConfigs: boolean): void {
210+
async displayImportResult(importResult: ImportResult, showConfigs: boolean): Promise<void> {
211211
store.set(store.progressState, null);
212212
this.progressState = null;
213213

214-
void this.updateRenderState(RenderStatus.DISPLAY_IMPORT_RESULT, { importResult, showConfigs });
214+
await this.updateRenderState(RenderStatus.DISPLAY_IMPORT_RESULT, { importResult, showConfigs });
215215
}
216216

217217
async promptSudo(pluginName: string, data: CommandRequestData): Promise<string | undefined> {
@@ -223,8 +223,8 @@ export class DefaultReporter implements Reporter {
223223
return password;
224224
}
225225

226-
displayPlan(plan: Plan): void {
227-
void this.updateRenderState(RenderStatus.DISPLAY_PLAN, plan)
226+
async displayPlan(plan: Plan): Promise<void> {
227+
await this.updateRenderState(RenderStatus.DISPLAY_PLAN, plan);
228228
}
229229

230230
async displayMessage(message: string) {
@@ -264,17 +264,17 @@ export class DefaultReporter implements Reporter {
264264
return options.indexOf(result);
265265
}
266266

267-
displayFileModifications(diff: Array<{ file: string; modification: FileModificationResult}>) {
268-
void this.updateRenderState(RenderStatus.DISPLAY_FILE_MODIFICATION, diff);
267+
async displayFileModifications(diff: Array<{ file: string; modification: FileModificationResult}>): Promise<void> {
268+
await this.updateRenderState(RenderStatus.DISPLAY_FILE_MODIFICATION, diff);
269269
}
270270

271-
displayPluginError(error: PluginError): void {
271+
async displayPluginError(error: PluginError): Promise<void> {
272272
if (error.errorData.errorType === 'apply_validation') {
273273
const resourcePlan = new ResourcePlan((error.errorData.data as any).plan);
274-
void this.updateRenderState(RenderStatus.APPLY_VALIDATION_ERROR, resourcePlan);
274+
await this.updateRenderState(RenderStatus.APPLY_VALIDATION_ERROR, resourcePlan);
275275
return;
276276
}
277-
void this.updateRenderState(RenderStatus.PLUGIN_ERROR, error.message);
277+
await this.updateRenderState(RenderStatus.PLUGIN_ERROR, error.message);
278278
}
279279

280280
private log(log: string): void {
@@ -388,6 +388,14 @@ export class DefaultReporter implements Reporter {
388388
return store.get(store.renderState) as { status: RenderStatus, data: any };
389389
}
390390

391+
/**
392+
* Update the render state. We need to make this async because there is currently a weird bug where if we switch the
393+
* layout too quickly then it can potentially crash with a memory error. We first switch to empty, wait 50ms and the
394+
* render the next state
395+
* @param status
396+
* @param data
397+
* @private
398+
*/
391399
private async updateRenderState(status: RenderStatus | null, data?: unknown): Promise<void> {
392400
const current = this.getRenderState();
393401
if (current?.status !== status) {

src/ui/reporters/json-reporter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Reporter } from './reporter.js';
99
export class JsonReporter implements Reporter {
1010
silent = false;
1111

12-
displayPlan(plan: Plan): void {
12+
async displayPlan(plan: Plan): Promise<void> {
1313
console.log(JSON.stringify(plan.resources.map((r) => r.raw), null, 2));
1414
}
1515

@@ -59,7 +59,7 @@ export class JsonReporter implements Reporter {
5959
async displayFileModifications(): Promise<void> {
6060
}
6161

62-
displayMessage(): void {
62+
async displayMessage(): Promise<void> {
6363
}
6464

6565
async displayImportWarning(): Promise<void> {
@@ -73,7 +73,7 @@ export class JsonReporter implements Reporter {
7373
throw new Error('Json reporter error: disableRawMode is not supported. Raw stdin mode requires interactive terminal access.');
7474
}
7575

76-
displayPluginError(error: PluginError): void {
76+
async displayPluginError(error: PluginError): Promise<void> {
7777
console.log(JSON.stringify({
7878
errorType: error.errorData.errorType,
7979
message: error.message,

src/ui/reporters/plain-reporter.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class PlainReporter implements Reporter {
7070
return Number.parseInt(response as string, 10);
7171
}
7272

73-
displayFileModifications(diffs: { file: string; modification: FileModificationResult; }[]): void {
73+
async displayFileModifications(diffs: { file: string; modification: FileModificationResult; }[]): Promise<void> {
7474
ctx.log(chalk.bold('File modifications\n'))
7575

7676
for (const diff of diffs) {
@@ -81,7 +81,7 @@ export class PlainReporter implements Reporter {
8181
}
8282
}
8383

84-
displayMessage(message: string): void {
84+
async displayMessage(message: string): Promise<void> {
8585
ctx.log(message);
8686
}
8787

@@ -128,7 +128,7 @@ export class PlainReporter implements Reporter {
128128
return availableTypes;
129129
}
130130

131-
displayImportResult(importResult: ImportResult) {
131+
async displayImportResult(importResult: ImportResult): Promise<void> {
132132
ctx.log();
133133
ctx.log(JSON.stringify(importResult.result.map((r) => r.raw), null, 2));
134134

@@ -159,13 +159,13 @@ Use this init flow to get started quickly with Codify.
159159
return response === 'yes';
160160
}
161161

162-
displayPlan(plan: Plan): void {
162+
async displayPlan(plan: Plan): Promise<void> {
163163
ctx.log(
164164
prettyFormatPlan(plan.filterNoopResources())
165165
);
166166
}
167167

168-
displayPluginError(error: PluginError): void {
168+
async displayPluginError(error: PluginError): Promise<void> {
169169
if (error.errorData.errorType === 'apply_validation') {
170170
ctx.log(chalk.red(formatApplyValidationError(error)));
171171
return;

src/ui/reporters/reporter.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export enum PromptType {
5151
export interface Reporter {
5252
silent: boolean;
5353

54-
displayPlan(plan: Plan): void
54+
displayPlan(plan: Plan): Promise<void>
5555

5656
displayInitBanner(): Promise<void>
5757

@@ -73,19 +73,19 @@ export interface Reporter {
7373

7474
promptPressKeyToContinue(message?: string): Promise<void>;
7575

76-
displayImportResult(importResult: ImportResult, showConfigs: boolean): void;
76+
displayImportResult(importResult: ImportResult, showConfigs: boolean): Promise<void>;
7777

78-
displayFileModifications(diff: Array<{ file: string, modification: FileModificationResult }>): void
78+
displayFileModifications(diff: Array<{ file: string, modification: FileModificationResult }>): Promise<void>
7979

80-
displayMessage(message: string): void
80+
displayMessage(message: string): Promise<void>
8181

8282
displayImportWarning(requiresParameters: string[], noParametersRequired: string[]): Promise<void>
8383

8484
setRawMode(): Promise<void>
8585

8686
disableRawMode(): Promise<void>
8787

88-
displayPluginError(error: PluginError): void;
88+
displayPluginError(error: PluginError): Promise<void>;
8989
}
9090

9191
export enum ReporterType {

0 commit comments

Comments
 (0)