Skip to content

Instantly share code, notes, and snippets.

@manastungare
Last active October 9, 2017 00:29
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 manastungare/440c135037d9182abeb2978e6c566451 to your computer and use it in GitHub Desktop.
Save manastungare/440c135037d9182abeb2978e6c566451 to your computer and use it in GitHub Desktop.
sass.sh: A fully native file system watcher for SASS (uses libsass, sassc & fswatch)
#!/bin/bash
# --------------------------------------------------------------------------------------------------
# Watches a directory for changes to SCSS (SASS) files, and compiles them to CSS.
#
# The original SASS implementation is Ruby-based, which requires Ruby to be installed, which can
# often be a pain. Many other solutions using `gulp` or other build systems require many more
# dependencies, such as Node and `npm`.
#
# I wanted a fully-native pipeline, both, for performance and ease of deployment.
#
# This script uses `sassc`, a pure C wrapper for `libsass`, a pure C implementation of SASS. These
# can be installed directly as binaries by your favorite package manager on your favorite OS,
# without needing twenty other binaries and package managers.
#
# The `sassc` binary does only one thing, compiling SASS to CSS, and this wrapper does the watching,
# so you can integrate it into a broader workflow.
#
# To install all dependencies on a Mac:
# brew install fswatch sassc
#
# --------------------------------------------------------------------------------------------------
#
# Copyright (c) 2017, Manas Tungare.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# --------------------------------------------------------------------------------------------------
SOURCE_DIR=scss/
DESTINATION_DIR=css/
function sassc-compile-one-file {
SOURCE=$1
DESTINATION="`echo ${SOURCE} | sed 's_/scss/_/css/_g' | sed 's_.scss_.css_g'`"
echo -n "[$(date +"%H:%M:%S")]: ${SOURCE} → ${DESTINATION}: "
sassc --style compressed ${SOURCE} ${DESTINATION} && echo "OK" || echo "FAILED"
}
export -f sassc-compile-one-file # Need to export because `xargs` can’t call functions directly.
function sassc-compile-all-files {
for FILE in ${SOURCE_DIR}/*; do
sassc-compile-one-file ${FILE}
done
}
export -f sassc-compile-all-files # Need to export because `xargs` can’t call functions directly.
echo "Monitoring: ${SOURCE_DIR} → ${DESTINATION_DIR} …"
fswatch -o ${SOURCE_DIR} | xargs -n1 -I{} bash -c 'sassc-compile-all-files'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment