Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#!/bin/sh

# Validates all sources of the project's node version are the same
if [ -f .tool-versions ] && [ -f .nvmrc ]; then
# Extract node version from .tool-versions
TOOL_VERSION=$(grep "^node" .tool-versions | cut -d' ' -f2)

# Extract version from .nvmrc
NVMRC_VERSION=$(tr -d '\n\r' < .nvmrc)
NVMRC_VERSION=$(tr -d '\n\r' <.nvmrc)
# Extract version from package.json > engines.node
PACKAGE_VERSION=$(grep -A 1 '"engines"' package.json | grep '"node"' | cut -d'"' -f4)
Copy link

Copilot AI Dec 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The grep-based extraction is fragile and may fail in certain scenarios. The current approach assumes "node" appears exactly one line after "engines" in the JSON, which may not always be true (e.g., if there are comments, different formatting, or additional whitespace). Consider using a more robust JSON parsing tool like jq to extract the value reliably, for example: jq -r '.engines.node // ""' package.json. This would handle various JSON formatting styles and provide an empty string if the field doesn't exist.

Suggested change
PACKAGE_VERSION=$(grep -A 1 '"engines"' package.json | grep '"node"' | cut -d'"' -f4)
PACKAGE_VERSION=$(jq -r '.engines.node // ""' package.json)

Copilot uses AI. Check for mistakes.

Copy link

Copilot AI Dec 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comparison logic doesn't validate whether PACKAGE_VERSION was successfully extracted. If package.json doesn't have an engines.node field or the extraction fails, PACKAGE_VERSION will be empty, and the comparison will likely pass incorrectly or produce confusing error messages. Add a check to ensure PACKAGE_VERSION is not empty before performing the comparison, or handle the case where it might not be present.

Suggested change
if [ -z "$PACKAGE_VERSION" ]; then
echo "❌ Could not extract Node version from package.json (missing or malformed engines.node field)."
exit 1
fi

Copilot uses AI. Check for mistakes.
# Compare versions (trim whitespace)
if [ "$TOOL_VERSION" != "$NVMRC_VERSION" ]; then
if [ "$TOOL_VERSION" != "$NVMRC_VERSION" ] || [ "$TOOL_VERSION" != "$PACKAGE_VERSION" ]; then
echo "❌ Node version mismatch!"
echo " .tool-versions: $TOOL_VERSION"
echo " .nvmrc: $NVMRC_VERSION"
echo " Please ensure both files have the same version."
echo " package.json: $PACKAGE_VERSION"
echo " Please ensure all files have the same node version."
exit 1
fi
fi