internal/functions/Edit-CmdFavExternalEditor.ps1

function Edit-CmdFavExternalEditor {
    <#
    .SYNOPSIS
    Editiert einen CmdFav-Eintrag im externen Editor über eine temporäre .ps1-Datei.
 
    .PARAMETER Name
    Name des zu bearbeitenden CmdFav-Eintrags.
 
    .OUTPUTS
    None
 
    .EXAMPLE
    Edit-CmdFavExternalEditor -Name "MeinFavorit"
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [string]$Name
    )

    $editorConfig = Get-CmdFavEditor

    if (-not $editorConfig.path) {
        Stop-PSFFunction -Level Warning -Message "No external editor configured. Please configure it using Set-CmdFavEditor."
        return
    }

    # Temporäre Datei mit .ps1-Endung erzeugen
    $tmp = Join-Path ([System.IO.Path]::GetTempPath()) "$([System.IO.Path]::GetRandomFileName()).ps1"

    # Exportieren
    Export-CmdFav -Name $Name -Path $tmp -Format PSM

    Write-PSFMessage -Level Host -Message "Opening external editor '$($editorConfig.path)' with file '$tmp'"

    $arg = $editorConfig.Parameter -replace '\[PATH\]', $tmp

    if ($editorConfig.waitMode -eq 'Process') {
        Start-Process -FilePath $editorConfig.path -ArgumentList $arg -Wait
    } elseif ($editorConfig.waitMode -eq 'ReadKey') {
        & $editorConfig.path $arg
        Write-Host "Press any key after closing the editor..."
        [void][System.Console]::ReadKey($true)
    }

    # Nach Bearbeitung importieren (nur diesen Eintrag, Modus OVERWRITE)
    Import-CmdFav -Path $tmp -Format PSM -ImportMode OVERWRITE -Name $Name

    Remove-Item $tmp -ErrorAction SilentlyContinue
}