Skip to content

Instantly share code, notes, and snippets.

@CodeByAidan
Last active July 29, 2024 12:41
Show Gist options
  • Save CodeByAidan/7f0ff31f6f115035ed3ea02e75b8cadc to your computer and use it in GitHub Desktop.
Save CodeByAidan/7f0ff31f6f115035ed3ea02e75b8cadc to your computer and use it in GitHub Desktop.
little sexy script I made to stop having fnm (Fast Node Manager - https://github.com/Schniz/fnm) from not being detected by the Path on PowerShell - just reinstalls it! You can do this same kinda thing with anything! This script is part of my $PROFILE
function Test-Fnm {
if (-not (Get-Command fnm -ErrorAction SilentlyContinue)) {
$flagFile = "$env:TEMP\fnm_installing.flag"
if (-not (Test-Path $flagFile)) {
New-Item -Path $flagFile -ItemType File | Out-Null
try {
Start-Process powershell -ArgumentList "-NoProfile -Command `"winget install Schniz.fnm; Remove-Item -Path $flagFile`"" -WindowStyle Hidden
$maxRetries = 12 # wait up to 60 seconds (12 * 5)
$retry = 0
while (-not (Get-Command fnm -ErrorAction SilentlyContinue) -and $retry -lt $maxRetries) {
Start-Sleep -Seconds 5
$retry++
}
}
catch {
Remove-Item -Path $flagFile -ErrorAction SilentlyContinue
}
}
}
}
Test-Fnm
if (Get-Command fnm -ErrorAction SilentlyContinue) {
fnm env --use-on-cd | Out-String | Invoke-Expression
}
else {
Write-Host "fnm installation did not complete successfully."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment