Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | 1x 3x 3x 1x 4x 4x 4x 4x 4x 5x 5x 5x 5x 5x 5x 5x 5x 3x 6x 2x 3x 6x 2x 3x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 6x 6x 5x 5x 5x 5x 4x 5x 5x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 2x 5x 2x 2x 2x 2x 2x 2x 3x 1x 2x 2x 4x 4x 4x 2x 2x 2x 1x 6x 1x 5x 5x 25x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 3x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5x 2x 2x 2x 2x 2x 2x 2x 1x 2x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x | import path from 'node:path';
import fs from 'fs-extra';
import chalk from 'chalk';
import { execSync } from 'node:child_process';
import { pathToFileURL } from 'node:url';
export function getShortSha(execSyncFn = execSync) {
try {
return execSyncFn('git rev-parse --short=7 HEAD', { encoding: 'utf8' }).trim();
} catch {
return 'unknown';
}
}
export function resolveConfigPath(configArg, cwd = process.cwd()) {
return path.resolve(cwd, configArg);
}
export async function loadConfig(configPath) {
Iif (!fs.existsSync(configPath)) {
throw new Error(`Config file not found: ${configPath}`);
}
const configUrl = pathToFileURL(configPath).href;
const configModule = await import(configUrl);
return configModule.default || configModule;
}
export function resolveCompareSettings(config, cliArgs, cwd = process.cwd()) {
const name = config.name || 'Comparison';
const sourceDir = path.resolve(cwd, cliArgs.source || config.source);
const baselineDir = path.resolve(cwd, config.baseline);
const outputDir = path.resolve(cwd, config.output);
const globPattern = config.glob || '**/*';
const failOnDiff = cliArgs['fail-on-diff'] !== undefined ? cliArgs['fail-on-diff'] : config.failOnDiff !== false;
const plugins = config.plugins || [];
return {
name,
sourceDir,
baselineDir,
outputDir,
globPattern,
failOnDiff,
plugins,
};
}
export function collectCompareHooks(plugins = []) {
const beforeCompareHooks = plugins
.filter((plugin) => typeof plugin.beforeCompare === 'function')
.map((plugin) => plugin.beforeCompare);
const afterCompareHooks = plugins
.filter((plugin) => typeof plugin.afterCompare === 'function')
.map((plugin) => plugin.afterCompare);
return {
beforeCompareHooks,
afterCompareHooks,
};
}
export function createReportDir({ outputDir, timestamp, shortSha, fsImpl = fs }) {
const timestampStr = timestamp.toISOString().replace(/T/, '_').replace(/:/g, '-').replace(/\..+/, '');
const reportDirName = `${timestampStr}_${shortSha}`;
const reportDir = path.join(outputDir, reportDirName);
fsImpl.ensureDirSync(reportDir);
return reportDir;
}
export function compareAllFiles({
allFiles,
baselineFilesDir,
sourceDir,
compareFiles,
beforeCompareHooks,
afterCompareHooks,
log = console.log,
}) {
const results = [];
const counts = {
identical: 0,
different: 0,
added: 0,
removed: 0,
};
for (const relativePath of allFiles) {
const baselinePath = path.join(baselineFilesDir, relativePath);
const currentPath = path.join(sourceDir, relativePath);
let result = compareFiles(baselinePath, currentPath, beforeCompareHooks);
if (!result) continue;
result.relativePath = relativePath;
result.baselinePath = baselinePath;
result.currentPath = currentPath;
for (const hook of afterCompareHooks) {
result = hook(result, relativePath);
}
results.push(result);
switch (result.status) {
case 'identical':
counts.identical++;
log(chalk.green(` ā ${relativePath}`));
break;
case 'different':
counts.different++;
log(chalk.yellow(` ā ${relativePath} (+${result.linesAdded} -${result.linesRemoved})`));
break;
case 'added':
counts.added++;
log(chalk.cyan(` + ${relativePath} (new)`));
break;
case 'removed':
counts.removed++;
log(chalk.red(` - ${relativePath} (removed)`));
break;
default:
break;
}
}
return {
results,
counts,
};
}
export function buildReport({ name, timestamp, shortSha, baselineDir, sourceDir, results, counts }) {
return {
name,
timestamp: timestamp.toISOString(),
sha: shortSha,
baselinePath: baselineDir,
sourcePath: sourceDir,
summary: {
total: results.length,
identical: counts.identical,
different: counts.different,
added: counts.added,
removed: counts.removed,
},
files: results.map((result) => ({
relativePath: result.relativePath,
status: result.status,
linesAdded: result.linesAdded,
linesRemoved: result.linesRemoved,
...(result.pluginData || {}),
})),
};
}
export function writeReportFiles({ reportDir, report, fsImpl = fs, generateTextReport, generateIndexHtml }) {
const reportJsonPath = path.join(reportDir, 'report.json');
fsImpl.writeFileSync(reportJsonPath, JSON.stringify(report, null, 2), 'utf8');
const reportTxtPath = path.join(reportDir, 'report.txt');
fsImpl.writeFileSync(reportTxtPath, generateTextReport(report), 'utf8');
const indexHtmlPath = path.join(reportDir, 'index.html');
fsImpl.writeFileSync(indexHtmlPath, generateIndexHtml(report), 'utf8');
}
export function writeHtmlDiffFiles({ filesWithDiffs, reportDir, name, generateHtmlDiff, log = console.log }) {
if (filesWithDiffs.length === 0) {
return;
}
log(chalk.gray(`\n Generating HTML diffs...`));
for (const result of filesWithDiffs) {
const safeFileName = result.relativePath.replace(/[^a-zA-Z0-9]/g, '_') + '.html';
const htmlPath = path.join(reportDir, safeFileName);
generateHtmlDiff(result, htmlPath, reportDir, name);
}
log(chalk.green(` ā Generated ${filesWithDiffs.length} HTML diff(s)`));
}
export function requiresFailureExit(counts, failOnDiff) {
const hasDifferences = counts.different > 0 || counts.added > 0 || counts.removed > 0;
return hasDifferences && failOnDiff;
}
export async function compareOutput(
cliArgs,
{
cwd = process.cwd(),
fsImpl = fs,
log = console.log,
now = () => new Date(),
execSyncFn = execSync,
deps = {},
} = {},
) {
if (!cliArgs?.config) {
throw new Error('Config argument is required');
}
const requiredDeps = [
'readManifest',
'compareFiles',
'getAllFiles',
'generateHtmlDiff',
'generateIndexHtml',
'generateTextReport',
];
for (const key of requiredDeps) {
if (typeof deps[key] !== 'function') {
throw new Error(`Missing required dependency: ${key}`);
}
}
const configPath = resolveConfigPath(cliArgs.config, cwd);
const config = await loadConfig(configPath);
const { name, sourceDir, baselineDir, outputDir, globPattern, failOnDiff, plugins } = resolveCompareSettings(
config,
cliArgs,
cwd,
);
log(chalk.blue(`š Comparing: ${name}\n`));
log(chalk.gray(` Source: ${sourceDir}`));
log(chalk.gray(` Baseline: ${baselineDir}`));
log(chalk.gray(` Output: ${outputDir}`));
log(chalk.gray(` Pattern: ${globPattern}\n`));
const baselineFilesDir = path.join(baselineDir, 'files');
const manifest = deps.readManifest(baselineDir);
if (!manifest) {
throw new Error(`Baseline not found. Run capture-baseline first. Expected: ${baselineDir}/manifest.json`);
}
if (!fsImpl.existsSync(sourceDir)) {
throw new Error(`Source directory not found: ${sourceDir}`);
}
const timestamp = now();
const shortSha = getShortSha(execSyncFn);
const reportDir = createReportDir({ outputDir, timestamp, shortSha, fsImpl });
const { beforeCompareHooks, afterCompareHooks } = collectCompareHooks(plugins);
const allFiles = deps.getAllFiles(baselineFilesDir, sourceDir, globPattern);
log(chalk.gray(` Comparing ${allFiles.size} file(s)...\n`));
const { results, counts } = compareAllFiles({
allFiles,
baselineFilesDir,
sourceDir,
compareFiles: deps.compareFiles,
beforeCompareHooks,
afterCompareHooks,
log,
});
const report = buildReport({
name,
timestamp,
shortSha,
baselineDir,
sourceDir,
results,
counts,
});
writeReportFiles({
reportDir,
report,
fsImpl,
generateTextReport: deps.generateTextReport,
generateIndexHtml: deps.generateIndexHtml,
});
const filesWithDiffs = results.filter((result) => result.status !== 'identical');
writeHtmlDiffFiles({
filesWithDiffs,
reportDir,
name,
generateHtmlDiff: deps.generateHtmlDiff,
log,
});
log(chalk.blue('\n' + '='.repeat(50)));
log(chalk.blue('Comparison Summary:'));
log(chalk.green(` ā Identical: ${counts.identical}`));
Eif (counts.different > 0) {
log(chalk.yellow(` ā Different: ${counts.different}`));
}
if (counts.added > 0) {
log(chalk.cyan(` + Added: ${counts.added}`));
}
if (counts.removed > 0) {
log(chalk.red(` - Removed: ${counts.removed}`));
}
log(chalk.blue('='.repeat(50)));
log(chalk.gray(`\nReports saved to: ${reportDir}`));
log(chalk.gray(` JSON: report.json`));
log(chalk.gray(` Text: report.txt`));
log(chalk.gray(` HTML: index.html`));
const shouldFailExit = requiresFailureExit(counts, failOnDiff);
if (counts.different > 0 || counts.added > 0 || counts.removed > 0) {
log(chalk.red('\nā Differences found!'));
} else E{
log(chalk.green('\nā
All files match baseline!'));
}
return {
report,
reportDir,
counts,
shouldFailExit,
};
}
|