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

Commit 5ef1ac5

Browse files
committed
fix(config): rework load order and support builtin configs
1 parent d05be29 commit 5ef1ac5

File tree

4 files changed

+213
-37
lines changed

4 files changed

+213
-37
lines changed

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,15 @@ console.log('configured registry:', config.read({
2626

2727
### API
2828

29-
##### <a name="read"></a> `> read(moreOpts, readOpts)`
29+
##### <a name="read"></a> `> read(cliOpts, builtinOpts)`
3030

3131
Reads configurations from the filesystem and the env and returns a
3232
[`figgy-pudding`](https://npm.im/figgy-pudding) object with the configuration
3333
values.
3434

35-
If `moreOpts` is provided, it will be merged with the returned config pudding,
36-
shadowing any read values.
35+
If `cliOpts` is provided, it will be merged with the returned config pudding,
36+
shadowing any read values. These are intended as CLI-provided options. Do your
37+
own `process.argv` parsing, though.
3738

38-
If `readOpts.cwd` is provided, it will be used instead of `process.cwd()` as the
39-
starting point for config searching.
40-
41-
If `readOpts.userconfig` is provided, it will be used as the user configuration
42-
file, shadowing `moreOpts.userconfig`, which is also acceptable.
39+
If `builtinOpts.cwd` is provided, it will be used instead of `process.cwd()` as
40+
the starting point for config searching.

index.js

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const fs = require('fs')
44
const figgyPudding = require('figgy-pudding')
5+
const findUp = require('find-up')
56
const ini = require('ini')
67
const os = require('os')
78
const path = require('path')
@@ -14,23 +15,15 @@ const NpmConfig = figgyPudding({}, {
1415
const ConfigOpts = figgyPudding({
1516
cache: { default: path.join(os.homedir(), '.npm') },
1617
cwd: { default: () => process.cwd() },
18+
globalconfig: {
19+
default: () => path.join(getGlobalPrefix(), 'etc', 'npmrc')
20+
},
1721
userconfig: { default: path.join(os.homedir(), '.npmrc') }
1822
})
1923

2024
module.exports.read = getNpmConfig
21-
function getNpmConfig (_opts, configOpts) {
22-
const opts = NpmConfig(_opts)
23-
const copts = ConfigOpts(configOpts)
24-
const configs = copts.cwd.split(path.sep).reduce((acc, next) => {
25-
acc.path = path.join(acc.path, next)
26-
acc.configs.push(maybeReadIni(path.join(acc.path, '.npmrc')))
27-
acc.configs.push(maybeReadIni(path.join(acc.path, 'npmrc')))
28-
return acc
29-
}, {
30-
path: '',
31-
configs: []
32-
}).configs.concat(
33-
).filter(x => x)
25+
function getNpmConfig (_opts, _builtin) {
26+
const builtin = ConfigOpts(_builtin)
3427
const env = Object.keys(process.env).reduce((acc, key) => {
3528
if (key.match(/^npm_config_/i)) {
3629
const newKey = key.toLowerCase()
@@ -40,10 +33,25 @@ function getNpmConfig (_opts, configOpts) {
4033
}
4134
return acc
4235
}, {})
43-
const userconfig = maybeReadIni(
44-
env.userconfig || copts.userconfig || opts.userconfig
36+
const cli = NpmConfig(_opts)
37+
const userConfPath = (
38+
builtin.userconfig ||
39+
cli.userconfig ||
40+
env.userconfig
41+
)
42+
const user = userConfPath && maybeReadIni(userConfPath)
43+
const globalConfPath = (
44+
builtin.globalconfig ||
45+
cli.globalconfig ||
46+
env.globalconfig
4547
)
46-
const newOpts = NpmConfig(...configs, userconfig, env, _opts)
48+
const global = globalConfPath && maybeReadIni(globalConfPath)
49+
const projConfPath = findUp.sync(['.npmrc', 'npmrc'], { cwd: builtin.cwd })
50+
let proj
51+
if (projConfPath && projConfPath !== userConfPath) {
52+
proj = maybeReadIni(projConfPath)
53+
}
54+
const newOpts = NpmConfig(builtin, global, user, proj, env, cli)
4755
if (newOpts.cache) {
4856
return newOpts.concat({
4957
cache: path.join(newOpts.cache, '_cacache')
@@ -66,3 +74,20 @@ function maybeReadIni (f) {
6674
}
6775
return ini.parse(txt)
6876
}
77+
78+
function getGlobalPrefix () {
79+
if (process.env.PREFIX) {
80+
return process.env.PREFIX
81+
} else if (process.platform === 'win32') {
82+
// c:\node\node.exe --> prefix=c:\node\
83+
return path.dirname(process.execPath)
84+
} else {
85+
// /usr/local/bin/node --> prefix=/usr/local
86+
let pref = path.dirname(path.dirname(process.execPath))
87+
// destdir only is respected on Unix
88+
if (process.env.DESTDIR) {
89+
pref = path.join(process.env.DESTDIR, pref)
90+
}
91+
return pref
92+
}
93+
}

package-lock.json

Lines changed: 165 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"homepage": "https://npmjs.com/package/libnpmconfig",
3333
"dependencies": {
3434
"figgy-pudding": "^3.5.1",
35+
"find-up": "^3.0.0",
3536
"ini": "^1.3.5"
3637
}
3738
}

0 commit comments

Comments
 (0)