Skip to content

Commit af041d5

Browse files
committed
Added an error catcher to plan and apply commands
1 parent 84b9455 commit af041d5

3 files changed

Lines changed: 37 additions & 47 deletions

File tree

src/commands/apply/index.ts

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,50 +18,41 @@ export default class Apply extends Command {
1818
]
1919

2020
static flags = {
21-
// flag with no value (-f, --force)
22-
force: Flags.boolean({ char: 'f' }),
23-
// flag with a value (-n, --name=VALUE)
24-
name: Flags.string({ char: 'n', description: 'name to print' }),
2521
// flag with a value (-p, --path=VALUE)
2622
path: Flags.string({ char: 'p', description: 'path to project' }),
2723
}
2824

2925
public async run(): Promise<void> {
30-
const { args, flags } = await this.parse(Apply)
26+
const { flags } = await this.parse(Apply)
3127
const reporter = new DefaultReporter()
3228

33-
const name = flags.name ?? 'world'
34-
this.log(`hello ${name} from /Users/kevinwang/Projects/codify/codify-core/src/commands/apply.ts`)
35-
if (args.file && flags.force) {
36-
this.log(`you input --force and --file: ${args.file}`)
37-
}
38-
39-
if (flags.path) {
40-
this.log(`Applying Codify from: ${flags.path}`);
41-
}
29+
try {
30+
if (flags.path) {
31+
this.log(`Applying Codify from: ${flags.path}`);
32+
}
4233

43-
const resolvedPath = path.resolve(flags.path ?? '.');
34+
const resolvedPath = path.resolve(flags.path ?? '.');
4435

45-
const planResult = await PlanOrchestrator.run(resolvedPath, false);
36+
const planResult = await PlanOrchestrator.run(resolvedPath, false);
37+
reporter.displayPlan(planResult.plan);
4638

47-
// Short circuit and exit if every change is NOOP
48-
if (planResult.plan.every((p) => p.operation === ResourceOperation.NOOP)) {
49-
console.log('No changes necessary. Exiting');
50-
await planResult.pluginCollection.destroy();
51-
return;
52-
}
39+
// Short circuit and exit if every change is NOOP
40+
if (planResult.plan.every((p) => p.operation === ResourceOperation.NOOP)) {
41+
console.log('No changes necessary. Exiting');
42+
await planResult.pluginCollection.destroy();
43+
return this.exit(0);
44+
}
5345

54-
reporter.displayPlan(planResult.plan);
46+
const confirm = await reporter.promptApplyConfirmation()
47+
if (!confirm) {
48+
return this.exit(0);
49+
}
5550

56-
const confirm = await reporter.promptApplyConfirmation()
57-
58-
if (!confirm) {
59-
return this.exit(0);
51+
await ApplyOrchestrator.run(planResult);
52+
} catch (error: unknown) {
53+
console.error(error);
6054
}
6155

62-
await ApplyOrchestrator.run(planResult);
63-
64-
// this.exit(0);
65-
56+
this.exit(0);
6657
}
6758
}

src/commands/plan/index.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,22 @@ export default class Plan extends Command {
2525
}
2626

2727
public async run(): Promise<void> {
28-
const { args, flags } = await this.parse(Plan)
28+
const { flags } = await this.parse(Plan)
2929
const reporter = new DefaultReporter()
3030

31-
const name = flags.name ?? 'world'
32-
this.log(`hello ${name} from /Users/kevinwang/Projects/codify/codify-core/src/commands/plan.ts`)
33-
if (args.file && flags.force) {
34-
this.log(`you input --force and --file: ${args.file}`)
35-
}
31+
try {
32+
if (flags.path) {
33+
this.log(`Applying Codify from: ${flags.path}`);
34+
}
3635

37-
if (flags.path) {
38-
this.log(`Applying Codify from: ${flags.path}`);
39-
}
36+
const resolvedPath = path.resolve(flags.path ?? '.');
4037

41-
const resolvedPath = path.resolve(flags.path ?? '.');
38+
const { plan } = await PlanOrchestrator.run(resolvedPath);
39+
reporter.displayPlan(plan);
4240

43-
const { plan } = await PlanOrchestrator.run(resolvedPath);
44-
reporter.displayPlan(plan);
41+
} catch (error) {
42+
console.error(error);
43+
}
4544

4645
this.exit(0);
4746
}

src/ui/reporters/reporter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { PlanResponseData } from 'codify-schemas';
22

33
export enum RenderEvent {
4-
STATE_TRANSITION = 'stateTransition',
54
LOG = 'log',
65
PROGRESS_UPDATE = 'progressUpdate',
7-
PROMPT_RESULT = 'promptResult'
6+
PROMPT_RESULT = 'promptResult',
7+
STATE_TRANSITION = 'stateTransition'
88
}
99

1010
/**
@@ -26,7 +26,7 @@ export interface DisplayPlanStateTransition extends StateTransition {
2626
}
2727

2828
export interface Reporter {
29-
promptApplyConfirmation(): Promise<boolean>
30-
3129
displayPlan(plan: PlanResponseData[]): void
30+
31+
promptApplyConfirmation(): Promise<boolean>
3232
}

0 commit comments

Comments
 (0)