build_helper/
npm.rs

1use std::error::Error;
2use std::path::{Path, PathBuf};
3use std::process::Command;
4use std::{fs, io};
5
6/// Install an exact package version, and return the path of `node_modules`.
7pub fn install_one(
8    out_dir: &Path,
9    npm_bin: &Path,
10    pkg_name: &str,
11    pkg_version: &str,
12) -> Result<PathBuf, io::Error> {
13    let nm_path = out_dir.join("node_modules");
14    let _ = fs::create_dir(&nm_path);
15    let mut child = Command::new(npm_bin)
16        .arg("install")
17        .arg("--audit=false")
18        .arg("--fund=false")
19        .arg(format!("{pkg_name}@{pkg_version}"))
20        .current_dir(out_dir)
21        .spawn()?;
22    let exit_status = child.wait()?;
23    if !exit_status.success() {
24        eprintln!("npm install did not exit successfully");
25        return Err(io::Error::other(Box::<dyn Error + Send + Sync>::from(format!(
26            "npm install returned exit code {exit_status}"
27        ))));
28    }
29    Ok(nm_path)
30}