integrate build_helper::npm into js checks and package.json usage

This commit is contained in:
binarycat
2025-07-19 14:56:42 -05:00
parent ca39010d3e
commit c6d50eaa49
3 changed files with 34 additions and 54 deletions

View File

@@ -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!(

View File

@@ -65,10 +65,8 @@ fn main() -> Result<(), ()> {
}
}
// FIXME(binarycat): once we get package.json in version control, this should be updated to install via that instead
let local_node_modules =
npm::install_one(&config.out_dir, &config.npm, "browser-ui-test", "0.21.1")
.expect("unable to install browser-ui-test");
let local_node_modules = npm::install(&config.rust_src, &config.out_dir, &config.npm)
.expect("unable to install browser-ui-test");
let mut command = Command::new(&config.nodejs);

View File

@@ -2,11 +2,11 @@
//! characters.
use std::ffi::OsStr;
use std::io;
use std::path::{Path, PathBuf};
use std::process::{Child, Command};
use std::{fs, io};
use build_helper::ci::CiEnv;
use build_helper::npm;
use ignore::DirEntry;
use crate::walk::walk_no_read;
@@ -24,38 +24,9 @@ fn spawn_cmd(cmd: &mut Command) -> Result<Child, io::Error> {
/// install all js dependencies from package.json.
pub(super) fn npm_install(root_path: &Path, outdir: &Path) -> Result<(), super::Error> {
let copy_to_build = |p| {
fs::copy(root_path.join(p), outdir.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(outdir);
let mut child = spawn_cmd(&mut cmd)?;
match child.wait() {
Ok(exit_status) => {
if exit_status.success() {
return Ok(());
}
Err(super::Error::FailedCheck("npm install"))
}
Err(error) => Err(super::Error::Generic(format!("npm install failed: {error:?}"))),
}
// FIXME(lolbinarycat): make this obey build.npm bootstrap option
npm::install(root_path, outdir, Path::new("npm"))?;
Ok(())
}
fn rustdoc_js_files(librustdoc_path: &Path) -> Vec<PathBuf> {