forked from r.muellner/velum-raycast
feat: run Velum project jobs in the background and process DOCX locally
This commit is contained in:
60
src/lib/mapping-export.ts
Normal file
60
src/lib/mapping-export.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { promises as fs } from "node:fs";
|
||||
import { homedir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import type { PlaceholderMapping } from "../types";
|
||||
import { mappingRoot } from "./mapping-groups";
|
||||
|
||||
export type MappingExportKind = "terms" | "replacements";
|
||||
|
||||
/** One record per line; literal backslashes and line breaks remain distinguishable. */
|
||||
function singleLine(value: string): string {
|
||||
return value.replace(/\\/g, "\\\\").replace(/\r/g, "\\r").replace(/\n/g, "\\n").replace(/\t/g, "\\t");
|
||||
}
|
||||
|
||||
export function mappingExport(mapping: PlaceholderMapping, kind: MappingExportKind): { text: string; count: number } {
|
||||
const compare = (left: string, right: string) => left.localeCompare(right, "de", { numeric: true });
|
||||
if (kind === "terms") {
|
||||
const terms = [...new Set(Object.values(mapping).map((entry) => entry.original))].sort(compare);
|
||||
return { text: terms.map(singleLine).join("\n") + (terms.length ? "\n" : ""), count: terms.length };
|
||||
}
|
||||
if (kind !== "replacements") throw new Error("Unbekanntes Exportformat.");
|
||||
const pairs = new Map<string, { original: string; replacement: string }>();
|
||||
for (const [key, entry] of Object.entries(mapping)) {
|
||||
const replacement = mappingRoot(key, mapping);
|
||||
pairs.set(JSON.stringify([entry.original, replacement]), { original: entry.original, replacement });
|
||||
}
|
||||
const rows = [...pairs.values()].sort((left, right) => compare(left.original, right.original)
|
||||
|| compare(left.replacement, right.replacement));
|
||||
return {
|
||||
text: rows.map(({ original, replacement }) => `${singleLine(original)} → ${singleLine(replacement)}`).join("\n") + (rows.length ? "\n" : ""),
|
||||
count: rows.length,
|
||||
};
|
||||
}
|
||||
|
||||
/** Save a private, complete UTF-8 copy, without replacing earlier exports. */
|
||||
export async function writeMappingExport(title: string, mapping: PlaceholderMapping, kind: MappingExportKind,
|
||||
directory = join(homedir(), "Downloads", "Velum")): Promise<{ path: string; count: number }> {
|
||||
const { text, count } = mappingExport(mapping, kind);
|
||||
if (!count) throw new Error("Diese Übersetzungstabelle enthält noch keine Begriffe.");
|
||||
const name = [...title.normalize("NFC").replace(/[^\p{L}\p{N}_. -]/gu, "-")].slice(0, 40).join("")
|
||||
.replace(/^[.\s-]+|[.\s-]+$/g, "") || "Velum";
|
||||
const suffix = kind === "terms" ? "begriffe" : "ersetzungen";
|
||||
await fs.mkdir(directory, { recursive: true, mode: 0o700 });
|
||||
const temporary = await fs.mkdtemp(join(directory, ".velum-export-"));
|
||||
try {
|
||||
const file = join(temporary, "export.txt");
|
||||
await fs.writeFile(file, text, { encoding: "utf8", flag: "wx", mode: 0o600 });
|
||||
for (let index = 0; index < 1000; index++) {
|
||||
const path = join(directory, `${name}-${suffix}${index ? ` (${index + 1})` : ""}.txt`);
|
||||
try {
|
||||
await fs.link(file, path);
|
||||
return { path, count };
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error;
|
||||
}
|
||||
}
|
||||
throw new Error("Zu viele gleichnamige Exportdateien. Bitte ältere Exporte umbenennen.");
|
||||
} finally {
|
||||
await fs.rm(temporary, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user