-
Notifications
You must be signed in to change notification settings - Fork 169
/
folderUtils.ts
60 lines (55 loc) · 1.71 KB
/
folderUtils.ts
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
import {existsSync, mkdirSync, renameSync} from 'fs';
import {homedir, tmpdir} from 'os';
import {join} from 'path';
import process from 'process';
import type {NodeError} from './nodeUtils';
export function getInstallFolder() {
if (process.env.COREPACK_HOME == null) {
// TODO: remove this block on the next major.
const oldCorepackDefaultHome = join(homedir(), `.node`, `corepack`);
const newCorepackDefaultHome = join(
process.env.XDG_CACHE_HOME ??
process.env.LOCALAPPDATA ??
join(
homedir(),
process.platform === `win32` ? `AppData/Local` : `.cache`,
),
`node/corepack`,
);
if (
existsSync(oldCorepackDefaultHome) &&
!existsSync(newCorepackDefaultHome)
) {
mkdirSync(newCorepackDefaultHome, {recursive: true});
renameSync(oldCorepackDefaultHome, newCorepackDefaultHome);
}
return newCorepackDefaultHome;
}
return (
process.env.COREPACK_HOME ??
join(
process.env.XDG_CACHE_HOME ??
process.env.LOCALAPPDATA ??
join(homedir(), process.platform === `win32` ? `AppData/Local` : `.cache`),
`node/corepack`,
)
);
}
export function getTemporaryFolder(target: string = tmpdir()) {
mkdirSync(target, {recursive: true});
while (true) {
const rnd = Math.random() * 0x100000000;
const hex = rnd.toString(16).padStart(8, `0`);
const path = join(target, `corepack-${process.pid}-${hex}`);
try {
mkdirSync(path);
return path;
} catch (error) {
if ((error as NodeError).code === `EEXIST`) {
continue;
} else {
throw error;
}
}
}
}