Skip to content

Instantly share code, notes, and snippets.

@tkqubo
Last active September 27, 2015 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkqubo/6fdacac7dc4cc3252430 to your computer and use it in GitHub Desktop.
Save tkqubo/6fdacac7dc4cc3252430 to your computer and use it in GitHub Desktop.
Bump npm version and create pull-request
#!/bin/bash
# Check commands existents
function commands() {
which $1 1>/dev/null || (echo "$1 not found" >&2; exit 1)
}
commands npm && commands git && commands hub && commands jq || exit 1
# Check git status
gitStatus=$(git status --porcelain 2>/dev/null)
[[ $? != 0 ]] && echo 'not a git repository' >&2 && exit 1
[[ -n $gitStatus ]] && echo 'repo is dirty' >&2 && exit 1
[[ $(git rev-parse --abbrev-ref HEAD) != 'master' ]] && echo 'not a master branch' >&2 && exit 1
git fetch --all
[[ -n $(git diff origin/master --name-only | cat) ]] && echo 'need to pull master' >&2 && exit 1
# Check option
option=${1:-patch}
[[ $option != 'major' && $option != 'minor' && $option != 'patch' ]] && echo "wrong parameter: $option" >&2 && exit 1
# Check package.json
[[ ! -e package.json ]] && echo 'package.json not found' && exit 1
# Check repo version
function getVersion() {
version=$(cat package.json| jq '.version' | sed 's/"//g')
[[ -z $version ]] && echo 'version not found' >&2 && exit 1
echo $version
}
getVersion || exit 1
# Create branch and
branch=$(cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
git checkout -b $branch
npm version $option
version=$(getVersion)
branch="npm-version-$option/$version"
git branch -m $branch
git push origin ${branch}
hub pull-request ${branch} -m "Bump version to ${version}"
# Back to master
git fetch --all && git checkout master && git pull
git branch -D ${branch}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment