Skip to content

Conversation

@Aerilym
Copy link
Collaborator

@Aerilym Aerilym commented Dec 11, 2025

No description provided.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR extends the pre-commit hook to validate that the Node version specified in package.json (under engines.node) matches the versions in .tool-versions and .nvmrc, ensuring consistency across all Node version declarations in the project.

  • Adds extraction of Node version from package.json engines field
  • Updates the version mismatch check to compare all three sources
  • Updates error messages to include package.json version and reflect the three-way comparison

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.
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 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants