import { dirname, join } from "../deno_ral/path.ts";
import { ensureDirSync, existsSync } from "../deno_ral/fs.ts";
import { normalizePath } from "../core/path.ts";
import { warning } from "../deno_ral/log.ts";
export const kQuartoScratch = ".quarto";
export function projectScratchPath(dir: string, path = "") {
const scratchDir = join(dir, kQuartoScratch);
ensureDirSync(scratchDir);
if (path) {
path = join(scratchDir, path);
ensureDirSync(dirname(path));
return path;
} else {
return normalizePath(scratchDir);
}
}
export function migrateProjectScratchPath(
dir: string,
oldSubpath: string,
newSubpath: string,
): string {
const scratchDir = join(dir, kQuartoScratch);
const oldPath = join(scratchDir, oldSubpath);
const newPath = join(scratchDir, newSubpath);
if (existsSync(oldPath) && !existsSync(newPath)) {
ensureDirSync(dirname(newPath));
try {
Deno.renameSync(oldPath, newPath);
} catch (e) {
warning(`Failed to migrate ${oldSubpath} to ${newSubpath}: ${e}`);
}
}
return projectScratchPath(dir, newSubpath);
}