Skip to content

Commit c346e82

Browse files
authored
chore(toolkit): option to strip color and style (#33244)
Closes [#33237](#33237) along with #33243. ```ts new Toolkit({ ioHost, color: false }); ``` Can strip color and style by specifying the property when creating the `Toolkit`. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent ba2f5c8 commit c346e82

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

packages/@aws-cdk/toolkit/lib/api/io/private/logger.ts

+29
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import type { ToolkitAction } from '../../../toolkit';
77
import { formatSdkLoggerContent } from '../../aws-cdk';
88
import type { IIoHost } from '../io-host';
99

10+
/**
11+
* An IoHost wrapper that adds the given action to an actionless message before
12+
* sending the message to the given IoHost
13+
*/
1014
export function withAction(ioHost: IIoHost, action: ToolkitAction) {
1115
return {
1216
notify: async <T>(msg: Omit<IoMessage<T>, 'action'>) => {
@@ -24,6 +28,31 @@ export function withAction(ioHost: IIoHost, action: ToolkitAction) {
2428
};
2529
}
2630

31+
/**
32+
* An IoHost wrapper that strips out ANSI colors and styles from the message before
33+
* sending the message to the given IoHost
34+
*/
35+
export function withoutColor(ioHost: IIoHost): IIoHost {
36+
return {
37+
notify: async <T>(msg: IoMessage<T>) => {
38+
await ioHost.notify({
39+
...msg,
40+
message: stripColor(msg.message),
41+
});
42+
},
43+
requestResponse: async <T, U>(msg: IoRequest<T, U>) => {
44+
return ioHost.requestResponse({
45+
...msg,
46+
message: stripColor(msg.message),
47+
});
48+
},
49+
};
50+
}
51+
52+
function stripColor(msg: string): string {
53+
return msg.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
54+
}
55+
2756
/**
2857
* An IoHost wrapper that strips out emojis from the message before
2958
* sending the message to the given IoHost

packages/@aws-cdk/toolkit/lib/toolkit/toolkit.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { CachedCloudAssemblySource, IdentityCloudAssemblySource, StackAssembly,
1919
import { ALL_STACKS, CloudAssemblySourceBuilder } from '../api/cloud-assembly/private';
2020
import { ToolkitError } from '../api/errors';
2121
import { IIoHost, IoMessageCode, IoMessageLevel } from '../api/io';
22-
import { asSdkLogger, withAction, Timer, confirm, error, highlight, info, success, warn, ActionAwareIoHost, debug, result, withoutEmojis } from '../api/io/private';
22+
import { asSdkLogger, withAction, Timer, confirm, error, highlight, info, success, warn, ActionAwareIoHost, debug, result, withoutEmojis, withoutColor } from '../api/io/private';
2323

2424
/**
2525
* The current action being performed by the CLI. 'none' represents the absence of an action.
@@ -54,6 +54,16 @@ export interface ToolkitOptions {
5454
*/
5555
emojis?: boolean;
5656

57+
/**
58+
* Whether to allow ANSI colors and formatting in IoHost messages.
59+
* Setting this value to `falsez enforces that no color or style shows up
60+
* in messages sent to the IoHost.
61+
* Setting this value to true is a no-op; it is equivalent to the default.
62+
*
63+
* @default - detects color from the TTY status of the IoHost
64+
*/
65+
color?: boolean;
66+
5767
/**
5868
* Configuration options for the SDK.
5969
*/
@@ -102,6 +112,9 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
102112
if (props.emojis === false) {
103113
ioHost = withoutEmojis(ioHost);
104114
}
115+
if (props.color === false) {
116+
ioHost = withoutColor(ioHost);
117+
}
105118
this.ioHost = ioHost;
106119
}
107120

packages/@aws-cdk/toolkit/test/toolkit/toolkit.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* - Source Builders: Tests for the Cloud Assembly Source Builders are in `test/api/cloud-assembly/source-builder.test.ts`
66
*/
77

8+
import * as chalk from 'chalk';
89
import { Toolkit } from '../../lib';
910
import { TestIoHost } from '../_helpers';
1011

@@ -27,3 +28,23 @@ test('emojis can be stripped from message', async () => {
2728
message: 'Smile123',
2829
}));
2930
});
31+
32+
test('color can be stripped from message', async () => {
33+
const ioHost = new TestIoHost();
34+
const toolkit = new Toolkit({ ioHost, color: false });
35+
36+
await toolkit.ioHost.notify({
37+
message: chalk.red('RED') + chalk.bold('BOLD') + chalk.blue('BLUE'),
38+
action: 'deploy',
39+
level: 'info',
40+
code: 'CDK_TOOLKIT_I0000',
41+
time: new Date(),
42+
});
43+
44+
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({
45+
action: 'deploy',
46+
level: 'info',
47+
code: 'CDK_TOOLKIT_I0000',
48+
message: 'REDBOLDBLUE',
49+
}));
50+
});

0 commit comments

Comments
 (0)