2
2
3
3
const fs = require ( 'fs' )
4
4
const figgyPudding = require ( 'figgy-pudding' )
5
+ const findUp = require ( 'find-up' )
5
6
const ini = require ( 'ini' )
6
7
const os = require ( 'os' )
7
8
const path = require ( 'path' )
@@ -14,23 +15,15 @@ const NpmConfig = figgyPudding({}, {
14
15
const ConfigOpts = figgyPudding ( {
15
16
cache : { default : path . join ( os . homedir ( ) , '.npm' ) } ,
16
17
cwd : { default : ( ) => process . cwd ( ) } ,
18
+ globalconfig : {
19
+ default : ( ) => path . join ( getGlobalPrefix ( ) , 'etc' , 'npmrc' )
20
+ } ,
17
21
userconfig : { default : path . join ( os . homedir ( ) , '.npmrc' ) }
18
22
} )
19
23
20
24
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 )
34
27
const env = Object . keys ( process . env ) . reduce ( ( acc , key ) => {
35
28
if ( key . match ( / ^ n p m _ c o n f i g _ / i) ) {
36
29
const newKey = key . toLowerCase ( )
@@ -40,10 +33,25 @@ function getNpmConfig (_opts, configOpts) {
40
33
}
41
34
return acc
42
35
} , { } )
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
45
47
)
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 )
47
55
if ( newOpts . cache ) {
48
56
return newOpts . concat ( {
49
57
cache : path . join ( newOpts . cache , '_cacache' )
@@ -66,3 +74,20 @@ function maybeReadIni (f) {
66
74
}
67
75
return ini . parse ( txt )
68
76
}
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
+ }
0 commit comments