Skip to content

Commit f231548

Browse files
committed
🐛 FIX: pre-commit hook for file creation
1 parent ceb09fe commit f231548

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v2.3.0
3+
rev: v3.2.0
44
hooks:
55
- id: check-yaml
66
- id: end-of-file-fixer
@@ -12,11 +12,11 @@ repos:
1212
- id: black
1313

1414
- repo: https://github.com/mgedmin/check-manifest
15-
rev: "0.39"
15+
rev: "0.42"
1616
hooks:
1717
- id: check-manifest
1818

1919
- repo: https://gitlab.com/pycqa/flake8
20-
rev: 3.7.9
20+
rev: 3.8.3
2121
hooks:
2222
- id: flake8

scss_compile/__init__.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import hashlib
55
import os
66
from pathlib import Path
7+
from subprocess import check_call
78
import sys
89
from typing import Set
910

@@ -237,18 +238,14 @@ def run_compile(
237238
else:
238239
css_out_path = out_dir / (out_name + ".css")
239240
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):
244243
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+
252249
if not quiet:
253250
click.echo(f"Compiled: {str(scss_path)} -> {str(css_out_path)}")
254251

@@ -262,5 +259,21 @@ def run_compile(
262259

263260
if changed_files:
264261
if not quiet:
265-
click.secho("File changed", fg="yellow")
262+
click.secho("File(s) changed", fg="yellow")
266263
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

Comments
 (0)