Skip to content

Commit

Permalink
chore: Made pre-commit hook require dep updates before running update…
Browse files Browse the repository at this point in the history
… third party deps (#251)
  • Loading branch information
bizob2828 authored May 6, 2024
1 parent c5773c7 commit bfab44d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
. "$(dirname "$0")/../bin/update-third-party-notices.sh"
./"$(dirname "$0")/../bin/update-third-party-notices.sh"

npx lint-staged
77 changes: 73 additions & 4 deletions bin/update-third-party-notices.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/usr/bin/env bash
#
# Checks all staged files for
# package.json. If it changes
Expand All @@ -8,15 +8,84 @@

STAGED_FILES=$(git diff-index --cached --name-only HEAD)

PKG_HAS_CHANGED=0
for FILE in $STAGED_FILES
do
if [ $FILE == "package.json" ]; then
RUN_THIRD_PARTY=1
if [ ${FILE} == "package.json" ]; then
PKG_HAS_CHANGED=1
break
fi
done

if [ -n "$RUN_THIRD_PARTY" ]; then
if [ ${PKG_HAS_CHANGED} -eq 0 ]; then
echo "package.json has not changed"
exit 0
fi

if ! [ -x "$(command -v jq)" ]; then
echo "Please install jq."
echo "https://jqlang.github.io/jq/"
exit 1
fi

MAIN_PKG=$(git show main:package.json | jq -rc)
HEAD_PKG=$(git cat-file blob :package.json | jq -rc)

MAIN_DEPS_LEN=$(echo "${MAIN_PKG}" | jq '.dependencies | length')
HEAD_DEPS_LEN=$(echo "${HEAD_PKG}" | jq '.dependencies | length')
MAIN_OPT_DEPS_LEN=$(echo "${MAIN_PKG}" | jq '.optionalDependencies | length')
HEAD_OPT_DEPS_LEN=$(echo "${HEAD_PKG}" | jq '.optionalDependencies | length')
MAIN_DEV_DEPS_LEN=$(echo "${MAIN_PKG}" | jq '.devDependencies | length')
HEAD_DEV_DEPS_LEN=$(echo "${HEAD_PKG}" | jq '.devDependencies | length')

RUN_THIRD_PARTY=0
if [ ${MAIN_DEPS_LEN} -ne ${HEAD_DEPS_LEN} ]; then
RUN_THIRD_PARTY=1
elif [ ${MAIN_OPT_DEPS_LEN} -ne ${HEAD_OPT_DEPS_LEN} ]; then
RUN_THIRD_PARTY=1
elif [ ${MAIN_DEV_DEPS_LEN} -ne ${HEAD_DEV_DEPS_LEN} ]; then
RUN_THIRD_PARTY=1
fi

if [ ${RUN_THIRD_PARTY} -eq 0 ]; then
for dep in $(echo "${MAIN_PKG}" | jq -rc '.dependencies | to_entries | .[]'); do
NAME=$(echo ${dep} | jq -r '.key')
VERSION=$(echo ${dep} | jq -r '.value')
HEAD_VERSION=$(echo "${HEAD_PKG}" | jq -rc --arg pkg "${NAME}" '.dependencies[$pkg]')
if [ "${VERSION}" != "${HEAD_VERSION}" ]; then
RUN_THIRD_PARTY=1
break
fi
done
fi

if [ ${RUN_THIRD_PARTY} -eq 0 ]; then
for dep in $(echo "${MAIN_PKG}" | jq -rc '.optionalDependencies | to_entries | .[]'); do
NAME=$(echo ${dep} | jq -r '.key')
VERSION=$(echo ${dep} | jq -r '.value')
HEAD_VERSION=$(echo "${HEAD_PKG}" | jq -rc --arg pkg "${NAME}" '.optionalDependencies[$pkg]')
if [ "${VERSION}" != "${HEAD_VERSION}" ]; then
RUN_THIRD_PARTY=1
break
fi
done
fi

if [ ${RUN_THIRD_PARTY} -eq 0 ]; then
for dep in $(echo "${MAIN_PKG}" | jq -rc '.devDependencies | to_entries | .[]'); do
NAME=$(echo ${dep} | jq -r '.key')
VERSION=$(echo ${dep} | jq -r '.value')
HEAD_VERSION=$(echo "${HEAD_PKG}" | jq -rc --arg pkg "${NAME}" '.devDependencies[$pkg]')
if [ "${VERSION}" != "${HEAD_VERSION}" ]; then
RUN_THIRD_PARTY=1
break
fi
done
fi

if [ ${RUN_THIRD_PARTY} -eq 1 ]; then
echo "package.json changed, running oss manifest and notices"
npm run third-party-updates
else
echo "package.json has not changed"
fi

0 comments on commit bfab44d

Please sign in to comment.