-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscripts.sh
96 lines (81 loc) · 2.78 KB
/
scripts.sh
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
#!/bin/bash
# strict mode http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'
function git_require_clean_work_tree(){
# Update the index
git update-index -q --ignore-submodules --refresh
err=0
# Disallow unstaged changes in the working tree
if ! git diff-files --quiet --ignore-submodules --
then
echo >&2 "You have unstaged changes."
git diff-files --name-status -r --ignore-submodules -- >&2
err=1
fi
# Disallow uncommitted changes in the index
if ! git diff-index --cached --quiet HEAD --ignore-submodules --
then
echo >&2 "Your index contains uncommitted changes."
git diff-index --cached --name-status -r --ignore-submodules HEAD -- >&2
err=1
fi
if [ $err = 1 ]
then
echo >&2 "Please commit or stash them."
exit 1
fi
}
function find_changelog_file(){
# find the changelog file
CHANGELOG=""
if test "$CHANGELOG" = ""; then
CHANGELOG=$(ls | egrep '^(change|history)' -i | head -n1)
if test "$CHANGELOG" = ""; then
CHANGELOG='CHANGELOG.md';
fi
fi
echo $CHANGELOG
}
function find_last_git_tag(){
echo $(git tag -l | sort -V | tail -n 1)
}
# based on https://github.com/tj/git-extras/blob/master/bin/git-changelog
function generate_git_changelog(){
GIT_LOG_OPTS="--no-merges"
local DATE=$(date +'%Y-%m-%d')
local HEAD='## '
# get the commits between the most recent tag and the second most recent
local lasttag=$(find_last_git_tag)
local version=$(git describe --tags --abbrev=0 $lasttag 2>/dev/null)
local previous_version=$(git describe --tags --abbrev=0 $lasttag^ 2>/dev/null)
if test -z "$version"; then
local head="$HEAD$DATE"
local changes=$(git log $GIT_LOG_OPTS --pretty="format:* %s%n" 2>/dev/null)
else
local head="$HEAD$version | $DATE"
local changes=$(git log $GIT_LOG_OPTS --pretty="format:* %s%n" $previous_version..$version 2>/dev/null)
fi
local CHANGELOG=$(find_changelog_file)
# insert the changes after the header (assumes markdown)
# this shells out to node b/c I couldn't figure out how to do it with awk
local tmp_changelog=/tmp/changelog
node -e "console.log(require('fs').readFileSync(process.argv[1]).toString().replace(/(#.*?\n\n)/, '\$1' + process.argv.slice(2).join('\n') + '\n\n'))" $CHANGELOG $head $changes > $tmp_changelog
# open the changelog in the editor for editing
test -n "$EDITOR" && $EDITOR $tmp_changelog
mv $tmp_changelog $CHANGELOG
}
function git_ammend_tag(){
git add $(find_changelog_file)
git commit --amend --no-edit --no-verify
git tag $(find_last_git_tag) -f
}
function npm_release(){
local version
if [ -z ${1:-} ]; then
version="patch"
else
version="$1"
fi
npm version $version && generate_git_changelog && git_ammend_tag && npm run gitPush && npm publish
}