Skip to content

Commit

Permalink
Add check_exists flag to get_target_paths()
Browse files Browse the repository at this point in the history
  • Loading branch information
Zamoca42 committed Jan 6, 2025
1 parent b643eb6 commit a123833
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
11 changes: 7 additions & 4 deletions scripts/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,20 @@ def test_get_pages_dir():
shutil.rmtree(root, True)


def get_target_paths(page: Path, pages_dirs: Path) -> list[Path]:
def get_target_paths(
page: Path, pages_dirs: Path, check_exists: bool = True
) -> list[Path]:
"""
Get all paths in all languages that match the page.
Parameters:
page (Path): the page to search for.
pages_dirs (Path): directories to search in
check_exists (bool): whether to only return existing paths (default: True)
Returns:
list (list of Path's): A list of Path's.
"""

target_paths = []

if not page.lower().endswith(".md"):
Expand All @@ -154,12 +157,12 @@ def get_target_paths(page: Path, pages_dirs: Path) -> list[Path]:
for pages_dir in pages_dirs:
page_path = pages_dir / arg_platform / arg_page

if not page_path.exists():
if check_exists and not page_path.exists():
print(create_colored_line(Colors.RED, f"Page {page_path} does not exist"))
continue
target_paths.append(page_path)

target_paths.sort()

return target_paths


Expand Down
16 changes: 10 additions & 6 deletions scripts/set-alias-page.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def set_alias_page(
"\x1b[36mpage would be added"
"\x1b[34mpage would updated"
"""

locale = get_locale(path)
if locale not in config.templates or (
config.language != "" and locale != config.language
Expand Down Expand Up @@ -233,6 +234,7 @@ def set_alias_page(

def get_locale_alias_pattern(locale: str) -> str:
"""Get alias pattern from template"""

template_line = re.search(r">.*`example`", config.templates[locale]).group(0)
locale_alias_pattern = template_line[2 : template_line.find("`example`")].strip()
return locale_alias_pattern
Expand All @@ -245,6 +247,7 @@ def get_alias_command_in_page(path: Path, alias_pattern: str) -> AliasPageConten
Returns:
AliasPageContent: The page content, or empty strings if not an alias page
"""

if not path.exists():
return AliasPageContent(title="", original_command="", documentation_command="")

Expand Down Expand Up @@ -295,6 +298,7 @@ def sync_alias_page_to_locale(pages_dir: Path, alias_page: AliasPage) -> list[Pa
Returns:
list[Path]: List of paths that were modified
"""

paths = []
path = config.root / pages_dir / alias_page.page_path
status = set_alias_page(path, alias_page.content)
Expand All @@ -315,6 +319,7 @@ def get_english_alias_pages(en_path: Path) -> list[AliasPage]:
Returns:
list[AliasPage]: List of alias pages with their content
"""

alias_pages = []
alias_pattern = get_locale_alias_pattern("en")

Expand Down Expand Up @@ -348,6 +353,7 @@ def prompt_alias_page_info(page_path: str) -> AliasPageContent:
Returns:
AliasPageContent: The collected page content
"""

print("\nCreating new alias page...")
print(create_colored_line(Colors.CYAN, f"Page path: {page_path}"))

Expand All @@ -368,11 +374,7 @@ def prompt_alias_page_info(page_path: str) -> AliasPageContent:
"\nThe original command will appear in 'This command is an alias of `command`'",
)
)
print(
create_colored_line(
Colors.GREEN, "Example: npm run"
)
)
print(create_colored_line(Colors.GREEN, "Example: npm run"))
original_command = input(
create_colored_line(Colors.CYAN, "Enter original command: ")
).strip()
Expand Down Expand Up @@ -455,7 +457,9 @@ def main():
# Use '--page' option
if args.page != "":
page_info = prompt_alias_page_info(args.page)
target_paths += get_target_paths(args.page, config.pages_dirs)
target_paths += get_target_paths(
args.page, config.pages_dirs, check_exists=False
)

for path in target_paths:
rel_path = "/".join(path.parts[-3:])
Expand Down

0 comments on commit a123833

Please sign in to comment.