44
55set -e
66
7- # Empty to run the rest of the line and "echo" for a dry run
7+ # CMD_PREFIX is prepended to each command line.
8+ # Empty to run the rest of the line and "echo" for a dry run.
89CMD_PREFIX=
910
10- # Add a quote if this is a dry run
11+ # For a dry run, extra quotes must be added to the text
1112QUOTE=
1213
1314NEW_VERSION=
@@ -21,6 +22,7 @@ function usage
2122{
2223 echo " Usage: $0 [parameters]"
2324 echo " -v | --version <version> set a new version"
25+ echo " -p | --publish publish the new version"
2426 echo " -c | --current show current version"
2527 echo " -d | --dry-run print the commands without executing them"
2628 echo " -h | --help print this information and exit"
@@ -36,21 +38,24 @@ function process_arguments
3638 shift
3739 NEW_VERSION=${1:- }
3840 if ! [[ " ${NEW_VERSION} " =~ [0-9]+\. [0-9]+\. [0-9]+ (\- .+)? ]]; then
39- echo_err " You must supply a new version after -v or --version"
41+ echo_err " You must supply a new semver version after -v or --version"
4042 echo_err " For example:"
4143 echo_err " 1.2.3"
4244 echo_err " 1.2.3-rc1"
4345 echo_err " "
4446 usage; return 1
4547 fi
4648 ;;
49+ -p | --publish )
50+ PUBLISH=y
51+ ;;
4752 -c | --current )
4853 echo ` current_version`
49- exit
54+ return 0
5055 ;;
5156 -d | --dry-run )
5257 CMD_PREFIX=echo
53- echo " Dry Run"
58+ echo " ========== Dry Run ========== "
5459 echo " "
5560 ;;
5661 -h | --help )
@@ -82,7 +87,7 @@ function popd
8287# ver_lte 1.2.4 1.2.3 && echo "yes" || echo "no" # no
8388function ver_lte
8489{
85- [ " $1 " = " ` echo -e " $1 \n$2 " | sort -V | head -n1` " ]
90+ [[ $1 == " ` echo -e " $1 \n$2 " | sort -V | head -n1` " ] ]
8691}
8792
8893# Extract the last entry or entry for a given version
@@ -135,18 +140,20 @@ function current_version
135140 grep -oiP ' (?<=version": ")([0-9.]+)(?=")' package.json
136141}
137142
143+ # Update version in package.json and create an entry in CHANGELOG.md
144+ # Variables:
145+ # NEW_VERSION - the new version to set
146+ # CDM_PREFIX - dry run command (echo)
138147function update_version
139148{
140- if [ -z " ${NEW_VERSION} " ]; then
141- usage; return 1
142- fi
143-
144149 # Enter git root
150+ # TODO support mono-repo, which may have multiple package.json files
145151 pushd $( git rev-parse --show-toplevel)
146152 local current_version=$( current_version)
147153
148- if [ -z " ${current_version} " ]; then
154+ if [[ -z " ${current_version} " ] ]; then
149155 echo_err " Failed getting current version, please check directory structure and/or contact developer"
156+ popd
150157 return 1
151158 fi
152159
@@ -155,16 +162,26 @@ function update_version
155162
156163 echo " # Current version is: ${current_version} "
157164 echo " # New version is: ${NEW_VERSION} "
158-
159- ver_lte " ${NEW_VERSION} " " ${current_version} " && { echo_err " New version is not greater than current version" ; return 1; }
165+ echo " "
166+ ver_lte " ${NEW_VERSION} " " ${current_version} " && {
167+ echo_err " New version is not greater than current version"
168+ popd
169+ return 1
170+ }
160171
161172 # Add a quote if this is a dry run
162- QUOTE=${CMD_PREFIX: +" ' " }
173+ QUOTE=${CMD_PREFIX: +" \\\" " }
163174
164- npm version --no-git-tag-version " ${NEW_VERSION} "
175+ ${CMD_PREFIX} npm version --no-git-tag-version " ${NEW_VERSION} "
165176
166177 ${CMD_PREFIX} git changelog -t ${NEW_VERSION} || true
167178
179+ # show the user the files that were modified
180+ [[ -z " ${CMD_PREFIX} " ]] && git status
181+
182+ # don't print the following if the publish function is about to be invoked
183+ [[ -n " ${PUBLISH} " ]] && return
184+
168185 echo " "
169186 echo " # After editing CHANGELOG.md, optionally review changes and issue these commands:"
170187 echo git add package.json CHANGELOG.md
@@ -176,24 +193,45 @@ function update_version
176193 CHANGELOG.md \
177194 \| git tag -a " '${NEW_VERSION} '" --file=-
178195
179- # Don't run those commands on dry run
180- [ -n " ${CMD_PREFIX} " ] && { popd ; return 0 ; }
196+ popd
197+ }
181198
182- echo " "
183- read -p " Run the above commands automatically? (y/N): " confirm && [[ ${confirm} == [yY] || ${confirm} == [yY][eE][sS] ]] || { popd ; return 0; }
199+ function publish
200+ {
201+ # Enter git root
202+ # TODO support mono-repo, which may have multiple package.json files
203+ pushd $( git rev-parse --show-toplevel)
204+ local version=${NEW_VERSION:- $(current_version)}
205+ if [[ -z $( git status --short) ]]; then
206+ ${CMD_PREFIX} git add package.json CHANGELOG.md &&
207+ ${CMD_PREFIX} git commit -m " ${QUOTE} Version ${version}${QUOTE} " &&
208+ [[ -z " ${CMD_PREFIX} " ]] && {
209+ sed -e " 1,/^${version// ./ \\ .} /d" \
210+ -e " /^=/d" \
211+ -e " /^$/d" \
212+ -e " /^[0-9]/,\$ d" \
213+ CHANGELOG.md \
214+ | git tag -a " ${version} " --file=-
215+ }
216+ # print the command instead of executing it
217+ [[ -n " ${CMD_PREFIX} " ]] &&
218+ echo sed -e " '1,/^${version// ./ \\ .} /d'" \
219+ -e " '/^=/d'" \
220+ -e " '/^$/d'" \
221+ -e " '/^[0-9]/,\$ d'" \
222+ CHANGELOG.md \
223+ \| git tag -a " '${version} '" --file=-
224+ fi
225+ [[ $? == 0 ]] && ${CMD_PREFIX} git push && ${CMD_PREFIX} git push --tags
226+
227+ [[ $? == 0 ]] && ${CMD_PREFIX} npm publish
184228
185- git add package.json CHANGELOG.md
186- git commit -m " Version ${NEW_VERSION} "
187- sed -e " 1,/^${NEW_VERSION// ./ \\ .} /d" \
188- -e " /^=/d" \
189- -e " /^$/d" \
190- -e " /^[0-9]/,\$ d" \
191- CHANGELOG.md \
192- | git tag -a " ${NEW_VERSION} " --file=-
229+ [[ -z ${CMD_PREFIX} ]] && echo " Remember to create a new release in github: ${$(git remote get-url origin)% .git} /releases"
193230
194231 popd
195232}
196233
197234verify_dependencies
198235process_arguments $*
199- update_version
236+ [[ -n " ${NEW_VERSION} " ]] && update_version
237+ [[ -n " ${PUBLISH} " ]] && publish
0 commit comments