0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-24 00:17:37 +01:00
mongodb/bazel/format/format.py
Zack Winter ee29a7b957 SERVER-95716 Skip running prettier on symlinks in repo root (#28336)
GitOrigin-RevId: 660bda2662cbd0ee8785499f31938dab1619f23d
2024-10-23 16:30:55 +00:00

67 lines
2.2 KiB
Python

import argparse
import os
import pathlib
import subprocess
def run_prettier(prettier: pathlib.Path, check: bool) -> int:
# 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 = {
"!./build",
"!./bazel-bin",
"!./bazel-out",
"!./bazel-mongo",
"!./external",
}
for path in pathlib.Path(".").iterdir():
if path.is_symlink():
force_exclude_dirs.add(f"!./{path}")
try:
command = [prettier, "."] + list(force_exclude_dirs)
if check:
command.append("--check")
else:
command.append("--write")
print(f"Running command: '{command}'")
subprocess.run(command, check=True)
except subprocess.CalledProcessError:
print("Found formatting errors. Run 'bazel run //:format' to fix")
print("*** IF BAZEL IS NOT INSTALLED, RUN THE FOLLOWING: ***\n")
print("python buildscripts/install_bazel.py")
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")
print("*** IF BAZEL IS NOT INSTALLED, RUN THE FOLLOWING: ***\n")
print("python buildscripts/install_bazel.py")
return 1
parser = argparse.ArgumentParser(
prog="Format", description="This script formats code in mongodb"
)
parser.add_argument("--check", help="Run in check mode", default=False, action="store_true")
parser.add_argument(
"--prettier", help="Set the path to prettier", required=True, type=pathlib.Path
)
args = parser.parse_args()
prettier_path: pathlib.Path = args.prettier.resolve()
os.chdir(default_dir)
return run_prettier(prettier_path, args.check)
if __name__ == "__main__":
exit(main())