Created
September 18, 2011 17:00
-
-
Save Port3M5/1225273 to your computer and use it in GitHub Desktop.
A bash script that compiles coffeescript and uglifies it. Requires node.js coffeescript and uglify-js
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
| #!/bin/bash | |
| function printHelp | |
| { | |
| echo "-h displays this page" | |
| echo "-v displays the version information" | |
| echo "-f compiles and minifies a single file" | |
| echo "-d compiles and minifies all files in a directory" | |
| echo "-i checks dependendancies and installs them if node is installed" | |
| } | |
| function printVersion | |
| { | |
| echo "Version 1.0" | |
| } | |
| function checkDependancies | |
| { | |
| # If any of the dependancies are missing install them | |
| if [ -z $(which coffee) ] || [ -z $(which uglify-js) ]; then | |
| hash node 2>&- || { echo >&2 "node.js is required. Aborting"; exit 1; } | |
| hash npm 2>&- || { echo >&2 "npm is required to install dependencies. Aborting"; exit 1; } | |
| npm install -g coffee-script uglify-js | |
| else | |
| echo "Nothing to do" | |
| fi | |
| } | |
| function compileAndMinify | |
| { | |
| # if the file exists | |
| if [ -f $1 ]; then | |
| # Get the dirname, filename, extension and define the names for the new files | |
| dir=$(dirname $1) | |
| file=$(basename $1) | |
| ext=${file##*.} | |
| file=${file%.*} | |
| jsfile="${dir}/${file}.js" | |
| shortjsfile="${dir}/${file}.min.js" | |
| # if the extension is coffee then compile the coffeescript | |
| if [ $ext == "coffee" ]; then | |
| coffee -c $1 | |
| # if the coffeescript file exists then uglify it. | |
| if [ -f $jsfile ]; then | |
| uglifyjs -o $shortjsfile $jsfile | |
| echo "Compiled as ${jsfile} and Uglified as ${shortjsfile}" | |
| else | |
| echo "${jsfile} does not exist" | |
| exit 1; | |
| fi | |
| else | |
| echo "File (${file}) should have the '.coffee' extension" | |
| exit 1; | |
| fi | |
| else | |
| echo "${1} does not exist!" | |
| exit 1; | |
| fi | |
| } | |
| function parseDirectory | |
| { | |
| # if the directory exists | |
| shopt -s nullglob | |
| if [ -d "$1" ]; then | |
| # Compile and Uglify files with the coffee extension | |
| for f in ${1}/*.coffee | |
| do | |
| compileAndMinify $f | |
| done | |
| else | |
| echo "${1} does not exist" | |
| fi | |
| } | |
| # Handles the options, must be after the functions | |
| while getopts "hvf:d:i" option; do | |
| case "$option" in | |
| h) printHelp 0;; | |
| v) printVersion 0;; | |
| f) compileAndMinify "$OPTARG";; | |
| d) parseDirectory "$OPTARG";; | |
| i) checkDependancies 0;; | |
| [?]) printHelp 0;; | |
| esac | |
| done | |
| exit 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment