diff --git a/nf_core/pipelines/create/create.py b/nf_core/pipelines/create/create.py index b1b9f45594..3fd3ecb258 100644 --- a/nf_core/pipelines/create/create.py +++ b/nf_core/pipelines/create/create.py @@ -12,6 +12,7 @@ import git import git.config import jinja2 +import pathspec import yaml import nf_core @@ -26,6 +27,22 @@ log = logging.getLogger(__name__) +def _get_prettier_files(outdir: Path) -> list[str]: + """Return non-gitignored files in outdir, further filtered by .prettierignore if present.""" + try: + repo = git.Repo(outdir) + files = [f for f in repo.git.ls_files("--cached", "--others", "--exclude-standard").splitlines() if f] + except git.InvalidGitRepositoryError: + files = [str(f.relative_to(outdir)) for f in outdir.rglob("*") if f.is_file()] + + prettierignore = outdir / ".prettierignore" + if prettierignore.exists() and files: + spec = pathspec.PathSpec.from_lines("gitwildmatch", prettierignore.read_text().splitlines()) + files = [f for f in files if not spec.match_file(f)] + + return [str(outdir / f) for f in files] + + class PipelineCreate: """Creates a nf-core pipeline a la carte from the nf-core best-practice template. @@ -281,7 +298,7 @@ def init_pipeline(self): ): current_dir = Path.cwd() os.chdir(self.outdir) - run_prettier_on_file([str(f) for f in self.outdir.glob("**/*")]) + run_prettier_on_file(_get_prettier_files(self.outdir)) os.chdir(current_dir) if self.config.is_nfcore and not self.is_interactive: @@ -399,7 +416,7 @@ def render_template(self) -> None: # Run prettier on files for pipelines sync log.debug("Running prettier on pipeline files") - run_prettier_on_file([str(f) for f in self.outdir.glob("**/*")]) + run_prettier_on_file(_get_prettier_files(self.outdir)) def fix_linting(self): """ diff --git a/nf_core/pipelines/lint_utils.py b/nf_core/pipelines/lint_utils.py index e1980cf89d..a17477cd8e 100644 --- a/nf_core/pipelines/lint_utils.py +++ b/nf_core/pipelines/lint_utils.py @@ -4,6 +4,7 @@ import sys from pathlib import Path +import click import git import rich import yaml @@ -149,6 +150,12 @@ def run_prettier_on_file(file: Path | str | list[str]) -> None: Warns: If Prettier is not installed, a warning is logged. """ + verbose = False + try: + ctx = click.get_current_context() + verbose = bool(ctx.obj and ctx.obj.get("verbose", False)) + except RuntimeError: + pass is_git = check_git_repo() @@ -160,6 +167,8 @@ def run_prettier_on_file(file: Path | str | list[str]) -> None: prek_bin = Path(sys.executable).parent / "prek" prek = str(prek_bin) if prek_bin.exists() else "prek" args = [prek, "run", "--config", str(nf_core_pre_commit_config), "prettier"] + if verbose: + args.append("--verbose") if isinstance(file, list): args.extend(["--files", *file]) else: