Skip to content

Instantly share code, notes, and snippets.

@patrickfuller
Last active September 18, 2019 16:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickfuller/c73d460ef0a45cf32915 to your computer and use it in GitHub Desktop.
Save patrickfuller/c73d460ef0a45cf32915 to your computer and use it in GitHub Desktop.
SCSS compilation pre-commit hook
#!/bin/bash
# When SCSS is changed in a commit, this compiles and commits the affected css.
# This script assumes a project directory of the form:
# main.scss
# main.css
# scss
# | file_1.scss
# | ...
# | file_n.scss
# Where `main.scss` imports other files, and `main.css` is the compiled product.
# The below command is most of the work here. It's complicated, so here's an explanation:
# * Get a list of all diffed files (various flags to remove frills)
# * Filter for .scss files
# * Use `dirname` to get the directory path
# * If the file is in a "scss" directory, go up one level
# * Use `uniq` to ensure we're not double counting directories
paths=`git --no-pager diff --cached --name-only --diff-filter=ACM \
| grep ".scss$" \
| xargs -L 1 dirname 2>/dev/null \
| sed 's/\/scss$//' \
| uniq`
for path in $paths; do
scss $path/main.scss > $path/main.css
git add $path/main.css
echo "Generated and added $path/main.css"
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment