forked from r.muellner/velum-raycast
fix: guide project mapping recovery and allow manual table entries
This commit is contained in:
@@ -2,7 +2,7 @@ import { createHash, randomUUID } from "node:crypto";
|
||||
import { constants as fsConstants, promises as fs } from "node:fs";
|
||||
import { basename, dirname, extname, join, relative, resolve, sep } from "node:path";
|
||||
import { environment } from "@raycast/api";
|
||||
import type { EntityType, PlaceholderMapping } from "../types";
|
||||
import type { EntityType, MappingEntry, PlaceholderMapping } from "../types";
|
||||
import { whitelistKey } from "./project-whitelist";
|
||||
import type { ProjectWhitelistEntry } from "./project-whitelist";
|
||||
import { restoreOriginalCase } from "./restore-case";
|
||||
@@ -382,6 +382,78 @@ export async function saveRun(directory: string, run: ProjectRun): Promise<void>
|
||||
await fs.rename(tmp, dest);
|
||||
}
|
||||
|
||||
export function prepareManualMappingEntry(current: PlaceholderMapping, earlier: PlaceholderMapping[],
|
||||
input: { original: string; type: string; placeholder?: string }): { placeholder: string; entry: MappingEntry } {
|
||||
const original = input.original.trim();
|
||||
const type = input.type.trim().toUpperCase();
|
||||
if (!original) throw new Error("Bitte einen Klartext-Begriff eingeben.");
|
||||
if (!/^[A-ZÄÖÜ][A-ZÄÖÜ0-9_]*$/u.test(type)) throw new Error("Ungültige Kategorie.");
|
||||
if (Object.values(current).some((entry) => entry.type === type
|
||||
&& entry.original.toLocaleLowerCase("de") === original.toLocaleLowerCase("de"))) {
|
||||
throw new Error("Dieser Begriff ist in der Kategorie bereits vorhanden.");
|
||||
}
|
||||
const known = [current, ...earlier];
|
||||
const keys = known.flatMap((mapping) => Object.keys(mapping));
|
||||
const placeholderPattern = new RegExp(`^${type}_(\\d+)$`, "u");
|
||||
const placeholder = input.placeholder?.trim().toUpperCase() || (() => {
|
||||
const max = keys.reduce((currentMax, key) => Math.max(currentMax,
|
||||
Number(placeholderPattern.exec(key)?.[1] ?? 0)), 0);
|
||||
return `${type}_${String(max + 1).padStart(2, "0")}`;
|
||||
})();
|
||||
if (!placeholderPattern.test(placeholder)) {
|
||||
throw new Error("Platzhalter muss zur Kategorie passen, z. B. PERSON_61.");
|
||||
}
|
||||
if (Object.keys(current).some((key) => key.toUpperCase() === placeholder)) {
|
||||
throw new Error("Dieser Platzhalter ist im aktuellen Stand bereits belegt.");
|
||||
}
|
||||
for (const mapping of earlier) for (const [key, entry] of Object.entries(mapping)) {
|
||||
if (key.toUpperCase() === placeholder && (entry.type !== type || entry.original !== original)) {
|
||||
throw new Error("Dieser Platzhalter hatte früher eine andere Bedeutung. Bitte die betroffene Datei einzeln zuordnen.");
|
||||
}
|
||||
}
|
||||
return { placeholder, entry: { original, type } };
|
||||
}
|
||||
|
||||
/** Save a mapping correction as a new immutable project stand without rewriting project files. */
|
||||
export async function addProjectMappingEntry(projectPath: string,
|
||||
input: { original: string; type: string; placeholder?: string }, support = environment.supportPath): Promise<ProjectRun> {
|
||||
const entries = await listRuns(projectPath, support);
|
||||
const latest = entries[0];
|
||||
if (!latest) throw new Error("Für dieses Projekt gibt es noch keinen gespeicherten Stand.");
|
||||
const { placeholder, entry } = prepareManualMappingEntry(latest.run.mapping,
|
||||
entries.slice(1).map((saved) => saved.run.mapping), input);
|
||||
const created = await createRun(projectPath, support);
|
||||
const { run, directory } = created;
|
||||
run.createdAt = new Date(Math.max(Date.now(), Date.parse(latest.run.createdAt) + 1)).toISOString();
|
||||
run.origin = latest.run.origin;
|
||||
run.revisedFrom = latest.run.id;
|
||||
run.mapping = { ...latest.run.mapping, [placeholder]: entry };
|
||||
run.matchModes = { ...latest.run.matchModes };
|
||||
run.files = latest.run.files.map((file) => ({ ...file }));
|
||||
run.inPlace = latest.run.inPlace;
|
||||
run.sourceInPlace = latest.run.sourceInPlace;
|
||||
run.commentAuthorCoverage = latest.run.commentAuthorCoverage;
|
||||
run.reviewAuthorCoverage = latest.run.reviewAuthorCoverage;
|
||||
run.copyPath = latest.run.copyPath;
|
||||
try {
|
||||
for (const file of run.files) {
|
||||
const source = resolveWithin(join(latest.directory, "files"), file.output);
|
||||
const target = resolveWithin(join(directory, "files"), file.output);
|
||||
await fs.mkdir(dirname(target), { recursive: true, mode: 0o700 });
|
||||
await fs.copyFile(source, target, fsConstants.COPYFILE_EXCL | fsConstants.COPYFILE_FICLONE);
|
||||
await fs.chmod(target, 0o600);
|
||||
}
|
||||
if ((await listRuns(projectPath, support))[0]?.run.id !== latest.run.id) {
|
||||
throw new Error("Es gibt bereits einen neueren Projektstand. Bitte die Liste aktualisieren.");
|
||||
}
|
||||
await saveRun(directory, run);
|
||||
} catch (error) {
|
||||
await fs.rm(directory, { recursive: true, force: true });
|
||||
throw error;
|
||||
}
|
||||
return run;
|
||||
}
|
||||
|
||||
export function runCopiesPath(run: ProjectRun, directory: string): string {
|
||||
return run.copyPath ?? join(directory, "files");
|
||||
}
|
||||
@@ -469,6 +541,17 @@ export async function matchesPreviousOutput(directory: string, relPath: string,
|
||||
}
|
||||
}
|
||||
|
||||
/** 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> {
|
||||
const matching = await Promise.all(earlier.filter(({ run }) =>
|
||||
run.files.some((file) => file.output === relPath))
|
||||
.map(async ({ run, directory }) => await matchesPreviousOutput(directory, relPath, currentHash)
|
||||
? run.mapping : undefined));
|
||||
return withUnambiguousHistoricalMappings({},
|
||||
matching.filter((mapping): mapping is PlaceholderMapping => Boolean(mapping)));
|
||||
}
|
||||
|
||||
export async function listAllRuns(support = environment.supportPath): Promise<Array<{
|
||||
run: ProjectRun; directory: string;
|
||||
}>> {
|
||||
|
||||
Reference in New Issue
Block a user