Skip to content

Commit e7bd356

Browse files
authoredApr 29, 2024··
chore: Made pre-commit hook require dependency changes (#2172)
1 parent ff44b39 commit e7bd356

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed
 

‎.husky/pre-commit

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/sh
22
. "$(dirname "$0")/_/husky.sh"
3-
. "$(dirname "$0")/../bin/update-third-party-notices.sh"
3+
./"$(dirname "$0")/../bin/update-third-party-notices.sh"
44

55
npx lint-staged

‎bin/update-third-party-notices.sh

+73-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/usr/bin/env bash
22
#
33
# Checks all staged files for
44
# package.json. If it changes
@@ -8,15 +8,84 @@
88

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

11+
PKG_HAS_CHANGED=0
1112
for FILE in $STAGED_FILES
1213
do
13-
if [ $FILE == "package.json" ]; then
14-
RUN_THIRD_PARTY=1
14+
if [ ${FILE} == "package.json" ]; then
15+
PKG_HAS_CHANGED=1
1516
break
1617
fi
1718
done
1819

19-
if [ -n "$RUN_THIRD_PARTY" ]; then
20+
if [ ${PKG_HAS_CHANGED} -eq 0 ]; then
21+
echo "package.json has not changed"
22+
exit 0
23+
fi
24+
25+
if ! [ -x "$(command -v jq)" ]; then
26+
echo "Please install jq."
27+
echo "https://jqlang.github.io/jq/"
28+
exit 1
29+
fi
30+
31+
MAIN_PKG=$(git show main:package.json | jq -rc)
32+
HEAD_PKG=$(git cat-file blob :package.json | jq -rc)
33+
34+
MAIN_DEPS_LEN=$(echo "${MAIN_PKG}" | jq '.dependencies | length')
35+
HEAD_DEPS_LEN=$(echo "${HEAD_PKG}" | jq '.dependencies | length')
36+
MAIN_OPT_DEPS_LEN=$(echo "${MAIN_PKG}" | jq '.optionalDependencies | length')
37+
HEAD_OPT_DEPS_LEN=$(echo "${HEAD_PKG}" | jq '.optionalDependencies | length')
38+
MAIN_DEV_DEPS_LEN=$(echo "${MAIN_PKG}" | jq '.devDependencies | length')
39+
HEAD_DEV_DEPS_LEN=$(echo "${HEAD_PKG}" | jq '.devDependencies | length')
40+
41+
RUN_THIRD_PARTY=0
42+
if [ ${MAIN_DEPS_LEN} -ne ${HEAD_DEPS_LEN} ]; then
43+
RUN_THIRD_PARTY=1
44+
elif [ ${MAIN_OPT_DEPS_LEN} -ne ${HEAD_OPT_DEPS_LEN} ]; then
45+
RUN_THIRD_PARTY=1
46+
elif [ ${MAIN_DEV_DEPS_LEN} -ne ${HEAD_DEV_DEPS_LEN} ]; then
47+
RUN_THIRD_PARTY=1
48+
fi
49+
50+
if [ ${RUN_THIRD_PARTY} -eq 0 ]; then
51+
for dep in $(echo "${MAIN_PKG}" | jq -rc '.dependencies | to_entries | .[]'); do
52+
NAME=$(echo ${dep} | jq -r '.key')
53+
VERSION=$(echo ${dep} | jq -r '.value')
54+
HEAD_VERSION=$(echo "${HEAD_PKG}" | jq -rc --arg pkg "${NAME}" '.dependencies[$pkg]')
55+
if [ "${VERSION}" != "${HEAD_VERSION}" ]; then
56+
RUN_THIRD_PARTY=1
57+
break
58+
fi
59+
done
60+
fi
61+
62+
if [ ${RUN_THIRD_PARTY} -eq 0 ]; then
63+
for dep in $(echo "${MAIN_PKG}" | jq -rc '.optionalDependencies | to_entries | .[]'); do
64+
NAME=$(echo ${dep} | jq -r '.key')
65+
VERSION=$(echo ${dep} | jq -r '.value')
66+
HEAD_VERSION=$(echo "${HEAD_PKG}" | jq -rc --arg pkg "${NAME}" '.optionalDependencies[$pkg]')
67+
if [ "${VERSION}" != "${HEAD_VERSION}" ]; then
68+
RUN_THIRD_PARTY=1
69+
break
70+
fi
71+
done
72+
fi
73+
74+
if [ ${RUN_THIRD_PARTY} -eq 0 ]; then
75+
for dep in $(echo "${MAIN_PKG}" | jq -rc '.devDependencies | to_entries | .[]'); do
76+
NAME=$(echo ${dep} | jq -r '.key')
77+
VERSION=$(echo ${dep} | jq -r '.value')
78+
HEAD_VERSION=$(echo "${HEAD_PKG}" | jq -rc --arg pkg "${NAME}" '.devDependencies[$pkg]')
79+
if [ "${VERSION}" != "${HEAD_VERSION}" ]; then
80+
RUN_THIRD_PARTY=1
81+
break
82+
fi
83+
done
84+
fi
85+
86+
if [ ${RUN_THIRD_PARTY} -eq 1 ]; then
2087
echo "package.json changed, running oss manifest and notices"
2188
npm run third-party-updates
89+
else
90+
echo "package.json has not changed"
2291
fi

0 commit comments

Comments
 (0)
Please sign in to comment.