Skip to content

Commit

Permalink
DX: set PRETTIER_LEGACY_CLI when Prettier v4 alpha (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer authored Dec 1, 2023
1 parent b8b5ef5 commit c1d66d5
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions src/repoma/check_dev_files/precommit.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Check content of :code:`.pre-commit-config.yaml` and related files."""

from pathlib import Path
from textwrap import dedent
from typing import List, Set

from ruamel.yaml.comments import CommentedMap, CommentedSeq
from ruamel.yaml.scalarstring import DoubleQuotedScalarString

from repoma.errors import PrecommitError
from repoma.utilities import CONFIG_PATH
Expand All @@ -22,6 +24,7 @@ def main() -> None:
executor(_check_plural_hooks_first, cfg)
executor(_check_single_hook_sorting, cfg)
executor(_check_skipped_hooks, cfg)
executor(_update_conda_environment, cfg)
executor.finalize()


Expand Down Expand Up @@ -115,15 +118,50 @@ def get_non_functional_hooks(config: PrecommitConfig) -> List[str]:
]


def _update_conda_environment(precommit_config: PrecommitConfig) -> None:
"""Temporary fix for Prettier v4 alpha releases.
https://prettier.io/blog/2023/11/30/cli-deep-dive#installation
"""
path = Path("environment.yml")
if not path.exists():
return
yaml = create_prettier_round_trip_yaml()
conda_env: CommentedMap = yaml.load(path)
variables: CommentedMap = conda_env.get("variables", {})
key = "PRETTIER_LEGACY_CLI"
if __has_prettier_v4alpha(precommit_config):
if key not in variables:
variables[key] = DoubleQuotedScalarString("1")
conda_env["variables"] = variables
yaml.dump(conda_env, path)
msg = f"Set {key} environment variable in {path}"
raise PrecommitError(msg)
elif key in variables:
del variables[key]
if not variables:
del conda_env["variables"]
yaml.dump(conda_env, path)
msg = f"Removed {key} environment variable {path}"
raise PrecommitError(msg)


def __get_skipped_hooks(config: PrecommitConfig) -> Set[str]:
skipped_hooks = {
"check-jsonschema",
"pyright",
"taplo",
}
repo = config.find_repo(r"^.*/mirrors-prettier$")
if repo is not None and repo.rev is not None:
rev = repo.rev
if rev.startswith("v4") and "alpha" in rev:
skipped_hooks.add("prettier")
if __has_prettier_v4alpha(config):
skipped_hooks.add("prettier")
return skipped_hooks


def __has_prettier_v4alpha(config: PrecommitConfig) -> bool:
repo = config.find_repo(r"^.*/mirrors-prettier$")
if repo is None:
return False
if repo.rev is None:
return False
rev = repo.rev
return rev.startswith("v4") and "alpha" in rev

0 comments on commit c1d66d5

Please sign in to comment.