Last active
March 28, 2025 10:31
-
-
Save heaven/40bc3f997011e21495505facc5a611e6 to your computer and use it in GitHub Desktop.
Watch and render .css.erb templates with Tailwind
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
INPUT_FILE="" | |
OUTPUT_FILE="" | |
WATCH=false | |
TAILWIND_FLAGS="" | |
TAILWIND_SCRIPT="$(dirname "$0")/tailwind-erb" | |
TAILWIND_PID=0 | |
print_help() { | |
echo "Usage: $0 -i <input_file> -o <output_file> [--watch] [<tailwind_flags>]" | |
} | |
# Parse arguments | |
while [ "$#" -gt 0 ]; do | |
case "$1" in | |
-h|--help) | |
print_help | |
exit 0 | |
;; | |
-i|--input) | |
INPUT_FILE="$2" | |
shift 2 | |
;; | |
-o|--output) | |
OUTPUT_FILE="$2" | |
shift 2 | |
;; | |
--watch) | |
WATCH=true | |
shift | |
;; | |
*) | |
TAILWIND_FLAGS="$TAILWIND_FLAGS $1" | |
shift | |
;; | |
esac | |
done | |
if [ -z "$INPUT_FILE" ] || [ -z "$OUTPUT_FILE" ]; then | |
print_help | |
exit 1 | |
fi | |
get_modified_time() { | |
if command -v stat >/dev/null 2>&1; then | |
if stat --version >/dev/null 2>&1; then | |
stat -c %Y "$INPUT_FILE" 2>/dev/null | |
else | |
stat -f %m "$INPUT_FILE" 2>/dev/null | |
fi | |
else | |
echo "0" | |
fi | |
} | |
start_tailwind() { | |
if [ "$TAILWIND_PID" -eq 0 ]; then | |
echo "Starting tailwind process..." | |
local flags="$TAILWIND_FLAGS" | |
if [ "$WATCH" = "true" ]; then | |
flags="--watch $flags" | |
fi | |
"$TAILWIND_SCRIPT" -i "$INPUT_FILE" -o "$OUTPUT_FILE" $flags & | |
TAILWIND_PID=$! | |
fi | |
} | |
stop_tailwind() { | |
if [ "$TAILWIND_PID" -ne 0 ]; then | |
echo "Stopping tailwind process and its children..." | |
disown "$TAILWIND_PID" 2>/dev/null | |
pkill -P "$TAILWIND_PID" 2>/dev/null | |
kill "$TAILWIND_PID" 2>/dev/null | |
TAILWIND_PID=0 | |
fi | |
} | |
if [ "$WATCH" = "true" ]; then | |
echo "Watching $INPUT_FILE for changes..." | |
LAST_MODIFIED=$(get_modified_time "$INPUT_FILE") | |
start_tailwind | |
while true; do | |
sleep 1 | |
NEW_MODIFIED=$(get_modified_time "$INPUT_FILE") | |
if [ "$NEW_MODIFIED" -gt "$LAST_MODIFIED" ]; then | |
LAST_MODIFIED=$NEW_MODIFIED | |
stop_tailwind | |
start_tailwind | |
fi | |
done | |
else | |
"$TAILWIND_SCRIPT" -i "$INPUT_FILE" -o "$OUTPUT_FILE" $TAILWIND_FLAGS | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment