@@ -8,15 +8,20 @@ const isWindows = process.platform === 'win32';
88
99const untildify = pathWithTilde => pathWithTilde && pathWithTilde . startsWith ( '~' ) ? path . join ( os . homedir ( ) , pathWithTilde . slice ( 1 ) ) : pathWithTilde ;
1010
11- const readRc = filePath => {
11+ const readConfigValue = ( filePath , key ) => {
12+ if ( ! filePath ) {
13+ return ;
14+ }
15+
1216 try {
13- return ini . parse ( fs . readFileSync ( filePath , 'utf8' ) ) . prefix ;
17+ return ini . parse ( fs . readFileSync ( filePath , 'utf8' ) ) [ key ] ;
1418 } catch { }
1519} ;
1620
17- const getEnvNpmPrefix = ( ) => {
18- const key = Object . keys ( process . env ) . find ( name => name . toLowerCase ( ) === 'npm_config_prefix' ) ;
19- return key ? process . env [ key ] : undefined ;
21+ const getEnvironmentNpmConfigValue = key => {
22+ const normalizedKey = `npm_config_${ key . replaceAll ( '-' , '_' ) } ` . toLowerCase ( ) ;
23+ const environmentKey = Object . keys ( process . env ) . find ( name => name . toLowerCase ( ) === normalizedKey ) ;
24+ return environmentKey ? process . env [ environmentKey ] : undefined ;
2025} ;
2126
2227const getGlobalNpmrc = ( ) => {
@@ -54,22 +59,22 @@ const getDefaultNpmPrefix = () => {
5459} ;
5560
5661const getNpmPrefix = ( ) => {
57- const envPrefix = getEnvNpmPrefix ( ) ;
58- if ( envPrefix ) {
59- return envPrefix ;
62+ const environmentPrefix = getEnvironmentNpmConfigValue ( 'prefix' ) ;
63+ if ( environmentPrefix !== undefined ) {
64+ return environmentPrefix ;
6065 }
6166
62- const homePrefix = readRc ( path . join ( os . homedir ( ) , '.npmrc' ) ) ;
63- if ( homePrefix ) {
67+ const homePrefix = readConfigValue ( path . join ( os . homedir ( ) , '.npmrc' ) , 'prefix' ) ;
68+ if ( homePrefix !== undefined ) {
6469 return homePrefix ;
6570 }
6671
6772 if ( process . env . PREFIX ) {
6873 return process . env . PREFIX ;
6974 }
7075
71- const globalPrefix = readRc ( getGlobalNpmrc ( ) ) ;
72- if ( globalPrefix ) {
76+ const globalPrefix = readConfigValue ( getGlobalNpmrc ( ) , 'prefix' ) ;
77+ if ( globalPrefix !== undefined ) {
7378 return globalPrefix ;
7479 }
7580
@@ -127,4 +132,77 @@ globalDirectory.yarn.prefix = yarnDataDir;
127132globalDirectory . yarn . packages = path . join ( yarnDataDir , 'global/node_modules' ) ;
128133globalDirectory . yarn . binaries = path . join ( path . resolve ( getYarnBinPrefix ( ) ) , 'bin' ) ;
129134
135+ const getPnpmDataDirectory = ( ) => {
136+ if ( process . env . PNPM_HOME ) {
137+ return process . env . PNPM_HOME ;
138+ }
139+
140+ if ( process . env . XDG_DATA_HOME ) {
141+ return path . join ( process . env . XDG_DATA_HOME , 'pnpm' ) ;
142+ }
143+
144+ if ( process . platform === 'darwin' ) {
145+ return path . join ( os . homedir ( ) , 'Library/pnpm' ) ;
146+ }
147+
148+ if ( ! isWindows ) {
149+ return path . join ( os . homedir ( ) , '.local/share/pnpm' ) ;
150+ }
151+
152+ if ( process . env . LOCALAPPDATA ) {
153+ return path . join ( process . env . LOCALAPPDATA , 'pnpm' ) ;
154+ }
155+
156+ return path . join ( os . homedir ( ) , '.pnpm' ) ;
157+ } ;
158+
159+ const getPnpmConfigFilePath = ( ) => {
160+ if ( process . env . XDG_CONFIG_HOME ) {
161+ return path . join ( process . env . XDG_CONFIG_HOME , 'pnpm' , 'rc' ) ;
162+ }
163+
164+ if ( isWindows ) {
165+ const localConfigHome = process . env . LOCALAPPDATA ?? path . join ( os . homedir ( ) , 'AppData' , 'Local' ) ;
166+ return path . join ( localConfigHome , 'pnpm' , 'config' , 'rc' ) ;
167+ }
168+
169+ if ( process . platform === 'darwin' ) {
170+ return path . join ( os . homedir ( ) , 'Library' , 'Preferences' , 'pnpm' , 'rc' ) ;
171+ }
172+
173+ return path . join ( os . homedir ( ) , '.config' , 'pnpm' , 'rc' ) ;
174+ } ;
175+
176+ const getPnpmConfigValue = key => {
177+ const environmentValue = getEnvironmentNpmConfigValue ( key ) ;
178+ if ( environmentValue !== undefined ) {
179+ return environmentValue ;
180+ }
181+
182+ const pnpmGlobalValue = readConfigValue ( getPnpmConfigFilePath ( ) , key ) ;
183+ if ( pnpmGlobalValue !== undefined ) {
184+ return pnpmGlobalValue ;
185+ }
186+
187+ const homeValue = readConfigValue ( path . join ( os . homedir ( ) , '.npmrc' ) , key ) ;
188+ if ( homeValue !== undefined ) {
189+ return homeValue ;
190+ }
191+
192+ const globalValue = readConfigValue ( getGlobalNpmrc ( ) , key ) ;
193+ if ( globalValue !== undefined ) {
194+ return globalValue ;
195+ }
196+ } ;
197+
198+ const pnpmDataDir = path . resolve ( getPnpmDataDirectory ( ) ) ;
199+ const pnpmGlobalDir = getPnpmConfigValue ( 'global-dir' ) ;
200+ const pnpmGlobalBinDir = getPnpmConfigValue ( 'global-bin-dir' ) ;
201+ const resolvedPnpmGlobalDir = path . resolve ( untildify ( pnpmGlobalDir ?? path . join ( pnpmDataDir , 'global' ) ) ) ;
202+ const resolvedPnpmGlobalBinDir = path . resolve ( untildify ( pnpmGlobalBinDir ?? pnpmDataDir ) ) ;
203+ globalDirectory . pnpm = { } ;
204+ globalDirectory . pnpm . prefix = pnpmDataDir ;
205+ globalDirectory . pnpm . packages = path . join ( resolvedPnpmGlobalDir , '5/node_modules' ) ;
206+ globalDirectory . pnpm . binaries = resolvedPnpmGlobalBinDir ;
207+
130208export default globalDirectory ;
0 commit comments