Skip to content

Commit 5cea77c

Browse files
refactor: set check=False in subprocess to hide subprocess's errors and only show the command's (e.g. ruff, pre-commit) original ones
1 parent 2ddc356 commit 5cea77c

File tree

9 files changed

+66
-23
lines changed

9 files changed

+66
-23
lines changed

afterpython/cli/commands/build.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import subprocess
1212

1313
import click
14+
from click.exceptions import Exit
1415

1516
import afterpython as ap
1617
from afterpython.utils import find_node_env
@@ -151,7 +152,7 @@ def build(ctx, dev: bool, execute: bool):
151152
click.echo(f"Building {content_type}/...")
152153
# NOTE: needs to set BASE_URL so that the project website can link to the content pages correctly at e.g. localhost:5173/doc
153154
build_env = {**node_env, "BASE_URL": f"/{content_type}"}
154-
subprocess.run(
155+
result = subprocess.run(
155156
[
156157
"myst",
157158
"build",
@@ -161,14 +162,18 @@ def build(ctx, dev: bool, execute: bool):
161162
],
162163
cwd=content_path,
163164
env=build_env,
164-
check=True,
165+
check=False,
165166
)
167+
if result.returncode != 0:
168+
raise Exit(result.returncode)
166169

167170
postbuild()
168171

169172
# website's production build
170173
if not dev:
171174
click.echo("Building project website...")
172-
subprocess.run(
173-
["pnpm", "build"], cwd=paths.website_path, env=node_env, check=True
175+
result = subprocess.run(
176+
["pnpm", "build"], cwd=paths.website_path, env=node_env, check=False
174177
)
178+
if result.returncode != 0:
179+
raise Exit(result.returncode)

afterpython/cli/commands/check.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import subprocess
22

33
import click
4+
from click.exceptions import Exit
45

56

67
@click.command(
@@ -17,6 +18,10 @@ def check(ctx):
1718
ruff_toml = paths.afterpython_path / "ruff.toml"
1819
if ruff_toml.exists():
1920
click.echo(f"Using ruff configuration from {ruff_toml}")
20-
subprocess.run(["ruff", "check", "--config", str(ruff_toml), *ctx.args])
21+
result = subprocess.run(
22+
["ruff", "check", "--config", str(ruff_toml), *ctx.args], check=False
23+
)
2124
else:
22-
subprocess.run(["ruff", "check", *ctx.args])
25+
result = subprocess.run(["ruff", "check", *ctx.args], check=False)
26+
if result.returncode != 0:
27+
raise Exit(result.returncode)

afterpython/cli/commands/clean.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import subprocess
99

1010
import click
11+
from click.exceptions import Exit
1112

1213
from afterpython.utils import find_node_env, has_content_for_myst
1314
from afterpython.const import CONTENT_TYPES
@@ -42,12 +43,14 @@ def clean(ctx, all: bool):
4243

4344
click.echo(f"Cleaning {content_type}/...")
4445
# Pass through any extra args to myst clean (e.g., --cache, --templates)
45-
subprocess.run(
46+
result = subprocess.run(
4647
["myst", "clean", *ctx.args, *(["--all"] if all else [])],
4748
cwd=content_path,
4849
env=node_env,
49-
check=True,
50+
check=False,
5051
)
52+
if result.returncode != 0:
53+
raise Exit(result.returncode)
5154

5255
# Clean afterpython's build directories if --all flag is used
5356
if all:

afterpython/cli/commands/dev.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import subprocess
99

1010
import click
11+
from click.exceptions import Exit
1112

1213
from afterpython.utils import find_node_env, find_available_port
1314
from afterpython.const import CONTENT_TYPES
@@ -55,7 +56,7 @@ def dev(ctx, all: bool, execute: bool, no_website: bool):
5556
paths = ctx.obj["paths"]
5657

5758
# OPTIMIZE: should implement incremental build?
58-
subprocess.run(["ap", "build", "--dev"], check=True)
59+
subprocess.run(["ap", "build", "--dev"])
5960

6061
def cleanup_processes():
6162
"""Clean up all MyST server processes"""
@@ -115,9 +116,11 @@ def cleanup_processes():
115116
if not no_website:
116117
node_env: NodeEnv = find_node_env()
117118
click.echo("Running the web dev server...")
118-
subprocess.run(
119-
["pnpm", "dev"], cwd=paths.website_path, env=node_env, check=True
119+
result = subprocess.run(
120+
["pnpm", "dev"], cwd=paths.website_path, env=node_env, check=False
120121
)
122+
if result.returncode != 0:
123+
raise Exit(result.returncode)
121124
else:
122125
click.echo(
123126
"Skipping website dev server (--no-website flag). Run 'pnpm dev' manually in afterpython/_website/ with your custom options."

afterpython/cli/commands/format.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import subprocess
22

33
import click
4+
from click.exceptions import Exit
45

56

67
@click.command(
@@ -13,4 +14,6 @@
1314
@click.pass_context
1415
def format(ctx):
1516
"""Simple wrapper for ruff format for convenience"""
16-
subprocess.run(["ruff", "format", *ctx.args])
17+
result = subprocess.run(["ruff", "format", *ctx.args], check=False)
18+
if result.returncode != 0:
19+
raise Exit(result.returncode)

afterpython/cli/commands/install.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import subprocess
22

33
import click
4+
from click.exceptions import Exit
45

56
from afterpython.utils import has_uv
67

@@ -16,4 +17,6 @@ def install():
1617
if not has_uv():
1718
click.echo("uv not found. Please install uv first.")
1819
return
19-
subprocess.run(["uv", "sync", "--all-extras", "--all-groups"], check=True)
20+
result = subprocess.run(["uv", "sync", "--all-extras", "--all-groups"], check=False)
21+
if result.returncode != 0:
22+
raise Exit(result.returncode)

afterpython/cli/commands/preview.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import subprocess
22

33
import click
4+
from click.exceptions import Exit
45

56

67
@click.command()
@@ -14,6 +15,8 @@ def preview(ctx):
1415
click.echo(
1516
"Previewing the production build of the project website (including myst's builds)..."
1617
)
17-
subprocess.run(
18-
["pnpm", "preview"], cwd=paths.website_path, env=node_env, check=True
18+
result = subprocess.run(
19+
["pnpm", "preview"], cwd=paths.website_path, env=node_env, check=False
1920
)
21+
if result.returncode != 0:
22+
raise Exit(result.returncode)

afterpython/cli/commands/start.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import subprocess
88

99
import click
10+
from click.exceptions import Exit
1011

1112

1213
command_kwargs = {
@@ -73,9 +74,11 @@ def start(ctx, doc: bool, blog: bool, tutorial: bool, example: bool, guide: bool
7374
click.echo(f"Skipping {content_type}/ (no content files found)")
7475
return
7576

76-
subprocess.run(
77-
["myst", "start", *ctx.args], cwd=content_path, env=node_env, check=True
77+
result = subprocess.run(
78+
["myst", "start", *ctx.args], cwd=content_path, env=node_env, check=False
7879
)
80+
if result.returncode != 0:
81+
raise Exit(result.returncode)
7982

8083

8184
def _run(ctx):

afterpython/cli/commands/update.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import shutil
1010

1111
import click
12+
from click.exceptions import Exit
1213

1314

1415
@click.group()
@@ -68,8 +69,14 @@ def dependencies(upgrade: bool, all: bool):
6869
update_dependencies(dependencies) # write the latest versions to pyproject.toml
6970
if has_uv():
7071
click.echo("Upgrading dependencies with uv...")
71-
subprocess.run(["uv", "lock"], check=True)
72-
subprocess.run(["uv", "sync", "--all-extras", "--all-groups"], check=True)
72+
result = subprocess.run(["uv", "lock"], check=False)
73+
if result.returncode != 0:
74+
raise Exit(result.returncode)
75+
result = subprocess.run(
76+
["uv", "sync", "--all-extras", "--all-groups"], check=False
77+
)
78+
if result.returncode != 0:
79+
raise Exit(result.returncode)
7380
click.echo(
7481
click.style(
7582
"✓ All dependencies upgraded successfully 🎉", fg="green", bold=True
@@ -80,7 +87,7 @@ def dependencies(upgrade: bool, all: bool):
8087
"uv not found. Updated pyproject.toml only (packages not installed)."
8188
)
8289
if all:
83-
subprocess.run(["ap", "pre-commit", "autoupdate"], check=True)
90+
subprocess.run(["ap", "pre-commit", "autoupdate"])
8491
click.echo("All pre-commit hooks updated successfully.")
8592

8693

@@ -124,10 +131,18 @@ def website(ctx, no_backup: bool):
124131
try:
125132
click.echo("Updating the project website template...")
126133
node_env: NodeEnv = find_node_env()
127-
subprocess.run(
128-
["pnpx", "degit", website_template_repo, str(website_path)], env=node_env
134+
result = subprocess.run(
135+
["pnpx", "degit", website_template_repo, str(website_path)],
136+
env=node_env,
137+
check=False,
138+
)
139+
if result.returncode != 0:
140+
raise Exit(result.returncode)
141+
result = subprocess.run(
142+
["pnpm", "install"], cwd=website_path, env=node_env, check=False
129143
)
130-
subprocess.run(["pnpm", "install"], cwd=website_path, env=node_env, check=True)
144+
if result.returncode != 0:
145+
raise Exit(result.returncode)
131146
except Exception as e:
132147
click.echo(f"✗ Error updating project website template: {e}", err=True)
133148
if not no_backup:

0 commit comments

Comments
 (0)