about summary refs log tree commit diff
path: root/src/build_helper
diff options
context:
space:
mode:
authorbinarycat <binarycat@envs.net>2025-07-19 14:56:42 -0500
committerbinarycat <binarycat@envs.net>2025-07-19 14:56:42 -0500
commitc6d50eaa497ef0c3013d21f23cd85e6e97c32c2c (patch)
tree92b3db0bc1f482fa78b11c635e44f7dcb5721392 /src/build_helper
parentca39010d3e7d3c512d5d8df8094862ffa146837f (diff)
downloadrust-c6d50eaa497ef0c3013d21f23cd85e6e97c32c2c.tar.gz
rust-c6d50eaa497ef0c3013d21f23cd85e6e97c32c2c.zip
integrate build_helper::npm into js checks and package.json usage
Diffstat (limited to 'src/build_helper')
-rw-r--r--src/build_helper/src/npm.rs43
1 files changed, 27 insertions, 16 deletions
diff --git a/src/build_helper/src/npm.rs b/src/build_helper/src/npm.rs
index dedef40978d..86cf6183bd0 100644
--- a/src/build_helper/src/npm.rs
+++ b/src/build_helper/src/npm.rs
@@ -3,23 +3,34 @@ use std::path::{Path, PathBuf};
 use std::process::Command;
 use std::{fs, io};
 
-/// Install an exact package version, and return the path of `node_modules`.
-pub fn install_one(
-    out_dir: &Path,
-    npm_bin: &Path,
-    pkg_name: &str,
-    pkg_version: &str,
-) -> Result<PathBuf, io::Error> {
+use crate::ci::CiEnv;
+
+/// Install all the npm deps, and return the path of `node_modules`.
+pub fn install(src_root_path: &Path, out_dir: &Path, npm: &Path) -> Result<PathBuf, io::Error> {
     let nm_path = out_dir.join("node_modules");
-    let _ = fs::create_dir(&nm_path);
-    let mut child = Command::new(npm_bin)
-        .arg("install")
-        .arg("--audit=false")
-        .arg("--fund=false")
-        .arg(format!("{pkg_name}@{pkg_version}"))
-        .current_dir(out_dir)
-        .spawn()?;
-    let exit_status = child.wait()?;
+    let copy_to_build = |p| {
+        fs::copy(src_root_path.join(p), out_dir.join(p)).map_err(|e| {
+            eprintln!("unable to copy {p:?} to build directory: {e:?}");
+            e
+        })
+    };
+    // copy stuff to the output directory to make node_modules get put there.
+    copy_to_build("package.json")?;
+    copy_to_build("package-lock.json")?;
+
+    let mut cmd = Command::new(npm);
+    if CiEnv::is_ci() {
+        // `npm ci` redownloads every time and thus is too slow for local development.
+        cmd.arg("ci");
+    } else {
+        cmd.arg("install");
+    }
+    // disable a bunch of things we don't want.
+    // this makes tidy output less noisy, and also significantly improves runtime
+    // of repeated tidy invokations.
+    cmd.args(&["--audit=false", "--save=false", "--fund=false"]);
+    cmd.current_dir(out_dir);
+    let exit_status = cmd.spawn()?.wait()?;
     if !exit_status.success() {
         eprintln!("npm install did not exit successfully");
         return Err(io::Error::other(Box::<dyn Error + Send + Sync>::from(format!(