Skip to content

Commit 9ad9979

Browse files
authored
fix(install): Warn when another flagsmith on PATH may shadow the install (#97)
1 parent d7fcce9 commit 9ad9979

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

‎install.ps1‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@ function Add-UserPath {
8484
return $true
8585
}
8686

87+
# Write-ConflictWarning reports other flagsmith commands on PATH that may shadow
88+
# (or be shadowed by) the one just installed.
89+
function Write-ConflictWarning {
90+
param([string]$Target)
91+
92+
$resolvedTarget = [System.IO.Path]::GetFullPath($Target)
93+
# Application catches flagsmith.exe and npm's flagsmith.cmd shim;
94+
# ExternalScript catches its flagsmith.ps1 shim. Both expose .Path, and
95+
# neither triggers module auto-loading the way an unfiltered -All can.
96+
$others = @(Get-Command -Name 'flagsmith' -CommandType Application, ExternalScript -All -ErrorAction SilentlyContinue |
97+
Where-Object { $_.Path -and ([System.IO.Path]::GetFullPath($_.Path) -ne $resolvedTarget) })
98+
foreach ($other in $others) {
99+
Write-Output "warning: another 'flagsmith' is on your PATH at $($other.Path)"
100+
}
101+
if ($others.Count -gt 0) {
102+
Write-Output "It may shadow $Target - uninstall it first ('npm uninstall -g flagsmith-cli' removes the old npm CLI), then open a new terminal."
103+
}
104+
}
105+
87106
# Add-CiPath makes the CLI available to later steps of a GitHub Actions job.
88107
function Add-CiPath {
89108
param([string]$Dir)
@@ -144,6 +163,7 @@ try {
144163

145164
Expand-Archive -LiteralPath $zip -DestinationPath $tmp -Force
146165
New-Item -ItemType Directory -Force -Path $BinDir | Out-Null
166+
$verb, $prep = if (Test-Path -LiteralPath (Join-Path $BinDir $ExeName)) { 'updated', 'at' } else { 'installed', 'to' }
147167
Move-Item -Force -LiteralPath (Join-Path $tmp $ExeName) -Destination (Join-Path $BinDir $ExeName)
148168
} finally {
149169
Remove-Item -Recurse -Force -LiteralPath $tmp
@@ -152,7 +172,8 @@ try {
152172
$exe = Join-Path $BinDir $ExeName
153173
$installed = & $exe --version
154174
if ($LASTEXITCODE -ne 0) { throw "$exe was installed but will not run" }
155-
Write-Output "installed $installed to $exe"
175+
Write-Output "$verb $installed $prep $exe"
176+
Write-ConflictWarning -Target $exe
156177

157178
$pathAdded = $false
158179
if (-not $NoModifyPath) {

‎install.sh‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,36 @@ end
166166
EOF
167167
}
168168

169+
# same_file returns 0 when both paths name the same file. -ef is not POSIX
170+
# but every common sh has it; fall back to a plain string compare.
171+
same_file() {
172+
[ "$1" = "$2" ] && return 0
173+
[ "$1" -ef "$2" ] 2>/dev/null
174+
}
175+
176+
# warn_conflicts reports other flagsmith binaries on PATH that may shadow
177+
# (or be shadowed by) the one just installed.
178+
warn_conflicts() {
179+
_target="${INSTALL_DIR}/${BIN_NAME}"
180+
_found=0
181+
_seen=
182+
_ifs_save=$IFS
183+
IFS=:
184+
for _dir in $PATH; do
185+
[ -n "$_dir" ] || continue
186+
case ":${_seen}:" in *:"${_dir}":*) continue ;; esac
187+
_seen="${_seen}:${_dir}"
188+
[ -x "${_dir}/${BIN_NAME}" ] || continue
189+
same_file "${_dir}/${BIN_NAME}" "$_target" && continue
190+
say "warning: another '${BIN_NAME}' is on your PATH at ${_dir}/${BIN_NAME}"
191+
_found=1
192+
done
193+
IFS=$_ifs_save
194+
if [ "$_found" = 1 ]; then
195+
say "It may shadow ${_target} — uninstall it first ('npm uninstall -g flagsmith-cli' removes the old npm CLI), then run 'hash -r' or open a new shell."
196+
fi
197+
}
198+
169199
# add_ci_path makes the CLI available to later steps of a GitHub Actions job.
170200
# GITHUB_PATH does not expand variables, so write the resolved directory.
171201
add_ci_path() {
@@ -248,11 +278,18 @@ If ${VERSION} was released moments ago its archives may still be uploading — r
248278
tar -xzf "${tmp}/${archive_name}" -C "$tmp" "$BIN_NAME"
249279
mkdir -p "$INSTALL_DIR"
250280
chmod 755 "${tmp}/${BIN_NAME}"
281+
verb=installed
282+
preposition=to
283+
if [ -e "${INSTALL_DIR}/${BIN_NAME}" ]; then
284+
verb=updated
285+
preposition='at'
286+
fi
251287
mv -f "${tmp}/${BIN_NAME}" "${INSTALL_DIR}/${BIN_NAME}"
252288

253289
installed=$("${INSTALL_DIR}/${BIN_NAME}" --version 2>/dev/null) ||
254290
err "${INSTALL_DIR}/${BIN_NAME} was installed but will not run — wrong platform?"
255-
say "installed ${installed} to ${INSTALL_DIR}/${BIN_NAME}"
291+
say "${verb} ${installed} ${preposition} ${INSTALL_DIR}/${BIN_NAME}"
292+
warn_conflicts
256293

257294
if [ "$NO_MODIFY_PATH" != 1 ]; then
258295
modify_path

0 commit comments

Comments
 (0)