Watching LESS files for changes over SSHFS Print

  • 3

Using SSFS to mounts drives into a development environment is a great way to streamline management of multiple accounts.  Unfortunately the inotify tools recommended for automating LESSC compilation do not work over SSH connections.  The next best solution is to create a daemon to monitor and compare file changes for this specific purpose.

The following script can be placed into a bash script and used as a replacement for the common lesscwatch script found around the web.

Note: This particular script is expects the source to be either a less file, or a folder containing only .less files.  This script does not process folders recursively.

Usage:
lesscmonitor source_file target_file
lesscmonitor source_folder target_folder

Store the following in a file labeled lesscmonitor.

#!/bin/bash

# Detect changes in .less file and automatically compile into .css
[[ "$1" && "$2" ]] || { echo "Specify both .less and .css files"; exit 1; }

echo "Watching $1 > $2";
while sleep 1;
    do

      if [[ -d "$1" && -d "$2" || ! -e "$2" ]]; then

            for f in $(ls "$1"); do
                if [ ! ${f/\.less} = "$f" ]; then
                    tFile=${f/.less/.css}
                    if [[ ! -e "$2/$tFile" || "$1/$f" -nt "$2/$tFile" ]]; then
                        echo "Compiling $1/$f > $2/$tFile";
                        lessc "$1/$f" "$2/$tFile";
                    fi
                    tFile=${f/\.less/\.min.css}
                    if [[ ! -e "$2/$tFile" || "$1/$f" -nt "$2/$tFile" ]]; then
                        echo "Compiling Minified $1/$f > $2/$tFile";
                        lessc "$1/$f" "$2/$tFile" --clean-css="--s1 --advanced --compatibility=ie8";
                    fi
                fi
            done

        elif [[ -f "$1" && -f "$2" || ! -e "2" ]]; then

            if [ ! ${1/\.less} = "$1" ]; then
                if [[ ! -e "$2" || "$1" -nt "$2" ]]; then
                    if [ ${2/\.min.css} = "$2" ]; then
                        echo "Compiling $1 > $2";
                        lessc "$1" "$2";
                    else
                        echo "Compiling Minified $1 > $2";
                        lessc "$1" "$2" --clean-css="--s1 --advanced --compatibility=ie8";
                    fi
                fi
            fi

      else

            echo "Specify either a source and target file or folder.";
            exit 1;

      fi

    done

Was this answer helpful?

« Back