Skip to content

Commit e8dbda0

Browse files
committed
fixes commands initialization
1 parent 1f27a5d commit e8dbda0

3 files changed

Lines changed: 53 additions & 43 deletions

File tree

private/Get-Commands.ps1

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
11
. $PSScriptRoot\..\classes\Command.ps1
22

3-
$script:CachedCommands = $null
4-
53
function Get-Commands {
6-
if ($null -eq $script:CachedCommands) {
7-
$script:CachedCommands = @(
8-
[Command]::new("cd", "Set-Alias-Location")
9-
[Command]::new("rider", "Open-Command rider" )
10-
[Command]::new("vs", "Open-Command visualstudio" )
11-
[Command]::new("visualstudio", "Open-Command visualstudio" )
12-
[Command]::new("intellij", "Open-Command intellij")
13-
[Command]::new("code", "Open-Command code")
14-
[Command]::new("ws", "Open-Command webstorm")
15-
[Command]::new("webstorm", "Open-Command webstorm")
16-
[Command]::new("explorer", "Open-Command explorer")
17-
[Command]::new("sourcefolder", "Set-Source-Folder")
18-
[Command]::new("help", 'Write-Host $(Get-DynamicHelp $commandNames)' )
19-
[Command]::new("alias", @(
20-
[Command]::new("add", "Add-Alias" ),
21-
[Command]::new("remove", "Remove-Alias" )
22-
[Command]::new("list", { Write-Host ($script:ALIASES | Format-Table | Out-String) })
23-
)
4+
if ($null -ne $script:COMMANDS) {
5+
return $script:COMMANDS
6+
}
7+
$script:COMMANDS = @(
8+
[Command]::new("cd", "Set-Alias-Location")
9+
[Command]::new("rider", "Open-Command rider" )
10+
[Command]::new("vs", "Open-Command visualstudio" )
11+
[Command]::new("visualstudio", "Open-Command visualstudio" )
12+
[Command]::new("intellij", "Open-Command intellij")
13+
[Command]::new("code", "Open-Command code")
14+
[Command]::new("ws", "Open-Command webstorm")
15+
[Command]::new("webstorm", "Open-Command webstorm")
16+
[Command]::new("explorer", "Open-Command explorer")
17+
[Command]::new("sourcefolder", "Set-Source-Folder")
18+
[Command]::new("help", 'Write-Host $(Get-DynamicHelp $commandNames)' )
19+
[Command]::new("alias", @(
20+
[Command]::new("add", "Add-Alias" ),
21+
[Command]::new("remove", "Remove-Alias" )
22+
[Command]::new("list", { Write-Host ($script:ALIASES | Format-Table | Out-String) })
2423
)
25-
[Command]::new("todo", @(
26-
[Command]::new("add", "Add-Todo")
27-
[Command]::new("remove", { Write-Host "TODO: remove item from todolist: qp todo remove x" })
28-
[Command]::new("list", { Write-Host "TODO: Output todo list" })
29-
)
24+
)
25+
[Command]::new("todo", @(
26+
[Command]::new("add", "Add-Todo")
27+
[Command]::new("remove", { Write-Host "TODO: remove item from todolist: qp todo remove x" })
28+
[Command]::new("list", { Write-Host "TODO: Output todo list" })
3029
)
31-
[Command]::new("version", { Write-Host (Get-MyModuleVersion) } )
32-
[Command]::new("update", { Update-QuickPath -FromGallery })
3330
)
34-
}
35-
return $script:CachedCommands
31+
[Command]::new("version", { Write-Host (Get-MyModuleVersion) } )
32+
[Command]::new("update", { Update-QuickPath -FromGallery })
33+
)
34+
return $script:COMMANDS
3635
}

private/Initialize-QuickPath.ps1

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
. $PSScriptRoot\..\private\Get-Commands.ps1
33

44
function Initialize-QuickPath {
5+
[CmdletBinding()]
6+
param()
7+
Write-Verbose "Initializing QuickPath..."
58
$aliasFilePath = Get-AliasFilePath
69
$aliasDirectory = Split-Path $aliasFilePath -Parent
710

8-
if (Test-Path $aliasFilePath) {
9-
return
10-
}
11-
11+
# Ensure directory and file exist, but do not return early; we still need to load aliases and commands
1212
if (-not (Test-Path $aliasDirectory)) {
1313
New-Item -Path $aliasDirectory -ItemType Directory -Force | Out-Null
1414
}
@@ -20,4 +20,7 @@ function Initialize-QuickPath {
2020
$script:JSON_FILE_PATH = $aliasFilePath
2121
$script:ALIASES = Import-Aliases $aliasFilePath
2222
$script:COMMANDS = Get-Commands
23+
24+
Write-Verbose "QuickPath initialized. JSON file: $aliasFilePath"
25+
Write-Verbose "Loaded $($script:ALIASES.Count) aliases and $($script:COMMANDS.Count) commands"
2326
}

quickpath.psm1

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,43 @@ $commandText
4343
}
4444

4545
function qp {
46+
[CmdletBinding()]
47+
param(
48+
[Parameter(Position = 0)]
49+
[string]$Command,
50+
51+
[Parameter(Position = 1, ValueFromRemainingArguments = $true)]
52+
[string[]]$Arguments
53+
)
54+
4655
try {
4756
Initialize-QuickPath
4857

49-
$firstArgument = $args[0]
50-
$remainingArguments = $args[1..($args.length - 1)]
58+
Write-Verbose "qp: commands available: $($script:COMMANDS.Count)"
5159

52-
$script:COMMANDS | ForEach-Object { $_.Name } | Sort-Object
60+
$commandNames = ($script:COMMANDS | ForEach-Object Name | Sort-Object)
5361
$helpText = Get-DynamicHelp $commandNames
5462

55-
$alias = Get-Alias $firstArgument
56-
$path = $alias.WindowsPath ?? $firstArgument
63+
$alias = Get-Alias @($Command)
64+
$path = if ($alias) { $alias.WindowsPath } else { $Command }
5765

5866
if (Test-Path -Path $path) {
5967
Set-Location $path
6068
return
6169
}
6270

63-
$command = $script:COMMANDS | Where-Object { $_.Name -eq $firstArgument }
64-
if ($null -eq $command) {
71+
$commandObj = $script:COMMANDS | Where-Object { $_.Name -eq $Command }
72+
if ($null -eq $commandObj) {
6573
Write-Host $helpText
6674
return
6775
}
6876

69-
if ($remainingArguments.length -eq 0) {
70-
$command.InvokeFunction()
77+
if (-not $Arguments -or $Arguments.length -eq 0) {
78+
$commandObj.InvokeFunction()
7179
return;
7280
}
7381

74-
$command.InvokeFunction($remainingArguments)
82+
$commandObj.InvokeFunction($Arguments)
7583
}
7684
catch {
7785
Write-Error "Error: $($_.Exception.Message)"

0 commit comments

Comments
 (0)