Skip to content

Commit 99d4420

Browse files
committed
Don't ask tidy users to run npm install eslint
`npx` already obviates the need to have the package installed locally, we can just ask `npx` to run the pre-configured version of `eslint`
1 parent 1ed0cbf commit 99d4420

File tree

1 file changed

+21
-38
lines changed

1 file changed

+21
-38
lines changed

src/tools/tidy/src/rustdoc_js.rs

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use ignore::DirEntry;
99

1010
use crate::walk::walk_no_read;
1111

12-
fn run_eslint(args: &[PathBuf], config_folder: PathBuf, bad: &mut bool) {
12+
fn run_eslint(eslint_version: &str, args: &[PathBuf], config_folder: PathBuf, bad: &mut bool) {
1313
let mut child = match Command::new("npx")
14-
.arg("eslint")
14+
.arg(format!("eslint@{eslint_version}"))
1515
.arg("-c")
1616
.arg(config_folder.join(".eslintrc.js"))
1717
.args(args)
@@ -36,19 +36,8 @@ fn run_eslint(args: &[PathBuf], config_folder: PathBuf, bad: &mut bool) {
3636
*bad = true;
3737
}
3838

39-
fn get_eslint_version_inner(global: bool) -> Option<String> {
40-
let mut command = Command::new("npm");
41-
command.arg("list").arg("--parseable").arg("--long").arg("--depth=0");
42-
if global {
43-
command.arg("--global");
44-
}
45-
let output = command.output().ok()?;
46-
let lines = String::from_utf8_lossy(&output.stdout);
47-
lines.lines().find_map(|l| l.split(':').nth(1)?.strip_prefix("eslint@")).map(|v| v.to_owned())
48-
}
49-
50-
fn get_eslint_version() -> Option<String> {
51-
get_eslint_version_inner(false).or_else(|| get_eslint_version_inner(true))
39+
fn has_npx() -> bool {
40+
Command::new("npxa").arg("--version").output().is_ok_and(|output| output.status.success())
5241
}
5342

5443
pub fn check(librustdoc_path: &Path, tools_path: &Path, src_path: &Path, bad: &mut bool) {
@@ -62,26 +51,10 @@ pub fn check(librustdoc_path: &Path, tools_path: &Path, src_path: &Path, bad: &m
6251
return;
6352
}
6453
};
65-
match get_eslint_version() {
66-
Some(version) => {
67-
if version != eslint_version {
68-
*bad = true;
69-
eprintln!(
70-
"⚠️ Installed version of eslint (`{version}`) is different than the \
71-
one used in the CI (`{eslint_version}`)",
72-
);
73-
eprintln!(
74-
"You can install this version using `npm update eslint` or by using \
75-
`npm install eslint@{eslint_version}`",
76-
);
77-
return;
78-
}
79-
}
80-
None => {
81-
eprintln!("`eslint` doesn't seem to be installed. Skipping tidy check for JS files.");
82-
eprintln!("You can install it using `npm install eslint@{eslint_version}`");
83-
return;
84-
}
54+
if !has_npx() {
55+
eprintln!("`npx` doesn't seem to be installed. Skipping tidy check for JS files.");
56+
eprintln!("Installing `node` should also install `npm` and `npx` on your system.");
57+
return;
8558
}
8659
let mut files_to_check = Vec::new();
8760
walk_no_read(
@@ -92,8 +65,18 @@ pub fn check(librustdoc_path: &Path, tools_path: &Path, src_path: &Path, bad: &m
9265
},
9366
);
9467
println!("Running eslint on rustdoc JS files");
95-
run_eslint(&files_to_check, librustdoc_path.join("html/static"), bad);
68+
run_eslint(&eslint_version, &files_to_check, librustdoc_path.join("html/static"), bad);
9669

97-
run_eslint(&[tools_path.join("rustdoc-js/tester.js")], tools_path.join("rustdoc-js"), bad);
98-
run_eslint(&[tools_path.join("rustdoc-gui/tester.js")], tools_path.join("rustdoc-gui"), bad);
70+
run_eslint(
71+
&eslint_version,
72+
&[tools_path.join("rustdoc-js/tester.js")],
73+
tools_path.join("rustdoc-js"),
74+
bad,
75+
);
76+
run_eslint(
77+
&eslint_version,
78+
&[tools_path.join("rustdoc-gui/tester.js")],
79+
tools_path.join("rustdoc-gui"),
80+
bad,
81+
);
9982
}

0 commit comments

Comments
 (0)