Skip to content

Commit 7bc3d18

Browse files
authored
fix(aws-cdk): cdk bootstrap print JSON template when using --json option (#21852)
Fix for the issue #21456. Added support of `--json` option for `cdk bootstrap --show-template` command. `cdk bootstrap --show-template` - will print YAML template `cdk bootstrap --show-template --json` - will print JSON template **How I tested it locally?** - Prepared a package with fix with `yarn package` and installed with `npm install -g dist/js/aws-cdk-0.0.0.tgz` - Reproduced steps from the bug #21456 - Ensured that the issue is fixed ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 6f9aeda commit 7bc3d18

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

packages/aws-cdk/lib/api/bootstrap/bootstrap-environment.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { info } from 'console';
22
import * as path from 'path';
33
import * as cxapi from '@aws-cdk/cx-api';
44
import { warning } from '../../logging';
5-
import { loadStructuredFile, toYAML } from '../../serialize';
5+
import { loadStructuredFile, serializeStructure } from '../../serialize';
66
import { rootDir } from '../../util/directories';
77
import { SdkProvider } from '../aws-auth';
88
import { DeployStackResult } from '../deploy-stack';
@@ -33,9 +33,9 @@ export class Bootstrapper {
3333
}
3434
}
3535

36-
public async showTemplate() {
36+
public async showTemplate(json: boolean) {
3737
const template = await this.loadTemplate();
38-
process.stdout.write(`${toYAML(template)}\n`);
38+
process.stdout.write(`${serializeStructure(template, json)}\n`);
3939
}
4040

4141
/**

packages/aws-cdk/lib/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ async function initCommandLine() {
431431
const bootstrapper = new Bootstrapper(source);
432432

433433
if (args.showTemplate) {
434-
return bootstrapper.showTemplate();
434+
return bootstrapper.showTemplate(args.json);
435435
}
436436

437437
return cli.bootstrap(args.ENVIRONMENTS, bootstrapper, {

packages/aws-cdk/test/api/bootstrap.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CreateChangeSetInput } from 'aws-sdk/clients/cloudformation';
2+
import { parse } from 'yaml';
23
import { Bootstrapper } from '../../lib/api/bootstrap';
34
import { deserializeStructure } from '../../lib/serialize';
45
import { MockSdkProvider, SyncHandlerSubsetOf } from '../util/mock-sdk';
@@ -297,3 +298,23 @@ test('stack is termination protected when set', async () => {
297298
expect(executed).toBeTruthy();
298299
expect(protectedTermination).toBeTruthy();
299300
});
301+
302+
test('do showTemplate YAML', async () => {
303+
process.stdout.write = jest.fn().mockImplementationOnce((template) => {
304+
// THEN
305+
expect(parse(template)).toHaveProperty('Description', 'The CDK Toolkit Stack. It was created by `cdk bootstrap` and manages resources necessary for managing your Cloud Applications with AWS CDK.');
306+
});
307+
308+
// WHEN
309+
await bootstrapper.showTemplate(false);
310+
});
311+
312+
test('do showTemplate JSON', async () => {
313+
process.stdout.write = jest.fn().mockImplementationOnce((template) => {
314+
// THEN
315+
expect(JSON.parse(template)).toHaveProperty('Description', 'The CDK Toolkit Stack. It was created by `cdk bootstrap` and manages resources necessary for managing your Cloud Applications with AWS CDK.');
316+
});
317+
318+
// WHEN
319+
await bootstrapper.showTemplate(true);
320+
});

0 commit comments

Comments
 (0)