summaryrefslogtreecommitdiffstats
path: root/bin/sassw
blob: e580f029c2c609f9ab0a3b8c80c327df732afeff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#! /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
import time

filename = sys.argv[1]

def build():
    global sources, directories
    subprocess.call(['sassc', '-mauto', filename, filename.replace('.scss', '.css')])
    basepath = os.path.abspath(os.path.dirname(filename))
    sources = [os.path.abspath(os.path.join(basepath, 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='')
            t0 = time.time()
            build()
            print(f' ({time.time() - t0:.2f}s)')

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()