Skip to content

Commit 0af636a

Browse files
committed
Add clean subcommand to remove stale files from public dir.
1 parent 11d03b9 commit 0af636a

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

logya/commands.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
11
import shutil
22
import sys
33
from importlib import resources
4+
from pathlib import Path
45

56
from logya.content import write_collection, write_page
67
from logya.core import Logya
78
from logya.util import paths
89

910

11+
def clean(dir_site: str, **kwargs) -> None:
12+
L = Logya(dir_site=dir_site, verbose=kwargs.get('verbose'))
13+
L.build()
14+
15+
search_index = L.collection_index.copy()
16+
search_index.update(L.doc_index)
17+
18+
for root, _, files in L.paths.public.walk():
19+
for f in files:
20+
rel_file = root.relative_to(L.paths.public).joinpath(f)
21+
url = '/' + rel_file.as_posix()
22+
23+
if url in search_index or url.replace('index.html', '') in search_index:
24+
continue
25+
if L.paths.static.joinpath(rel_file).exists():
26+
continue
27+
28+
stale_file = Path(root, f)
29+
L.info(f'Remove: {stale_file} - {url}')
30+
stale_file.unlink()
31+
32+
1033
def create(dir_site: str, name: str, site: str, **_kwargs) -> None:
1134
"""Create a new site in the specified directory."""
1235

@@ -23,10 +46,10 @@ def create(dir_site: str, name: str, site: str, **_kwargs) -> None:
2346
print(f'Site created in "{target}".')
2447

2548

26-
def generate(dir_site: str, verbose: bool, keep: bool, **_kwargs):
49+
def generate(dir_site: str, keep: bool, **kwargs):
2750
"""Generate a site in the public directory."""
2851

29-
L = Logya(dir_site=dir_site, verbose=verbose)
52+
L = Logya(dir_site=dir_site, verbose=kwargs.get('verbose'))
3053
L.build()
3154

3255
mtime_templates = None

logya/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ def main():
1818
parser.add_argument('--version', '-V', action='version', version=__version__)
1919
subparsers = parser.add_subparsers()
2020

21+
# clean up public directory
22+
p_clean = subparsers.add_parser('clean', parents=[parent], help='Remove stale files and directories from public directory. Use when deploying a site.')
23+
p_clean.set_defaults(func=commands.clean)
24+
2125
# create a basic site with the given name
2226
p_create = subparsers.add_parser(
2327
'create', parents=[parent], help='Create a starter site in the specified directory.'

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ path = "logya/__init__.py"
5454
[tool.hatch.envs.logya-dev]
5555
extra-dependencies = ["flake8", "ipdb", "mypy>=1.0.0", "pytest"]
5656

57+
[tool.hatch.envs.logya-dev.env-vars]
58+
PYTHONBREAKPOINT = "ipdb.set_trace"
59+
5760
[tool.hatch.envs.logya-dev.scripts]
5861
type_check = "mypy --install-types --non-interactive {args:logya tests}"
5962
qa = [

0 commit comments

Comments
 (0)