Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions nf_core/pipelines/create/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import git
import git.config
import jinja2
import pathspec
import yaml

import nf_core
Expand All @@ -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.

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
"""
Expand Down
9 changes: 9 additions & 0 deletions nf_core/pipelines/lint_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
from pathlib import Path

import click
import git
import rich
import yaml
Expand Down Expand Up @@ -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()

Expand All @@ -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:
Expand Down
Loading