functions/Export-CmdFav.ps1


function Export-CmdFav {
    <#
    .SYNOPSIS
        Exports favorite commands to a JSON file.
 
    .DESCRIPTION
        The Export-CmdFav function exports the cache of favorite commands to a JSON file specified by the Path parameter.
        If no favorite commands are stored, a warning message is displayed, and the function returns.
 
    .PARAMETER Path
        Specifies the path to the JSON file where favorite commands will be exported. This parameter is mandatory.
 
    .EXAMPLE
        Export-CmdFav -Path "C:\Path\To\Favorites.json"
 
        Exports the cache of favorite commands to the specified JSON file.
 
    #>


    [CmdletBinding()]
    param (
        [parameter(mandatory = $true, Position = 1)]
        [String]$Path,
        [PSFramework.TabExpansion.PsfArgumentCompleterAttribute("CmdFav.Names")]
        [string[]]$Name,
        [ValidateSet('AUTO', 'JSON', 'XML', 'PSM')]
        [string]$Format = 'AUTO'
    )

    # Retrieving the favorite commands cache.
    $cmdCache = Get-CmdFavCache
    if ($Name) {
        $cmdCache = $cmdCache | Where-Object { $Name -contains $_.Name }
    }

    # Handling the case where no favorite commands are stored.
    if (-not $cmdCache) {
        Stop-PSFFunction -Level Warning -Message "No favorite commands stored"
        return
    }

    $actualFormat = $Format
    if ($Format -eq 'AUTO') {
        switch -Regex ($Path) {
            '\.json$' { $actualFormat = 'JSON' }
            '\.xml$' { $actualFormat = 'XML' }
            '\.psm1$' { $actualFormat = 'PSM' }
            default {
                Stop-PSFFunction -Level Error -Message "Could not determine file format by file extension in: $Path. Use -Format [JSON|XML|PSM] instead AUTO."
                return
            }
        }
    }

    switch ($actualFormat) {
        'JSON' {
            $cmdCache | ConvertTo-Json | Out-File $Path
        }
        'XML' {
            $cmdCache | Export-PSFClixml -Depth 5 -Encoding UTF8 -Path $Path -Style String
        }
        'PSM' {
            $psmContent = [System.Text.StringBuilder]::new()
            foreach ($fav in $cmdCache) {

                $funcName = $fav.Name -replace '[^.a-zA-Z0-9_]', '_'
                $desc = $fav.Description -replace '"', '`"'

                $repo = $fav.Repository
                $command = $fav.CommandLine -replace '^', ' ' #-replace '"', '`"'
                $intro = @"
function $funcName {
    <#
    .SYNOPSIS
        $($fav.Name)
    .DESCRIPTION
        $desc
    .NOTES
        This function is auto-generated from CmdFav repository. You can modify the following Parameters:
        Tags: $($fav.Tag -join ', ')
        Repository: $repo
 
        Tags are separated by commas, and the repository is the name of the CmdFav repository where this command is stored.
        For changing the Name/Description/Commandline: Modify the relevant parts of the pseudo-function
    #>
 
$command
}
"@

                [void]$psmContent.AppendLine($intro)
            }
            $psmContent = $psmContent.ToString()  | Format-CmdFavString
            Set-Content -Path $Path -Value $psmContent -Encoding UTF8
        }
    }
}

# Define an alias for Export-CmdFav
Set-Alias -Name "Export-Favorite" -Value "Export-CmdFav"