noredink-ui/prelude-nri/node/build_node_modules.py

62 lines
1.8 KiB
Python

#!/usr/bin/env python3
"""
Make a fresh `node_modules` directory in an isolated directory.
"""
import argparse
import os
import subprocess
import sys
import shutil
import tempfile
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("out", help="Where you want node_modules to end up")
parser.add_argument(
"--package",
help="The package.json to install dependencies from",
default="package.json",
)
parser.add_argument(
"--package-lock",
help="The package-lock.json corresponding to package.json",
default="package-lock.json",
)
parser.add_argument(
"--no-ignore-scripts",
action="store_true",
help="Don't run package post-install scripts",
)
parser.add_argument(
"--bin-dir",
help="Path to a node installation's binary directory. If present, will be treated as an extra entry in PATH for the duration of this command.",
)
args = parser.parse_args()
if args.bin_dir:
os.environ["PATH"] = "{}:{}".format(
os.path.abspath(args.bin_dir), os.environ["PATH"]
)
with tempfile.TemporaryDirectory() as tempdir:
# npm wants these to be real files for whatever reason, and will throw
# errors if they're symlinks.
shutil.copy(args.package, os.path.join(tempdir, "package.json"))
shutil.copy(args.package_lock, os.path.join(tempdir, "package-lock.json"))
cmd = ["npm", "clean-install"]
if not args.no_ignore_scripts:
cmd.append("--ignore-scripts")
proc = subprocess.Popen(cmd, cwd=tempdir)
proc.communicate()
if proc.returncode == 0:
os.rename(
os.path.join(tempdir, "node_modules"),
args.out,
)
sys.exit(proc.returncode)