From f793503b0bbd18ba339205f152c54de4615ff8d0 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Tue, 9 Jan 2024 15:51:02 -0800 Subject: [PATCH] Revert "Revert "temp. This is probably the wrong approach so far, but save it just in case this other idea doesn't work"" This reverts commit e1e4d34c25f67284bb38dd92df1e444e671d428f. --- .../cloudformation-diff/lib/format.ts | 60 ++++++++++++++++--- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/packages/@aws-cdk/cloudformation-diff/lib/format.ts b/packages/@aws-cdk/cloudformation-diff/lib/format.ts index 98ef1dc69d1ea..4e9a951e463d3 100644 --- a/packages/@aws-cdk/cloudformation-diff/lib/format.ts +++ b/packages/@aws-cdk/cloudformation-diff/lib/format.ts @@ -81,6 +81,7 @@ const UPDATE = chalk.yellow('[~]'); const REMOVAL = chalk.red('[-]'); class Formatter { + private nestedDiffIndent: string = ' '; constructor( private readonly stream: FormatStream, private readonly logicalToPathMap: { [logicalId: string]: string }, @@ -96,6 +97,10 @@ class Formatter { this.stream.write(chalk.white(format(fmt, ...args)) + '\n'); } + public write(fmt: string, ...args: any[]) { + this.stream.write(chalk.white(format(fmt, ...args))); + } + public warning(fmt: string, ...args: any[]) { this.stream.write(chalk.yellow(format(fmt, ...args)) + '\n'); } @@ -104,17 +109,29 @@ class Formatter { title: string, entryType: string, collection: DifferenceCollection, - formatter: (type: string, id: string, diff: T) => void = this.formatDifference.bind(this)) { + formatter: (type: string, id: string, diff: T, nestedIndent: boolean) => void = this.formatDifference.bind(this)) { if (collection.differenceCount === 0) { return; } this.printSectionHeader(title); - collection.forEachDifference((id, diff) => formatter(entryType, id, diff)); + collection.forEachDifference((id, diff) => formatter(entryType, id, diff, false)); this.printSectionFooter(); } + public formatNestedSection>( + entryType: string, + collection: DifferenceCollection, + formatter: (type: string, id: string, diff: T, nestedIndent: boolean) => void = this.formatDifference.bind(this)) { + + if (collection.differenceCount === 0) { + return; + } + + collection.forEachDifference((id, diff) => formatter(entryType, id, diff, true)); + } + public printSectionHeader(title: string) { this.print(chalk.underline(chalk.bold(title))); } @@ -153,15 +170,14 @@ class Formatter { * @param logicalId the logical ID of the resource that changed. * @param diff the change to be rendered. */ - public formatResourceDifference(_type: string, logicalId: string, diff: ResourceDifference) { + public formatResourceDifference(_type: string, logicalId: string, diff: ResourceDifference, useNestedIndent: boolean) { if (!diff.isDifferent) { return; } const resourceType = diff.isRemoval ? diff.oldResourceType : diff.newResourceType; - if (Object.keys(diff.nestedChanges).length > 0) { - formatDifferences(this.stream, diff.nestedChanges as TemplateDiff, this.logicalToPathMap, this.context); + if (useNestedIndent) { + this.write(this.nestedDiffIndent); } - // eslint-disable-next-line max-len this.print(`${this.formatPrefix(diff)} ${this.formatValue(resourceType, chalk.cyan)} ${this.formatLogicalId(logicalId)} ${this.formatImpact(diff.changeImpact)}`); @@ -170,8 +186,11 @@ class Formatter { let processedCount = 0; diff.forEachDifference((_, name, values) => { processedCount += 1; - this.formatTreeDiff(name, values, processedCount === differenceCount); + this.formatTreeDiff(name, values, processedCount === differenceCount, useNestedIndent); }); + if (Object.keys(diff.nestedChanges).length > 0) { + this.formatNestedDifferences(diff.nestedChanges as TemplateDiff, this.formatPrefix(diff)); + } } } @@ -221,7 +240,7 @@ class Formatter { * @param diff the difference on the tree. * @param last whether this is the last node of a parent tree. */ - public formatTreeDiff(name: string, diff: Difference, last: boolean) { + public formatTreeDiff(name: string, diff: Difference, last: boolean, useNestedIndent: boolean) { let additionalInfo = ''; if (isPropertyDifference(diff)) { if (diff.changeImpact === ResourceImpact.MAY_REPLACE) { @@ -230,6 +249,9 @@ class Formatter { additionalInfo = ' (requires replacement)'; } } + if (useNestedIndent) { + this.write(this.nestedDiffIndent); + } this.print(' %sā”€ %s %s%s', last ? 'ā””' : 'ā”œ', this.changeTag(diff.oldValue, diff.newValue), name, additionalInfo); return this.formatObjectDiff(diff.oldValue, diff.newValue, ` ${last ? ' ' : 'ā”‚'}`); } @@ -398,6 +420,28 @@ class Formatter { return '${' + (this.normalizedLogicalIdPath(logId) || logId) + (suffix || '') + '}'; }); } + + private formatNestedDifferences(templateDiff: TemplateDiff, prefix: string) { + this.nestedDiffIndent += ' '; + this.print(`${this.nestedDiffIndent}${prefix} NestedTemplate`); + if (templateDiff.awsTemplateFormatVersion || templateDiff.transform || templateDiff.description) { + this.printSectionHeader('Template'); + this.formatDifference('AWSTemplateFormatVersion', 'AWSTemplateFormatVersion', templateDiff.awsTemplateFormatVersion); + this.formatDifference('Transform', 'Transform', templateDiff.transform); + this.formatDifference('Description', 'Description', templateDiff.description); + this.printSectionFooter(); + } + + //formatSecurityChangesWithBanner(this, templateDiff); + + this.formatNestedSection('Parameter', templateDiff.parameters); + this.formatNestedSection('Metadata', templateDiff.metadata); + this.formatNestedSection('Mapping', templateDiff.mappings); + this.formatNestedSection('Condition', templateDiff.conditions); + this.formatNestedSection('Resource', templateDiff.resources, this.formatResourceDifference.bind(this)); + this.formatNestedSection('Output', templateDiff.outputs); + this.formatNestedSection('Unknown', templateDiff.unknown); + } } /**