Skip to content

Commit 4566d8d

Browse files
authored
refactor(cli): replace getters and setters with a public field (#215)
The `CliIoHost` had a number of fields that had the following pattern: ```ts class CliIoHost { private _someField: string; /** Get someField */ public get someField() { return this._someField; } /** Set someField */ public set someField(value: string) { this._someField = value; } } ``` There is no additional code in the getters and setters other than directly forwarding every access to a private field, so the above is equivalent to the following: ```ts class CliIoHost { public someField: string; } ``` Since the above is simpler, this PR proposed to remove the getters and setters and just expose the field mutably. If we ever want to run additional code in a setter in some of these fields, we can refactor back to a getter and setter pair without impacting source or binary compatibility (forwards compatibility was the argument in the olden C++ and Java days for pre-emptively wrapping fields in getters and setters, but doesn't apply in JS land). (🤖 This PR was generated using AI) --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent ac56f9b commit 4566d8d

File tree

1 file changed

+35
-88
lines changed

1 file changed

+35
-88
lines changed

packages/aws-cdk/lib/toolkit/cli-io-host.ts

+35-88
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,36 @@ export class CliIoHost implements IIoHost {
9090
*/
9191
private static _instance: CliIoHost | undefined;
9292

93-
// internal state for getters/setter
94-
private _currentAction: CliAction;
95-
private _isCI: boolean;
96-
private _isTTY: boolean;
97-
private _logLevel: IoMessageLevel;
93+
/**
94+
* The current action being performed by the CLI.
95+
*/
96+
public currentAction: CliAction;
97+
98+
/**
99+
* Whether the CliIoHost is running in CI mode.
100+
*
101+
* In CI mode, all non-error output goes to stdout instead of stderr.
102+
*/
103+
public isCI: boolean;
104+
105+
/**
106+
* Whether the host can use interactions and message styling.
107+
*/
108+
public isTTY: boolean;
109+
110+
/**
111+
* The current threshold.
112+
*
113+
* Messages with a lower priority level will be ignored.
114+
*/
115+
public logLevel: IoMessageLevel;
116+
117+
/**
118+
* The conditions for requiring approval in this CliIoHost.
119+
*/
120+
public requireDeployApproval: RequireApproval;
121+
98122
private _internalIoHost?: IIoHost;
99-
private _requireDeployApproval: RequireApproval;
100123
private _progress: StackActivityProgress = StackActivityProgress.BAR;
101124

102125
// Stack Activity Printer
@@ -107,11 +130,11 @@ export class CliIoHost implements IIoHost {
107130
private readonly corkedLoggingBuffer: IoMessage<any>[] = [];
108131

109132
private constructor(props: CliIoHostProps = {}) {
110-
this._currentAction = props.currentAction ?? 'none';
111-
this._isTTY = props.isTTY ?? process.stdout.isTTY ?? false;
112-
this._logLevel = props.logLevel ?? 'info';
113-
this._isCI = props.isCI ?? isCI();
114-
this._requireDeployApproval = props.requireDeployApproval ?? RequireApproval.BROADENING;
133+
this.currentAction = props.currentAction ?? 'none';
134+
this.isTTY = props.isTTY ?? process.stdout.isTTY ?? false;
135+
this.logLevel = props.logLevel ?? 'info';
136+
this.isCI = props.isCI ?? isCI();
137+
this.requireDeployApproval = props.requireDeployApproval ?? RequireApproval.BROADENING;
115138

116139
this.stackProgress = props.stackProgress ?? StackActivityProgress.BAR;
117140
}
@@ -168,82 +191,6 @@ export class CliIoHost implements IIoHost {
168191
return this._progress;
169192
}
170193

171-
/**
172-
* The current action being performed by the CLI.
173-
*/
174-
public get currentAction(): CliAction {
175-
return this._currentAction;
176-
}
177-
178-
/**
179-
* Sets the current action being performed by the CLI.
180-
*
181-
* @param action The action being performed by the CLI.
182-
*/
183-
public set currentAction(action: CliAction) {
184-
this._currentAction = action;
185-
}
186-
187-
/**
188-
* Whether the host can use interactions and message styling.
189-
*/
190-
public get isTTY(): boolean {
191-
return this._isTTY;
192-
}
193-
194-
/**
195-
* Set TTY mode, i.e can the host use interactions and message styling.
196-
*
197-
* @param value set TTY mode
198-
*/
199-
public set isTTY(value: boolean) {
200-
this._isTTY = value;
201-
}
202-
203-
/**
204-
* Return the conditions for requiring approval in this CliIoHost.
205-
*/
206-
public get requireDeployApproval() {
207-
return this._requireDeployApproval;
208-
}
209-
210-
/**
211-
* Set the conditions for requiring approval in this CliIoHost.
212-
*/
213-
public set requireDeployApproval(approval: RequireApproval) {
214-
this._requireDeployApproval = approval;
215-
}
216-
217-
/**
218-
* Whether the CliIoHost is running in CI mode. In CI mode, all non-error output goes to stdout instead of stderr.
219-
*/
220-
public get isCI(): boolean {
221-
return this._isCI;
222-
}
223-
224-
/**
225-
* Set the CI mode. In CI mode, all non-error output goes to stdout instead of stderr.
226-
* @param value set the CI mode
227-
*/
228-
public set isCI(value: boolean) {
229-
this._isCI = value;
230-
}
231-
232-
/**
233-
* The current threshold. Messages with a lower priority level will be ignored.
234-
*/
235-
public get logLevel(): IoMessageLevel {
236-
return this._logLevel;
237-
}
238-
239-
/**
240-
* Sets the current threshold. Messages with a lower priority level will be ignored.
241-
* @param level The new log level threshold
242-
*/
243-
public set logLevel(level: IoMessageLevel) {
244-
this._logLevel = level;
245-
}
246-
247194
/**
248195
* Executes a block of code with corked logging. All log messages during execution
249196
* are buffered and only written when all nested cork blocks complete (when CORK_COUNTER reaches 0).
@@ -431,7 +378,7 @@ export class CliIoHost implements IIoHost {
431378
*/
432379
private formatMessage(msg: IoMessage<any>): string {
433380
// apply provided style or a default style if we're in TTY mode
434-
let message_text = this._isTTY
381+
let message_text = this.isTTY
435382
? styleMap[msg.level](msg.message)
436383
: msg.message;
437384

0 commit comments

Comments
 (0)