Last active
March 28, 2025 10:30
-
-
Save heaven/4f65f5a05789f00e345467f8e3683a53 to your computer and use it in GitHub Desktop.
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="" | |
TAILWIND_FLAGS="" | |
print_help() { | |
echo "Usage: $0 -i <input_file> -o <output_file> [<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 | |
;; | |
*) | |
# Collect all other arguments for Tailwind CLI | |
TAILWIND_FLAGS="$TAILWIND_FLAGS $1" | |
shift | |
;; | |
esac | |
done | |
if [ -z "$INPUT_FILE" ] || [ -z "$OUTPUT_FILE" ]; then | |
print_help | |
exit 1 | |
fi | |
render_file() { | |
# Memoize the original directory | |
ORIGINAL_DIR=$(pwd) | |
# Get absolute path of input file | |
cd "$(dirname "$INPUT_FILE")" || exit 1 | |
ABS_INPUT_DIR=$(pwd) | |
ABS_INPUT_FILE="$ABS_INPUT_DIR/$(basename "$INPUT_FILE")" | |
# Ensure we operate from the original directory | |
cd "$ORIGINAL_DIR" || exit 1 | |
cd "$(dirname "$OUTPUT_FILE")" || exit 1 | |
# Get absolute path of output file | |
ABS_OUTPUT_DIR=$(pwd) | |
ABS_OUTPUT_FILE="$ABS_OUTPUT_DIR/$(basename "$OUTPUT_FILE")" | |
# Change to the input file's directory (if different) | |
if [ "$ABS_INPUT_DIR" != "$ORIGINAL_DIR" ]; then | |
cd "$ABS_INPUT_DIR" || exit 1 | |
fi | |
# Run ERB and Tailwind processing | |
erb "$ABS_INPUT_FILE" | npx @tailwindcss/cli -i - -o "$ABS_OUTPUT_FILE" $TAILWIND_FLAGS | |
# Switch back to the original directory | |
cd "$ORIGINAL_DIR" || exit 1 | |
} | |
render_file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment