-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.sh
95 lines (82 loc) · 3.24 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
#!/bin/bash
# strict mode http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'
# ensure a modern version of sort is installed
# get the version of sort, find the line that has the version number
# get the last word with awk, which is the version number
# if that version number is greater than 7, we're good,
# else, we need to install coreutils
# ensure the gnu version of sort is avaliable
if [ $(PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH" && echo "$(sort --version | grep '(GNU' | awk 'NF>1{print $NF}') > 7" | bc) != 1 ]; then
echo "Installing coreutils to give you a modern shell experience"
brew install coreutils
fi
function lint(){
eslint --no-eslintrc --config .eslintrc "${@-.}" --ext .jsx --ext .js --ext .es6
}
function git_require_clean_work_tree(){
git diff --exit-code
}
function find_changelog_file(){
# find the changelog file
local 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(){
# ensure the gnu version of sort is avaliable
PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH" && 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=$(date +'%Y-%m-%d')
local HEAD='## '
# get the commits between the most recent tag and the second most recent
local lasttag
lasttag=$(find_last_git_tag)
local version
version=$(git describe --tags --abbrev=0 "$lasttag" 2>/dev/null)
local previous_version
previous_version=$(git describe --tags --abbrev=0 "$lasttag^" 2>/dev/null)
# if we don't have a previous version to look at
if test -z "$version"; then
local head="$HEAD$DATE"
local changes
changes=$(git log $GIT_LOG_OPTS --pretty="format:* %s%n" 2>/dev/null)
# the more common case, there's a version to git the changes betwen
else
local head="$HEAD$version | $DATE"
# tail to get remove the first line, which will always just be the version commit
# awk to remove empty lines
local changes
changes=$(tail -n +2 <<< "$(git log $GIT_LOG_OPTS --pretty="format:* %s%n" "$previous_version..$version" 2>/dev/null)" | awk NF)
fi
local CHANGELOG
CHANGELOG=$(find_changelog_file)
echo "Editing $CHANGELOG"
# 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
${EDITOR:-'vi'} $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 access="${1-public}"
local version="${2-patch}"
npm version "$version" && generate_git_changelog && git_ammend_tag && npm run gitPush && npm publish --access "$access"
}