forked from r.muellner/velum-raycast
fix: reuse unchanged project files by source hash
This commit is contained in:
@@ -116,6 +116,7 @@ export async function reviseProjectRun(args: {
|
||||
run.commentAuthorCoverage = previous.commentAuthorCoverage;
|
||||
run.reviewAuthorCoverage = previous.reviewAuthorCoverage;
|
||||
run.mapping = plan.mapping;
|
||||
run.sourceState = {};
|
||||
run.matchModes = Object.fromEntries(Object.entries(previous.matchModes ?? {})
|
||||
.filter(([key]) => key in plan.mapping));
|
||||
run.sourceInPlace = inPlace;
|
||||
@@ -153,6 +154,15 @@ export async function reviseProjectRun(args: {
|
||||
isMarkdownFile(file.output)), "utf8");
|
||||
await fs.writeFile(target, output, { flag: "wx", mode: 0o600 });
|
||||
run.files.push({ source: file.output, output: outputPath, kind: file.kind });
|
||||
if (inPlace) run.sourceState[outputPath] = { hash: digest(output), output: outputPath };
|
||||
}
|
||||
if (!inPlace && previous.sourceState) {
|
||||
const revisedOutput = new Map(run.files.map((file) => [file.source, file.output]));
|
||||
run.sourceState = Object.fromEntries(Object.entries(previous.sourceState)
|
||||
.filter(([, state]) => revisedOutput.has(state.output))
|
||||
.map(([path, state]) => [path, {
|
||||
hash: state.hash, output: revisedOutput.get(state.output)!,
|
||||
}]));
|
||||
}
|
||||
if (plan.whitelist.length) {
|
||||
await saveProjectWhitelist(previous.projectPath, [...previousWhitelist, ...plan.whitelist], support);
|
||||
|
||||
@@ -23,6 +23,8 @@ export type ProjectRun = {
|
||||
projectName: string;
|
||||
createdAt: string;
|
||||
mapping: PlaceholderMapping;
|
||||
/** Hash of each live project file and its immutable output in this run. */
|
||||
sourceState?: Record<string, { hash: string; output: string }>;
|
||||
/** Generic replacement mode for placeholders embedded in code identifiers. */
|
||||
matchModes?: Record<string, "token" | "anywhere">;
|
||||
files: RunFile[];
|
||||
@@ -187,7 +189,8 @@ export function forceMasks(rules: ProjectRule[]): Record<string, string[]> {
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function scanProjectFiles(root: string, maxSize = 20 * 1024 * 1024): Promise<{
|
||||
export async function scanProjectFiles(root: string, maxSize = 20 * 1024 * 1024,
|
||||
validateText = true): Promise<{
|
||||
files: ProjectFile[]; skipped: SkippedProjectFile[];
|
||||
}> {
|
||||
const files: ProjectFile[] = [];
|
||||
@@ -219,7 +222,7 @@ export async function scanProjectFiles(root: string, maxSize = 20 * 1024 * 1024)
|
||||
skip(relPath, "Kein lesbares Office-ZIP-Archiv (beschädigt, verschlüsselt oder falsche Endung)");
|
||||
continue;
|
||||
}
|
||||
if (kind === "text") {
|
||||
if (kind === "text" && validateText) {
|
||||
if (await isBinary(path)) { skip(relPath, "Binärdatei"); continue; }
|
||||
try { new TextDecoder("utf-8", { fatal: true }).decode(await fs.readFile(path)); }
|
||||
catch { skip(relPath, "Keine gültige UTF-8-Textdatei"); continue; }
|
||||
@@ -431,6 +434,7 @@ export async function updateProjectMapping(projectPath: string,
|
||||
run.mapping = mapping;
|
||||
run.matchModes = { ...latest.run.matchModes };
|
||||
run.files = latest.run.files.map((file) => ({ ...file }));
|
||||
run.sourceState = latest.run.sourceState ? { ...latest.run.sourceState } : undefined;
|
||||
run.inPlace = latest.run.inPlace;
|
||||
run.sourceInPlace = latest.run.sourceInPlace;
|
||||
run.commentAuthorCoverage = latest.run.commentAuthorCoverage;
|
||||
@@ -550,6 +554,26 @@ export async function matchesPreviousOutput(directory: string, relPath: string,
|
||||
}
|
||||
}
|
||||
|
||||
/** Reuse only an output proven to correspond to the current source bytes. */
|
||||
export async function reusableProjectOutput(run: ProjectRun, directory: string,
|
||||
relPath: string, currentHash: string): Promise<string | undefined> {
|
||||
const state = run.sourceState?.[relPath];
|
||||
if (state) {
|
||||
if (state.hash !== currentHash || !run.files.some((file) => file.output === state.output)) return undefined;
|
||||
try {
|
||||
const output = resolveWithin(join(directory, "files"), state.output);
|
||||
return (await fs.lstat(output)).isFile() ? state.output : undefined;
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
// Older in-place snapshots lack source hashes. An identical saved output is
|
||||
// still authoritative; copy-mode source bytes cannot be reconstructed.
|
||||
if (!(run.inPlace || run.sourceInPlace) || !run.files.some((file) => file.output === relPath)) return undefined;
|
||||
return await matchesPreviousOutput(directory, relPath, currentHash) ? relPath : undefined;
|
||||
}
|
||||
|
||||
/** Only an identical saved file can disambiguate a placeholder reused across runs. */
|
||||
export async function mappingForMatchingSnapshots(relPath: string, currentHash: string,
|
||||
earlier: Array<{ run: ProjectRun; directory: string }>): Promise<PlaceholderMapping> {
|
||||
|
||||
Reference in New Issue
Block a user