import fs from "node:fs/promises"; import path from "node:path";
export function resolveRepoRelativeOutputDir(repoRoot: string, outputDir?: string) { if (!outputDir) { return undefined;
} if (path.isAbsolute(outputDir)) { thrownew Error("--output-dir must be a relative path inside the repo root.");
} const resolved = path.resolve(repoRoot, outputDir); const relative = path.relative(repoRoot, resolved); if (relative.startsWith("..") || path.isAbsolute(relative)) { thrownew Error("--output-dir must stay within the repo root.");
} return resolved;
}
async function resolveNearestExistingPath(targetPath: string) {
let current = path.resolve(targetPath); while (true) { try {
await fs.lstat(current); return current;
} catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") { throw error;
}
} const parent = path.dirname(current); if (parent === current) { thrownew Error(`failed to resolve existing path for ${targetPath}`);
}
current = parent;
}
}
function assertRepoRelativePath(repoRoot: string, targetPath: string, label: string) { const relative = path.relative(repoRoot, targetPath); if (relative.startsWith("..") || path.isAbsolute(relative)) { thrownew Error(`${label} must stay within the repo root.`);
} return relative;
}
async function assertNoSymlinkSegments(repoRoot: string, targetPath: string, label: string) { const relative = assertRepoRelativePath(repoRoot, targetPath, label);
let current = repoRoot; for (const segment of relative.split(path.sep).filter((entry) => entry.length > 0)) {
current = path.join(current, segment);
let stats: Awaited<ReturnType<typeof fs.lstat>> | null = null; try {
stats = await fs.lstat(current);
} catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") { break;
} throw error;
} if (stats.isSymbolicLink()) { thrownew Error(`${label} must not traverse symlinks.`);
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.