Skip to content

Commit 10fd76e

Browse files
olehermanseclaude
andcommitted
Refactored and simplified code for running black and prettier
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Ole Herman Schumacher Elgesem <ole@northern.tech>
1 parent 495ef8f commit 10fd76e

1 file changed

Lines changed: 18 additions & 22 deletions

File tree

src/cfengine_cli/docs.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -346,20 +346,22 @@ def _process_markdown_code_blocks(
346346
os.remove(snippet_path)
347347

348348

349-
def _run_black(path):
350-
print(f"Formatting '{path}' with black...")
349+
def _run_formatter(tool, args, cwd, install_hint):
350+
print(f"Formatting with {tool}...")
351351
try:
352352
subprocess.run(
353-
["black", path],
353+
[tool, *args],
354354
capture_output=True,
355355
text=True,
356356
check=True,
357-
cwd=".",
357+
cwd=cwd,
358358
)
359359
except:
360-
raise UserError(
361-
"Encountered an error running black\nInstall: pipx install black"
362-
)
360+
raise UserError(f"Encountered an error running {tool}\nInstall: {install_hint}")
361+
362+
363+
def _run_black(path):
364+
_run_formatter("black", [path], ".", "pipx install black")
363365

364366

365367
def _run_prettier(path):
@@ -378,21 +380,15 @@ def _run_prettier(path):
378380
args.append("**.md")
379381
if not args:
380382
return
381-
try:
382-
# Warning: Beware of shell expansion if you try to run this in your terminal.
383-
# Wrong: prettier --write **.markdown **.md
384-
# Right: prettier --write '**.markdown' '**.md'
385-
subprocess.run(
386-
["prettier", "--embedded-language-formatting", "off", "--write", *args],
387-
capture_output=True,
388-
text=True,
389-
check=True,
390-
cwd=directory,
391-
)
392-
except:
393-
raise UserError(
394-
"Encountered an error running prettier\nInstall: npm install --global prettier"
395-
)
383+
# Warning: Beware of shell expansion if you try to run this in your terminal.
384+
# Wrong: prettier --write **.markdown **.md
385+
# Right: prettier --write '**.markdown' '**.md'
386+
_run_formatter(
387+
"prettier",
388+
["--embedded-language-formatting", "off", "--write", *args],
389+
directory,
390+
"npm install --global prettier",
391+
)
396392

397393

398394
def _update_docs_single_arg(path):

0 commit comments

Comments
 (0)