Skip to content

Instantly share code, notes, and snippets.

@imatefx
Last active June 3, 2016 10:56
Show Gist options
  • Save imatefx/cafc543cce4dd2f7e427105c2f37c41e to your computer and use it in GitHub Desktop.
Save imatefx/cafc543cce4dd2f7e427105c2f37c41e to your computer and use it in GitHub Desktop.
bash script to automatically create a git / git flow release, bump npm version, build module, commit, publish module to npm
#!/bin/bash
autoFinishGitFlow=1
autoBuild=1
autoPublish=1
autoCreateGitFlowRelease=1
autoCommit=1
release="patch"
function getVersions {
PACKAGE_VERSION=$(node -p -e "require('./package.json').version")
if [ -z "${preid}" ]; then
NEXT_PACKAGE_VERSION=$(semver -i ${release} $PACKAGE_VERSION)
else
NEXT_PACKAGE_VERSION=$(semver -i ${release} --preid ${preid} $PACKAGE_VERSION)
fi
echo "** Current version : ${PACKAGE_VERSION}"
echo "** Next version : ${NEXT_PACKAGE_VERSION}"
}
function bump {
echo "** Bumping version using npm to ${NEXT_PACKAGE_VERSION}"
output=$(npm version ${NEXT_PACKAGE_VERSION} --no-git-tag-version)
echo "** NPM Version : ${output}"
}
function help {
echo "Usage: $(basename $0) -r [<newversion> | major | minor | patch | premajor | preminor | prepatch ] -p <prerelease>"
echo "-a: disable auto finish release"
echo "-b: disable npm run build"
echo "-p: disable npm publish"
echo "-f: without git flow"
echo "-c: disable commit"
}
while getopts ":r:p:abp" opt; do
case $opt in
r)
echo "** -r was triggered!, Parameter: $OPTARG" >&2
release=$OPTARG
;;
p)
echo "** -p was triggered!, Parameter: $OPTARG" >&2
preid=$OPTARG
;;
a)
echo "** -a was triggered!, Parameter: $OPTARG" >&2
autoFinishGitFlow=0
;;
b)
echo "** -b was triggered!, Parameter: $OPTARG" >&2
autoBuild=0
;;
p)
echo "** -p was triggered!, Parameter: $OPTARG" >&2
autoPublish=0
;;
f)
echo "** -f was triggered!, Parameter: $OPTARG" >&2
autoCreateGitFlowRelease=0
;;
c)
echo "** -c was triggered!, Parameter: $OPTARG" >&2
autoCommit=0
;;
\?)
echo "** Invalid option: -$OPTARG" >&2
;;
:)
echo "** Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
getVersions
if [ -z "$release" ]; then
help
exit
fi
if [ -d ".git" ]; then
changes=$(git status --porcelain)
if [ -z "${changes}" ]; then
echo "** Starting release ${NEXT_PACKAGE_VERSION}"
if (( autoCreateGitFlowRelease==1 )) ; then
echo '** Git Flow Release'
git flow release start ${NEXT_PACKAGE_VERSION}
fi
if (( autoBuild==1 )) ; then
echo '** Build Release'
npm run build
rc=$?; if [[ $rc != 0 ]]; then exit $rc; fi
fi
bump
if (( autoCommit==1 )) ; then
echo "** Adding all files to git."
git add .
echo "** GIT Status."
git status
echo "** Commit files to git."
git commit -m "Bump to ${NEXT_PACKAGE_VERSION}"
fi
if (( autoFinishGitFlow==1 && autoCreateGitFlowRelease==1 )) ; then
echo "** Finish release ${NEXT_PACKAGE_VERSION}."
git flow release finish ${NEXT_PACKAGE_VERSION} -m "Release ${NEXT_PACKAGE_VERSION}"
fi
if (( autoPublish==1 )) ; then
echo '** Publishing Release'
npm publish
fi
else
echo "** Please commit staged files prior to bumping"
fi
else
bump
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment