Skip to content
This repository was archived by the owner on Jun 17, 2021. It is now read-only.

Commit 63ba3bb

Browse files
committed
feat(cache): improved cache parsing/handling
1 parent e878e04 commit 63ba3bb

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

index.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const NpmConfig = figgyPudding({}, {
1414

1515
const ConfigOpts = figgyPudding({
1616
cache: { default: path.join(os.homedir(), '.npm') },
17+
configNames: { default: ['npmrc', '.npmrc'] },
18+
envPrefix: { default: /^npm_config_/i },
1719
cwd: { default: () => process.cwd() },
1820
globalconfig: {
1921
default: () => path.join(getGlobalPrefix(), 'etc', 'npmrc')
@@ -26,9 +28,9 @@ function getNpmConfig (_opts, _builtin) {
2628
const builtin = ConfigOpts(_builtin)
2729
const env = {}
2830
for (let key of Object.keys(process.env)) {
29-
if (!/^npm_config_/i.test(key)) continue
31+
if (!key.match(builtin.envPrefix)) continue
3032
const newKey = key.toLowerCase()
31-
.replace(/^npm_config_/i, '')
33+
.replace(builtin.envPrefix, '')
3234
.replace(/(?!^)_/g, '-')
3335
env[newKey] = process.env[key]
3436
}
@@ -45,15 +47,28 @@ function getNpmConfig (_opts, _builtin) {
4547
env.globalconfig
4648
)
4749
const global = globalConfPath && maybeReadIni(globalConfPath)
48-
const projConfPath = findUp.sync(['.npmrc', 'npmrc'], { cwd: builtin.cwd })
50+
const projConfPath = findUp.sync(builtin.configNames, { cwd: builtin.cwd })
4951
let proj
5052
if (projConfPath && projConfPath !== userConfPath) {
5153
proj = maybeReadIni(projConfPath)
5254
}
5355
const newOpts = NpmConfig(builtin, global, user, proj, env, cli)
5456
if (newOpts.cache) {
5557
return newOpts.concat({
56-
cache: path.join(newOpts.cache, '_cacache')
58+
cache: path.resolve(
59+
(
60+
(cli.cache || env.cache)
61+
? builtin.cwd
62+
: proj.cache
63+
? path.dirname(projConfPath)
64+
: user.cache
65+
? path.dirname(userConfPath)
66+
: global.cache
67+
? path.dirname(globalConfPath)
68+
: path.dirname(userConfPath)
69+
),
70+
newOpts.cache
71+
)
5772
})
5873
} else {
5974
return newOpts

0 commit comments

Comments
 (0)