fix: complete Finder project workflow and add in-place output

This commit is contained in:
muena
2026-09-24 19:55:34 +02:00
parent 8e928bc69f
commit 8b254eb87d
4 changed files with 172 additions and 27 deletions

View File

@@ -1,5 +1,5 @@
import { createHash, randomUUID } from "node:crypto";
import { promises as fs } from "node:fs";
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 { PlaceholderMapping } from "../types";
@@ -18,6 +18,7 @@ export type ProjectRun = {
/** Generic replacement mode for placeholders embedded in code identifiers. */
matchModes?: Record<string, "token" | "anywhere">;
files: RunFile[];
inPlace?: boolean;
};
const DOCUMENTS = new Set([".docx", ".xlsx", ".pptx", ".pdf"]);
@@ -278,3 +279,74 @@ export function resolveWithin(root: string, relPath: string): string {
if (!target.startsWith(resolve(root) + sep)) throw new Error("Ausgabepfad verlässt den Zielordner.");
return target;
}
async function assertNoSymlinkParents(root: string, relPath: string): Promise<void> {
const parts = safeRelativePath(relPath).split("/");
let parent = resolve(root);
for (const part of parts.slice(0, -1)) {
parent = join(parent, part);
try {
if ((await fs.lstat(parent)).isSymbolicLink()) throw new Error(`Symbolischer Link im Ausgabepfad: ${relPath}`);
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
}
}
}
/** Replaces selected originals only after every output exists and a private backup is complete. */
export async function replaceProjectFilesInPlace(projectPath: string, runDirectory: string, files: RunFile[]): Promise<void> {
const root = resolve(projectPath);
const sourcePaths = new Set(files.map((file) => resolveWithin(root, file.source)));
const targetPaths = new Set<string>();
const sourceModes = new Map<string, number>();
for (const file of files) {
const source = resolveWithin(root, file.source);
const target = resolveWithin(root, file.output);
if (targetPaths.has(target)) throw new Error(`Doppelter Zielname: ${file.output}`);
targetPaths.add(target);
await assertNoSymlinkParents(root, file.output);
const sourceInfo = await fs.lstat(source);
if (!sourceInfo.isFile()) throw new Error(`Quelldatei ist keine reguläre Datei: ${file.source}`);
sourceModes.set(source, sourceInfo.mode & 0o777);
try {
await fs.lstat(target);
if (!sourcePaths.has(target)) throw new Error(`Zielname bereits vorhanden: ${file.output}`);
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
}
await fs.access(resolveWithin(join(runDirectory, "files"), file.output));
}
const backupRoot = join(runDirectory, "backup");
for (const file of files) {
const backup = resolveWithin(backupRoot, file.source);
await fs.mkdir(dirname(backup), { recursive: true, mode: 0o700 });
await fs.copyFile(resolveWithin(root, file.source), backup, fsConstants.COPYFILE_EXCL);
await fs.chmod(backup, 0o600);
}
const written: string[] = [];
try {
for (const file of files) await fs.unlink(resolveWithin(root, file.source));
for (const file of files) {
const target = resolveWithin(root, file.output);
await fs.mkdir(dirname(target), { recursive: true, mode: 0o700 });
await fs.copyFile(resolveWithin(join(runDirectory, "files"), file.output), target, fsConstants.COPYFILE_EXCL);
written.push(target);
await fs.chmod(target, sourceModes.get(resolveWithin(root, file.source)) ?? 0o600);
}
} catch (error) {
for (const target of written) await fs.rm(target, { force: true });
for (const file of files) {
const source = resolveWithin(root, file.source);
try { await fs.lstat(source); }
catch (missing) {
if ((missing as NodeJS.ErrnoException).code !== "ENOENT") throw missing;
await fs.mkdir(dirname(source), { recursive: true, mode: 0o700 });
await fs.copyFile(resolveWithin(backupRoot, file.source), source, fsConstants.COPYFILE_EXCL);
await fs.chmod(source, sourceModes.get(source) ?? 0o600);
}
}
throw new Error(`Ersetzen fehlgeschlagen; Originale wurden wiederhergestellt. Sicherung: ${backupRoot}. ${(error as Error).message}`);
}
}