diff --git a/packages/aws-cdk/lib/api/bootstrap/bootstrap-environment.ts b/packages/aws-cdk/lib/api/bootstrap/bootstrap-environment.ts index a7b63214a3c80..f86a3d96b85b5 100644 --- a/packages/aws-cdk/lib/api/bootstrap/bootstrap-environment.ts +++ b/packages/aws-cdk/lib/api/bootstrap/bootstrap-environment.ts @@ -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'; @@ -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`); } /** diff --git a/packages/aws-cdk/lib/cli.ts b/packages/aws-cdk/lib/cli.ts index 28fd01f308e39..b9cf877c7873b 100644 --- a/packages/aws-cdk/lib/cli.ts +++ b/packages/aws-cdk/lib/cli.ts @@ -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, { diff --git a/packages/aws-cdk/test/api/bootstrap.test.ts b/packages/aws-cdk/test/api/bootstrap.test.ts index 888a649cc735e..0362ad8089b40 100644 --- a/packages/aws-cdk/test/api/bootstrap.test.ts +++ b/packages/aws-cdk/test/api/bootstrap.test.ts @@ -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'; @@ -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); +});