-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtempDirectories.ts
More file actions
85 lines (75 loc) · 2.51 KB
/
tempDirectories.ts
File metadata and controls
85 lines (75 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import fs from "node:fs";
import path from "node:path";
import { onExit } from "signal-exit";
/**
* A short-lived directory. Automatically removed when the process exits, but
* can be removed earlier by calling `remove()`.
*/
export interface EphemeralDirectory {
path: string;
remove(): void;
}
/**
* Gets a temporary directory in the project's `.trigger` folder with the
* specified prefix. We create temporary directories in `.trigger` as opposed
* to the OS's temporary directory to avoid issues with different drive letters
* on Windows. For example, when `esbuild` outputs a file to a different drive
* than the input sources, the generated source maps are incorrect.
*/
export function getTmpDir(
projectRoot: string | undefined,
prefix: string,
keep: boolean = false
): EphemeralDirectory {
projectRoot ??= process.cwd();
const tmpRoot = path.join(projectRoot, ".trigger", "tmp");
fs.mkdirSync(tmpRoot, { recursive: true });
const tmpPrefix = path.join(tmpRoot, `${prefix}-`);
const tmpDir = fs.realpathSync(fs.mkdtempSync(tmpPrefix));
const removeDir = () => {
try {
return fs.rmSync(tmpDir, { recursive: true, force: true });
} catch (e) {
// This sometimes fails on Windows with EBUSY
}
};
const removeExitListener = keep || process.env.KEEP_TMP_DIRS ? () => {} : onExit(removeDir);
return {
path: tmpDir,
remove() {
removeExitListener();
removeDir();
},
};
}
export function clearTmpDirs(projectRoot: string | undefined) {
projectRoot ??= process.cwd();
const tmpRoot = path.join(projectRoot, ".trigger", "tmp");
try {
fs.rmSync(tmpRoot, { recursive: true, force: true });
} catch (e) {
// This sometimes fails on Windows with EBUSY
}
}
/**
* Gets the shared store directory for content-addressable build outputs.
* This directory persists across rebuilds and is used to deduplicate
* identical chunk files between build versions.
* Automatically cleaned up when the process exits.
*/
export function getStoreDir(projectRoot: string | undefined, keep: boolean = false): string {
projectRoot ??= process.cwd();
const storeDir = path.join(projectRoot, ".trigger", "tmp", "store");
fs.mkdirSync(storeDir, { recursive: true });
// Register exit handler to clean up the store directory
if (!keep && !process.env.KEEP_TMP_DIRS) {
onExit(() => {
try {
fs.rmSync(storeDir, { recursive: true, force: true });
} catch (e) {
// This sometimes fails on Windows with EBUSY
}
});
}
return storeDir;
}