Skip to content

Commit bd2cd34

Browse files
authored
feat: add support for process.env.npminstall_cache (#471)
### 现状 npminstall 内置了 tarball 的缓存能力,默认的缓存地址为 `~/.npminstall_tarball`. npminstall 不支持通过 `--cache` 配置缓存目录的位置,但支持通过 `process.env.npm_config_cache` 进行配置。 ### 我们的预期 预期能通过配置文件或环境变量修改 npminstall 缓存的位置,且缓存的目录能与 npm 区分开 ### 遇到的问题 如果使用 `npm_config_cache` 配置 npminstall 的缓存位置,会同时修改 npm 的缓存位置,导致两个缓存总是位于同一个父目录 ### 修改方法 新增 `process.env.npminstall_cache` 配置,允许配置缓存位置,且能够和 npm 的缓存目录分隔开。 (或者其他方式,能满足需求即可)
1 parent 13b20e9 commit bd2cd34

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

bin/install.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ if (production) {
196196
if (cacheDir === null && process.env.npm_config_cache) {
197197
cacheDir = process.env.npm_config_cache;
198198
}
199+
if (process.env.npminstall_cache) {
200+
cacheDir = process.env.npminstall_cache;
201+
}
199202

200203
let forbiddenLicenses = argv['forbidden-licenses'];
201204
forbiddenLicenses = forbiddenLicenses ? forbiddenLicenses.split(',') : null;

test/install-cache-strict.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,17 @@ describe('test/install-cache-strict.test.js', () => {
5959
.end();
6060
assert(await fs.stat(path.join(homedir, '.npminstall_tarball/d/e/b/u/debug')));
6161
});
62+
63+
it('should read disk cache from npminstall_cache env', async () => {
64+
await coffee.fork(helper.npminstall, [], {
65+
cwd: demo,
66+
env: Object.assign({}, process.env, {
67+
HOME: homedir,
68+
npminstall_cache: path.join(homedir, 'foocache/.npminstall_tarball'),
69+
}),
70+
})
71+
.debug()
72+
.end();
73+
assert(await fs.stat(path.join(homedir, 'foocache/.npminstall_tarball/d/e/b/u/debug')));
74+
});
6275
});

0 commit comments

Comments
 (0)