@@ -3,6 +3,7 @@ import { readFileSync, rmSync } from "node:fs";
33import { tmpdir } from "node:os" ;
44import { join } from "node:path" ;
55import { pkgPrNewCommitSha } from "./install-script-urls.js" ;
6+ import { parseInstalledVpVersion } from "./version.js" ;
67
78// Keep installer network calls bounded so a hung source fails over quickly.
89const CURL_TIMEOUT_FLAGS = "--connect-timeout 5 --max-time 15" ;
@@ -24,8 +25,8 @@ export function supportsVitePlusDirs(version: string): boolean {
2425 if ( pkgPrNewCommitSha ( version ) ) return true ;
2526
2627 const match = version . match ( EXACT_VERSION_RE ) ;
27- // Dist-tags resolve during installation. They track current releases, so
28- // keep VpDirs detection enabled when an exact version is not available .
28+ // Dist-tags resolve during installation. Keep the probe enabled so the
29+ // installed version can decide whether missing VpDirs output is valid .
2930 if ( ! match ) return true ;
3031
3132 const major = Number ( match [ 1 ] ) ;
@@ -42,22 +43,43 @@ export function removeVitePlusDirsFile(filePath: string): void {
4243}
4344
4445export function readVitePlusDirs ( filePath : string ) : VitePlusDirs | undefined {
46+ const output = readVitePlusProbe ( filePath ) ;
47+ return output === undefined ? undefined : parseVitePlusDirs ( output ) ;
48+ }
49+
50+ function readVitePlusProbe ( filePath : string ) : string | undefined {
4551 try {
46- return parseVitePlusDirs ( readFileSync ( filePath , "utf8" ) ) ;
52+ return readFileSync ( filePath , "utf8" ) ;
4753 } catch ( error ) {
4854 if ( ( error as NodeJS . ErrnoException ) . code === "ENOENT" ) return undefined ;
4955 throw error ;
5056 }
5157}
5258
53- export function resolveVitePlusBinDir ( dirsFile : string | undefined , legacyBinDir : string ) : string {
54- if ( ! dirsFile ) return legacyBinDir ;
55-
56- const dirs = readVitePlusDirs ( dirsFile ) ;
57- if ( ! dirs ) {
59+ export function resolveVitePlusBinDir (
60+ requestedVersion : string ,
61+ dirsFile : string | undefined ,
62+ legacyBinDir : string ,
63+ ) : string {
64+ if ( ! dirsFile ) {
65+ if ( ! supportsVitePlusDirs ( requestedVersion ) ) return legacyBinDir ;
5866 throw new Error ( "Vite+ was installed successfully, but setup-vp could not resolve its VpDirs." ) ;
5967 }
60- return dirs . bin ;
68+
69+ const output = readVitePlusProbe ( dirsFile ) ;
70+ const dirs = output === undefined ? undefined : parseVitePlusDirs ( output ) ;
71+ if ( dirs ) return dirs . bin ;
72+
73+ const hasKnownRequestedVersion =
74+ pkgPrNewCommitSha ( requestedVersion ) !== undefined || EXACT_VERSION_RE . test ( requestedVersion ) ;
75+ if ( ! hasKnownRequestedVersion && output !== undefined ) {
76+ const installedVersion = parseInstalledVpVersion ( output ) ;
77+ if ( installedVersion !== "unknown" && ! supportsVitePlusDirs ( installedVersion ) ) {
78+ return legacyBinDir ;
79+ }
80+ }
81+
82+ throw new Error ( "Vite+ was installed successfully, but setup-vp could not resolve its VpDirs." ) ;
6183}
6284
6385export function parseVitePlusDirs ( output : string ) : VitePlusDirs | undefined {
@@ -101,10 +123,22 @@ export function getInstallScriptCommand(
101123$dirsFile = $env:${ VP_DIRS_FILE_ENV }
102124Set-Content -LiteralPath $dirsFile -Value '' -NoNewline
103125. ([scriptblock]::Create((irm -TimeoutSec ${ PWSH_TIMEOUT_SEC } ${ url } )))
104- $vpPath = if ($script:ShimDir) { Join-Path $script:ShimDir 'vp.exe' } else { $null }
105- if ($vpPath -and (Test-Path -LiteralPath $vpPath)) {
126+ $vpDir = if ($script:ShimDir) {
127+ $script:ShimDir
128+ } elseif ($InstallDir) {
129+ Join-Path $InstallDir 'bin'
130+ } else {
131+ Join-Path $env:USERPROFILE '.vite-plus\\bin'
132+ }
133+ $vpPath = Join-Path $vpDir 'vp.exe'
134+ if (-not (Test-Path -LiteralPath $vpPath)) {
135+ $vpPath = Join-Path $vpDir 'vp.cmd'
136+ }
137+ if (Test-Path -LiteralPath $vpPath) {
138+ & $vpPath --version | Set-Content -LiteralPath $dirsFile
139+ if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
106140 $env:VP_DUMP_DIRS = '1'
107- & $vpPath | Set -Content -LiteralPath $dirsFile
141+ & $vpPath | Add -Content -LiteralPath $dirsFile
108142 if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
109143}
110144` . trim ( ) ;
@@ -126,8 +160,10 @@ trap 'rm -f "$installer_file"' EXIT
126160: > "$${ VP_DIRS_FILE_ENV } "
127161curl -fsSL ${ CURL_TIMEOUT_FLAGS } ${ url } -o "$installer_file"
128162source "$installer_file"
129- if [ -n "\${SHIM_DIR:-}" ] && [ -x "$SHIM_DIR/vp" ]; then
130- VP_DUMP_DIRS=1 "$SHIM_DIR/vp" > "$${ VP_DIRS_FILE_ENV } "
163+ vp_dir="\${SHIM_DIR:-\${INSTALL_DIR:-\${VP_HOME:-$HOME/.vite-plus}}/bin}"
164+ if [ -x "$vp_dir/vp" ]; then
165+ "$vp_dir/vp" --version > "$${ VP_DIRS_FILE_ENV } "
166+ VP_DUMP_DIRS=1 "$vp_dir/vp" >> "$${ VP_DIRS_FILE_ENV } "
131167fi
132168` . trim ( ) ;
133169 return { command : "bash" , args : [ "-c" , script ] } ;
0 commit comments