Skip to content

Instantly share code, notes, and snippets.

@pwang2
Last active October 21, 2016 02:59
Show Gist options
  • Save pwang2/e6036b6405c0c18697ff4e8d68d50c2f to your computer and use it in GitHub Desktop.
Save pwang2/e6036b6405c0c18697ff4e8d68d50c2f to your computer and use it in GitHub Desktop.
#!/bin/bash
# script to publish the related version to npm
function replaceJsonProp {
replaceInFile $1 '"('$2')"[ ]*:[ ]*"'$3'"' '"\1": "'$4'"'
}
function replaceInFile {
sed --in-place=.tmp -E "s/$2/$3/" $1
rm $1.tmp
}
curdir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
publish_type="patch"
publish_tag="--tag latest"
dry_run=0
ci=0
: ${npm_registry:="https://msnexus.morningstar.com/content/repositories/npm-snapshot/"}
names=(quikr-bower-local-resolver \
html-webpack-wiredep-plugin \
quikr-config-loader \
generator-quikr-component \
generator-ec-quikr-component \
generator-quikr \
quikr-cli \
quikr-log \
quikr)
while test $# -gt 0; do
case "$1" in
-h|--help)
echo "publish npm package to registry from monolithic repo"
echo "options:"
echo "-h, --help show brief help"
echo "--type [major|minor|patch]"
echo "--canary flag, pass to publish npm canary dist tag"
echo "--no-canary flag, pass to publish npm canary latest tag"
echo "--dry-run flag, no change made just output the change to be made"
echo "--ci ci will skip manded code"
exit 0
;;
--type)
publish_type="$2"
shift 2
;;
--canary)
publish_tag="--tag canary"
shift
;;
--no-canary)
publish_tag="--tag latest"
shift
;;
--ci)
ci=1
shift
;;
--dry-run)
dry_run=1
shift
;;
-*)
echo "Error: Unknown option: $1" >&2
exit 1
;;
*)
break
;;
esac
done
PATH=${curdir}/node_modules/.bin:$PATH
which semver >/dev/null
if [[ $? != 0 ]]; then
echo "semver CLI not found, try npm install from repo root first"
exit
fi
if [[ "`git status --porcelain`" != "" ]]; then
echo "git repo is not clean."
exit 1;
fi
current_branch=$(git rev-parse --abbrev-ref HEAD);
if [[ "$publish_tag" == "--tag canary" ]] && [[ "$current_branch" != "develop" ]]; then
echo "canary release should be from develop branch, currently you are at $current_branch"
exit 1
fi
if [[ "$publish_tag" == "--tag latest" ]] && [[ ! "$current_branch" =~ ^release\/.*$ ]]; then
echo "official release should be from release branch, currently you are at $current_branch"
exit 1
fi
# pull all tag to local
git fetch origin --tags
# There shall be no tag created between branching points (2 and A in chart below) and the current release branch tagging position.
# so we always find the latest tag via develop branch
# once release version created, commits added on release branch shall be merged back to develop before we create another release branch
# sometimes, to fix a most recent release bug with a new patch, we will branch out from the previous merge point (illustrated as A below)
# or we could reuse the previous release branch is not deleted
#
# (v1.0.0) (v1.0.1)
# 1------2-------3------4------5--------6-------7-------8(develop)
# \ /
# 2a-------2b-------A (v1.0.2)
# [release/A] \
# 2c-------2d------B(?)
# [release/B]
# when we are ready for a new number at B, we still consult with develop branch to get a most recent tag.
# if 6 was not tagged with v1.0.3, B will be v1.0.3.
# if 6 was tagged with v1.0.3, B will be v1.0.4
#
# one issue is a newer version number may contain fewer feature.
# here v1.0.1 possibly have more feature than the 1.0.3.
# not a huge problem, just need to keep in mind that, canary version number is NOT comparable to official (latest)
if [[ "$current_branch" != "develop" ]]; then
git checkout develop
fi
prev_tag="`git describe --tags --abbrev=0`"
if [[ "$current_branch" != "develop" ]]; then
git checkout $current_branch
fi
new_tag="`semver -i $publish_type $prev_tag`"
echo "=> using CI: $ci"
echo "=> publish type: $publish_type"
echo "=> publish to: $publish_tag"
echo "=> current branch: $current_branch"
echo "=> previous tag: $prev_tag"
echo "=> version to publish: $new_tag"
if [[ "$ci" != "1" ]]; then
echo "Publishing components to $npm_registry. Press Ctrl-C to abort"
printf "=======>20%%";sleep 1
printf "\x08\x08\x08\x08=======>40%%";sleep 1
printf "\x08\x08\x08\x08=======>60%%";sleep 1
printf "\x08\x08\x08\x08=======>80%%";sleep 1
printf "\x08\x08\x08\x08=======>100%%";sleep 1
fi
for name in ${names[@]}; do
cd $curdir/$name
if [[ "$dry_run" == "1" ]];then
echo "[dryrun]: npm version $new_tag --no-git-tag-version"
echo "[dryrun]: replacing peer module verison in $name package.json"
echo "[dryrun]: npm publish --registry $npm_registry $publish_tag"
else
npm version $new_tag --no-git-tag-version
if [[ $publish_type == "major" ]];then
for pkg in ${names[@]}; do
# no need to replace itself's version as pkg should not reference itslef.
# also, this is needed to get around
# "bin": {
#- "quikr": "bin/quikr.js" #should not be updated
# }
if [[ "$pkg" != "$name" ]]; then
replaceJsonProp "package.json" "$pkg" ".*" "^${new_tag}"
fi
done
fi
npm publish --registry $npm_registry $publish_tag
fi
done
if [[ "$dry_run" == "1" ]];then
echo "
git add -A
git commit -m \"release $publish_type $new_tag\"
git tag -a v${new_tag} -m \"release $publish_type $new_tag\"
git push origin --tags "
else
git add -A
git commit -m "release $publish_type $new_tag"
git tag -a v${new_tag} -m "release $publish_type $new_tag"
git push origin --tags $current_branch
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment