Last active
March 31, 2020 12:54
-
-
Save giehlman/0f58f32d6d977551641a4119a45b5624 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env sh | |
#title : bb-pipelines-builder.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 : 20200324 | |
#version : 1.5 | |
#usage : sh ./bb-pipelines-builder.sh <project-name> <target-zip-name> | |
#============================================================================== | |
PROJECT_NAME=${1:-$(basename "$PWD")} | |
TARGET_NAME=${2} | |
#WITH_MODULES=true # TODO: improve selection; via args, etc. | |
# 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" | |
echo " <with-modules> optional; if set, the node_modules folder is added to the artifact" | |
} | |
# | |
# Zips the project and adds node_modules to the artifact. Uses npm pack. | |
# Should be used for Lambda-deployments | |
# | |
pack_withModules() { | |
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!" | |
} | |
# | |
# Zips the project using npm-pack-zip. Note: you need to add a "pack": "npm-pack-zip" command to your package.json. | |
# Should be used for Elastic-Beanstalk-deployments | |
# | |
pack_withoutModules() { | |
PROJECT_NAME=${1} | |
rm *.zip | |
rm *.tgz | |
# rm -rf node_modules | |
rm -rf package | |
# yarn install --production | |
npm run pack # creates zip | |
PACKAGE_VERSION=$(cat package.json \ | |
| grep version \ | |
| head -1 \ | |
| awk -F: '{ print $2 }' \ | |
| sed 's/[",]//g' \ | |
| tr -d '[[:space:]]') | |
echo "Extracted version: ${PACKAGE_VERSION}" | |
ARTIFACT=$(find ./ -iname "${PROJECT_NAME}*.zip" -print) | |
echo "---> ${ARTIFACT}" | |
W_VERSION=$(basename ${ARTIFACT} .zip)-${PACKAGE_VERSION}.zip | |
mv ${ARTIFACT} ${W_VERSION} | |
} | |
echo "### Packaging..." | |
rm -rf artifacts | |
# set +x | |
if [ ! -z "${WITH_MODULES}" ] ; then | |
pack_withModules ${PROJECT_NAME} | |
else | |
pack_withoutModules ${PROJECT_NAME} | |
fi | |
# 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/$(basename ${ARTIFACT}) | |
cd ./artifacts | |
if [ ! -z "${TARGET_NAME}" ] ; then | |
set -x | |
mv $(basename ${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