Skip to content

Commit 003cc49

Browse files
committed
wip
1 parent 328484c commit 003cc49

2 files changed

Lines changed: 67 additions & 2 deletions

File tree

packages/cli/install.ps1

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,27 @@ function Get-PreviousInstallDir {
297297
return $oldDir
298298
}
299299

300+
function Test-NestedInstallDir {
301+
param(
302+
[string]$OldDir,
303+
[string]$NewDir
304+
)
305+
if ([string]::IsNullOrWhiteSpace($OldDir) -or [string]::IsNullOrWhiteSpace($NewDir)) {
306+
return $false
307+
}
308+
309+
$oldDir = Normalize-InstallDir $OldDir
310+
$newDir = Normalize-InstallDir $NewDir
311+
if ([string]::IsNullOrWhiteSpace($oldDir) -or [string]::IsNullOrWhiteSpace($newDir) -or $oldDir -eq $newDir) {
312+
return $false
313+
}
314+
315+
$oldPrefix = $oldDir.TrimEnd([System.IO.Path]::DirectorySeparatorChar, [System.IO.Path]::AltDirectorySeparatorChar) + [System.IO.Path]::DirectorySeparatorChar
316+
$newPrefix = $newDir.TrimEnd([System.IO.Path]::DirectorySeparatorChar, [System.IO.Path]::AltDirectorySeparatorChar) + [System.IO.Path]::DirectorySeparatorChar
317+
return $oldPrefix.StartsWith($newPrefix, [System.StringComparison]::OrdinalIgnoreCase) `
318+
-or $newPrefix.StartsWith($oldPrefix, [System.StringComparison]::OrdinalIgnoreCase)
319+
}
320+
300321
function Prompt-RemovePreviousInstallDir {
301322
param([string]$PreviousInstallDir)
302323
if (-not $PreviousInstallDir) {
@@ -723,7 +744,9 @@ function Main {
723744
}
724745

725746
$previousInstallDir = Get-PreviousInstallDir
726-
Prompt-RemovePreviousInstallDir -PreviousInstallDir $previousInstallDir
747+
if ($previousInstallDir -and (Test-NestedInstallDir -OldDir $previousInstallDir -NewDir $InstallDir)) {
748+
Write-Error-Exit "Previous Vite+ install at $previousInstallDir overlaps with VP_HOME $InstallDir. Choose a separate VP_HOME or remove the previous install first."
749+
}
727750

728751
# Suppress progress bars for cleaner output
729752
$ProgressPreference = 'SilentlyContinue'
@@ -951,6 +974,8 @@ exec "`$VP_HOME/current/bin/vp.exe" "`$@"
951974
# Setup Node.js version manager (shims) - separate component
952975
$nodeManagerResult = Setup-NodeManager -BinDir $BinDir
953976

977+
Prompt-RemovePreviousInstallDir -PreviousInstallDir $previousInstallDir
978+
954979
# Use ~ shorthand if install dir is under USERPROFILE, otherwise show full path
955980
$displayDir = $InstallDir -replace [regex]::Escape($env:USERPROFILE), '~'
956981

packages/cli/install.sh

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@ normalize_existing_dir() {
146146
if [ -d "$dir" ]; then
147147
(cd "$dir" 2>/dev/null && pwd -P) || printf '%s\n' "$dir"
148148
else
149+
local parent base parent_dir
150+
parent="$(dirname "$dir")"
151+
base="$(basename "$dir")"
152+
if [ -d "$parent" ]; then
153+
parent_dir="$(cd "$parent" 2>/dev/null && pwd -P)" || parent_dir=""
154+
if [ -n "$parent_dir" ]; then
155+
if [ "$parent_dir" = "/" ]; then
156+
printf '/%s\n' "$base"
157+
else
158+
printf '%s/%s\n' "$parent_dir" "$base"
159+
fi
160+
return 0
161+
fi
162+
fi
149163
printf '%s\n' "$dir"
150164
fi
151165
}
@@ -198,6 +212,28 @@ detect_previous_install_dir() {
198212
printf '%s\n' "$old_dir"
199213
}
200214

215+
is_nested_install_dir() {
216+
[ -n "$1" ] && [ -n "$2" ] || return 1
217+
218+
local old_dir install_dir
219+
old_dir="$(normalize_existing_dir "$1")"
220+
install_dir="$(normalize_existing_dir "$2")"
221+
222+
[ "$old_dir" != "$install_dir" ] || return 1
223+
if [ "$old_dir" = "/" ] || [ "$install_dir" = "/" ]; then
224+
return 0
225+
fi
226+
227+
case "$old_dir" in
228+
"$install_dir"/*) return 0 ;;
229+
esac
230+
case "$install_dir" in
231+
"$old_dir"/*) return 0 ;;
232+
esac
233+
234+
return 1
235+
}
236+
201237
prompt_remove_previous_install_dir() {
202238
local old_dir="$1"
203239
[ -n "$old_dir" ] || return 0
@@ -984,7 +1020,9 @@ main() {
9841020

9851021
local previous_install_dir
9861022
previous_install_dir="$(detect_previous_install_dir || true)"
987-
prompt_remove_previous_install_dir "$previous_install_dir"
1023+
if [ -n "$previous_install_dir" ] && is_nested_install_dir "$previous_install_dir" "$INSTALL_DIR"; then
1024+
error "Previous Vite+ install at $previous_install_dir overlaps with VP_HOME $INSTALL_DIR. Choose a separate VP_HOME or remove the previous install first."
1025+
fi
9881026

9891027
local platform
9901028
platform=$(detect_platform)
@@ -1177,6 +1215,8 @@ WRAPPER_EOF
11771215
# Setup Node.js version manager (shims) - separate component
11781216
setup_node_manager "$BIN_DIR"
11791217

1218+
prompt_remove_previous_install_dir "$previous_install_dir"
1219+
11801220
# Use ~ shorthand if install dir is under HOME, otherwise show full path
11811221
local display_dir="${INSTALL_DIR/#$HOME/~}"
11821222
local display_location="${display_dir}/bin"

0 commit comments

Comments
 (0)