Skip to content

Instantly share code, notes, and snippets.

@robertsLando
Last active August 26, 2018 10:40
Show Gist options
  • Save robertsLando/236ec81326a7408f52b7e8e61ae03604 to your computer and use it in GitHub Desktop.
Save robertsLando/236ec81326a7408f52b7e8e61ae03604 to your computer and use it in GitHub Desktop.
sh script to create a nodejs application executable using pkg https://www.npmjs.com/package/pkg and automatically including .node files and zip with all application files
#!/bin/bash
ask() {
# http://djm.me/ask
while true; do
if [ "${2:-}" = "Y" ]; then
prompt="Y/n"
default=Y
elif [ "${2:-}" = "N" ]; then
prompt="y/N"
default=N
else
prompt="y/n"
default=
fi
# Ask the question
read -p "$1 [$prompt] " REPLY
# Default?
if [ -z "$REPLY" ]; then
REPLY=$default
fi
# Check if the reply is valid
case "$REPLY" in
Y*|y*) return 0 ;;
N*|n*) return 1 ;;
esac
done
}
read -p "Application name: " APP
read -p "Version: " VERSION
read -p "Folder: " PKG_FOLDER
# Check pkg folder exist
if [ ! -d "$PKG_FOLDER" ]; then
echo "## $PKG_FOLDER must exist, create the folder before run this script"
exit 1
fi
echo "## Clear $PKG_FOLDER folder"
rm -rf $PKG_FOLDER/*
if ask "Build application ?"; then
echo "## Building application"
npm run build
fi
echo "## Creating application package in $PKG_FOLDER folder"
sudo pkg package.json -t node8-linux-x64 --out-path $PKG_FOLDER
echo "## Check for .node files to include in executable folder"
declare TO_INCLUDE=($(find ./node_modules/ -type f -name "*.node"))
TOTAL_INCLUDE=${#TO_INCLUDE[@]}
echo "## Found $TOTAL_INCLUDE files to include"
i=0
while [ "$i" -lt "$TOTAL_INCLUDE" ]
do
IFS='/' path=(${TO_INCLUDE[$i]})
file=${path[-1]}
echo "## Copying $file to $PKG_FOLDER folder"
cp "${TO_INCLUDE[$i]}" "./$PKG_FOLDER"
let "i = $i + 1"
done
cd $PKG_FOLDER
echo "## Create zip file $APP-v$VERSION"
zip $APP-v$VERSION *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment