Skip to content

Commit c6d50ea

Browse files
committed
integrate build_helper::npm into js checks and package.json usage
1 parent ca39010 commit c6d50ea

File tree

3 files changed

+34
-54
lines changed

3 files changed

+34
-54
lines changed

src/build_helper/src/npm.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,34 @@ use std::path::{Path, PathBuf};
33
use std::process::Command;
44
use std::{fs, io};
55

6-
/// Install an exact package version, and return the path of `node_modules`.
7-
pub fn install_one(
8-
out_dir: &Path,
9-
npm_bin: &Path,
10-
pkg_name: &str,
11-
pkg_version: &str,
12-
) -> Result<PathBuf, io::Error> {
6+
use crate::ci::CiEnv;
7+
8+
/// Install all the npm deps, and return the path of `node_modules`.
9+
pub fn install(src_root_path: &Path, out_dir: &Path, npm: &Path) -> Result<PathBuf, io::Error> {
1310
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()?;
11+
let copy_to_build = |p| {
12+
fs::copy(src_root_path.join(p), out_dir.join(p)).map_err(|e| {
13+
eprintln!("unable to copy {p:?} to build directory: {e:?}");
14+
e
15+
})
16+
};
17+
// copy stuff to the output directory to make node_modules get put there.
18+
copy_to_build("package.json")?;
19+
copy_to_build("package-lock.json")?;
20+
21+
let mut cmd = Command::new(npm);
22+
if CiEnv::is_ci() {
23+
// `npm ci` redownloads every time and thus is too slow for local development.
24+
cmd.arg("ci");
25+
} else {
26+
cmd.arg("install");
27+
}
28+
// disable a bunch of things we don't want.
29+
// this makes tidy output less noisy, and also significantly improves runtime
30+
// of repeated tidy invokations.
31+
cmd.args(&["--audit=false", "--save=false", "--fund=false"]);
32+
cmd.current_dir(out_dir);
33+
let exit_status = cmd.spawn()?.wait()?;
2334
if !exit_status.success() {
2435
eprintln!("npm install did not exit successfully");
2536
return Err(io::Error::other(Box::<dyn Error + Send + Sync>::from(format!(

src/tools/rustdoc-gui-test/src/main.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,8 @@ fn main() -> Result<(), ()> {
6565
}
6666
}
6767

68-
// FIXME(binarycat): once we get package.json in version control, this should be updated to install via that instead
69-
let local_node_modules =
70-
npm::install_one(&config.out_dir, &config.npm, "browser-ui-test", "0.21.1")
71-
.expect("unable to install browser-ui-test");
68+
let local_node_modules = npm::install(&config.rust_src, &config.out_dir, &config.npm)
69+
.expect("unable to install browser-ui-test");
7270

7371
let mut command = Command::new(&config.nodejs);
7472

src/tools/tidy/src/ext_tool_checks/rustdoc_js.rs

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
//! characters.
33
44
use std::ffi::OsStr;
5+
use std::io;
56
use std::path::{Path, PathBuf};
67
use std::process::{Child, Command};
7-
use std::{fs, io};
88

9-
use build_helper::ci::CiEnv;
9+
use build_helper::npm;
1010
use ignore::DirEntry;
1111

1212
use crate::walk::walk_no_read;
@@ -24,38 +24,9 @@ fn spawn_cmd(cmd: &mut Command) -> Result<Child, io::Error> {
2424

2525
/// install all js dependencies from package.json.
2626
pub(super) fn npm_install(root_path: &Path, outdir: &Path) -> Result<(), super::Error> {
27-
let copy_to_build = |p| {
28-
fs::copy(root_path.join(p), outdir.join(p)).map_err(|e| {
29-
eprintln!("unable to copy {p:?} to build directory: {e:?}");
30-
e
31-
})
32-
};
33-
// copy stuff to the output directory to make node_modules get put there.
34-
copy_to_build("package.json")?;
35-
copy_to_build("package-lock.json")?;
36-
37-
let mut cmd = Command::new("npm");
38-
if CiEnv::is_ci() {
39-
// `npm ci` redownloads every time and thus is too slow for local development.
40-
cmd.arg("ci");
41-
} else {
42-
cmd.arg("install");
43-
}
44-
// disable a bunch of things we don't want.
45-
// this makes tidy output less noisy, and also significantly improves runtime
46-
// of repeated tidy invokations.
47-
cmd.args(&["--audit=false", "--save=false", "--fund=false"]);
48-
cmd.current_dir(outdir);
49-
let mut child = spawn_cmd(&mut cmd)?;
50-
match child.wait() {
51-
Ok(exit_status) => {
52-
if exit_status.success() {
53-
return Ok(());
54-
}
55-
Err(super::Error::FailedCheck("npm install"))
56-
}
57-
Err(error) => Err(super::Error::Generic(format!("npm install failed: {error:?}"))),
58-
}
27+
// FIXME(lolbinarycat): make this obey build.npm bootstrap option
28+
npm::install(root_path, outdir, Path::new("npm"))?;
29+
Ok(())
5930
}
6031

6132
fn rustdoc_js_files(librustdoc_path: &Path) -> Vec<PathBuf> {

0 commit comments

Comments
 (0)