import fs from 'fs';
import path from 'path';
import crypto from 'crypto';
// --------------------
// Snapshot utilities
// --------------------
const SNAPSHOT_DIR = '.mcp-snapshots';
function getSnapshotPath(filePath: string) {
const relativePath = path.relative(process.cwd(), filePath);
const hash = crypto.createHash('md5').update(relativePath).digest('hex');
return path.join(SNAPSHOT_DIR, `${hash}.snap`);
}
export function saveSnapshot(filePath: string) {
if (!fs.existsSync(SNAPSHOT_DIR)) fs.mkdirSync(SNAPSHOT_DIR, { recursive: true });
const content = fs.readFileSync(filePath, 'utf-8');
fs.writeFileSync(getSnapshotPath(filePath), content, 'utf-8');
}
export function compareSnapshot(filePath: string) {
const snapPath = getSnapshotPath(filePath);
if (!fs.existsSync(snapPath)) return { changed: true, message: 'No snapshot exists' };
const oldContent = fs.readFileSync(snapPath, 'utf-8');
const newContent = fs.readFileSync(filePath, 'utf-8');
if (oldContent !== newContent) {
return { changed: true, message: `File changed: ${filePath}` };
}
return { changed: false, message: '' };
}
// --------------------
// Batch operations
// --------------------
export function saveAllSnapshots({
components,
pageAnalyses,
hooksPath,
}: {
components: { filePath: string }[];
pageAnalyses: { pagePath: string }[];
hooksPath?: string;
}) {
components.forEach((c) => saveSnapshot(c.filePath));
pageAnalyses.forEach((p) => saveSnapshot(p.pagePath));
if (hooksPath) saveSnapshot(hooksPath);
}
export function checkAllSnapshots({
components,
pageAnalyses,
hooksPath,
}: {
components: { filePath: string }[];
pageAnalyses: { pagePath: string }[];
hooksPath?: string;
}) {
const results: string[] = [];
components.forEach((c) => {
const diff = compareSnapshot(c.filePath);
if (diff.changed) results.push(diff.message);
});
pageAnalyses.forEach((p) => {
const diff = compareSnapshot(p.pagePath);
if (diff.changed) results.push(diff.message);
});
if (hooksPath) {
const diff = compareSnapshot(hooksPath);
if (diff.changed) results.push(diff.message);
}
return results;
}