Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run prettier after dumping a json schema file #2124

Merged
merged 5 commits into from
Dec 15, 2022
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Linting

- Allow specifying containers in less than three lines ([#2121](https://github.com/nf-core/tools/pull/2121))
- Run prettier after dumping a json schema file ([#2124](https://github.com/nf-core/tools/pull/2124))

### Modules

Expand Down
5 changes: 2 additions & 3 deletions nf_core/launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import nf_core.schema
import nf_core.utils
from nf_core.lint_utils import dump_json_with_prettier

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -700,9 +701,7 @@ def build_command(self):

# Write the user selection to a file and run nextflow with that
if self.use_params_file:
with open(self.params_out, "w") as fp:
json.dump(self.schema_obj.input_params, fp, indent=4)
fp.write("\n")
dump_json_with_prettier(self.params_out, self.schema_obj.input_params)
self.nextflow_cmd += f' -params-file "{os.path.relpath(self.params_out)}"'

# Call nextflow with a list of command line flags
Expand Down
12 changes: 12 additions & 0 deletions nf_core/lint_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
import subprocess
from pathlib import Path
Expand Down Expand Up @@ -82,3 +83,14 @@ def run_prettier_on_file(file):
"There was an error running the prettier pre-commit hook.\n"
f"STDOUT: {e.stdout.decode()}\nSTDERR: {e.stderr.decode()}"
)


def dump_json_with_prettier(file_name, file_content):
"""Dump a JSON file and run prettier on it.
Args:
file_name (Path | str): A file identifier as a string or pathlib.Path.
file_content (dict): Content to dump into the JSON file
"""
with open(file_name, "w") as fh:
json.dump(file_content, fh, indent=4)
run_prettier_on_file(file_name)
7 changes: 2 additions & 5 deletions nf_core/modules/modules_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import nf_core.utils
from nf_core.components.components_utils import get_components_to_install
from nf_core.lint_utils import run_prettier_on_file
from nf_core.lint_utils import dump_json_with_prettier
from nf_core.modules.modules_repo import (
NF_CORE_MODULES_NAME,
NF_CORE_MODULES_REMOTE,
Expand Down Expand Up @@ -1023,10 +1023,7 @@ def dump(self):
"""
# Sort the modules.json
self.modules_json["repos"] = nf_core.utils.sort_dictionary(self.modules_json["repos"])
with open(self.modules_json_path, "w") as fh:
json.dump(self.modules_json, fh, indent=4)
fh.write("\n")
run_prettier_on_file(self.modules_json_path)
dump_json_with_prettier(self.modules_json_path, self.modules_json)

def resolve_missing_installation(self, missing_installation, component_type):
missing_but_in_mod_json = [
Expand Down
5 changes: 2 additions & 3 deletions nf_core/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import nf_core.list
import nf_core.utils
from nf_core.lint_utils import dump_json_with_prettier

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -170,9 +171,7 @@ def save_schema(self, suppress_logging=False):
num_params += sum(len(d.get("properties", {})) for d in self.schema.get("definitions", {}).values())
if not suppress_logging:
log.info(f"Writing schema with {num_params} params: '{self.schema_filename}'")
with open(self.schema_filename, "w") as fh:
json.dump(self.schema, fh, indent=4)
fh.write("\n")
dump_json_with_prettier(self.schema_filename, self.schema)

def load_input_params(self, params_path):
"""Load a given a path to a parameters file (JSON/YAML)
Expand Down