@@ -3,38 +3,147 @@ import path from 'node:path'
33import process from 'node:process'
44
55import constants from '../constants'
6- import { findNpmPath } from '../utils/path-resolve'
6+ import { findBinPathDetailsSync , findNpmPathSync } from '../utils/path-resolve'
77
8- const { NODE_MODULES , SOCKET_CLI_ISSUES_URL } = constants
8+ const { NODE_MODULES , NPM , NPX , SOCKET_CLI_ISSUES_URL } = constants
99
10- const npmEntrypoint = realpathSync . native ( process . argv [ 1 ] ! )
11- const npmPath = findNpmPath ( npmEntrypoint )
12- if ( npmPath === undefined ) {
10+ function exitWithBinPathError ( binName : string ) : never {
1311 console . error (
14- `Unable to find npm CLI install directory.
15- Searched parent directories of ${ npmEntrypoint } .
16-
17- This is may be a bug with socket-npm related to changes to the npm CLI.
18- Please report to ${ SOCKET_CLI_ISSUES_URL } .`
12+ `Socket unable to locate ${ binName } ; ensure it is available in the PATH environment variable.`
1913 )
2014 // The exit code 127 indicates that the command or binary being executed
2115 // could not be found.
2216 process . exit ( 127 )
2317}
2418
25- export const npmNmPath = path . join ( npmPath , NODE_MODULES )
26- export const arboristPkgPath = path . join ( npmNmPath , '@npmcli/arborist' )
27- export const arboristClassPath = path . join (
28- arboristPkgPath ,
29- 'lib/arborist/index.js'
30- )
31- export const arboristDepValidPath = path . join (
32- arboristPkgPath ,
33- 'lib/dep-valid.js'
34- )
35- export const arboristEdgeClassPath = path . join ( arboristPkgPath , 'lib/edge.js' )
36- export const arboristNodeClassPath = path . join ( arboristPkgPath , 'lib/node.js' )
37- export const arboristOverrideSetClassPath = path . join (
38- arboristPkgPath ,
39- 'lib/override-set.js'
40- )
19+ let _npmBinPathDetails : ReturnType < typeof findBinPathDetailsSync > | undefined
20+ function getNpmBinPathDetails ( ) : ReturnType < typeof findBinPathDetailsSync > {
21+ if ( _npmBinPathDetails === undefined ) {
22+ _npmBinPathDetails = findBinPathDetailsSync ( NPM )
23+ }
24+ return _npmBinPathDetails
25+ }
26+
27+ let _npxBinPathDetails : ReturnType < typeof findBinPathDetailsSync > | undefined
28+ function getNpxBinPathDetails ( ) : ReturnType < typeof findBinPathDetailsSync > {
29+ if ( _npxBinPathDetails === undefined ) {
30+ _npxBinPathDetails = findBinPathDetailsSync ( NPX )
31+ }
32+ return _npxBinPathDetails
33+ }
34+
35+ let _npmBinPath : string | undefined
36+ export function getNpmBinPath ( ) : string {
37+ if ( _npmBinPath === undefined ) {
38+ _npmBinPath = getNpmBinPathDetails ( ) . path
39+ if ( ! _npmBinPath ) {
40+ exitWithBinPathError ( NPM )
41+ }
42+ }
43+ return _npmBinPath
44+ }
45+
46+ export function isNpmBinPathShadowed ( ) {
47+ return getNpmBinPathDetails ( ) . shadowed
48+ }
49+
50+ let _npxBinPath : string | undefined
51+ export function getNpxBinPath ( ) : string {
52+ if ( _npxBinPath === undefined ) {
53+ _npxBinPath = getNpxBinPathDetails ( ) . path
54+ if ( ! _npxBinPath ) {
55+ exitWithBinPathError ( NPX )
56+ }
57+ }
58+ return _npxBinPath
59+ }
60+
61+ export function isNpxBinPathShadowed ( ) {
62+ return getNpxBinPathDetails ( ) . shadowed
63+ }
64+
65+ let _npmPath : string | undefined
66+ export function getNpmPath ( ) {
67+ if ( _npmPath === undefined ) {
68+ const npmEntrypoint = path . dirname ( realpathSync . native ( getNpmBinPath ( ) ) )
69+ _npmPath = findNpmPathSync ( npmEntrypoint )
70+ if ( ! _npmPath ) {
71+ console . error (
72+ `Unable to find npm CLI install directory.
73+ Searched parent directories of ${ npmEntrypoint } .
74+
75+ This is may be a bug with socket-npm related to changes to the npm CLI.
76+ Please report to ${ SOCKET_CLI_ISSUES_URL } .`
77+ )
78+ // The exit code 127 indicates that the command or binary being executed
79+ // could not be found.
80+ process . exit ( 127 )
81+ }
82+ }
83+ return _npmPath
84+ }
85+
86+ let _npmNmPath : string | undefined
87+ export function getNpmNodeModulesPath ( ) {
88+ if ( _npmNmPath === undefined ) {
89+ _npmNmPath = path . join ( getNpmPath ( ) , NODE_MODULES )
90+ }
91+ return _npmNmPath
92+ }
93+
94+ let _arboristPkgPath : string | undefined
95+ export function getArboristPackagePath ( ) {
96+ if ( _arboristPkgPath === undefined ) {
97+ _arboristPkgPath = path . join ( getNpmNodeModulesPath ( ) , '@npmcli/arborist' )
98+ }
99+ return _arboristPkgPath
100+ }
101+
102+ let _arboristClassPath : string | undefined
103+ export function getArboristClassPath ( ) {
104+ if ( _arboristClassPath === undefined ) {
105+ _arboristClassPath = path . join (
106+ getArboristPackagePath ( ) ,
107+ 'lib/arborist/index.js'
108+ )
109+ }
110+ return _arboristClassPath
111+ }
112+
113+ let _arboristDepValidPath : string | undefined
114+ export function getArboristDepValidPath ( ) {
115+ if ( _arboristDepValidPath === undefined ) {
116+ _arboristDepValidPath = path . join (
117+ getArboristPackagePath ( ) ,
118+ 'lib/dep-valid.js'
119+ )
120+ }
121+ return _arboristDepValidPath
122+ }
123+
124+ let _arboristEdgeClassPath : string | undefined
125+ export function getArboristEdgeClassPath ( ) {
126+ if ( _arboristEdgeClassPath === undefined ) {
127+ _arboristEdgeClassPath = path . join ( getArboristPackagePath ( ) , 'lib/edge.js' )
128+ }
129+ return _arboristEdgeClassPath
130+ }
131+
132+ let _arboristNodeClassPath : string | undefined
133+ export function getArboristNodeClassPath ( ) {
134+ if ( _arboristNodeClassPath === undefined ) {
135+ _arboristNodeClassPath = path . join ( getArboristPackagePath ( ) , 'lib/node.js' )
136+ }
137+ return _arboristNodeClassPath
138+ }
139+
140+ let _arboristOverrideSetClassPath : string | undefined
141+ export function getArboristOverrideSetClassPath ( ) {
142+ if ( _arboristOverrideSetClassPath === undefined ) {
143+ _arboristOverrideSetClassPath = path . join (
144+ getArboristPackagePath ( ) ,
145+ 'lib/override-set.js'
146+ )
147+ }
148+ return _arboristOverrideSetClassPath
149+ }
0 commit comments