(path: Path, block_name: str, lines: list[str])
| 237 | |
| 238 | |
| 239 | def upsert_shell_block(path: Path, block_name: str, lines: list[str]) -> Path: |
| 240 | start_marker = f"# >>> uncommon-route {block_name} >>>" |
| 241 | end_marker = f"# <<< uncommon-route {block_name} <<<" |
| 242 | block_lines = [start_marker, "# Added by `uncommon-route init`.", *lines, end_marker] |
| 243 | block = "\n".join(block_lines) |
| 244 | |
| 245 | existing = path.read_text() if path.exists() else "" |
| 246 | if start_marker in existing and end_marker in existing: |
| 247 | start = existing.index(start_marker) |
| 248 | end = existing.index(end_marker, start) + len(end_marker) |
| 249 | updated = f"{existing[:start].rstrip()}\n\n{block}\n{existing[end:].lstrip()}" |
| 250 | else: |
| 251 | separator = "\n\n" if existing.strip() else "" |
| 252 | updated = f"{existing.rstrip()}{separator}{block}\n" |
| 253 | |
| 254 | path.parent.mkdir(parents=True, exist_ok=True) |
| 255 | path.write_text(updated) |
| 256 | return path |
no outgoing calls
no test coverage detected