Skip to content

Commit 83288ae

Browse files
feat: add rules to user's gitignore to ignore website build
1 parent 86980e0 commit 83288ae

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/afterpython/cli/commands/update.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def dependencies(upgrade: bool, all: bool):
126126
)
127127
def website(ctx, no_backup: bool):
128128
"""Update project website template to the latest version"""
129-
from afterpython.utils import find_node_env
129+
from afterpython.utils import ensure_website_gitignore_rules, find_node_env
130130

131131
website_template_repo = "AfterPythonOrg/project-website-template"
132132

@@ -166,6 +166,9 @@ def website(ctx, no_backup: bool):
166166
)
167167
if result.returncode != 0:
168168
raise Exit(result.returncode)
169+
170+
# Ensure gitignore rules are present
171+
ensure_website_gitignore_rules()
169172
except Exception as e:
170173
click.echo(f"✗ Error updating project website template: {e}", err=True)
171174
if not no_backup:

src/afterpython/utils.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,54 @@ def is_website_initialized() -> bool:
312312
package_json = website_path / "package.json"
313313

314314
return website_path.exists() and package_json.exists()
315+
316+
317+
def ensure_website_gitignore_rules() -> None:
318+
"""Ensure website-related gitignore rules are present in the project's .gitignore.
319+
320+
Adds the following rules if they don't exist:
321+
- **/_build/ (ignore all build directories)
322+
- !afterpython/_website/src/lib/ (un-ignore website source for customization)
323+
- afterpython/_website.backup/ (ignore backup created by ap update website)
324+
"""
325+
import afterpython as ap
326+
327+
gitignore_path = ap.paths.user_path / ".gitignore"
328+
329+
# Rules that should be present
330+
required_rules = [
331+
"**/_build/",
332+
"!afterpython/_website/src/lib/",
333+
"afterpython/_website.backup/",
334+
]
335+
336+
# Read existing gitignore or create empty list
337+
if gitignore_path.exists():
338+
existing_lines = gitignore_path.read_text().splitlines()
339+
else:
340+
existing_lines = []
341+
342+
# Find which rules are missing
343+
missing_rules = []
344+
for rule in required_rules:
345+
# Check if rule exists (exact match or as part of a line)
346+
if not any(rule in line for line in existing_lines):
347+
missing_rules.append(rule)
348+
349+
# Add missing rules if any
350+
if missing_rules:
351+
# Ensure file ends with newline if it exists and has content
352+
content = gitignore_path.read_text() if gitignore_path.exists() else ""
353+
if content and not content.endswith("\n"):
354+
content += "\n"
355+
356+
# Add a section header if we're adding rules
357+
if content:
358+
content += "\n"
359+
content += "# AfterPython website rules\n"
360+
content += "\n".join(missing_rules) + "\n"
361+
362+
gitignore_path.write_text(content)
363+
click.echo(f"Added website-related rules to {gitignore_path}")
364+
else:
365+
click.echo("Website gitignore rules already present")

0 commit comments

Comments
 (0)