Skip to content

Instantly share code, notes, and snippets.

@cojack
Last active June 11, 2020 17:54
Show Gist options
  • Save cojack/0f033d68a05b8d2aed3a429f359c53c5 to your computer and use it in GitHub Desktop.
Save cojack/0f033d68a05b8d2aed3a429f359c53c5 to your computer and use it in GitHub Desktop.
Bump version for git project with npm package.json file
#!/usr/bin/env bash
bump_version () {
LOCKFILE="/var/lock/bump_version_for_$(basename `pwd`).lock"
trap "{ rm -f $LOCKFILE ; return 255; }" EXIT
if [[ -f $LOCKFILE ]]; then
echo "Lockfile ${LOCKFILE} exists, clean it fist"
return 1
fi
touch $LOCKFILE
if [[ ! -d ./.git ]]; then
echo "Neither git repository or root of the project"
return 1
fi
if [[ `git status --porcelain -uno` ]]; then
echo "Repository is not clean, clean your repo first!"
return 1
fi
local LEVEL=$1
if [[ -z ${LEVEL} ]]; then
LEVEL="patch"
fi
case ${LEVEL} in
patch|minor|major)
;;
*)
echo "Wrong type for semver update, available only patch|minor|major"
return 1
;;
esac
if [[ ! -e ./package.json ]]; then
echo "File package.json is missing in the directory `pwd`"
return 1
fi
git fetch origin
if [[ $? -ne 0 ]]; then
echo "There is origin?!"
return 1
fi
git checkout develop
git pull origin develop
if [[ $? -ne 0 ]]; then
echo "Failed to clean pull develop, check conflicts"
return 1
fi
npm version ${LEVEL} --no-git-tag-version
VERSION=$(cat package.json|python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["version"])')
git commit -am "chore(package): Bump version up to ${VERSION}"
git push origin develop
if [[ $? -ne 0 ]]; then
echo "Failed to push develop to origin, maybe some1 did push before you?"
return 1
fi
git tag "$VERSION"
if [[ $? -ne 0 ]]; then
echo "Failed to tag version: ${VERSION}, take a look what happen!"
return 1
fi
echo "Yes Master, I'm your slave!"
git checkout master
git pull origin master
if [[ $? -ne 0 ]]; then
echo "Failed to clean pull master, did you put your dirty fingers on holly master?!"
return 1
fi
git merge --no-ff --no-edit develop
if [[ $? -ne 0 ]]; then
echo "Failed to clean merge develop to master, check conflicts"
return 1
fi
git push origin master
if [[ $? -ne 0 ]]; then
echo "Failed to push master to origin, some1 did it before you"
fi
git push --tags
git checkout develop
}
export -f bump_version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment