diff --git a/src/cfn/convertStackToIidy.ts b/src/cfn/convertStackToIidy.ts index 1a44a200..9868c4c4 100644 --- a/src/cfn/convertStackToIidy.ts +++ b/src/cfn/convertStackToIidy.ts @@ -8,6 +8,7 @@ import def from '../default'; import {getKMSAliasForParameter} from '../params'; import {SUCCESS} from '../statusCodes'; import * as yaml from '../yaml'; +import {Visitor} from '../preprocess/visitor'; import {getStackDescription} from './getStackDescription'; import {parseTemplateBody} from "./parseTemplateBody"; import {StackArgs} from "./types"; @@ -106,6 +107,23 @@ export type ConvertStackArguments = GenericCLIArguments & { moveParamsToSsm: boolean; }; +export function readTemplateObj(templateBody: string, sortkeys: boolean): Object { + const templateObj0 = parseTemplateBody(templateBody); + let templateObj; + if (sortkeys) { + templateObj = deepMapValues(templateObj0, sortCallback, 'root'); + } else { + templateObj = templateObj0; + } + + const visitor = new Visitor(); + return visitor.visitNode(templateObj, 'Root', { + GlobalAccumulator: {}, + $envValues: {}, + Stack: [] + }); +}; + export async function convertStackToIIDY(argv0: GenericCLIArguments): Promise { const argv = argv0 as ConvertStackArguments; await configureAWS(argv); @@ -119,14 +137,7 @@ export async function convertStackToIIDY(argv0: GenericCLIArguments): Promise { @@ -19,4 +21,23 @@ describe('cfn', () => { expect(args.ServiceRoleARN).to.equal('arn:aws:iam::001234567890:role/name'); }); }); + + describe('readTemplateObj', () => { + it('handles AWSTemplateFormatVersion strings properly', async function() { + const templateBody = 'AWSTemplateFormatVersion: 2010-09-09'; + const templateObj = readTemplateObj(templateBody, true); + expect(yaml.dump(templateObj)).to.equal( + "AWSTemplateFormatVersion: '2010-09-09'\n" + ); + }); + + it('handles Version strings properly', async function() { + const templateBody = 'Version: 2010-09-09\n'; + const templateObj = readTemplateObj(templateBody, false); + expect(yaml.dump(templateObj)).to.equal( + "Version: '2010-09-09'\n" + ); + }); + }); + });