Skip to content

Commit

Permalink
feat(prettier): add PowerShell installer
Browse files Browse the repository at this point in the history
  • Loading branch information
coolaj86 committed Oct 25, 2023
1 parent 8d7b5db commit 36a4591
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions prettier/install.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env pwsh

####################
# Install prettier #
####################

function command-v-silent($cmdname) {
$my_cmd = Get-Command $command -ErrorAction SilentlyContinue
# $my_path = $my_cmd | Select-Object -ExpandProperty Definition

if ($my_cmd) {
return True
}
return False
}

function mktemp-d-t() {
# create random suffix for dirname
$my_bytes = New-Object byte[] 4
$my_rng = [Security.Cryptography.RNGCryptoServiceProvider]::Create()
$my_rng.GetBytes($bytes)
$my_hex_delimited = [BitConverter]::ToString($bytes)
$my_hex = $my_hex_delimited -replace "-", ""

# create random directory
$my_systmpdir = [System.IO.Path]::GetTempPath()
$my_tmpdir = Join-Path "$my_systmpdir" "$my_hex"
New-Item -ItemType Directory -Path "$my_tmpdir"

return "$my_tmpdir"
}

function npm-install-global($pkgname) {
# Fetch npm package manager
Write-Output "Checking for npm..."
if (-Not (command-v-silent("npm"))) {
& "$Env:USERPROFILE\.local\bin\webi-pwsh.ps1" node
}

if (command-v-silent($pkgname)) {
$my_cmd = Get-Command $pkgname
$my_cmd Select-Object -ExpandProperty Definition
Write-Host "Found '$my_cmd'"
return
}

$my_tmpdir = mktemp-d-t()

# npm install works best from a directory with no package.json
Push-Location "$my_tmpdir"
if (command-v-silent("npm")) {
& npm --location-golbal prettier
} else {
& "$Env:USERPROFILE\.local\opt\node\npm" --location-golbal "$pkgname"
}
Pop-Location
}

npm-install-global prettier

0 comments on commit 36a4591

Please sign in to comment.