Public/Remove-GhostLocalSite.ps1
|
function Remove-GhostLocalSite { <# .SYNOPSIS Stops a Ghost site that runs on this machine and deletes it. .DESCRIPTION Deletes the folder of a site that Start-GhostLocalSite installed, with its database and everything that was uploaded to it. That cannot be undone, which is why this function asks for confirmation; use -Confirm:$false to remove a site from a script. A theme that is linked into the site is unlinked first, so the theme itself is not touched. A theme that was cloned for the site with -ThemeRepository lives in the folder of the site and goes with it. Nothing is returned. A folder without a Ghost site in it is reported and left alone. .EXAMPLE Remove-GhostLocalSite Removes the site in the folder .local-ghost of the current location, after confirmation. .EXAMPLE Remove-GhostLocalSite -Path ../my-site -Confirm:$false Removes the site in the folder my-site without asking. .LINK https://docs.ghost.org/install/local #> [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')] [OutputType([void])] param( # Folder of the site. Defaults to .local-ghost in the current location. [Parameter(Position = 0)] [string] $Path ) try { if (-not $PSBoundParameters.ContainsKey('InformationAction')) { $InformationPreference = 'Continue' } $Path = Resolve-GhostLocalSitePath -Path $Path if (-not (Test-Path -LiteralPath (Join-Path -Path $Path -ChildPath '.ghost-cli'))) { Write-Information -MessageData "There is no Ghost site in $Path." return } if ($PSCmdlet.ShouldProcess($Path, 'Delete the local Ghost site and its content')) { Invoke-GhostCli -Arguments 'stop', '--dir', $Path # A linked theme is unlinked first, so that deleting the folder cannot reach into the theme itself. $themes = Join-Path -Path $Path -ChildPath 'content/themes' if (Test-Path -LiteralPath $themes) { foreach ($theme in Get-ChildItem -LiteralPath $themes -Force) { if ($theme.Attributes -band [System.IO.FileAttributes]::ReparsePoint) { $theme.Delete() } } } Invoke-GhostCli -Arguments 'uninstall', '--force', '--dir', $Path if (Test-Path -LiteralPath $Path) { Remove-Item -LiteralPath $Path -Recurse -Force } Write-Information -MessageData "Removed the Ghost site in $Path." } } catch { $PSCmdlet.ThrowTerminatingError($_) } } |