Skip to content

Instantly share code, notes, and snippets.

@mateutek
Created January 4, 2018 14:06
Show Gist options
  • Save mateutek/b8f3a7152059b8c4d63ef6f902768fa4 to your computer and use it in GitHub Desktop.
Save mateutek/b8f3a7152059b8c4d63ef6f902768fa4 to your computer and use it in GitHub Desktop.
SassWars
#!/usr/bin/env bash
#
# Approach:
# 1. Find variable declaration in the form of "$my-var: anyvalue"
# 2. Loop through found variables and find occurrences of each variable in all scss files
if [ -z "$1" ]; then
echo "Please specify a directory as the first argument."
exit 1
fi
if [ ! -d "$1" ]; then
echo "Not a valid directory."
exit 1
fi
echo "Finding unused variables. This might take some time..."
varNameChars='A-Za-z0-9_-'
vars=$(find "$1" -type f -name "*.scss" -exec grep -o "\$[$varNameChars]*:" {} \; | grep -o '[^:]*')
total=0
zero=0
one=0
other=0
for var in $vars; do
count=$(find "$1" -type f -name "*.scss" -exec grep -o " *$var.*" {} \; | wc -l)
echo "Occurrences of $var:$count"
echo "Occurrences of $var:$count" >> ./test.txt
if (( $count == 2 )) ; then
((zero++))
elif (( $count == 1 )) ; then
((one++))
else
((other++))
fi
((total++))
done
echo "=================="
echo "Never used $zero"
echo "Used once $one"
echo "Used more than once $other"
echo "Total variables $total"
##Print to File
echo "==================">> ./test.txt
echo "Never used $zero">> ./test.txt
echo "Used once $one">> ./test.txt
echo "Used more than once $other">> ./test.txt
echo "Total variables $total">> ./test.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment