4
4
import hashlib
5
5
import os
6
6
from pathlib import Path
7
+ from subprocess import check_call
7
8
import sys
8
9
from typing import Set
9
10
@@ -237,18 +238,14 @@ def run_compile(
237
238
else :
238
239
css_out_path = out_dir / (out_name + ".css" )
239
240
if not test_run :
240
- if css_out_path .exists ():
241
- if css_str != css_out_path .read_text (encoding = encoding ):
242
- changed_files = True
243
- else :
241
+
242
+ if update_file (css_out_path , css_str , encoding ):
244
243
changed_files = True
245
- css_out_path .write_text (css_str , encoding = encoding )
246
- if sourcemap :
247
- if not (out_dir / (scss_path .name + ".map.json" )).exists ():
248
- changed_files = True
249
- (out_dir / (scss_path .name + ".map.json" )).write_text (
250
- sourcemap_str , encoding = encoding
251
- )
244
+ if sourcemap and update_file (
245
+ out_dir / (scss_path .name + ".map.json" ), sourcemap_str , encoding
246
+ ):
247
+ changed_files = True
248
+
252
249
if not quiet :
253
250
click .echo (f"Compiled: { str (scss_path )} -> { str (css_out_path )} " )
254
251
@@ -262,5 +259,21 @@ def run_compile(
262
259
263
260
if changed_files :
264
261
if not quiet :
265
- click .secho ("File changed" , fg = "yellow" )
262
+ click .secho ("File(s) changed" , fg = "yellow" )
266
263
sys .exit (exit_code )
264
+
265
+
266
+ def update_file (path , text , encoding ) -> bool :
267
+
268
+ if not path .exists ():
269
+ path .write_text (text , encoding = encoding )
270
+ # this is required, to ensure creations are picked up by pre-commit
271
+ # TODO ignore for non-git repositories?
272
+ check_call (["git" , "add" , "--intent-to-add" , str (path )])
273
+ return True
274
+
275
+ if text != path .read_text (encoding = encoding ):
276
+ path .write_text (text , encoding = encoding )
277
+ return True
278
+
279
+ return False
0 commit comments