Skip to content

Commit

Permalink
feat: make it possible to print a DB
Browse files Browse the repository at this point in the history
We used to only render a pretty diff of a spec database.

Make it possible to diff with an empty DB, which is effectively the same
as pretty-printing an entire DB.

Update the printing style a little to add more whitespace in places that
feel cramped.
  • Loading branch information
rix0rrr committed Dec 19, 2024
1 parent 66b7511 commit 725c00d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
22 changes: 17 additions & 5 deletions packages/@aws-cdk/service-spec-importers/src/cli/diff-db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { loadDatabase } from '@aws-cdk/service-spec-types';
import { emptyDatabase, loadDatabase } from '@aws-cdk/service-spec-types';
import { Command } from 'commander';
import { handleFailure } from './util';
import { DbDiff } from '../db-diff';
Expand All @@ -11,14 +11,26 @@ async function main() {
.name('diff-db')
.description('Calculate differences between two databases')
.argument('<db1>', 'First database file')
.argument('<db2>', 'Second database file')
.argument('[db2]', 'Second database file')
.option('-j, --json', 'Output json', false)
.parse();
const options = program.opts();
const args = program.args;

const db1 = await loadDatabase(args[0]);
const db2 = await loadDatabase(args[1]);
let db1;
let db2;
let realDiff;
if (args[1]) {
// Compare 2 actual database
db1 = await loadDatabase(args[0]);
db2 = await loadDatabase(args[1]);
realDiff = true;
} else {
// Compare 1 database to an empty one (count everything as added)
db1 = emptyDatabase();
db2 = await loadDatabase(args[0]);
realDiff = false;
}

const result = new DbDiff(db1, db2).diff();

Expand All @@ -34,7 +46,7 @@ async function main() {
console.log(new DiffFormatter(db1, db2).format(result));
}

process.exitCode = hasChanges ? 1 : 0;
process.exitCode = hasChanges && realDiff ? 1 : 0;
}

main().catch(handleFailure);
14 changes: 7 additions & 7 deletions packages/@aws-cdk/service-spec-importers/src/diff-fmt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { PrintableTree } from './printable-tree';
const ADDITION = '[+]';
const UPDATE = '[~]';
const REMOVAL = '[-]';
const META_INDENT = 2;
const META_INDENT = 6;

const [OLD_DB, NEW_DB] = [0, 1];

Expand Down Expand Up @@ -52,7 +52,7 @@ export class DiffFormatter {
),
listWithCaption(
'resources',
this.dbs[db].follow('hasResource', s).map((e) => this.renderResource(e.entity, db)),
this.dbs[db].follow('hasResource', s).map((e) => this.renderResource(e.entity, db).prefix([' '])),
),
]);
}
Expand All @@ -66,8 +66,8 @@ export class DiffFormatter {
'resources',
this.renderMapDiff(
s.resourceDiff,
(r, db) => this.renderResource(r, db),
(k, u) => this.renderUpdatedResource(k, u),
(r, db) => this.renderResource(r, db).prefix([' ']),
(k, u) => this.renderUpdatedResource(k, u).prefix([' ']),
),
),
];
Expand Down Expand Up @@ -95,7 +95,7 @@ export class DiffFormatter {
listWithCaption('attributes', this.renderProperties(r.attributes, db)),
listWithCaption(
'types',
this.dbs[db].follow('usesType', r).map((e) => this.renderTypeDefinition(e.entity, db)),
this.dbs[db].follow('usesType', r).map((e) => this.renderTypeDefinition(e.entity, db).prefix([' '])),
),
]);
}
Expand All @@ -121,7 +121,7 @@ export class DiffFormatter {
'types',
this.renderMapDiff(
r.typeDefinitionDiff,
(t, db) => this.renderTypeDefinition(t, db),
(t, db) => this.renderTypeDefinition(t, db).prefix([' ']),
(k, u) => this.renderUpdatedTypeDefinition(k, u),
),
),
Expand Down Expand Up @@ -177,7 +177,7 @@ export class DiffFormatter {
}

private renderProperties(ps: Record<string, Property>, db: number): PrintableTree[] {
return Object.entries(ps).map(([name, p]) => this.renderProperty(p, db).prefix([`${name}: `]));
return Object.entries(ps).map(([name, p]) => this.renderProperty(p, db).prefix([' ', `${name}: `]));
}

private renderMapDiff<E, U>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ export class PrintableTree {
return this;
}

/**
* Prefix all lines of the tree with the given list of cells.
*
* The first line will be prefixed with `first`.
*
* The other lines will be prefixed with `rest` if given, or the length of
* `first` in spaces if rest is not given.
*/
public prefix(first: string[], rest?: string[]) {
rest = rest ?? first.map((x) => ' '.repeat(x.length));

Expand Down

0 comments on commit 725c00d

Please sign in to comment.