Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 56 additions & 30 deletions packages/cli/src/commands/diff.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need the complete license header in each file.

// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { defineCommand } from 'citty';
import { lint } from '../linter/index.js';
Expand All @@ -22,62 +12,98 @@ export default defineCommand({
name: 'diff',
description: 'Compare two DESIGN.md files and report changes.',
},

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to set up pre-commit hooks for linting but if you could remove the extra spaces before than that would be much appreciated.

args: {
before: {
type: 'positional',
description: 'Path to the "before" DESIGN.md',
required: true,
},

after: {
type: 'positional',
description: 'Path to the "after" DESIGN.md',
required: true,
},

format: {
type: 'string',
description: 'Output format: json or text',
default: 'json',
},
},

async run({ args }) {
const beforeContent = await readInput(args.before);
const afterContent = await readInput(args.after);
// Parallel I/O
const [beforeContent, afterContent] = await Promise.all([
readInput(args.before),
readInput(args.after),
]);

// Parallel CPU work
const [beforeReport, afterReport] = await Promise.all([
Comment on lines -43 to +44
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great. readInput() is async and this is a good win for parallelization.

Promise.resolve(lint(beforeContent)),
Promise.resolve(lint(afterContent)),
]);
Comment on lines +43 to +47
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lint() function is sync so wrapping it in a promise is extra overhead and doesn't add parallelization.

Suggested change
// Parallel CPU work
const [beforeReport, afterReport] = await Promise.all([
Promise.resolve(lint(beforeContent)),
Promise.resolve(lint(afterContent)),
]);
const beforeReport = lint(beforeContent);
const afterReport = lint(afterContent);


const beforeDS = beforeReport.designSystem;
const afterDS = afterReport.designSystem;

const beforeSummary = beforeReport.summary;
const afterSummary = afterReport.summary;

const errorDelta = afterSummary.errors - beforeSummary.errors;
const warningDelta = afterSummary.warnings - beforeSummary.warnings;

const beforeReport = lint(beforeContent);
const afterReport = lint(afterContent);
const regression = errorDelta > 0 || warningDelta > 0;

const diff = {
tokens: {
colors: diffMaps(beforeReport.designSystem.colors, afterReport.designSystem.colors),
typography: diffMaps(beforeReport.designSystem.typography, afterReport.designSystem.typography),
rounded: diffMaps(beforeReport.designSystem.rounded, afterReport.designSystem.rounded),
spacing: diffMaps(beforeReport.designSystem.spacing, afterReport.designSystem.spacing),
colors: diffMaps(beforeDS.colors, afterDS.colors),
typography: diffMaps(beforeDS.typography, afterDS.typography),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extra spaces are most pronounced in this block.

rounded: diffMaps(beforeDS.rounded, afterDS.rounded),
spacing: diffMaps(beforeDS.spacing, afterDS.spacing),

// Lazy serialization only when needed
components: diffMaps(
serializeComponents(beforeReport.designSystem.components),
serializeComponents(afterReport.designSystem.components),
serializeComponents(beforeDS.components),
serializeComponents(afterDS.components),
),
},

findings: {
before: beforeReport.summary,
after: afterReport.summary,
before: beforeSummary,
after: afterSummary,

delta: {
errors: afterReport.summary.errors - beforeReport.summary.errors,
warnings: afterReport.summary.warnings - beforeReport.summary.warnings,
errors: errorDelta,
warnings: warningDelta,
},
},
regression: afterReport.summary.errors > beforeReport.summary.errors
|| afterReport.summary.warnings > beforeReport.summary.warnings,

regression,
};

console.log(formatOutput(diff, args));
process.exitCode = diff.regression ? 1 : 0;

process.exitCode = regression ? 1 : 0;
},
});

function serializeComponents(components: Map<string, ComponentDef>): Map<string, Record<string, unknown>> {
function serializeComponents(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original uses Object.fromEntries() which is concise and idiomatic. This changes removes it for what effectively a manual reimplementation. It's more code for the same result.

Also, .entries() is added to components.entries() but Map is already iterable. The original [key, value] pair (const [name, comp] of components) is equivalent and more concise.

components: Map<string, ComponentDef>,
): Map<string, Record<string, unknown>> {
const result = new Map<string, Record<string, unknown>>();
for (const [name, comp] of components) {
result.set(name, Object.fromEntries(comp.properties));

for (const [name, comp] of components.entries()) {
const props: Record<string, unknown> = {};

for (const [key, value] of comp.properties) {
props[key] = value;
}

result.set(name, props);
}

return result;
}
Loading