Skip to content

Instantly share code, notes, and snippets.

@giehlman
Last active March 22, 2020 20:30
Show Gist options
  • Save giehlman/23c8a45b5b8e16a9aa984b78d4c39852 to your computer and use it in GitHub Desktop.
Save giehlman/23c8a45b5b8e16a9aa984b78d4c39852 to your computer and use it in GitHub Desktop.
#!/usr/bin/env sh
#title : bb-pipelines-builder-nodejs.sh
#description : For npm/yarn projects! Convenience script to build, check and pack fat artifacts. For personal and experimental use only!
#author : Christian-André Giehl <[email protected]>
#date : 20200322
#version : 1.4
#usage : sh ./bb-pipelines-builder-nodejs.sh <project-name> <target-zip-name>
#==============================================================================
PROJECT_NAME=${1:-$(basename "$PWD")}
TARGET_NAME=${2}
# Exits in case the supplied state is != 0. State is typically supplied via $?
exitOnError() {
state=$1
msg=$2
if [ $state -ne 0 ]; then
echo "!!! ${msg}"
echo "### Exiting."
exit $state
fi
}
printUsage() {
echo " Usage: sh ./bb-pipelines-builder.sh <project-name> <target-zip-name>"
echo " with:"
echo " <project-name> project name, important for naming the artifact"
echo " <target-zip-name> optional; if set, the artifact gets renamed to this value"
}
pack() {
PROJECT_NAME=${1}
rm *.zip
rm *.tgz
rm -rf node_modules
rm -rf package
yarn install --production
npm pack
FILE=$(find ./ -type f -iname "${PROJECT_NAME}*.tgz" -print|head -n 1)
echo "File found is '${FILE}'"
tar -xf ${FILE}
cd package && zip -r ../${FILE}.zip ./ && cd ..
rm *.tgz
rm -rf package
exitOnError $? "Unable to zip!"
FILE=$(find ./ -type f -iname "${PROJECT_NAME}*.zip" -print|head -n 1)
echo "File found is '${FILE}'"
echo "##### Adding node_modules..."
zip -ur $FILE node_modules
exitOnError $? "Unable to add node_modules to zip!"
}
echo "### Packaging..."
# set +x
pack ${PROJECT_NAME}
# set -x
exitOnError $? "Unable to package!"
ARTIFACT=$(find ./ -iname "${PROJECT_NAME}*.zip" -print)
echo "Built artifact in '${ARTIFACT}'"
echo "### Moving artifact to subfolder..."
mkdir -p artifacts
mv ${ARTIFACT} ./artifacts/${ARTIFACT}
cd ./artifacts
if [ ! -z "${TARGET_NAME}" ] ; then
set -x
mv ${ARTIFACT} ${TARGET_NAME}
set +x
fi
ls -lat
exit $?
# Optional: Check size
minsize=1024000 # 1MB
case $OSTYPE in
darwin*) filesize=$(stat -f%z ${TARGET_NAME}) ;;
*) filesize=$(stat -c%s ${TARGET_NAME}) ;;
esac
exitOnError $? "Unable to determine package size!"
echo "Size of ${TARGET_NAME} = ${filesize} bytes."
if [ ${filesize} -lt ${minsize} ]; then
exitOnError 1 "### Filesize too low! Please check!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment