Skip to content

Commit

Permalink
Revert "Revert "Revert "Revert "Revert "temp. This is probably the wr…
Browse files Browse the repository at this point in the history
…ong approach so far, but save it just in case this other idea doesn't work"""""

This reverts commit 7725f60.
  • Loading branch information
comcalvi committed Jan 10, 2024
1 parent 7725f60 commit aa3e4c7
Showing 1 changed file with 8 additions and 52 deletions.
60 changes: 8 additions & 52 deletions packages/@aws-cdk/cloudformation-diff/lib/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ const UPDATE = chalk.yellow('[~]');
const REMOVAL = chalk.red('[-]');

class Formatter {
private nestedDiffIndent: string = ' ';
constructor(
private readonly stream: FormatStream,
private readonly logicalToPathMap: { [logicalId: string]: string },
Expand All @@ -97,10 +96,6 @@ 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');
}
Expand All @@ -109,29 +104,17 @@ class Formatter {
title: string,
entryType: string,
collection: DifferenceCollection<V, T>,
formatter: (type: string, id: string, diff: T, nestedIndent: boolean) => void = this.formatDifference.bind(this)) {
formatter: (type: string, id: string, diff: T) => void = this.formatDifference.bind(this)) {

if (collection.differenceCount === 0) {
return;
}

this.printSectionHeader(title);
collection.forEachDifference((id, diff) => formatter(entryType, id, diff, false));
collection.forEachDifference((id, diff) => formatter(entryType, id, diff));
this.printSectionFooter();
}

public formatNestedSection<V, T extends Difference<V>>(
entryType: string,
collection: DifferenceCollection<V, T>,
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)));
}
Expand Down Expand Up @@ -170,14 +153,15 @@ 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, useNestedIndent: boolean) {
public formatResourceDifference(_type: string, logicalId: string, diff: ResourceDifference) {
if (!diff.isDifferent) { return; }

const resourceType = diff.isRemoval ? diff.oldResourceType : diff.newResourceType;

if (useNestedIndent) {
this.write(this.nestedDiffIndent);
if (Object.keys(diff.nestedChanges).length > 0) {
formatDifferences(this.stream, diff.nestedChanges as TemplateDiff, this.logicalToPathMap, this.context);
}

// eslint-disable-next-line max-len
this.print(`${this.formatPrefix(diff)} ${this.formatValue(resourceType, chalk.cyan)} ${this.formatLogicalId(logicalId)} ${this.formatImpact(diff.changeImpact)}`);

Expand All @@ -186,11 +170,8 @@ class Formatter {
let processedCount = 0;
diff.forEachDifference((_, name, values) => {
processedCount += 1;
this.formatTreeDiff(name, values, processedCount === differenceCount, useNestedIndent);
this.formatTreeDiff(name, values, processedCount === differenceCount);
});
if (Object.keys(diff.nestedChanges).length > 0) {
this.formatNestedDifferences(diff.nestedChanges as TemplateDiff, this.formatPrefix(diff));
}
}
}

Expand Down Expand Up @@ -240,7 +221,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<any>, last: boolean, useNestedIndent: boolean) {
public formatTreeDiff(name: string, diff: Difference<any>, last: boolean) {
let additionalInfo = '';
if (isPropertyDifference(diff)) {
if (diff.changeImpact === ResourceImpact.MAY_REPLACE) {
Expand All @@ -249,9 +230,6 @@ 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 ? ' ' : '│'}`);
}
Expand Down Expand Up @@ -420,28 +398,6 @@ 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);
}
}

/**
Expand Down

0 comments on commit aa3e4c7

Please sign in to comment.