Skip to content

Commit 09e24cf

Browse files
aduh95merceyz
andauthored
feat: do not use ~/.node as default value for COREPACK_HOME (#152)
* feat: do not use `~/.node` as default value for `COREPACK_HOME` Fixes: #145 Refs: nodejs/node#43859 * fixup! feat: do not use `~/.node` as default value for `COREPACK_HOME` * Update sources/folderUtils.ts Co-authored-by: Kristoffer K. <merceyz@users.noreply.github.com> * Update README * migrate old corepack home folder Co-authored-by: Kristoffer K. <merceyz@users.noreply.github.com>
1 parent a6b8b93 commit 09e24cf

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ This command will retrieve the given package manager from the specified archive
106106

107107
- `COREPACK_ENABLE_NETWORK` can be set to `0` to prevent Corepack from accessing the network (in which case you'll be responsible for hydrating the package manager versions that will be required for the projects you'll run, using `corepack hydrate`).
108108

109-
- `COREPACK_HOME` can be set in order to define where Corepack should install the package managers. By default it is set to `$HOME/.node/corepack`.
109+
- `COREPACK_HOME` can be set in order to define where Corepack should install
110+
the package managers. By default it is set to `%LOCALAPPDATA%\node\corepack`
111+
on Windows, and to `$HOME/.cache/node/corepack` everywhere else.
110112

111113
- `COREPACK_ROOT` has no functional impact on Corepack itself; it's automatically being set in your environment by Corepack when it shells out to the underlying package managers, so that they can feature-detect its presence (useful for commands like `yarn init`).
112114

sources/folderUtils.ts

+36-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,41 @@
1-
import {mkdirSync} from 'fs';
2-
import {homedir, tmpdir} from 'os';
3-
import {join} from 'path';
4-
import type { NodeError } from './nodeUtils';
1+
import {existsSync, mkdirSync, renameSync} from 'fs';
2+
import {homedir, tmpdir} from 'os';
3+
import {join} from 'path';
4+
import process from 'process';
5+
6+
import type {NodeError} from './nodeUtils';
57

68
export function getInstallFolder() {
7-
return process.env.COREPACK_HOME ?? join(homedir(), `.node/corepack`);
9+
if (process.env.COREPACK_HOME == null) {
10+
// TODO: remove this block on the next major.
11+
const oldCorepackDefaultHome = join(homedir(), `.node`, `corepack`);
12+
const newCorepackDefaultHome = join(
13+
process.env.XDG_CACHE_HOME ??
14+
process.env.LOCALAPPDATA ??
15+
join(
16+
homedir(),
17+
process.platform === `win32` ? `AppData/Local` : `.cache`,
18+
),
19+
`node/corepack`,
20+
);
21+
if (
22+
existsSync(oldCorepackDefaultHome) &&
23+
!existsSync(newCorepackDefaultHome)
24+
) {
25+
mkdirSync(newCorepackDefaultHome, {recursive: true});
26+
renameSync(oldCorepackDefaultHome, newCorepackDefaultHome);
27+
}
28+
return newCorepackDefaultHome;
29+
}
30+
return (
31+
process.env.COREPACK_HOME ??
32+
join(
33+
process.env.XDG_CACHE_HOME ??
34+
process.env.LOCALAPPDATA ??
35+
join(homedir(), process.platform === `win32` ? `AppData/Local` : `.cache`),
36+
`node/corepack`,
37+
)
38+
);
839
}
940

1041
export function getTemporaryFolder(target: string = tmpdir()) {

0 commit comments

Comments
 (0)