Skip to content

Commit e6b09f3

Browse files
authored
feat(install): add flag support (#911)
* feat(install): show errors from spicetify * feat(install): add flag support * feat: use native ps flags
1 parent 20c1530 commit e6b09f3

File tree

1 file changed

+83
-39
lines changed

1 file changed

+83
-39
lines changed

resources/install.ps1

+83-39
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,103 @@
1+
[CmdletBinding()]
2+
param(
3+
[Parameter()]
4+
[switch]$BypassAdmin
5+
)
6+
17
$ErrorActionPreference = 'Stop'
28
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
39

10+
function Invoke-Spicetify {
11+
param (
12+
[Parameter(Mandatory = $true, Position = 0, ValueFromRemainingArguments = $true)]
13+
[string[]]$Arguments
14+
)
15+
16+
$spicetifyArgs = @()
17+
if ($BypassAdmin) {
18+
$spicetifyArgs += "--bypass-admin"
19+
}
20+
$spicetifyArgs += $Arguments
21+
22+
& spicetify $spicetifyArgs
23+
return $LASTEXITCODE
24+
}
25+
26+
function Invoke-SpicetifyWithOutput {
27+
param (
28+
[Parameter(Mandatory = $true, Position = 0, ValueFromRemainingArguments = $true)]
29+
[string[]]$Arguments
30+
)
31+
32+
$spicetifyArgs = @()
33+
if ($BypassAdmin) {
34+
$spicetifyArgs += "--bypass-admin"
35+
}
36+
$spicetifyArgs += $Arguments
37+
38+
$output = (& spicetify $spicetifyArgs 2>&1 | Out-String).Trim()
39+
return @{
40+
Output = $output
41+
ExitCode = $LASTEXITCODE
42+
}
43+
}
44+
445
Write-Host -Object 'Setting up...' -ForegroundColor 'Cyan'
546

647
if (-not (Get-Command -Name 'spicetify' -ErrorAction 'SilentlyContinue')) {
7-
Write-Host -Object 'Spicetify not found.' -ForegroundColor 'Yellow'
8-
Write-Host -Object 'Installing it for you...' -ForegroundColor 'Cyan'
9-
$Parameters = @{
10-
Uri = 'https://raw.githubusercontent.com/spicetify/cli/main/install.ps1'
11-
UseBasicParsing = $true
12-
}
13-
Invoke-WebRequest @Parameters | Invoke-Expression
48+
Write-Host -Object 'Spicetify not found.' -ForegroundColor 'Yellow'
49+
Write-Host -Object 'Installing it for you...' -ForegroundColor 'Cyan'
50+
$Parameters = @{
51+
Uri = 'https://raw.githubusercontent.com/spicetify/cli/main/install.ps1'
52+
UseBasicParsing = $true
53+
}
54+
Invoke-WebRequest @Parameters | Invoke-Expression
1455
}
1556

1657
try {
17-
$spicetifyOutput = spicetify path userdata 2>&1 | Out-String
18-
if ($LASTEXITCODE -ne 0) {
58+
$result = Invoke-SpicetifyWithOutput "path" "userdata"
59+
if ($result.ExitCode -ne 0) {
1960
Write-Host -Object "Error from Spicetify:" -ForegroundColor 'Red'
20-
Write-Host -Object $spicetifyOutput -ForegroundColor 'Red'
21-
exit 1
61+
Write-Host -Object $result.Output -ForegroundColor 'Red'
62+
return
2263
}
23-
$spiceUserDataPath = $spicetifyOutput.Trim()
64+
$spiceUserDataPath = $result.Output
2465
} catch {
2566
Write-Host -Object "Error running Spicetify:" -ForegroundColor 'Red'
26-
Write-Host -Object $_.Exception.Message -ForegroundColor 'Red'
27-
exit 1
67+
Write-Host -Object $_.Exception.Message.Trim() -ForegroundColor 'Red'
68+
return
2869
}
2970

3071
if (-not (Test-Path -Path $spiceUserDataPath -PathType 'Container' -ErrorAction 'SilentlyContinue')) {
3172
$spiceUserDataPath = "$env:APPDATA\spicetify"
3273
}
3374
$marketAppPath = "$spiceUserDataPath\CustomApps\marketplace"
3475
$marketThemePath = "$spiceUserDataPath\Themes\marketplace"
76+
3577
$isThemeInstalled = $(
36-
spicetify path -s | Out-Null
37-
-not $LASTEXITCODE
78+
Invoke-Spicetify "path" "-s" | Out-Null
79+
-not $LASTEXITCODE
3880
)
39-
$currentTheme = (spicetify config current_theme)
81+
$currentTheme = (Invoke-SpicetifyWithOutput "config" "current_theme").Output
4082
$setTheme = $true
4183

4284
Write-Host -Object 'Removing and creating Marketplace folders...' -ForegroundColor 'Cyan'
4385
try {
44-
$spicetifyOutput = spicetify path userdata 2>&1 | Out-String
45-
if ($LASTEXITCODE -ne 0) {
86+
$result = Invoke-SpicetifyWithOutput "path" "userdata"
87+
if ($result.ExitCode -ne 0) {
4688
Write-Host -Object "Error: Failed to get Spicetify path. Details:" -ForegroundColor 'Red'
47-
Write-Host -Object $spicetifyOutput -ForegroundColor 'Red'
48-
exit 1
89+
Write-Host -Object $result.Output -ForegroundColor 'Red'
90+
return
4991
}
5092

5193
Remove-Item -Path $marketAppPath, $marketThemePath -Recurse -Force -ErrorAction 'SilentlyContinue' | Out-Null
5294
if (-not (New-Item -Path $marketAppPath, $marketThemePath -ItemType 'Directory' -Force -ErrorAction 'Stop')) {
5395
Write-Host -Object "Error: Failed to create Marketplace directories." -ForegroundColor 'Red'
54-
exit 1
96+
return
5597
}
5698
} catch {
57-
Write-Host -Object "Error: $($_.Exception.Message)" -ForegroundColor 'Red'
58-
exit 1
99+
Write-Host -Object "Error: $($_.Exception.Message.Trim())" -ForegroundColor 'Red'
100+
return
59101
}
60102

61103
Write-Host -Object 'Downloading Marketplace...' -ForegroundColor 'Cyan'
@@ -72,9 +114,9 @@ Write-Host -Object 'Unzipping and installing...' -ForegroundColor 'Cyan'
72114
Expand-Archive -Path $marketArchivePath -DestinationPath $marketAppPath -Force
73115
Move-Item -Path "$unpackedFolderPath\*" -Destination $marketAppPath -Force
74116
Remove-Item -Path $marketArchivePath, $unpackedFolderPath -Force
75-
spicetify config custom_apps spicetify-marketplace- -q
76-
spicetify config custom_apps marketplace
77-
spicetify config inject_css 1 replace_colors 1
117+
Invoke-Spicetify "config" "custom_apps" "spicetify-marketplace-" "-q"
118+
Invoke-Spicetify "config" "custom_apps" "marketplace"
119+
Invoke-Spicetify "config" "inject_css" "1" "replace_colors" "1"
78120

79121
Write-Host -Object 'Downloading placeholder theme...' -ForegroundColor 'Cyan'
80122
$Parameters = @{
@@ -86,18 +128,20 @@ Invoke-WebRequest @Parameters
86128

87129
Write-Host -Object 'Applying...' -ForegroundColor 'Cyan'
88130
if ($isThemeInstalled -and ($currentTheme -ne 'marketplace')) {
89-
$Host.UI.RawUI.Flushinputbuffer()
90-
$choice = $Host.UI.PromptForChoice(
91-
'Local theme found',
92-
'Do you want to replace it with a placeholder to install themes from the Marketplace?',
93-
('&Yes', '&No'),
94-
0
95-
)
96-
if ($choice = 1) { $setTheme = $false }
131+
$Host.UI.RawUI.Flushinputbuffer()
132+
$choice = $Host.UI.PromptForChoice(
133+
'Local theme found',
134+
'Do you want to replace it with a placeholder to install themes from the Marketplace?',
135+
('&Yes', '&No'),
136+
0
137+
)
138+
if ($choice = 1) { $setTheme = $false }
139+
}
140+
if ($setTheme) {
141+
Invoke-Spicetify "config" "current_theme" "marketplace"
97142
}
98-
if ($setTheme) { spicetify config current_theme marketplace }
99-
spicetify backup
100-
spicetify apply
143+
Invoke-Spicetify "backup"
144+
Invoke-Spicetify "apply"
101145

102146
Write-Host -Object 'Done!' -ForegroundColor 'Green'
103-
Write-Host -Object 'If nothing has happened, check the messages above for errors'
147+
Write-Host -Object 'If nothing has happened, check the messages above for errors'

0 commit comments

Comments
 (0)