Skip to content

Commit 9f406c6

Browse files
committed
Improve the CLI
1 parent 4474514 commit 9f406c6

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

pyjsx/cli.py

+20-17
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,37 @@ def cli(*, version: bool) -> None:
1616

1717
@cli.command()
1818
@click.argument("sources", type=click.Path(exists=True), nargs=-1)
19-
@click.option("-r", "--recursive", type=bool, default=True, help="Recurse into directories.")
19+
@click.option("-r", "--recursive", type=bool, is_flag=True, default=False, help="Recurse into directories.")
2020
def compile(sources: list[str], recursive: bool) -> None:
21-
"""Compile a PX file to a PY file."""
21+
"""Compile .px files to regular .py files."""
22+
count = 0
2223
for source in sources:
2324
path = Path(source)
24-
if path.is_dir():
25-
transpile_dir(path, recursive=recursive)
26-
elif path.suffix == ".px":
27-
transpile_file(path)
28-
else:
29-
click.echo(f"Skipping {source} (not a PX file)")
30-
click.echo("Compilation complete.")
25+
count += transpile_dir(path, recursive=recursive)
26+
msg = f"Compiled {count} file" + ("s" if count != 1 else "") + "."
27+
click.secho(msg, fg="green", bold=True)
3128

3229

33-
def transpile_dir(path: Path, *, recursive: bool = False) -> None:
34-
assert path.is_dir()
30+
def transpile_dir(path: Path, *, recursive: bool = False) -> int:
31+
if path.is_file():
32+
return transpile_file(path)
33+
count = 0
3534
for file in path.iterdir():
3635
if file.is_dir() and recursive:
37-
transpile_dir(file)
38-
elif file.suffix == ".px":
39-
transpile_file(file)
36+
count += transpile_dir(file)
37+
elif file.is_file() and file.suffix == ".px":
38+
count += transpile_file(file)
39+
return count
4040

4141

42-
def transpile_file(path: Path) -> None:
43-
"""Transpile a PX file to a PY file."""
44-
assert path.suffix == ".px"
42+
def transpile_file(path: Path) -> int:
43+
if path.suffix != ".px":
44+
click.secho(f"Skipping {path} (not a .px file)", fg="yellow")
45+
return 0
46+
click.echo(f"Compiling {path}...")
4547
transpiled = transpile(path.read_text())
4648
path.with_suffix(".py").write_text(transpiled)
49+
return 1
4750

4851

4952
if __name__ == "__main__":

0 commit comments

Comments
 (0)