-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.rs
More file actions
48 lines (44 loc) · 1.36 KB
/
Copy pathbuild.rs
File metadata and controls
48 lines (44 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use std::fs;
use std::process::Command;
const INSTALL_STAMP: &str = "node_modules/.install-stamp";
fn main() {
for path in ["package.json", "package-lock.json", "input.css", "src"] {
println!("cargo:rerun-if-changed={}", path);
}
if gt_mtime("package-lock.json", INSTALL_STAMP) {
npm(&["install"]);
fs::File::create(INSTALL_STAMP).unwrap();
}
npm(&["run", "build"]);
}
fn npm(args: &[&str]) {
let npm_cmd = if cfg!(target_os = "windows") {
"npm.cmd"
} else {
"npm"
};
let output = Command::new(npm_cmd)
.args(args)
.output()
.map_err(|e| {
// Simulate missing npm using PATH=~/.cargo/bin:/usr/bin cargo check
panic!(
"npm not found, please install it: https://nodejs.org/en/download\nerror: {}",
e
)
})
.unwrap();
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
panic!("command 'npm {}' failed:\n{}", args.join(" "), stderr);
}
}
fn gt_mtime(left: &str, right: &str) -> bool {
fn get_mtime(path: &str) -> std::io::Result<std::time::SystemTime> {
fs::metadata(path)?.modified()
}
match (get_mtime(left), get_mtime(right)) {
(Ok(left_mtime), Ok(right_mtime)) => left_mtime > right_mtime,
_ => true,
}
}