Skip to content

Instantly share code, notes, and snippets.

@colelawrence
Created July 14, 2020 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colelawrence/df5cc84eda481173effe839eb732724b to your computer and use it in GitHub Desktop.
Save colelawrence/df5cc84eda481173effe839eb732724b to your computer and use it in GitHub Desktop.
pre-commit configs which basically does what checks both rust and typescript files hmm...
git config core.hooksPath .githooks/
chmod +x .githooks/pre-commit
{
"private": true,
"scripts": {
"postinstall": "sh ./install-hooks.sh"
},
"devDependencies": {
"husky": "^4.2.5",
"lint-staged": "^10.2.11",
"prettier": "^2.0.5"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged",
"post-commit": "rustfmt && prettier --write && git update-index --again"
}
},
"lint-staged": {
"*.rs": "rustfmt",
"*.{ts,js,scss}": "prettier --write"
}
}
{
"private": true,
"scripts": {
"postinstall": "sh ./install-hooks.sh"
}
}
#!/bin/sh
RESET="\033[0m"
GREEN="\033[0;32m"
BLUE="\033[0;34m"
TS_FILES=$(git diff --cached --name-only --diff-filter=ACMR "*.js" "*.ts" | sed 's| |\\ |g')
if ! [ -z "$TS_FILES" ]; then
echo "$TS_FILES" | xargs git stash push --keep-index --
# Prettify all selected files
echo "$TS_FILES" | xargs ./web_ui/node_modules/.bin/prettier --config ./web_ui/.prettierrc.toml --write
# Add back the modified/prettified files to staging
echo "$TS_FILES" | xargs git add
git stash pop
fi
RS_FILES=$(git diff --cached --name-only --diff-filter=ACMR "*.rs" | sed 's| |\\ |g')
if ! [ -z "$RS_FILES" ]; then
# echo "PWD: $(pwd)"
# echo "Changes: $RS_FILES"
git status | cat
# echo "${BLUE}next git stash${RESET}"
echo "$RS_FILES" | xargs git stash push --keep-index --
# echo "${GREEN}git stash done${RESET}"
# git status | cat
# echo "${BLUE}git diff${RESET}"
# git diff | cat
# echo "${BLUE}next rustfmt${RESET}"
# Rustfmt all selected files
echo "$RS_FILES" | xargs rustfmt
# echo "${GREEN}rustfmt done${RESET}"
# git status | cat
# echo "${BLUE}git diff${RESET}"
# git diff | cat
# echo "${BLUE}next git add${RESET}"
# Add back the modified/prettified files to staging
echo "$RS_FILES" | xargs git add
# echo "${GREEN}git add done${RESET}"
# git status | cat
# echo "${BLUE}git diff${RESET}"
# git diff | cat
# echo "${BLUE}next git stash pop${RESET}"
git stash pop
# echo "${GREEN}git stash pop done${RESET}"
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment