Skip to content

Instantly share code, notes, and snippets.

@mirismaili
Last active March 12, 2023 20:36
Show Gist options
  • Save mirismaili/6b299991a05b3f3b986bccbe657dfb52 to your computer and use it in GitHub Desktop.
Save mirismaili/6b299991a05b3f3b986bccbe657dfb52 to your computer and use it in GitHub Desktop.
Auto tag current version (from package.json) after commit
#!/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