Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/project/project-scratch.ts
6458 views
1
/*
2
* project-scratch.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*/
6
7
import { dirname, join } from "../deno_ral/path.ts";
8
import { ensureDirSync, existsSync } from "../deno_ral/fs.ts";
9
import { normalizePath } from "../core/path.ts";
10
import { warning } from "../deno_ral/log.ts";
11
12
export const kQuartoScratch = ".quarto";
13
14
export function projectScratchPath(dir: string, path = "") {
15
const scratchDir = join(dir, kQuartoScratch);
16
ensureDirSync(scratchDir);
17
if (path) {
18
path = join(scratchDir, path);
19
ensureDirSync(dirname(path));
20
return path;
21
} else {
22
return normalizePath(scratchDir);
23
}
24
}
25
26
// Migrate a scratch path from an old location to a new location.
27
// If the old path exists and the new path doesn't, moves the old to new.
28
// Returns the new path (via projectScratchPath which ensures it exists).
29
export function migrateProjectScratchPath(
30
dir: string,
31
oldSubpath: string,
32
newSubpath: string,
33
): string {
34
const scratchDir = join(dir, kQuartoScratch);
35
const oldPath = join(scratchDir, oldSubpath);
36
const newPath = join(scratchDir, newSubpath);
37
38
if (existsSync(oldPath) && !existsSync(newPath)) {
39
// Ensure parent directory of new path exists
40
ensureDirSync(dirname(newPath));
41
try {
42
Deno.renameSync(oldPath, newPath);
43
} catch (e) {
44
// Migration failed - not fatal, cache will be rebuilt
45
warning(`Failed to migrate ${oldSubpath} to ${newSubpath}: ${e}`);
46
}
47
}
48
49
return projectScratchPath(dir, newSubpath);
50
}
51
52