2024-04-04 01:12:53 +02:00
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import pathlib
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
def run_prettier(prettier: pathlib.Path, check: bool) -> int:
|
2024-10-23 17:48:43 +02:00
|
|
|
# Explicitly ignore anything in the output directories or any symlinks in the root of the repository
|
|
|
|
# to prevent bad symlinks from failing the run, see https://github.com/prettier/prettier/issues/11568 as
|
|
|
|
# to why it the paths being present in .prettierignore isn't sufficient
|
|
|
|
force_exclude_dirs = {
|
2024-09-18 17:34:23 +02:00
|
|
|
"!./build",
|
|
|
|
"!./bazel-bin",
|
|
|
|
"!./bazel-out",
|
|
|
|
"!./bazel-mongo",
|
|
|
|
"!./external",
|
2024-10-23 17:48:43 +02:00
|
|
|
}
|
|
|
|
for path in pathlib.Path(".").iterdir():
|
|
|
|
if path.is_symlink():
|
|
|
|
force_exclude_dirs.add(f"!./{path}")
|
2024-04-04 01:12:53 +02:00
|
|
|
try:
|
2024-10-23 17:48:43 +02:00
|
|
|
command = [prettier, "."] + list(force_exclude_dirs)
|
2024-05-17 01:57:57 +02:00
|
|
|
if check:
|
|
|
|
command.append("--check")
|
|
|
|
else:
|
|
|
|
command.append("--write")
|
2024-04-04 01:12:53 +02:00
|
|
|
print(f"Running command: '{command}'")
|
|
|
|
subprocess.run(command, check=True)
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
print("Found formatting errors. Run 'bazel run //:format' to fix")
|
2024-04-27 01:58:26 +02:00
|
|
|
print("*** IF BAZEL IS NOT INSTALLED, RUN THE FOLLOWING: ***\n")
|
2024-04-11 02:47:07 +02:00
|
|
|
print("python buildscripts/install_bazel.py")
|
2024-04-04 01:12:53 +02:00
|
|
|
return 1
|
|
|
|
|
|
|
|
if check:
|
|
|
|
print("No formatting errors")
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
def main() -> int:
|
|
|
|
# If we are running in bazel, default the directory to the workspace
|
|
|
|
default_dir = os.environ.get("BUILD_WORKSPACE_DIRECTORY")
|
|
|
|
if not default_dir:
|
|
|
|
print("This script must be run though bazel. Please run 'bazel run //:format' instead")
|
2024-04-27 01:58:26 +02:00
|
|
|
print("*** IF BAZEL IS NOT INSTALLED, RUN THE FOLLOWING: ***\n")
|
2024-04-11 02:47:07 +02:00
|
|
|
print("python buildscripts/install_bazel.py")
|
2024-04-04 01:12:53 +02:00
|
|
|
return 1
|
|
|
|
|
2024-05-17 18:01:09 +02:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
prog="Format", description="This script formats code in mongodb"
|
|
|
|
)
|
2024-04-04 01:12:53 +02:00
|
|
|
|
|
|
|
parser.add_argument("--check", help="Run in check mode", default=False, action="store_true")
|
2024-05-17 18:01:09 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--prettier", help="Set the path to prettier", required=True, type=pathlib.Path
|
|
|
|
)
|
2024-04-04 01:12:53 +02:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
prettier_path: pathlib.Path = args.prettier.resolve()
|
|
|
|
|
|
|
|
os.chdir(default_dir)
|
2024-05-17 01:57:57 +02:00
|
|
|
return run_prettier(prettier_path, args.check)
|
2024-04-04 01:12:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
exit(main())
|