Skip to content

Commit ceb09fe

Browse files
committed
🐛 FIX: non-zero exit code on fie change
1 parent adce41f commit ceb09fe

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Options:
6565
-p, --precision INTEGER precision for numbers. [default: 5]
6666
-q, --quiet Remove stdout logging.
6767
-v, --verbose Increase stdout logging.
68+
--exit-code INTEGER Exit code when files changed. [default: 2]
6869
--test-run Do not delete/create any files.
6970
--config FILE Read default configuration from a file
7071
(allowed extensions: .json, .toml, .yml,
@@ -111,7 +112,7 @@ scss-compile:
111112
hash_filenames: true
112113
output_format: compressed
113114
partial_depth: 1
114-
translate: [tests/example_sass:tests/output_css]
115+
translate: ["tests/example_sass:tests/output_css"]
115116
```
116117

117118
## Usage

scss-compile-config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ scss-compile:
44
hash_filenames: true
55
output_format: compressed
66
partial_depth: 1
7-
translate: [tests/example_scss:tests/output_css]
7+
translate: ["tests/example_scss:tests/output_css"]

scss_compile/__init__.py

Lines changed: 31 additions & 6 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+
import sys
78
from typing import Set
89

910
import click
@@ -91,6 +92,13 @@ def config_provider(file_path, cmd_name):
9192
)
9293
@click.option("-q", "--quiet", is_flag=True, help="Remove stdout logging.")
9394
@click.option("-v", "--verbose", is_flag=True, help="Increase stdout logging.")
95+
@click.option(
96+
"--exit-code",
97+
default=2,
98+
type=int,
99+
show_default=True,
100+
help="Exit code when files changed.",
101+
)
94102
@click.option("--test-run", is_flag=True, help="Do not delete/create any files.")
95103
@click_config_file.configuration_option(
96104
provider=config_provider,
@@ -113,6 +121,7 @@ def run_compile(
113121
precision,
114122
quiet,
115123
verbose,
124+
exit_code,
116125
test_run,
117126
):
118127
"""Compile all SCSS files in the paths provided.
@@ -177,6 +186,7 @@ def run_compile(
177186
parent_dir = parent_dir.parent
178187

179188
compilation_errors = {}
189+
changed_files = False
180190
for scss_path in scss_paths:
181191

182192
out_dir = scss_path.parent
@@ -209,23 +219,33 @@ def run_compile(
209219

210220
out_name, _ = os.path.splitext(scss_path.name)
211221
if hash_filenames:
212-
# remove old hashes
213-
for path in out_dir.glob(out_name + "#*.css"):
214-
if verbose:
215-
click.secho(f"Removed: {str(path)}", fg="yellow")
216-
if not test_run:
217-
path.unlink()
218222
css_out_path = out_dir / (
219223
out_name
220224
+ "#"
221225
+ hashlib.md5(css_str.encode(encoding)).hexdigest()
222226
+ ".css"
223227
)
228+
# remove old hashes
229+
for path in out_dir.glob(out_name + "#*.css"):
230+
if path == css_out_path:
231+
continue
232+
if verbose:
233+
click.secho(f"Removed: {str(path)}", fg="yellow")
234+
if not test_run:
235+
changed_files = True
236+
path.unlink()
224237
else:
225238
css_out_path = out_dir / (out_name + ".css")
226239
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:
244+
changed_files = True
227245
css_out_path.write_text(css_str, encoding=encoding)
228246
if sourcemap:
247+
if not (out_dir / (scss_path.name + ".map.json")).exists():
248+
changed_files = True
229249
(out_dir / (scss_path.name + ".map.json")).write_text(
230250
sourcemap_str, encoding=encoding
231251
)
@@ -239,3 +259,8 @@ def run_compile(
239259

240260
if not quiet:
241261
click.secho("Compilation succeeded!", fg="green")
262+
263+
if changed_files:
264+
if not quiet:
265+
click.secho("File changed", fg="yellow")
266+
sys.exit(exit_code)

tests/test_run_compile.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ def test_help():
2121

2222

2323
def test_file(scss_folder):
24+
result = CliRunner().invoke(run_compile, [str(scss_folder / "example1.scss")])
25+
assert result.exit_code == 2, result.output
26+
assert (scss_folder / "example1.css").exists(), result.output
27+
28+
# should not change any files
2429
result = CliRunner().invoke(run_compile, [str(scss_folder / "example1.scss")])
2530
assert result.exit_code == 0, result.output
2631
assert (scss_folder / "example1.css").exists(), result.output
@@ -30,7 +35,7 @@ def test_file_hash(scss_folder):
3035
result = CliRunner().invoke(
3136
run_compile, [str(scss_folder / "example1.scss"), "--hash-filenames"]
3237
)
33-
assert result.exit_code == 0, result.output
38+
assert result.exit_code == 2, result.output
3439
assert len(list(scss_folder.glob("example1#*.css"))) == 1, result.output
3540
path = list(scss_folder.glob("example1#*.css"))[0]
3641

@@ -47,7 +52,7 @@ def test_file_sourcemap(scss_folder):
4752
result = CliRunner().invoke(
4853
run_compile, [str(scss_folder / "example1.scss"), "--sourcemap"]
4954
)
50-
assert result.exit_code == 0, result.output
55+
assert result.exit_code == 2, result.output
5156
assert (scss_folder / "example1.css").exists(), result.output
5257
assert (scss_folder / "example1.scss.map.json").exists(), result.output
5358

@@ -63,13 +68,13 @@ def test_partials(scss_folder):
6368
run_compile,
6469
[str(scss_folder / "partials" / "_example1.scss"), "--partial-depth=1"],
6570
)
66-
assert result.exit_code == 0, result.output
71+
assert result.exit_code == 2, result.output
6772
assert (scss_folder / "example1.css").exists(), result.output
6873

6974

7075
def test_folder(scss_folder):
7176
result = CliRunner().invoke(run_compile, [str(scss_folder)])
72-
assert result.exit_code == 0, result.output
77+
assert result.exit_code == 2, result.output
7378
assert (scss_folder / "example1.css").exists(), result.output
7479
assert (scss_folder / "example2.css").exists(), result.output
7580

@@ -83,6 +88,6 @@ def test_translate(scss_folder):
8388
str(scss_folder) + ":" + str(scss_folder.parent / "css"),
8489
],
8590
)
86-
assert result.exit_code == 0, result.output
91+
assert result.exit_code == 2, result.output
8792
assert not (scss_folder / "example1.css").exists(), result.output
8893
assert (scss_folder.parent / "css" / "example1.css").exists(), result.output

0 commit comments

Comments
 (0)