build_helper/npm.rs
1use std::error::Error;
2use std::path::{Path, PathBuf};
3use std::process::Command;
4use std::{fs, io};
5
6/// Install all the npm deps, and return the path of `node_modules`.
7pub fn install(src_root_path: &Path, out_dir: &Path, yarn: &Path) -> Result<PathBuf, io::Error> {
8 let nm_path = out_dir.join("node_modules");
9 let copy_to_build = |p| {
10 fs::copy(src_root_path.join(p), out_dir.join(p)).map_err(|e| {
11 eprintln!("unable to copy {p:?} to build directory: {e:?}");
12 e
13 })
14 };
15 // copy stuff to the output directory to make node_modules get put there.
16 copy_to_build("package.json")?;
17 copy_to_build("yarn.lock")?;
18
19 let mut cmd = Command::new(yarn);
20 cmd.arg("install");
21 // make sure our `yarn.lock` file actually means something
22 cmd.arg("--frozen");
23
24 cmd.current_dir(out_dir);
25 let exit_status = cmd.spawn()?.wait()?;
26 if !exit_status.success() {
27 eprintln!("yarn install did not exit successfully");
28 return Err(io::Error::other(Box::<dyn Error + Send + Sync>::from(format!(
29 "yarn install returned exit code {exit_status}"
30 ))));
31 }
32 Ok(nm_path)
33}