Skip to content

Instantly share code, notes, and snippets.

@pixelass
Last active December 31, 2015 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixelass/7964010 to your computer and use it in GitHub Desktop.
Save pixelass/7964010 to your computer and use it in GitHub Desktop.
Allow watching files from bash using the default lessc
## compile less files from bash (shell)
## v 0.0.1-SNAPSHOT
#########################################################
## This script will compile ONE less-file to ONE css-file
## allow to compile "once" or "watch"
#########################################################
## DEPENDENCIES:
## observr https://github.com/kevinburke/observr
## lessc https://github.com/less/less.js/
#########################################################
## $ (sudo) gem install observr
## $ npm install (-g) less
#########################################################
################### START CONFIG ########################
#########################################################
## set files and path
## modify these to fit your needs
lessPath="less"
cssPath="css"
inputFile="input.less"
outputFile="output.css"
#########################################################
#################### END CONFIG #########################
#########################################################
#########################################################
## NO MODIFICATIONS SHOULD BE NEEDED BEYOND THIS POINT ##
#########################################################
#########################################################
################### START SCRIPT ########################
#########################################################
## build filePath + fileName
INPUT="${lessPath}/${inputFile}"
OUTPUT="${cssPath}/${outputFile}"
## flag to check if command was sucessfully executed
COMPLETED="NO"
LINEBREAK="--------------------------------------------------------------------------------"
## define PARAMETER
PARAMETER=""
if [ -n "${2}" ] ; then
if [ ${2} = "-m" ] || [ ${2} = "-mediaquery" ] ; then
PARAMETER="--line-numbers=mediaquery"
elif [ ${2} = "-c" ] || [ ${2} = "-comments" ] ; then
PARAMETER="--line-numbers=comments"
elif [ ${2} = "-a" ] || [ ${2} = "-all" ] ; then
PARAMETER="--line-numbers=all"
fi
fi
## will compile files with given parameters
function compile-less () {
echo "start compiling less files"
echo "${LINEBREAK}"
lessc ${PARAMETER} ${INPUT} ${OUTPUT} && COMPLETED="YES"
if [ ${COMPLETED} = "YES" ] ; then
echo "${INPUT} > ${OUTPUT}"
echo "${LINEBREAK}"
else
echo "seems like something went wrong"
fi
}
## will compile files with given parameters
## and then watch for changes
function watch-less () {
## fist compile the files
compile-less
## use observr to watch files
observr -e "; \
puts 'start watching less files'; \
puts '-' * 80; \
watch('.*\.less') \
{ cmd = 'lessc ${PARAMETER} ${INPUT} ${OUTPUT}'; \
log = 'echo \"${INPUT} > ${OUTPUT}\"'; \
o = \`#{cmd}\`; \
l = \`#{log}\`; \
puts o << l;}"
}
## provide error message
function illegal-option (){
echo "illegal option"
echo "use '--help' to show options"
}
## output help when requested
function show-help () {
echo -e "c_LESS.sh [OPTION] <PARAMETER>"
echo -e ""
echo -e "OPTION---------------------HELP------------------------------"
echo -e "watch watch files"
echo -e "once compile files once"
echo -e ""
echo -e "PARAMETER------------------HELP------------------------------"
echo -e "-c, --comments output comments"
echo -e "-m, --mediaquery output mediaquery"
echo -e "-a, --all output comments & mediaquery"
echo -e ""
echo -e "-------------------------------------------------------------"
echo -e "-h, --help show this info"
echo -e "-------------------------------------------------------------"
}
## keep this small and modular ##
#################################
## only execute if option is set
if [ -n "${1}" ] ; then
echo ""
if [ ${1} = "-h" ] || [ ${1} = "--help" ] ; then
show-help
elif [ ${1} = "watch" ] ; then
watch-less
elif [ ${1} = "once" ] ; then
compile-less
else
illegal-option
fi
## otherwise display help
else
show-help
fi
#########################################################
#################### END SCRIPT #########################
#########################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment