Skip to content

Instantly share code, notes, and snippets.

@dimabory
Last active September 4, 2019 12:30
Show Gist options
  • Save dimabory/6a1f5d272e971dc2167d3b814713df3b to your computer and use it in GitHub Desktop.
Save dimabory/6a1f5d272e971dc2167d3b814713df3b to your computer and use it in GitHub Desktop.
The following script installs and configure `commitlint` and `husky` into your project.
#!/usr/bin/env bash
#
# Author: Dmytro Borysovskyi ([email protected])
# Date : 04.09.2019
#
# Description: The following script installs and configures `commitlint` and `husky` into your project.
#
VERSION=0.0.2
# just a helper for echo
function print {
echo -e "$1"
}
# colorize console
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
CWD=$0
DIR=$1
#
# Validate argument (path to repo)
#
shift $((OPTIND - 1))
if [ ! "${DIR}" ]
then
print "${RED}Please provide the repository path."
exit 2
fi
if [ ! -d "${DIR}" ]
then
print "${RED}${DIR}: No such directory"
exit 2
fi
cd $DIR || exit
print "${GREEN}I'm going to update your project:"
print "๐Ÿ“‚ ${PWD}${NC}\n"
# prompt whether to proceed or not
read -e -p "Are you sure you want to continue? [Y/n] " YN </dev/tty
[[ $YN != "y" && $YN != "Y" && $YN != "" ]] && exit
print "\nChecking your environment..."
print "node version $(node -v)"
print "npm version $(npm -v)"
git --version
print "\nSeems like we are ready to proceed, so let's do it. Please fasten your seatbelt ๐Ÿš€๐Ÿš€๐Ÿš€ \n"
npm install --save-dev --loglevel=error --unsafe-perm=true --allow-root @commitlint/cli @commitlint/config-conventional husky
COMMITLINT_CONFIG_FILENAME="commitlint.config.js"
COMMITLINT_CONFIG_CONTENT="module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'type-enum': [
2,
'always',
[
'build',
'chore',
'ci',
'docs',
'feat',
'fix',
'perf',
'refactor',
'revert',
'style',
'test'
]
]
}
}"
printf '%s' "$COMMITLINT_CONFIG_CONTENT" > "$PWD/$COMMITLINT_CONFIG_FILENAME"
HUSKY_CONFIG='husky: {"hooks": {"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"}}'
node -p "JSON.stringify(
(({ scripts: { prepush, ...scripts }, ...props }) => ({
...props,
scripts,
...{
husky: {
hooks: {
'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS',
},
},
},
}))(require('./package.json')),
null,
2,
)" | cat > package-copy.json && mv package-copy.json package.json
print "${GREEN} Great! Everything is ready. Enjoy ๐Ÿ˜Œ"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment