-
Notifications
You must be signed in to change notification settings - Fork 2
/
npm-release
executable file
·112 lines (91 loc) · 2.73 KB
/
npm-release
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
#
# This script allows (me) to automate releases of npm libraries with GitHub.
# It relies on `hub` (https://hub.github.com/), `npm version`, and the
# `format-release-notes` script (see `scripts/format-release-notes` in this
# repository).
set -e -o pipefail
NAME='npm-release'
USAGE='[help|major|minor|patch]'
LONG_USAGE="git $NAME help
print this help message.
git $NAME major
create a major version.
git $NAME minor
create a minor version.
git $NAME patch
create a patch version.
"
source "$(git --exec-path)/git-sh-setup"
REMOTE='origin'
BRANCH=$(git symbolic-ref --short -q HEAD)
require_user_confirmation() {
read -p "Continue? [y/N] " -n 1 -r
echo
if [[ ! "$REPLY" =~ ^[Yy]$ ]]; then
die "Aborted: user didn't confirm."
fi
}
npm_release() {
local type="$1"
local old_version
local new_version
local tmp_release_notes
local git_log
cd_to_toplevel
if [[ ! -f "package.json" ]]; then
die "Aborted: this script only works with JS and Node projects."
fi
require_clean_work_tree rebase "Please commit or stash them."
if [[ -f "CHANGELOG.md" ]]; then
echo "Warning: this project contains a CHANGELOG."
require_user_confirmation
fi
old_version=$(node -pe "require('./package.json').version")
echo "--> found current npm version: $old_version"
new_version=$(npm --no-git-tag-version version "$type")
git checkout -- package.json
# `npm version` also updates the lockfile (if any).
if [[ -f 'package-lock.json' ]]; then
git checkout -- package-lock.json
fi
echo "--> new $type version will be: $new_version"
require_user_confirmation
new_version=$(npm version "$type")
echo "--> created new npm version: $new_version"
tmp_release_notes=$(mktemp)
git_log=$(git log --format="%s" "$old_version"..."$new_version"^)
git_log=$(format-release-notes "$git_log" "./package.json")
cat <<EOF > "$tmp_release_notes"
$new_version $(date +'(%Y-%m-%d)')
# git log: $old_version...$new_version^
$git_log
EOF
# We could pass the message above to `hub release` but sometimes writing good
# release notes takes time. Let's push and publish the release once we have
# the notes (we have to push before creating the release, otherwise GitHub
# will make a tag and we don't want that).
git_editor "$tmp_release_notes"
git push --quiet "$REMOTE" "$BRANCH" --tag
echo "--> pushed changes to GitHub"
hub release create --edit -F "$tmp_release_notes" "$new_version"
rm -f "$tmp_release_notes"
}
if [[ "$#" -eq 0 ]]; then
usage
fi
for arg in "$@"
do
case "$arg" in
help)
git "$NAME" -h ;;
patch)
npm_release "patch" ;;
minor)
npm_release "minor" ;;
major)
npm_release "major" ;;
*)
;;
esac
done