Skip to content

Commit 8d1c195

Browse files
committed
Minor improvements:
1. Added exit codes to existing commands 2. Added FORCE_COLOR flag to make the output show up with color 3. Added short circuit to terminal applies if every change is NOOP
1 parent 43ef810 commit 8d1c195

4 files changed

Lines changed: 31 additions & 9 deletions

File tree

src/commands/apply/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@ export default class Apply extends Command {
3737

3838
const resolvedPath = path.resolve(flags.path ?? '.');
3939
await ApplyOrchestrator.run(resolvedPath);
40+
41+
this.exit(0);
4042
}
4143
}

src/commands/plan/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,7 @@ export default class Plan extends Command {
3838
const resolvedPath = path.resolve(flags.path ?? '.');
3939

4040
await PlanOrchestrator.run(resolvedPath);
41+
42+
this.exit(0);
4143
}
4244
}

src/orchestrators/apply.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ResourceOperation } from 'codify-schemas';
12
import readline from 'node:readline';
23

34
import { PlanOrchestrator } from './plan.js';
@@ -8,6 +9,13 @@ export const ApplyOrchestrator = {
89
async run(rootDirectory: string): Promise<void> {
910
const { plan, pluginCollection } = await PlanOrchestrator.run(rootDirectory, false);
1011

12+
// Short circuit and exit if every change is NOOP
13+
if (plan.every((p) => p.operation === ResourceOperation.NOOP)) {
14+
console.log('No changes necessary. Exiting');
15+
await pluginCollection.destroy();
16+
return;
17+
}
18+
1119
const response = await new Promise((resolve) => {
1220
rl.question('Is this okay?\n', (answer) => resolve(answer));
1321
});

src/plugins/plugin-process.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,21 @@ export class PluginProcess {
1919
}
2020

2121
static async start(jsFileDir: string): Promise<PluginProcess> {
22-
const process = fork(
22+
const _process = fork(
2323
jsFileDir,
2424
[],
25-
{ execArgv: ['--import', 'tsx'], silent: true },
25+
{
26+
execArgv: ['--import', 'tsx'],
27+
env: { ...process.env, FORCE_COLOR: '1' },
28+
silent: true
29+
},
2630
);
2731

28-
process.stdout!.on('data', (message) => console.log(message.toString()));
29-
process.stderr!.on('data', (message) => console.log(message.toString()));
32+
_process.stdout!.on('data', (message) => console.log(message.toString()));
33+
_process.stderr!.on('data', (message) => console.log(message.toString()));
3034

3135

32-
return new PluginProcess(process);
36+
return new PluginProcess(_process);
3337
}
3438

3539
killPlugin(): void {
@@ -58,12 +62,18 @@ class SendMessageForResultHandler {
5862
promiseReject: Reject;
5963
timer: NodeJS.Timeout;
6064

61-
constructor(messageToSend: PluginMessage, process: ChildProcess, resolve: Resolve, reject: Reject) {
65+
constructor(
66+
messageToSend: PluginMessage,
67+
process: ChildProcess,
68+
resolve: Resolve,
69+
reject: Reject,
70+
timeout = 600_000, // Default time is 10 minutes for a command
71+
) {
6272
this.messageToSend = messageToSend;
6373
this.process = process;
6474
this.promiseResolve = resolve;
6575
this.promiseReject = reject;
66-
this.timer = this.setResultTimeout();
76+
this.timer = this.setResultTimeout(timeout);
6777
}
6878

6979
messageListener = (incomingMessage: unknown) => {
@@ -98,9 +108,9 @@ class SendMessageForResultHandler {
98108
this.promiseResolve(value);
99109
}
100110

101-
private setResultTimeout = () => setTimeout(() => {
111+
private setResultTimeout = (timeout: number) => setTimeout(() => {
102112
this.reject(new Error(`Plugin did not respond in 10s to call: ${this.messageToSend.cmd}`))
103-
}, 10_000);
113+
}, timeout);
104114

105115
private validateIpcMessage(response: unknown): response is IpcMessage {
106116
return ipcMessageValidator(response);

0 commit comments

Comments
 (0)