def test_find_static_sass(self): files = find_static_scss() # Assert that it found all of our scss files. self.assertTrue( os.path.join(THIS_DIR, "app1", "static", "app1", "scss", "_include.scss") in files) self.assertTrue( os.path.join(THIS_DIR, "app2", "static", "app2", "scss", "_samedir.scss") in files) self.assertTrue( os.path.join(THIS_DIR, "app2", "static", "app2", "scss", "test.scss") in files) self.assertTrue( os.path.join( THIS_DIR, "app2", "static", "app2", "scss", "subdir", "_subdir.scss", ) in files) self.assertTrue( os.path.join(THIS_DIR, "app3", "static", "app3", "sass", "indent_test.sass") in files)
import os import re from django_sass import compile_sass, find_static_paths, find_static_scss import django os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings') django.setup() for file in find_static_scss(): matches = re.search(r"((_\w*\.scss)$)", file) if matches: continue compile_sass(inpath=file, outpath=file.replace('scss', 'css'), output_style="compressed", precision=8, source_map=True)
def handle(self, *args, **options) -> None: """ Finds all static paths used by the project, and runs sass including those paths. """ # Parse options. o_inpath = options["in"][0] o_outpath = options["out"][0] o_srcmap = options["g"] o_precision = options["p"] o_style = options["t"] # Watch files for changes if specified. if options["watch"]: try: self.stdout.write("Watching...") # Track list of files to watch and their modified time. watchfiles = {} while True: needs_updated = False # Build/update list of ALL scss files in static paths. for fullpath in find_static_scss(): prev_mtime = watchfiles.get(fullpath, 0) curr_mtime = os.stat(fullpath).st_mtime if curr_mtime > prev_mtime: needs_updated = True watchfiles.update({fullpath: curr_mtime}) # Recompile the sass if needed. if needs_updated: # Catch compile errors to keep the watcher running. try: compile_sass( inpath=o_inpath, outpath=o_outpath, output_style=o_style, precision=o_precision, source_map=o_srcmap, ) self.stdout.write("Updated files at %s" % time.time()) except sass.CompileError as exc: self.stdout.write(str(exc)) # Go back to sleep. time.sleep(3) except (KeyboardInterrupt, InterruptedError): self.stdout.write("Bye.") sys.exit(0) # Write css. self.stdout.write("Writing css...") compile_sass( inpath=o_inpath, outpath=o_outpath, output_style=o_style, precision=o_precision, source_map=o_srcmap, ) self.stdout.write("Done.")