Skip to content

Found Husky reminding to update version#

Always forgetful on the updation to increment your version in package.json when building Node.js projects?

I found Husky 🐶 can woof and remind you more than it can help you automatically lint commit messages, code and run tests upon commiting or pushing to GitHub.

Introduction#

Husky is an npm package that simplifies task and help you easily create your own custom git hooks.

For example you want to execute npm test on pre-commit, and when the test fails, the git hook will stop you from commiting your code and helps you not to commit unwanted unit test failures.

Getting Started#

Assumed you already have an existing Node.js project with git setup (can be via git init)

Husky Installation#

npm install --save-dev husky

Husky Init#

Requires git

It is recommended that you have a git setup before npx husky init.

If you don't have git setup, then first run git init.

npx husky init

It creates a pre-commit in .husky/ and updates the prepare script in package.json

Reference: https://typicode.github.io/husky/get-started.html

Customize pre-commit#

See reference: (1)

  1. https://github.com/lightzane/test-labs/blob/main/.husky/pre-commit
pre-commit
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Function to echo text in red
echo_red() {
    echo -e "\e[31m$1\e[0m"
    # -e -> will intepret char such as "\" special characters
    # rather than being printed literally
}

# Function to echo text in green
echo_green() {
    echo -e "\e[32m$1\e[0m"
}

# Function to echo text in yellow
echo_yellow() {
    echo -e "\e[33m$1\e[0m"
}

# Check if package-lock.json exists
if [ ! -f "package-lock.json" ]; then
    echo_red "Error: package-lock.json not found."
    echo_yellow "Try delete node_modules and run: npm install"
    exit 1
fi

# Extract versions
version=$(node -e "console.log(require('./package.json').version)")
lock_version=$(node -e "console.log(require('./package-lock.json').version)")

# Compare the versions
if [ "$version" == "$lock_version" ]; then
    echo_red "Please increment version in package.json"
    exit 1
else
    npm i --package-lock-only # automatically reflect version in package-lock.json
    git add package-lock.json
    echo_green "🌸 Everything is Great!"
fi

Try It#

git commit -m "keep calm and commit"
# test script will run every time you commit

And when you forgot to update your version, it will stop your from committing your code and displays the following message:

Please increment version in package.json
husky - pre-commit script failed (code 1)

Try updating your version in package.json without updating the package-lock.json. Then run the commit command again.

You should now be able to proceed and notice that the package-lock.json gets automatically update as well.

🌸 Everything is Great!

Conclusion#

There are times where you forget to update the version. SO it's good that we add automation on these small tasks and save time!

Bonus#

Whenever you push your code, are you also looking a way to automate create release tags or git tag, on your GitHub repository based on the version in your package.json?

Then see how to automatically release tag.

Comments