Private/ConvertTo-GhostThemeArchive.ps1
|
function ConvertTo-GhostThemeArchive { <# .SYNOPSIS Packs the folder of a theme into a zip archive, the form Ghost takes a theme in. .DESCRIPTION Returns the bytes of an archive with the files of the theme, package.json at its root. The archive is built here instead of with Compress-Archive, which writes backslashes in the paths on Windows PowerShell. The files are added in a fixed order, with forward slashes in their paths and with the time they were last changed, so that a folder that did not change gives the same archive again. What a theme does not need at run time is left out: the folders node_modules and dist, every file and folder whose name starts with a dot, such as .git, and zip files, such as an earlier package of the theme. .EXAMPLE ConvertTo-GhostThemeArchive -Path ./my-theme Returns the bytes of a zip archive of the theme in the folder my-theme. #> [CmdletBinding()] [OutputType([byte[]])] param( # Folder of the theme. It has a package.json. [Parameter(Mandatory)] [string] $Path ) $root = (Resolve-Path -LiteralPath $Path).ProviderPath.TrimEnd('\', '/') if (-not (Test-Path -LiteralPath (Join-Path -Path $root -ChildPath 'package.json') -PathType Leaf)) { throw "$root has no package.json, so it is not the folder of a Ghost theme." } # Folder by folder, so that a folder that is left out, such as node_modules, is not read at all. # The path of a file within the theme is built up from the names on the way down. Cutting the folder off the # full path would go wrong where Windows writes the same folder in two ways, with a short name and a long one. $files = [System.Collections.Generic.List[string]]::new() $folders = [System.Collections.Generic.Stack[object]]::new() $folders.Push(@{ Path = $root; Name = '' }) while ($folders.Count -gt 0) { $folder = $folders.Pop() foreach ($item in Get-ChildItem -LiteralPath $folder.Path -Force) { if ($item.Name.StartsWith('.')) { continue } if ($item.PSIsContainer) { if (@('node_modules', 'dist') -notcontains $item.Name) { $below = $folder.Name + $item.Name + '/' $folders.Push(@{ Path = $item.FullName; Name = $below }) } } elseif ($item.Extension -ne '.zip') { $files.Add($folder.Name + $item.Name) } } } $names = $files.ToArray() [System.Array]::Sort($names, [System.StringComparer]::Ordinal) # Windows PowerShell does not load these by itself. Add-Type -AssemblyName 'System.IO.Compression' $stream = [System.IO.MemoryStream]::new() try { $mode = [System.IO.Compression.ZipArchiveMode]::Create $archive = [System.IO.Compression.ZipArchive]::new($stream, $mode, $true) try { foreach ($name in $names) { $file = Join-Path -Path $root -ChildPath $name $entry = $archive.CreateEntry($name, [System.IO.Compression.CompressionLevel]::Optimal) # The time of the file instead of the time of packing: a folder that did not change gives the # same archive again. $changed = [System.IO.File]::GetLastWriteTime($file) if ($changed.Year -lt 1980 -or $changed.Year -gt 2107) { # A zip archive cannot hold a date outside these years, and some tools write files without # a real date. $changed = [datetime]::new(1980, 1, 1) } $entry.LastWriteTime = $changed $content = [System.IO.File]::ReadAllBytes($file) $target = $entry.Open() try { $target.Write($content, 0, $content.Length) } finally { $target.Dispose() } } } finally { # Closing the archive writes its directory to the stream. $archive.Dispose() } # As one object: without this the bytes would leave the function one by one. Write-Output -InputObject $stream.ToArray() -NoEnumerate } finally { $stream.Dispose() } } |