@@ -26,8 +26,37 @@ scripts/lib/NodeJs.psm1
2626 The path to pnpm's global node_modules directory, or $null if not found.
2727#>
2828function Get-PnpmGlobalPath {
29- # Use Validation module if available
30- $useValidation = Get-Command Test-ValidPath - ErrorAction SilentlyContinue
29+ # Prefer core Get-Command so Pester mocks of Get-Command cannot break the Validation probe.
30+ $useValidation = $false
31+ try {
32+ $useValidation = $null -ne (Microsoft.PowerShell.Core\Get-Command - Name ' Test-ValidPath' - ErrorAction SilentlyContinue)
33+ }
34+ catch {
35+ $useValidation = $false
36+ }
37+
38+ # Local helper: Test-ValidPath when available, else Test-Path (never throw on missing helper).
39+ $testPathExists = {
40+ param ([string ]$Path , [string ]$PathType = ' Any' )
41+ if (-not $Path -or [string ]::IsNullOrWhiteSpace($Path )) {
42+ return $false
43+ }
44+ if ($useValidation ) {
45+ try {
46+ return [bool ](Test-ValidPath - Path $Path - PathType $PathType )
47+ }
48+ catch {
49+ # Fall through to Test-Path
50+ }
51+ }
52+ if ($PathType -eq ' Directory' ) {
53+ return (Test-Path - LiteralPath $Path - PathType Container)
54+ }
55+ if ($PathType -eq ' File' ) {
56+ return (Test-Path - LiteralPath $Path - PathType Leaf)
57+ }
58+ return (Test-Path - LiteralPath $Path )
59+ }
3160
3261 # First, check common pnpm/Node.js-related environment variables (highest priority)
3362 $pnpmEnvVars = @ (' PNPM_HOME' , ' PNPM_ROOT' , ' NPM_CONFIG_PREFIX' , ' NODE_PATH' , ' NVM_DIR' )
@@ -45,36 +74,18 @@ function Get-PnpmGlobalPath {
4574 # PNPM_HOME typically points to the pnpm installation directory
4675 # Check for global node_modules
4776 $testPath = Join-Path $envValue ' node_modules'
48- $pathExists = if ($useValidation ) {
49- Test-ValidPath - Path $testPath - PathType Directory
50- }
51- else {
52- $testPath -and -not [string ]::IsNullOrWhiteSpace($testPath ) -and (Test-Path - LiteralPath $testPath )
53- }
54- if ($pathExists ) {
77+ if (& $testPathExists - Path $testPath - PathType Directory) {
5578 return $testPath
5679 }
5780 # Also check if the value itself is a node_modules path
58- $pathExists = if ($useValidation ) {
59- Test-ValidPath - Path $envValue - PathType Directory
60- }
61- else {
62- $envValue -and -not [string ]::IsNullOrWhiteSpace($envValue ) -and (Test-Path - LiteralPath $envValue )
63- }
64- if ($pathExists -and $envValue -like ' *node_modules*' ) {
81+ if ((& $testPathExists - Path $envValue - PathType Directory) -and ($envValue -like ' *node_modules*' )) {
6582 return $envValue
6683 }
6784 }
6885 # For NPM_CONFIG_PREFIX - this points to npm global installation
6986 elseif ($envVar -eq ' NPM_CONFIG_PREFIX' ) {
7087 $testPath = Join-Path $envValue ' node_modules'
71- $pathExists = if ($useValidation ) {
72- Test-ValidPath - Path $testPath - PathType Directory
73- }
74- else {
75- $testPath -and -not [string ]::IsNullOrWhiteSpace($testPath ) -and (Test-Path - LiteralPath $testPath )
76- }
77- if ($pathExists ) {
88+ if (& $testPathExists - Path $testPath - PathType Directory) {
7889 return $testPath
7990 }
8091 }
@@ -83,13 +94,7 @@ function Get-PnpmGlobalPath {
8394 $paths = $envValue -split ([System.IO.Path ]::PathSeparator)
8495 foreach ($path in $paths ) {
8596 if ($path -and -not [string ]::IsNullOrWhiteSpace($path )) {
86- $pathExists = if ($useValidation ) {
87- Test-ValidPath - Path $path - PathType Directory
88- }
89- else {
90- $path -and -not [string ]::IsNullOrWhiteSpace($path ) -and (Test-Path - LiteralPath $path )
91- }
92- if ($pathExists ) {
97+ if (& $testPathExists - Path $path - PathType Directory) {
9398 return $path
9499 }
95100 }
@@ -99,24 +104,12 @@ function Get-PnpmGlobalPath {
99104 elseif ($envVar -eq ' NVM_DIR' ) {
100105 # nvm typically has versions in versions/node directory
101106 $testPath = Join-Path $envValue ' versions' ' node'
102- $pathExists = if ($useValidation ) {
103- Test-ValidPath - Path $testPath - PathType Directory
104- }
105- else {
106- $testPath -and -not [string ]::IsNullOrWhiteSpace($testPath ) -and (Test-Path - LiteralPath $testPath )
107- }
108- if ($pathExists ) {
107+ if (& $testPathExists - Path $testPath - PathType Directory) {
109108 # Return the first version's node_modules if available
110109 $versions = Get-ChildItem - Path $testPath - Directory - ErrorAction SilentlyContinue | Sort-Object Name - Descending
111110 if ($versions ) {
112111 $latestVersionPath = Join-Path $versions [0 ].FullName ' lib' ' node_modules'
113- $latestExists = if ($useValidation ) {
114- Test-ValidPath - Path $latestVersionPath - PathType Directory
115- }
116- else {
117- $latestVersionPath -and -not [string ]::IsNullOrWhiteSpace($latestVersionPath ) -and (Test-Path - LiteralPath $latestVersionPath )
118- }
119- if ($latestExists ) {
112+ if (& $testPathExists - Path $latestVersionPath - PathType Directory) {
120113 return $latestVersionPath
121114 }
122115 }
@@ -143,16 +136,8 @@ function Get-PnpmGlobalPath {
143136 if ($pnpmRoot ) {
144137 $pnpmGlobalPath = $pnpmRoot.ToString ().Trim()
145138 # Validate that the path exists
146- if ($useValidation ) {
147- if (Test-ValidPath - Path $pnpmGlobalPath - PathType Directory) {
148- return $pnpmGlobalPath
149- }
150- }
151- else {
152- # Fallback to manual validation
153- if ($pnpmGlobalPath -and -not [string ]::IsNullOrWhiteSpace($pnpmGlobalPath ) -and (Test-Path - LiteralPath $pnpmGlobalPath )) {
154- return $pnpmGlobalPath
155- }
139+ if (& $testPathExists - Path $pnpmGlobalPath - PathType Directory) {
140+ return $pnpmGlobalPath
156141 }
157142 }
158143 }
0 commit comments