diff --git a/README.md b/README.md index 6fc940814..dee7f8995 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,9 @@ This command will retrieve the given package manager from the specified archive - `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`). -- `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`. +- `COREPACK_HOME` can be set in order to define where Corepack should install + the package managers. By default it is set to `%LOCALAPPDATA%\node\corepack` + on Windows, and to `$HOME/.cache/node/corepack` everywhere else. - `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`). diff --git a/sources/folderUtils.ts b/sources/folderUtils.ts index 75b803d25..1d2d70b83 100644 --- a/sources/folderUtils.ts +++ b/sources/folderUtils.ts @@ -1,10 +1,41 @@ -import {mkdirSync} from 'fs'; -import {homedir, tmpdir} from 'os'; -import {join} from 'path'; -import type { NodeError } from './nodeUtils'; +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() { - return process.env.COREPACK_HOME ?? join(homedir(), `.node/corepack`); + 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()) {