Skip to content

Instantly share code, notes, and snippets.

@bymathias
Last active August 29, 2015 14:23
Show Gist options
  • Save bymathias/4898e212ef6163e899df to your computer and use it in GitHub Desktop.
Save bymathias/4898e212ef6163e899df to your computer and use it in GitHub Desktop.
Last run sh git changelog
#!/bin/bash
set -e
## Variables
GULP_TEST_FILES="gulpfile.js .gulp/**/*.js $npm_package_directories_test/*.js"
CHANGELOG_FILE="CHANGELOG.md"
TODAY=$(date +"%Y-%m-%d")
REQ_DEP=(git node npm gulp bower)
## Colorful logs
echo_section() { echo -e "\n\033[4;37m$@\033[0m\n"; } # Underline text
echo_success() { echo -e "\033[0;34m✔ $@ \033[0m"; } # Blue text
echo_error() { echo -e "\033[0;31m✖ $@ \033[0m"; } # Red text
## ==================================== ##
## Main function
## ==================================== ##
runs()
{
if [ $# -ne 1 ]
then
runs help
return
fi
case "$1" in
"help")
echo ""
echo " Usage: npm run [ help | test | release ]"
echo ""
;;
"test")
echo_section "Gulp and tests files syntax check..."
node_modules/gulp-jscs/node_modules/jscs/bin/jscs $GULP_TEST_FILES
node_modules/gulp-jshint/node_modules/jshint/bin/jshint $GULP_TEST_FILES
echo_section "Build '$npm_package_directories_dest' directory (development)..."
gulp default
echo_section "Run client tests (mocha-phantomjs)..."
gulp test
echo_success "Test successful"
;;
"release")
# Test it first
# runs test
echo_section "Bump project version..."
## Display infos
echo "Current Tag:" $npm_package_version
echo "Release Types: <newversion>|major|minor|patch|premajor|preminor|prepatch|prerelease"
## Prompt for version type
read -p "Type => " V_TYPE
## Bump the version and write the new data back to package.json
npm version -m "chore(release): v%s" $V_TYPE
echo_section "Generate changelog from git metadata"
## Create changelog from git log
generate_changelog $CHANGELOG_FILE $V_TYPE
echo_section "Build '$npm_package_directories_dest' directory (bundle and production)..."
## Compile for production
gulp bundle --production --homepage
## Cleanup
gulp clean:bundle
echo ""
echo_success "Release successful"
;;
*)
runs help
;;
esac
}
## ==================================== ##
## Helper functions
## ==================================== ##
check_dependencies()
{
local CMD=$(command -v $1)
if [[ -x $CMD ]]
then
echo_success "$1"
else
echo_error "$1 is required"
if [[ $1 == "npm" || "gulp" || "bower" ]]
then
echo "installing $1 globally..."
npm install -g $1 &> /dev/null
check_dependencies $1
fi
fi
}
check_git()
{
if [[ -d ".git" ]]
then
GIT_READY=true
echo_success "Git ready"
else
GIT_READY=false
echo_error "Not a git repository"
fi
}
generate_changelog()
{
## Get new git tag
local GIT_NEW_TAG=`git describe --abbrev=0 --tags`
local GIT_LOG_FORMAT="- %s [%h]($npm_package_repository_url/commit/%H \"view commit\")%n"
## Heading depending of the release type
[[ $2 == "major" ]] && HD="#" || HD="##"
## Backup the current changelog
mv $1 $TODAY-$1
## Create changelog file and add title
echo -e "\n$HD $GIT_NEW_TAG - $TODAY\n" > $1
## Append the log from the last release to the new changelog
git log \
--no-merges \
--date=short \
--pretty=format:"$GIT_LOG_FORMAT" v$npm_package_version..HEAD >> $1
## Append the old log to the new changelog
cat $TODAY-$1 >> $1
## Remove backup changelog
rm $TODAY-$1
## Changelog done !
echo "'$1' updated"
## Git add and commits the changelog
git add $1
git commit -m "docs(changelog): add changes for $GIT_NEW_TAG" &> /dev/null
}
## ==================================== ##
## Run it !
## ==================================== ##
echo_section "Check dependencies..."
for i in "${REQ_DEP[@]}"
do
check_dependencies $i
done
check_git
runs "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment