Skip to content

Commit

Permalink
chore(toolkit): option to strip emojis (#33243)
Browse files Browse the repository at this point in the history
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*
  • Loading branch information
kaizencc authored Jan 31, 2025
1 parent ffe9863 commit 2e7c3e8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
26 changes: 26 additions & 0 deletions packages/@aws-cdk/toolkit/lib/api/io/private/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,32 @@ export function withAction(ioHost: IIoHost, action: ToolkitAction) {
};
}

/**
* An IoHost wrapper that strips out emojis from the message before
* sending the message to the given IoHost
*/
export function withoutEmojis(ioHost: IIoHost): IIoHost {
return {
notify: async <T>(msg: IoMessage<T>) => {
await ioHost.notify({
...msg,
message: stripEmojis(msg.message),
});
},
requestResponse: async <T, U>(msg: IoRequest<T, U>) => {
return ioHost.requestResponse({
...msg,
message: stripEmojis(msg.message),
});
},
};
}

function stripEmojis(msg: string): string {
// https://www.unicode.org/reports/tr51/#def_emoji_presentation
return msg.replace(/\p{Emoji_Presentation}/gu, '');
}

// @todo these cannot be awaited WTF
export function asSdkLogger(ioHost: IIoHost, action: ToolkitAction): Logger {
return new class implements Logger {
Expand Down
19 changes: 15 additions & 4 deletions packages/@aws-cdk/toolkit/lib/toolkit/toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { CachedCloudAssemblySource, IdentityCloudAssemblySource, StackAssembly,
import { ALL_STACKS, CloudAssemblySourceBuilder } from '../api/cloud-assembly/private';
import { ToolkitError } from '../api/errors';
import { IIoHost, IoMessageCode, IoMessageLevel } from '../api/io';
import { asSdkLogger, withAction, Timer, confirm, error, highlight, info, success, warn, ActionAwareIoHost, debug, result } from '../api/io/private';
import { asSdkLogger, withAction, Timer, confirm, error, highlight, info, success, warn, ActionAwareIoHost, debug, result, withoutEmojis } from '../api/io/private';

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

/**
* Allow emojis in messages sent to the IoHost.
*
* @default true
*/
emojis?: boolean;

/**
* Configuration options for the SDK.
*/
Expand Down Expand Up @@ -77,9 +84,9 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
public readonly toolkitStackName: string;

/**
* @todo should probably be public in one way or the other.
* The IoHost of this Toolkit
*/
private readonly ioHost: IIoHost;
public readonly ioHost: IIoHost;
private _sdkProvider?: SdkProvider;

public constructor(private readonly props: ToolkitOptions = {}) {
Expand All @@ -91,7 +98,11 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
if (props.ioHost) {
globalIoHost.registerIoHost(props.ioHost as any);
}
this.ioHost = globalIoHost as IIoHost;
let ioHost = globalIoHost as IIoHost;
if (props.emojis === false) {
ioHost = withoutEmojis(ioHost);
}
this.ioHost = ioHost;
}

public async dispose(): Promise<void> {
Expand Down
23 changes: 22 additions & 1 deletion packages/@aws-cdk/toolkit/test/toolkit/toolkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,25 @@
* - Source Builders: Tests for the Cloud Assembly Source Builders are in `test/api/cloud-assembly/source-builder.test.ts`
*/

test('test', () => expect(true).toBe(true));
import { Toolkit } from '../../lib';
import { TestIoHost } from '../_helpers';

test('emojis can be stripped from message', async () => {
const ioHost = new TestIoHost();
const toolkit = new Toolkit({ ioHost, emojis: false });

await toolkit.ioHost.notify({
message: '💯Smile123😀',
action: 'deploy',
level: 'info',
code: 'CDK_TOOLKIT_I0000',
time: new Date(),
});

expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({
action: 'deploy',
level: 'info',
code: 'CDK_TOOLKIT_I0000',
message: 'Smile123',
}));
});

0 comments on commit 2e7c3e8

Please sign in to comment.