-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
51 lines (42 loc) · 1.35 KB
/
build.rs
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
49
50
51
use std::env;
use home::home_dir;
use std::process::Command;
use std::process::Child;
use which::which;
// ==================
// === ChildGuard ===
// ==================
struct ChildGuard(Child);
impl Drop for ChildGuard {
fn drop(&mut self) {
self.0.kill().ok();
}
}
// ============
// === Main ===
// ============
fn main() {
println!("cargo:rerun-if-changed=js");
println!("cargo:rerun-if-changed=build.rs");
let home_path = home_dir().unwrap();
let bin_path = home_path.join(".cargo").join("bin");
let crate_bin_js_path = bin_path.join("cargo-watch-plus-js");
let root_path_str = env::var("CARGO_MANIFEST_DIR").unwrap();
let root_path = std::path::Path::new(&root_path_str);
let js_sources = root_path.join("js");
let options = fs_extra::dir::CopyOptions{
content_only: true,
..Default::default()
};
std::fs::remove_dir_all(&crate_bin_js_path).ok();
std::fs::create_dir(&crate_bin_js_path).unwrap();
fs_extra::dir::copy(&js_sources, &crate_bin_js_path, &options).unwrap();
env::set_current_dir(&crate_bin_js_path).unwrap();
let npm = which("npm").expect("Program 'npm' not found.");
let child = Command::new(npm)
.arg("install")
.spawn()
.expect("Failed to execute 'npm' command.");
let mut guard = ChildGuard(child);
guard.0.wait().unwrap();
}