Skip to content

Commit 0358b6d

Browse files
committed
Made updateRenderState async and render to nothing first to avoid memory crashes.
Improved the progress output for destroy
1 parent 58e62e8 commit 0358b6d

4 files changed

Lines changed: 32 additions & 46 deletions

File tree

src/commands/apply.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ For more information, visit: https://codifycli.com/docs/commands/apply
4343
'<%= config.bin %> <%= command.id %> -S <sudo password>',
4444
]
4545

46-
async init(): Promise<void> {
47-
console.log('Running Codify apply...')
48-
return super.init();
49-
}
50-
5146
public async run(): Promise<void> {
5247
const { flags, args } = await this.parse(Apply)
5348

49+
50+
if (flags.output !== 'json') {
51+
console.log('Running Codify apply...')
52+
}
53+
5454
if (flags.path && args.pathArgs) {
5555
throw new Error('Cannot specify both --path and path argument');
5656
}

src/commands/destroy.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@ For more information, visit: https://codifycli.com/docs/commands/destory`
3939
default: false,
4040
}),
4141
}
42-
42+
4343
public async run(): Promise<void> {
4444
const { flags, raw } = await this.parse(Destroy)
4545

46+
if (flags.output !== 'json') {
47+
console.log('Running Codify destroy...')
48+
}
49+
4650
const args = raw
4751
.filter((r) => r.type === 'arg')
4852
.map((r) => r.input);
4953

50-
if (flags.path) {
51-
this.log(`Applying Codify from: ${flags.path}`);
52-
}
53-
5454
await DestroyOrchestrator.run({
5555
verbosityLevel: flags.debug ? 3 : 0,
5656
typeIds: args,

src/orchestrators/destroy.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class DestroyOrchestrator {
2121

2222
static async run(args: DestroyArgs, reporter: Reporter) {
2323
const typeIds = args.typeIds?.filter(Boolean)
24-
ctx.processStarted(ProcessName.DESTROY)
24+
ctx.processStarted(ProcessName.PLAN)
2525

2626
const initializationResult = await PluginInitOrchestrator.run(
2727
{ ...args, allowEmptyProject: true, },
@@ -37,6 +37,8 @@ export class DestroyOrchestrator {
3737
? await DestroyOrchestrator.destroyExistingProject(reporter, initializationResult)
3838
: await DestroyOrchestrator.destroySpecificResources(typeIds, reporter, initializationResult)
3939

40+
ctx.processFinished(ProcessName.DESTROY)
41+
4042
plan.sortByEvalOrder(project.evaluationOrder);
4143
destroyProject.removeNoopFromEvaluationOrder(plan);
4244

@@ -55,6 +57,8 @@ export class DestroyOrchestrator {
5557
}
5658
}
5759

60+
ctx.processStarted(ProcessName.DESTROY)
61+
5862
const filteredPlan = plan.filterNoopResources()
5963

6064
let currentVerbosity = args.verbosityLevel ?? 0;

src/ui/reporters/default-reporter.tsx

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class DefaultReporter implements Reporter {
8888
RenderEvent.PROMPT_RESULT,
8989
)
9090

91-
this.updateRenderState(previousRenderState.status, previousRenderState.data);
91+
await this.updateRenderState(previousRenderState.status, previousRenderState.data);
9292
}
9393

9494
async displayInitBanner(): Promise<void> {
@@ -106,11 +106,11 @@ export class DefaultReporter implements Reporter {
106106
}
107107

108108
async displayProgress(): Promise<void> {
109-
this.updateRenderState(RenderStatus.PROGRESS);
109+
await this.updateRenderState(RenderStatus.PROGRESS);
110110
}
111111

112112
async hide(): Promise<void> {
113-
this.updateRenderState(RenderStatus.NOTHING);
113+
await this.updateRenderState(RenderStatus.NOTHING);
114114
}
115115

116116
async displayImportWarning(requiresParameters: string[], noParametersRequired: string[]): Promise<void> {
@@ -173,7 +173,7 @@ export class DefaultReporter implements Reporter {
173173
exitFullScreen()
174174
process.off('beforeExit', exitFullScreen);
175175

176-
this.updateRenderState(RenderStatus.PROGRESS);
176+
await this.updateRenderState(RenderStatus.PROGRESS);
177177

178178
return userInput.map((v) => ResourceConfig.fromJson({
179179
core: { type: v.section.title },
@@ -195,7 +195,7 @@ export class DefaultReporter implements Reporter {
195195
store.set(store.progressState, null);
196196
this.progressState = null;
197197

198-
this.updateRenderState(RenderStatus.DISPLAY_IMPORT_RESULT, { importResult, showConfigs });
198+
void this.updateRenderState(RenderStatus.DISPLAY_IMPORT_RESULT, { importResult, showConfigs });
199199
}
200200

201201
async promptSudo(pluginName: string, data: CommandRequestData, secureMode: boolean): Promise<string | undefined> {
@@ -212,11 +212,11 @@ export class DefaultReporter implements Reporter {
212212
}
213213

214214
displayPlan(plan: Plan): void {
215-
this.updateRenderState(RenderStatus.DISPLAY_PLAN, plan)
215+
void this.updateRenderState(RenderStatus.DISPLAY_PLAN, plan)
216216
}
217217

218218
displayMessage(message: string) {
219-
this.updateRenderState(RenderStatus.DISPLAY_MESSAGE, message);
219+
void this.updateRenderState(RenderStatus.DISPLAY_MESSAGE, message);
220220
}
221221

222222
async promptInitResultSelection(availableTypes: string[]): Promise<string[]> {
@@ -234,12 +234,6 @@ export class DefaultReporter implements Reporter {
234234

235235
this.log(result ? `${message} -> "Yes"` : `${message} -> "No"`)
236236

237-
// This was added because there was a very hard to debug memory bug with Yoga (ink.js layout engine). Could not
238-
// identify the root cause of the problem but this alleviates it.
239-
await sleep(50)
240-
this.updateRenderState(RenderStatus.NOTHING, null);
241-
await sleep(50);
242-
243237
return result;
244238
}
245239

@@ -253,17 +247,13 @@ export class DefaultReporter implements Reporter {
253247

254248
this.log(`${message} -> "${result}"`)
255249

256-
// This was added because there was a very hard to debug memory bug with Yoga (ink.js layout engine). Could not
257-
// identify the root cause of the problem but this alleviates it.
258-
await sleep(50)
259-
this.updateRenderState(prevRenderState.status, prevRenderState.data);
260-
await sleep(50);
250+
await this.updateRenderState(prevRenderState.status, prevRenderState.data);
261251

262252
return options.indexOf(result);
263253
}
264254

265255
displayFileModifications(diff: Array<{ file: string; modification: FileModificationResult}>) {
266-
this.updateRenderState(RenderStatus.DISPLAY_FILE_MODIFICATION, diff);
256+
void this.updateRenderState(RenderStatus.DISPLAY_FILE_MODIFICATION, diff);
267257
}
268258

269259
private log(args: string): void {
@@ -326,18 +316,14 @@ export class DefaultReporter implements Reporter {
326316
private async handleInlineSudoPassword(): Promise<void> {
327317
let attemptCount = 0;
328318

329-
await sleep(50);
330-
this.updateRenderState(RenderStatus.NOTHING);
331-
await sleep(50);
332-
333319
while (attemptCount < 3) {
334320
const result = (await Promise.all([
335321
this.updateRenderState(RenderStatus.SUDO_PROMPT, { attemptCount, cancellable: true }),
336322
Promise.race([
337323
this.awaitEvent<string>(RenderEvent.SUDO_PROMPT_RESULT),
338324
this.awaitEvent<'cancel'>(RenderEvent.SUDO_PASSWORD_CANCEL).then(() => Symbol.for('cancel')),
339325
]),
340-
])).at(1) as string | Symbol;
326+
])).at(1) as string | symbol;
341327

342328
if (result === Symbol.for('cancel')) {
343329
ctx.log('Sudo password cancelled');
@@ -346,13 +332,10 @@ export class DefaultReporter implements Reporter {
346332
ctx.log('Sudo password attempt');
347333
}
348334

349-
const isValid = this.sudoPasswordSubmittedCallback?.(result) ?? false;
335+
const isValid = this.sudoPasswordSubmittedCallback?.(result as string) ?? false;
350336
if (isValid) {
351337
ctx.log('Sudo password successful!');
352338

353-
await sleep(50);
354-
this.updateRenderState(RenderStatus.NOTHING, null);
355-
await sleep(50);
356339
await this.displayProgress();
357340
this.renderEmitter.emit(RenderEvent.SUDO_PASSWORD_PRE_SUPPLIED);
358341
return;
@@ -363,18 +346,12 @@ export class DefaultReporter implements Reporter {
363346
}
364347

365348
// Cancelled or all attempts exhausted — restore progress display
366-
await sleep(50);
367-
this.updateRenderState(RenderStatus.NOTHING, null);
368-
await sleep(50);
369349
await this.displayProgress();
370350
}
371351

372352
private async getUserPassword(): Promise<string> {
373353
let attemptCount = 0;
374354

375-
this.updateRenderState(RenderStatus.NOTHING);
376-
await sleep(50);
377-
378355
while (attemptCount < 3) {
379356
const passwordAttempt = await this.updateStateAndAwaitEvent<string>(
380357
() => this.updateRenderState(RenderStatus.SUDO_PROMPT, { attemptCount, cancellable: false }),
@@ -404,7 +381,12 @@ export class DefaultReporter implements Reporter {
404381
return store.get(store.renderState) as { status: RenderStatus, data: any };
405382
}
406383

407-
private updateRenderState(status: RenderStatus | null, data?: unknown): void {
384+
private async updateRenderState(status: RenderStatus | null, data?: unknown): Promise<void> {
385+
const current = this.getRenderState();
386+
if (current?.status !== status) {
387+
store.set(store.renderState, { status: RenderStatus.NOTHING, data: null });
388+
await sleep(50);
389+
}
408390
store.set(store.renderState, { status, data });
409391
}
410392

0 commit comments

Comments
 (0)