Skip to content

Instantly share code, notes, and snippets.

@simonauner
Last active March 14, 2023 22:57
Show Gist options
  • Save simonauner/484f4d965d442ada60a50e3d68dc6d28 to your computer and use it in GitHub Desktop.
Save simonauner/484f4d965d442ada60a50e3d68dc6d28 to your computer and use it in GitHub Desktop.
Are we typed yet? A bash script that counts percentage of files converted to .ts[x] in a folder
#!/usr/bin/env bash
# By Simon Aunér 2023
# https://gist.github.com/simonauner/484f4d965d442ada60a50e3d68dc6d28
# To install in usr bin folder, run:
# sudo ln -s /path/to/are_we_typed_yet.sh /usr/local/bin/are_we_typed_yet
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
# Print help section if first argument is -h or --help
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo "Usage: are_we_typed_yet [path]"
echo " are_we_typed_yet [-h | --help]"
echo " are_we_typed_yet [-v | --verbose]"
echo ""
echo " path: path to search for js-ish files"
echo " defaults to ./src"
exit 0
fi
# Find first arg that is not a flag
for arg in "$@"; do
if [[ $arg != -* ]]; then
FOLDER=$arg
break
fi
done
# If no folder is given, default to ./src
if [[ -z $FOLDER ]]; then
FOLDER="./src"
fi
# Find if verbose flag is set in any of the arguments
for arg in "$@"; do
if [[ $arg == "-v" || $arg == "--verbose" ]]; then
VERBOSE=true
break
fi
done
# Print a progress bar with the given percentage
# Thanks https://github.com/fearside/ProgressBar/
function print_progress_bar {
# Process data
let _width=50
let _progress=${1}
let _done=(${_progress}*${_width})/100
let _left=$_width-$_done
# Build progressbar string lengths
_fill=$(printf "%${_done}s")
_empty=$(printf "%${_left}s")
printf "\r[$GREEN${_fill// /■}${_empty// /□}$NC] ${_progress}%%"
}
function animate_progress_bar {
# Variables
_start=0
_end=$1
for number in $(seq ${_start} ${_end})
do
sleep 0.10
print_progress_bar ${number}
done
echo ""
}
# Find all files in given folder that match the given extension
# and are not in node_modules, static/libs or bower_components
# and write the number of files found to FIND_FILES_RETURN_VALUE.
# If verbose flag is set, print all files found.
FIND_FILES_RETURN_VALUE=0
function find_files_2 {
FILES=$(find $FOLDER \( -name \*.$1 \) \
-not \( -path "*node_modules*" -prune \) \
-not \( -path "*static/libs*" -prune \) \
-not \( -path "*bower_components*" -prune \))
# Print all files if verbose flag is set
if [[ $VERBOSE ]]; then
echo "$FILES"
fi
# "Return" number of files
FIND_FILES_RETURN_VALUE=$(echo "$FILES" | wc -l)
echo "$FIND_FILES_RETURN_VALUE .$1 files"
}
echo -e "Searching for js-ish files in $YELLOW$FOLDER$NC..."
echo "Found"
find_files_2 "js"
JS_COUNT=$FIND_FILES_RETURN_VALUE
find_files_2 "mjs"
MJS_COUNT=$FIND_FILES_RETURN_VALUE
find_files_2 "jsx"
JSX_COUNT=$FIND_FILES_RETURN_VALUE
find_files_2 "ts"
TS_COUNT=$FIND_FILES_RETURN_VALUE
find_files_2 "tsx"
TSX_COUNT=$FIND_FILES_RETURN_VALUE
echo "-------------------"
echo "Total: $((JS_COUNT + MJS_COUNT + JSX_COUNT + TS_COUNT + TSX_COUNT)) js-ish files"
echo "Out of all files, $((JS_COUNT + MJS_COUNT + JSX_COUNT)) are non-typed and $((TS_COUNT + TSX_COUNT)) are typed"
PERCENTAGE=$((100 * (TS_COUNT + TSX_COUNT) / (JS_COUNT + MJS_COUNT + JSX_COUNT + TS_COUNT + TSX_COUNT)))
animate_progress_bar $PERCENTAGE
echo -e "That's $GREEN$PERCENTAGE%$NC typed!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment