diff --git a/packages/@aws-cdk/service-spec-importers/src/cli/diff-db.ts b/packages/@aws-cdk/service-spec-importers/src/cli/diff-db.ts index 0836224d7..bd7c169de 100644 --- a/packages/@aws-cdk/service-spec-importers/src/cli/diff-db.ts +++ b/packages/@aws-cdk/service-spec-importers/src/cli/diff-db.ts @@ -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'; @@ -11,14 +11,26 @@ async function main() { .name('diff-db') .description('Calculate differences between two databases') .argument('', 'First database file') - .argument('', '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(); @@ -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); diff --git a/packages/@aws-cdk/service-spec-importers/src/diff-fmt.ts b/packages/@aws-cdk/service-spec-importers/src/diff-fmt.ts index 54d7b9f19..c9b9d95f2 100644 --- a/packages/@aws-cdk/service-spec-importers/src/diff-fmt.ts +++ b/packages/@aws-cdk/service-spec-importers/src/diff-fmt.ts @@ -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]; @@ -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([' '])), ), ]); } @@ -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([' ']), ), ), ]; @@ -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([' '])), ), ]); } @@ -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), ), ), @@ -177,7 +177,7 @@ export class DiffFormatter { } private renderProperties(ps: Record, 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( diff --git a/packages/@aws-cdk/service-spec-importers/src/printable-tree.ts b/packages/@aws-cdk/service-spec-importers/src/printable-tree.ts index 9137ea83c..d2931863a 100644 --- a/packages/@aws-cdk/service-spec-importers/src/printable-tree.ts +++ b/packages/@aws-cdk/service-spec-importers/src/printable-tree.ts @@ -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));