apisurf
Version:
Analyze API surface changes between npm package versions to catch breaking changes
46 lines (45 loc) • 2.13 kB
JavaScript
import { analyzeNpmPackageDiff } from './analyzeNpmPackageDiff.js';
import { compareApiSurfaces } from './compareApiSurfaces.js';
import { extractApiSurface } from '../utilities/extractApiSurface.js';
import { findPackagesToAnalyze } from '../utilities/findPackagesToAnalyze.js';
import { getCurrentBranch } from '../utilities/getCurrentBranch.js';
import { calculateSemverImpact } from '../utilities/calculateSemverImpact.js';
import { writeApiSurfaceLog } from '../utilities/writeApiSurfaceLog.js';
/**
* Analyzes API surface differences between Git branches or NPM package versions.
* Supports both Git-based comparisons and NPM package version comparisons.
*/
export async function analyzeDiff(options) {
// Check if this is npm mode
if (options.npmPackage) {
return analyzeNpmPackageDiff(options);
}
// Git mode (existing logic)
const currentBranch = getCurrentBranch();
const headBranch = options.head || currentBranch;
// Find packages to analyze
const packages = findPackagesToAnalyze(options.packages);
const packageDiffs = [];
for (const pkg of packages) {
const baseSurface = await extractApiSurface(pkg, options.base);
const headSurface = await extractApiSurface(pkg, headBranch);
// Log API surfaces if logfile is specified
if (options.logfile) {
await writeApiSurfaceLog(options.logfile, baseSurface, headSurface, pkg.name, options.base, headBranch, options.format);
}
const diff = compareApiSurfaces(baseSurface, headSurface, pkg);
if (diff.breakingChanges.length > 0 || diff.nonBreakingChanges.length > 0) {
packageDiffs.push(diff);
}
}
const hasBreakingChanges = packageDiffs.some(pkg => pkg.breakingChanges.length > 0);
const semverImpact = calculateSemverImpact(packageDiffs);
return {
hasBreakingChanges,
packages: packageDiffs,
summary: hasBreakingChanges
? `Breaking changes detected. Minimum semver bump: ${semverImpact.minimumBump}`
: 'No breaking changes detected',
semverImpact
};
}