Private/Invoke-GhostCli.ps1

function Invoke-GhostCli {
    <#
    .SYNOPSIS
        Runs a Ghost-CLI command and fails when the command fails.

    .DESCRIPTION
        Ghost-CLI is run through npx, which downloads the newest version once and keeps it in the cache of npm.
        Nothing is installed for the whole machine, and a Ghost-CLI that is installed there is not used: a new
        Ghost needs a new Ghost-CLI.

        Ghost-CLI installs Ghost with pnpm and needs a recent one, so npx brings the newest pnpm along. It comes
        first on the path of the command, before a pnpm of the machine that may be too old.

        What the command writes is passed on as information, line by line.

    .EXAMPLE
        Invoke-GhostCli -Arguments 'start', '--dir', $Path

        Starts the Ghost site in that folder.
    #>

    [CmdletBinding()]
    [OutputType([void])]
    param(
        # The command and its options, such as 'start', '--dir', $Path.
        [Parameter(Mandatory)]
        [string[]] $Arguments
    )

    # On Windows the .cmd is named: the .ps1 next to it needs an execution policy that allows scripts.
    $npx = if ($env:OS -eq 'Windows_NT') { 'npx.cmd' } else { 'npx' }

    # Ghost-CLI reports its progress on the error stream, which is not a failure.
    $ErrorActionPreference = 'Continue'
    & $npx --yes --package ghost-cli@latest --package pnpm@latest ghost @Arguments --no-prompt --no-color 2>&1 |
        ForEach-Object { Write-Information -MessageData " $_" }

    if ($LASTEXITCODE -ne 0) {
        throw "Ghost-CLI failed with exit code ${LASTEXITCODE}: ghost $($Arguments -join ' ')"
    }
}