Skip to content

Instantly share code, notes, and snippets.

@mirismaili
Created November 13, 2021 19:41
Show Gist options
  • Save mirismaili/301ebf8fd49012d851399084965d05dd to your computer and use it in GitHub Desktop.
Save mirismaili/301ebf8fd49012d851399084965d05dd to your computer and use it in GitHub Desktop.
1. Auto tag commit, based on npm-version 2. Auto calculate the build-version and insert it into package.json file
#!/bin/bash
# ATTENTION: YOU NEED TO FIRST PUT `build-version: 0` FIELD IN YOUR "package.json" FILE!
version=$(grep -oP '\"version\":\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).
# Calculate `buildVersion`:
currentCommitNum=$(git rev-list --count HEAD 2>/dev/null || echo 0)
lastVersionCommitNum=$(git rev-list --count v$version 2>/dev/null || echo 0)
buildVersion=$(( currentCommitNum - lastVersionCommitNum ))
# Put the new calculated `buildVersion` in "package.json":
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