From e107c8e2106ead1ebb4d2708d278ffc310e378ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Tue, 20 Aug 2019 13:23:05 +0200 Subject: [PATCH] add wrapper script adding inotify support to sassc --- bin/sassw | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 bin/sassw diff --git a/bin/sassw b/bin/sassw new file mode 100755 index 0000000..870bdde --- /dev/null +++ b/bin/sassw @@ -0,0 +1,38 @@ +#! /usr/bin/python3 +# +# Run sassc and watch for changes in source files (using inotitfy) to rebuild +# automatically. +# +# Usage: sassw INPUT +# (output will automatically be INPUT with .scss changed to .css) + +import json +import os +import pyinotify +import subprocess +import sys + +filename = sys.argv[1] + +def build(): + global sources, directories + subprocess.call(['sassc', '-mauto', filename, filename.replace('.scss', '.css')]) + sources = [os.path.abspath(x) for x in json.load(open(filename.replace('.scss', '.css.map')))['sources']] + directories = set([os.path.dirname(x) for x in sources]) + +class EventManager(pyinotify.ProcessEvent): + def process_default(self, event): + if event.pathname in sources: + filename = os.path.basename(event.pathname) + print(f'{filename} changed, building', end='') + build() + print('.') + +build() +wm = pyinotify.WatchManager() +notifier = pyinotify.Notifier(wm, default_proc_fun=EventManager()) +for directory in directories: + wm.add_watch(directory, pyinotify.IN_CLOSE_WRITE) +notifier.coalesce_events() +print('>>> Sassw is watching for changes. Press Ctrl-C to stop.') +notifier.loop()