Skip to content

Commit

Permalink
fix(aws-cdk): cdk bootstrap print JSON template when using --json opt…
Browse files Browse the repository at this point in the history
…ion (#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*
  • Loading branch information
arewa authored Aug 31, 2022
1 parent 6f9aeda commit 7bc3d18
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/aws-cdk/lib/api/bootstrap/bootstrap-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { info } from 'console';
import * as path from 'path';
import * as cxapi from '@aws-cdk/cx-api';
import { warning } from '../../logging';
import { loadStructuredFile, toYAML } from '../../serialize';
import { loadStructuredFile, serializeStructure } from '../../serialize';
import { rootDir } from '../../util/directories';
import { SdkProvider } from '../aws-auth';
import { DeployStackResult } from '../deploy-stack';
Expand Down Expand Up @@ -33,9 +33,9 @@ export class Bootstrapper {
}
}

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

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ async function initCommandLine() {
const bootstrapper = new Bootstrapper(source);

if (args.showTemplate) {
return bootstrapper.showTemplate();
return bootstrapper.showTemplate(args.json);
}

return cli.bootstrap(args.ENVIRONMENTS, bootstrapper, {
Expand Down
21 changes: 21 additions & 0 deletions packages/aws-cdk/test/api/bootstrap.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CreateChangeSetInput } from 'aws-sdk/clients/cloudformation';
import { parse } from 'yaml';
import { Bootstrapper } from '../../lib/api/bootstrap';
import { deserializeStructure } from '../../lib/serialize';
import { MockSdkProvider, SyncHandlerSubsetOf } from '../util/mock-sdk';
Expand Down Expand Up @@ -297,3 +298,23 @@ test('stack is termination protected when set', async () => {
expect(executed).toBeTruthy();
expect(protectedTermination).toBeTruthy();
});

test('do showTemplate YAML', async () => {
process.stdout.write = jest.fn().mockImplementationOnce((template) => {
// THEN
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.');
});

// WHEN
await bootstrapper.showTemplate(false);
});

test('do showTemplate JSON', async () => {
process.stdout.write = jest.fn().mockImplementationOnce((template) => {
// THEN
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.');
});

// WHEN
await bootstrapper.showTemplate(true);
});

0 comments on commit 7bc3d18

Please sign in to comment.