@adobe/spectrum-diff-core
v1.1.1
Published
Shared core library for Spectrum diff generation tools (tokens, component schemas, etc.)
Readme
Spectrum Diff Core
Shared core library for Spectrum diff generation tools (tokens, component schemas, etc.).
Overview
This package provides the foundational components for creating consistent diff tools across the Spectrum ecosystem. It includes:
- Core utilities: File import/export, diff operations, type checking
- CLI framework: Base CLI structure with consistent option handling
- Template system: Handlebars-based formatting with shared helpers
- Output handling: File output and console formatting
Installation
pnpm add @adobe/spectrum-diff-coreUsage
Creating a Diff Tool
import { BaseCLI, FileLoader, detailedDiff } from "@adobe/spectrum-diff-core";
class MyDiffTool extends BaseCLI {
constructor() {
super({
toolName: "mydiff",
dataType: "mydata",
packagePath: "packages/mydata/src",
manifestFile: "manifest.json",
});
}
async generateDiff(original, updated) {
const changes = detailedDiff(original, updated);
// Add your specific change detection logic here
return this.processChanges(changes);
}
}Using File Loading
import { FileLoader } from "@adobe/spectrum-diff-core";
const loader = new FileLoader();
// Load from remote repository
const remoteData = await loader.loadRemoteFiles(
["file1.json", "file2.json"],
"v1.0.0",
null,
"owner/repo",
"api-key",
"packages/data/src",
);
// Load from local filesystem
const localData = await loader.loadLocalFiles("packages/data/src", [
"file1.json",
"file2.json",
]);Using Templates
import { HandlebarsFormatter } from "@adobe/spectrum-diff-core";
const formatter = new HandlebarsFormatter({
template: "cli",
dataType: "tokens",
});
const output = await formatter.format(diffResult, options);CLI Framework
The BaseCLI class provides a consistent CLI structure:
mydiff report --old-version v1.0.0 --new-version v1.1.0 --format markdown --output report.mdCommon CLI Options:
--old-[type]-version/--new-[type]-version(version comparison)--old-[type]-branch/--new-[type]-branch(branch comparison)--local(local file comparison)--format(cli, markdown, handlebars)--template(built-in template names)--output(file output)--debug(debug output)
Template System
Templates are organized by data type:
templates/
├── base/ # Shared templates
│ ├── cli.hbs
│ ├── markdown.hbs
│ └── json.hbs
├── tokens/ # Token-specific templates
└── schemas/ # Schema-specific templatesAvailable Helpers
{{hasKeys obj}}- Check if object has properties{{totalItems result}}- Calculate total number of changes{{capitalize str}}- Capitalize string{{cleanPath path}}- Clean up property paths{{formatDate date}}- Format timestamps{{hilite}},{{error}},{{passing}}- Terminal colors{{bold}},{{dim}},{{emphasis}}- Text formatting
API Reference
Core
detailedDiff(original, updated)- Generate detailed diffisObject(obj),isString(str), etc. - Type checking utilitiesdeepClone(obj)- Deep clone objectsgetNestedProperty(obj, path)- Safe property access
File Import
FileLoader- Main file loading orchestratorRemoteFileFetcher- Handle remote repository fetchingLocalFileSystem- Handle local file operations
CLI
BaseCLI- Base class for creating CLI toolsnormalizeOptions(options)- Normalize CLI optionsdetermineStrategy(options)- Determine loading strategy
Formatters
HandlebarsFormatter- Template-based formattingstoreOutput(content, path)- File output utility
Development
# Install dependencies
pnpm install
# Run tests
pnpm test
# Lint code
pnpm lint
# Fix linting issues
pnpm lint:fixLicense
Apache-2.0
