-
Notifications
You must be signed in to change notification settings - Fork 45
chore: add package.json node engine to pre commit version check #1734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) | ||||||||||||||
|
|
||||||||||||||
|
||||||||||||||
| if [ -z "$PACKAGE_VERSION" ]; then | |
| echo "❌ Could not extract Node version from package.json (missing or malformed engines.node field)." | |
| exit 1 | |
| fi |
There was a problem hiding this comment.
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
jqto 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.