Skip to content

Commit

Permalink
Rough out a build script to pack build metadata into the package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
kj4ezj committed Aug 1, 2024
1 parent a6a2ce0 commit c5d491c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
run: yarn test

- name: Package - node v${{ matrix.node-version }}
run: npm pack
run: yarn build

- name: Validation - node v${{ matrix.node-version }}
run: npm install kj4ezj-is-*.tgz
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"license": "MIT",
"scripts": {
"build": "scripts/build.sh",
"lint": "eslint .",
"reset": "scripts/reset.sh",
"test": "jest --coverage"
Expand Down
74 changes: 30 additions & 44 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,38 @@ pushd "$NPM_ROOT"
ee node --version
ee yarn --version
ee npm --version
NODE_MAJOR_VERSION="$(node --version | tr -d 'v' | cut -d '.' -f '1')"
# package info
PACKAGE_NAME="$(cat package.json | jq -r '.name')"
PACKAGE_VERSION="$(cat package.json | jq -r '.version')"
echo "Found package.json for \"$PACKAGE_NAME\" version \"$PACKAGE_VERSION\"."
# git info
export GIT_BRANCH="$(git branch --show-current)"
if [[ -z "$GIT_BRANCH" ]]; then
export GIT_BRANCH="$(git branch --contains 'tags/v0.1.0' | tail -n +2 | tail -n 1 | tr -d '[:space:]')" # get branch containing tag
fi
export GIT_COMMIT="$(git rev-parse HEAD)"
export GIT_SHORT_COMMIT="$(git rev-parse --short HEAD)"
export GIT_TAG="$(git --no-pager tag --points-at HEAD)"
SANITIZED_BRANCH="$(sanitize "$GIT_BRANCH")"
SANITIZED_TAG="$(sanitize "$GIT_TAG")"
# verify git tag matches package.json version, if it exists
if [[ -n "$GIT_TAG" && "$GIT_TAG" != "v$PACKAGE_VERSION" ]]; then
printf '\e[1;31mERROR: The git tag does not match the package.json version!\e[0m\n'
echo " git tag: $GIT_TAG"
echo "package.json version: $PACKAGE_VERSION"
echo 'These must match to build a release. Rejecting build.'
exit 10
fi
# git branch
export GIT_BRANCH="$(git branch --show-current)"
if [[ -z "$GIT_BRANCH" ]]; then # detached head, find tag on base branch or return a feature branch
BRANCHES="$(git branch --contains "tags/$GIT_TAG" | tail -n +2 | tr -d ' ')" # get branches containing tag
if [[ "$(echo "$BRANCHES" | grep -cP '^develop$')" == '1' ]]; then
export GIT_BRANCH='develop'
elif [[ "$(echo "$BRANCHES" | grep -cP '^main$')" == '1' ]]; then
export GIT_BRANCH='main'
elif [[ "$(echo "$BRANCHES" | grep -cP '^master$')" == '1' ]]; then
export GIT_BRANCH='master'
else
export GIT_BRANCH="$(echo "$BRANCHES" | tail -n 1 | tr -d '[:space:]')"
fi
fi
SANITIZED_BRANCH="$(sanitize "$GIT_BRANCH")"
# github actions info
if [[ -n "$GITHUB_TRIGGERING_ACTOR" ]]; then
export ACTOR="$GITHUB_TRIGGERING_ACTOR"
Expand All @@ -43,22 +60,9 @@ else
export ACTOR="$USER@$HOSTNAME"
fi
[[ -z "$GITHUB_ACTIONS" ]] && export GITHUB_ACTIONS='false'
# verify tag matches package.json version, if it exists
if [[ -n "$GIT_TAG" && "$GIT_TAG" != "v$PACKAGE_VERSION" ]]; then
printf '\e[1;31mERROR: The git tag does not match the package.json version!\e[0m\n'
echo " git tag: $GIT_TAG"
echo "package.json version: $PACKAGE_VERSION"
echo 'These must match to build a release. Rejecting build.'
exit 10
fi
# backup node_modules
export UNIX_TIME="$(date +%s)"
if [[ -d node_modules ]]; then
echo 'Backing up your node_modules folder. It will be restored after the build.'
ee "mv 'node_modules' 'node_modules.$UNIX_TIME.bak'"
fi
# pack metadata into the package.json
echo 'Adding git metadata to package.json.'
export UNIX_TIME="$(date +%s)"
ee "mv package.json package.json.$UNIX_TIME.bak"
cat package.json.$UNIX_TIME.bak | jq \
--arg branch "$GIT_BRANCH" \
Expand All @@ -77,29 +81,11 @@ cat package.json.$UNIX_TIME.bak | jq \
short_commit: env.GIT_SHORT_COMMIT,
tag: (if $tag == "" then null else $tag end)
}' > package.json
# install dependencies, but not dev dependencies
echo 'Installing production dependencies...'
ee 'yarn --prod --frozen-lockfile --non-interactive'
echo 'Done installing production dependencies.'
# pack a dist.zip for AWS
if [[ -n "$SANITIZED_TAG" ]]; then
ZIP_NAME="$PACKAGE_NAME-$SANITIZED_TAG-node-$NODE_MAJOR_VERSION.dist.zip"
else
ZIP_NAME="$PACKAGE_NAME-$SANITIZED_BRANCH-$GIT_SHORT_COMMIT-node-$NODE_MAJOR_VERSION.dist.zip"
fi
echo "Packing \"$ZIP_NAME\" for AWS..."
FILES="$(cat package.json | jq -r '.files[]' | tr '\n' ' ')"
ee "zip -r '$ZIP_NAME' ${FILES}LICENSE node_modules package.json README.md"
echo "Done packing \"$ZIP_NAME\" for AWS."
# put package.json back
ee "mv package.json.$UNIX_TIME.bak package.json"
# restore original node_modules, if it existed
if [[ -d "node_modules.$UNIX_TIME.bak" ]]; then
echo 'Restoring your node_modules folder.'
ee 'rm -rf node_modules'
ee "mv 'node_modules.$UNIX_TIME.bak' 'node_modules'"
fi
printf "\e[1;32mOUTPUT:\e[0;32m $(pwd)/$ZIP_NAME\e[0m\n"
echo 'This zip folder can be uploaded directly to AWS lambda.'
ee 'cat package.json | jq .git'
# build
echo 'Building...'
ee 'npm pack'
# clean up
ee mv package.json.$UNIX_TIME.bak package.json
popd
echo "Done. - ${BASH_SOURCE[0]}"

0 comments on commit c5d491c

Please sign in to comment.