Last active
March 12, 2023 20:36
-
-
Save mirismaili/6b299991a05b3f3b986bccbe657dfb52 to your computer and use it in GitHub Desktop.
Auto tag current version (from package.json) after commit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#X # ATTENTION: YOU NEED TO FIRST PUT `build-version: 0` FIELD IN YOUR "package.json" FILE! | |
version=$(grep -oP '\"version\"\s*:\s*\"\K\d+\.\d+\.\d+' package.json) # extract `version` from "package.json". ex: 2.0.4 | |
git tag v$version 2>/dev/null # ex: `git tag v2.0.4` # Auto tag version if needed (if not already existed). | |
#X # Calculate `buildVersion`: | |
#X | |
#X currentCommitNum=$(git rev-list --count HEAD 2>/dev/null || echo 0) | |
#X | |
#X lastVersionCommitNum=$(git rev-list --count v$version 2>/dev/null || echo 0) | |
#X | |
#X buildVersion=$(( currentCommitNum - lastVersionCommitNum )) | |
#X | |
#X # Put the new calculated `buildVersion` in "package.json": | |
#X sed -i -E "s/(\"build-version\":\s*)\"?.*\"?,/\1$buildVersion,/" package.json # https://regex101.com/r/LqkQcD/1/ | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment