Skip to content

Commit

Permalink
feat: add support for process.env.npminstall_cache (#471)
Browse files Browse the repository at this point in the history
### 现状

npminstall 内置了 tarball 的缓存能力,默认的缓存地址为 `~/.npminstall_tarball`.
npminstall 不支持通过 `--cache` 配置缓存目录的位置,但支持通过
`process.env.npm_config_cache` 进行配置。

### 我们的预期

预期能通过配置文件或环境变量修改 npminstall 缓存的位置,且缓存的目录能与 npm 区分开

### 遇到的问题

如果使用 `npm_config_cache` 配置 npminstall 的缓存位置,会同时修改 npm
的缓存位置,导致两个缓存总是位于同一个父目录

### 修改方法
新增 `process.env.npminstall_cache` 配置,允许配置缓存位置,且能够和 npm 的缓存目录分隔开。
(或者其他方式,能满足需求即可)
  • Loading branch information
feichao93 authored Dec 18, 2023
1 parent 13b20e9 commit bd2cd34
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions bin/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ if (production) {
if (cacheDir === null && process.env.npm_config_cache) {
cacheDir = process.env.npm_config_cache;
}
if (process.env.npminstall_cache) {
cacheDir = process.env.npminstall_cache;
}

let forbiddenLicenses = argv['forbidden-licenses'];
forbiddenLicenses = forbiddenLicenses ? forbiddenLicenses.split(',') : null;
Expand Down
13 changes: 13 additions & 0 deletions test/install-cache-strict.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,17 @@ describe('test/install-cache-strict.test.js', () => {
.end();
assert(await fs.stat(path.join(homedir, '.npminstall_tarball/d/e/b/u/debug')));
});

it('should read disk cache from npminstall_cache env', async () => {
await coffee.fork(helper.npminstall, [], {
cwd: demo,
env: Object.assign({}, process.env, {
HOME: homedir,
npminstall_cache: path.join(homedir, 'foocache/.npminstall_tarball'),
}),
})
.debug()
.end();
assert(await fs.stat(path.join(homedir, 'foocache/.npminstall_tarball/d/e/b/u/debug')));
});
});

0 comments on commit bd2cd34

Please sign in to comment.