Last active
July 4, 2021 13:17
-
-
Save christianscott/5445ce1d118f88199e7962e7d3330224 to your computer and use it in GitHub Desktop.
Executable zip file
This file contains 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
set -e | |
rm -rf out | |
mkdir out | |
cat >"index.js" <<EOF | |
console.log("hello from nodejs!"); | |
EOF | |
zip -q out/archive.zip index.js | |
rm index.js | |
# escape all the $s so they're not eval'd till later | |
cat >"header.sh" <<EOF | |
#!/bin/bash | |
tmp=\$(mktemp --directory) | |
unzip -qo \$0 -d \$tmp | |
node "\$tmp/index.js" | |
exit | |
EOF | |
# This creates a zip file that looks like this; | |
# <header> | |
# <archive> | |
cat header.sh out/archive.zip > out/archive.zip.bak | |
mv out/archive.zip.bak out/archive.zip | |
# the "A" option adjusts the offset, telling zip where the actual | |
# archive starts | |
zip -qA out/archive.zip | |
chmod +x out/archive.zip | |
# $ bash executable_zip_file_demo.sh | |
# $ ./out/archive.zip | |
# hello from nodejs! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment