Skip to content

Commit 56ee510

Browse files
authored
feat: verify package-lock.json UTD (up to date) (#4598)
* feat: we'd like to provide a quick check regarding * chore: worth a try * Revert "chore: worth a try" This reverts commit eeeea2d. * Reapply "chore: worth a try" This reverts commit cd63f49. * refactor: incorrect parameter * refactor: corrected package-lock file * chore: another try * Revert "chore: another try" This reverts commit 52c9f51. * chore: another try * Revert "chore: another try" This reverts commit ae9a456. * Reapply "chore: another try" This reverts commit 3e6295f. * Revert "Reapply "chore: another try"" This reverts commit 024e42e. * Reapply "Reapply "chore: another try"" This reverts commit a8fd3d4. * refactor: modified this file * Revert "Reapply "Reapply "chore: another try""" This reverts commit b990c9f. * Reapply "Reapply "Reapply "chore: another try""" This reverts commit 8d59270. * Revert "Reapply "Reapply "Reapply "chore: another try"""" This reverts commit 21dfc4e. * Reapply "Reapply "Reapply "Reapply "chore: another try"""" This reverts commit c8e1a6a. * chore: another try * Revert "chore: another try" This reverts commit 921422d. * chore: another try * refactor: another try * refactor: necessary update * refactor: regenerated package lock file * refactor: generalize this * refactor: removed that package again * refactor: get rid of the dependency * refactor: ensure that npm audit has fixed dependencies * Update pre-push
1 parent 895effd commit 56ee510

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

.config/.lintstagedrc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ export default {
88
'stylelint.config.*': 'stylelint --validate --allow-empty-input',
99
// And elsewhere we don't, compare to https://github.com/stylelint/stylelint/pull/8009
1010
'*.{css,scss}': 'stylelint --fix --allow-empty-input --no-validate',
11-
'*.{js,ts,tsx,jsx,mjs,cjs}': 'xo --fix'
11+
'*.{js,ts,tsx,jsx,mjs,cjs}': 'xo --fix',
12+
// ensure that security vulnerabilities are fixed before committing
13+
'package-lock.json': 'npm audit fix'
1214
};

.husky/pre-push

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Configuration: Define checks as functions for better maintainability
2+
# Each check function should:
3+
# - Define a PATTERN variable for file matching
4+
# - Define a COMMAND variable for the command to run
5+
# - Define a DESCRIPTION variable for user feedback
6+
7+
check_npm_files() {
8+
PATTERN='^(package\.json|package-lock\.json)$'
9+
COMMAND='npm install --package-lock-only --ignore-scripts'
10+
DESCRIPTION='package.json or package-lock.json – please run npm install to update dependencies'
11+
}
12+
13+
check_pnpm_files() {
14+
PATTERN='^(package\.json|pnpm-lock\.yaml)$'
15+
COMMAND='pnpm install --lockfile-only --ignore-scripts'
16+
DESCRIPTION='package.json or pnpm-lock.yaml – please run pnpm install to update dependencies'
17+
}
18+
19+
# List of all check functions
20+
# Detect the lock file to determine the package manager
21+
if [ -f "pnpm-lock.yaml" ]; then
22+
CHECK_FUNCTIONS=(
23+
"check_pnpm_files"
24+
)
25+
elif [ -f "package-lock.json" ]; then
26+
CHECK_FUNCTIONS=(
27+
"check_npm_files"
28+
)
29+
else
30+
echo "No lock file detected for pnpm or npm. Aborting pre-push checks."
31+
exit 1
32+
fi
33+
34+
# Check for changes in specified files before pushing and run corresponding commands
35+
## Get the upstream branch
36+
UPSTREAM=$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null || echo "")
37+
if [ -z "$UPSTREAM" ]; then
38+
echo "No upstream configured, detecting default branch."
39+
# Try to detect the default branch from origin/HEAD
40+
DEFAULT_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@')
41+
if [ -z "$DEFAULT_BRANCH" ]; then
42+
echo "Could not detect default branch, falling back to 'main'."
43+
DEFAULT_BRANCH="main"
44+
fi
45+
UPSTREAM="$DEFAULT_BRANCH"
46+
fi
47+
48+
## Get the list of files changed between upstream and HEAD
49+
FILES=$(git diff --name-only "$UPSTREAM"..HEAD)
50+
51+
## Check each pattern and run corresponding command
52+
for check_function in "${CHECK_FUNCTIONS[@]}"; do
53+
# Call the check function to set variables
54+
$check_function
55+
56+
if echo "$FILES" | grep --quiet --extended-regexp --recursive "$PATTERN"; then
57+
echo "Detected changes in $DESCRIPTION"
58+
59+
## Run the corresponding command
60+
$COMMAND
61+
62+
if [ $? -ne 0 ]; then
63+
echo "Command failed: $COMMAND. Aborting push."
64+
exit 1
65+
fi
66+
67+
# Check for file modifications after running the command
68+
MODIFIED_FILES=$(git diff --name-only)
69+
if [ -n "$MODIFIED_FILES" ]; then
70+
echo "Detected file modifications after running $COMMAND:"
71+
echo "$MODIFIED_FILES"
72+
echo "Please stage the changes before pushing."
73+
exit 1
74+
fi
75+
fi
76+
done
77+
78+
echo "No monitored file changes detected. Skipping checks."

0 commit comments

Comments
 (0)