Created
February 9, 2022 02:08
-
-
Save hsupu/65eaf0097f52e201c7835681ca3ce426 to your computer and use it in GitHub Desktop.
Zip nodejs (pnpm) project for AWS Lambda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env pwsh | |
param() | |
$ErrorActionPreference = 'Stop' | |
# base on project root | |
$includes = @( | |
'index.js', | |
'package.json' | |
) | |
# packages will be excluded | |
$exclude_pkgs = @( | |
'aws-sdk' | |
) | |
# TODO dirs will be stripped | |
$strip = @( | |
'node_modules/axios/dist/' | |
} | |
pushd $PSScriptRoot | |
try { | |
& pnpm install | |
$deps = [System.Collections.Generic.HashSet[string]]::new() | |
$src0 = $(& pnpm list --prod --parseable) -split "`n" | |
# echo $src0.Length | |
foreach ($src0e in $src0) { | |
$relative = $src0e.Substring($PSScriptRoot.Length) | |
if ($relative -match '\.pnpm/(?:[^+]*\+)?([^@]+)@') { | |
$pkg = $Matches.1 | |
if ($pkg -in $exclude_pkgs) { | |
echo "skip $pkg ($relative)" | |
continue | |
} | |
} | |
if (Test-Path -Path "$src0e/package.json") { | |
if ($relative.Length -ne 0) { | |
$realpath = $(& realpath -L $item.FullName) | |
# echo $realpath | |
$deps.Add($realpath) | Out-Null | |
} | |
continue | |
} | |
$items = Get-ChildItem -Path "$src0e/node_modules" | |
foreach ($item in $items) { | |
if ($item.Name.StartsWith('.')) { | |
continue | |
} | |
if (-not (Test-Path -Path $item.FullName -PathType Container)) { | |
continue | |
} | |
$realpath = $(& realpath -L $item.FullName) | |
# echo $realpath | |
$deps.Add($realpath) | Out-Null | |
} | |
} | |
$zipFilePath = "$PSScriptRoot/bundle.zip" | |
rm $zipFilePath | |
# It's auto-imported by PowerShell Core now | |
# Add-Type -Assembly 'System.IO.Compression.FileSystem' | |
$zip = [System.IO.Compression.ZipFile]::Open($zipFilePath, [System.IO.Compression.ZipArchiveMode]::Update) | |
try { | |
echo "zip `$include" | |
# Sad, dotnet only providers "CreateEntryFromFile" | |
function Update-Zip() { | |
param( | |
[System.IO.Compression.ZipArchive]$zip, | |
[string]$src, | |
[string]$prefix | |
) | |
# $https://docs.microsoft.com/en-us/dotnet/api/system.io.compression.zipfileextensions.createentryfromfile?view=net-6.0 | |
$zipLevel = [System.IO.Compression.CompressionLevel]::Optimal | |
$root = Get-Item -Path $src | |
if ($root.Attributes.HasFlag([System.IO.FileAttributes]::Directory)) { | |
$files = Get-ChildItem -Path $root -Recurse -File | |
foreach ($file in $files) { | |
$rel = "$prefix$($file.FullName.Substring($root.FullName.Length + 1))" | |
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $file.FullName, $rel, $zipLevel) | Out-Null | |
} | |
} | |
else { | |
$rel = "$prefix$($root.Name)" | |
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $root.FullName, $rel, $zipLevel) | Out-Null | |
} | |
} | |
foreach ($e in $includes) { | |
Update-Zip -zip $zip -src $e -prefix '' | |
} | |
foreach ($e in $deps) { | |
$ppdir = Get-Item -Path $(Join-Path $e "../..") | |
$relative = $e.Substring($ppdir.FullName.Length + 1) | |
echo "zip $relative" | |
Update-Zip -zip $zip -src $e -prefix "$relative/" | |
} | |
} | |
finally { | |
$zip.Dispose() | |
} | |
} | |
finally { | |
popd | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment