Skip to content

Commit 2e7c3e8

Browse files
authored
chore(toolkit): option to strip emojis (#33243)
Implements half of [#33237](#33237) ```ts new Toolkit({ ioHost, emojis: false }); ``` Can strip emojis 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 ffe9863 commit 2e7c3e8

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,32 @@ export function withAction(ioHost: IIoHost, action: ToolkitAction) {
2424
};
2525
}
2626

27+
/**
28+
* An IoHost wrapper that strips out emojis from the message before
29+
* sending the message to the given IoHost
30+
*/
31+
export function withoutEmojis(ioHost: IIoHost): IIoHost {
32+
return {
33+
notify: async <T>(msg: IoMessage<T>) => {
34+
await ioHost.notify({
35+
...msg,
36+
message: stripEmojis(msg.message),
37+
});
38+
},
39+
requestResponse: async <T, U>(msg: IoRequest<T, U>) => {
40+
return ioHost.requestResponse({
41+
...msg,
42+
message: stripEmojis(msg.message),
43+
});
44+
},
45+
};
46+
}
47+
48+
function stripEmojis(msg: string): string {
49+
// https://www.unicode.org/reports/tr51/#def_emoji_presentation
50+
return msg.replace(/\p{Emoji_Presentation}/gu, '');
51+
}
52+
2753
// @todo these cannot be awaited WTF
2854
export function asSdkLogger(ioHost: IIoHost, action: ToolkitAction): Logger {
2955
return new class implements Logger {

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

Lines changed: 15 additions & 4 deletions
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 } from '../api/io/private';
22+
import { asSdkLogger, withAction, Timer, confirm, error, highlight, info, success, warn, ActionAwareIoHost, debug, result, withoutEmojis } from '../api/io/private';
2323

2424
/**
2525
* The current action being performed by the CLI. 'none' represents the absence of an action.
@@ -47,6 +47,13 @@ export interface ToolkitOptions {
4747
*/
4848
ioHost?: IIoHost;
4949

50+
/**
51+
* Allow emojis in messages sent to the IoHost.
52+
*
53+
* @default true
54+
*/
55+
emojis?: boolean;
56+
5057
/**
5158
* Configuration options for the SDK.
5259
*/
@@ -77,9 +84,9 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
7784
public readonly toolkitStackName: string;
7885

7986
/**
80-
* @todo should probably be public in one way or the other.
87+
* The IoHost of this Toolkit
8188
*/
82-
private readonly ioHost: IIoHost;
89+
public readonly ioHost: IIoHost;
8390
private _sdkProvider?: SdkProvider;
8491

8592
public constructor(private readonly props: ToolkitOptions = {}) {
@@ -91,7 +98,11 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
9198
if (props.ioHost) {
9299
globalIoHost.registerIoHost(props.ioHost as any);
93100
}
94-
this.ioHost = globalIoHost as IIoHost;
101+
let ioHost = globalIoHost as IIoHost;
102+
if (props.emojis === false) {
103+
ioHost = withoutEmojis(ioHost);
104+
}
105+
this.ioHost = ioHost;
95106
}
96107

97108
public async dispose(): Promise<void> {

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,25 @@
55
* - Source Builders: Tests for the Cloud Assembly Source Builders are in `test/api/cloud-assembly/source-builder.test.ts`
66
*/
77

8-
test('test', () => expect(true).toBe(true));
8+
import { Toolkit } from '../../lib';
9+
import { TestIoHost } from '../_helpers';
10+
11+
test('emojis can be stripped from message', async () => {
12+
const ioHost = new TestIoHost();
13+
const toolkit = new Toolkit({ ioHost, emojis: false });
14+
15+
await toolkit.ioHost.notify({
16+
message: '💯Smile123😀',
17+
action: 'deploy',
18+
level: 'info',
19+
code: 'CDK_TOOLKIT_I0000',
20+
time: new Date(),
21+
});
22+
23+
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({
24+
action: 'deploy',
25+
level: 'info',
26+
code: 'CDK_TOOLKIT_I0000',
27+
message: 'Smile123',
28+
}));
29+
});

0 commit comments

Comments
 (0)