-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathmain.rs
110 lines (91 loc) · 3.14 KB
/
main.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#![allow(clippy::redundant_closure, clippy::redundant_pattern_matching)]
extern crate atty;
extern crate env_logger;
#[macro_use]
extern crate failure;
extern crate human_panic;
extern crate log;
extern crate structopt;
extern crate wasm_pack;
extern crate which;
use std::env;
use std::panic;
use std::sync::mpsc;
use std::thread;
use structopt::StructOpt;
use wasm_pack::{
build::{self, WasmPackVersion},
command::run_wasm_pack,
Cli, PBAR,
};
mod installer;
fn background_check_for_updates() -> mpsc::Receiver<WasmPackVersion> {
let (sender, receiver) = mpsc::channel();
let _detached_thread = thread::spawn(move || {
if let Ok(wasm_pack_version) = build::check_wasm_pack_versions() {
if !wasm_pack_version.local.is_empty()
&& !wasm_pack_version.latest.is_empty()
&& wasm_pack_version.local != wasm_pack_version.latest
{
let _ = sender.send(wasm_pack_version);
}
}
});
receiver
}
fn main() {
env_logger::init();
setup_panic_hooks();
if let Err(e) = run() {
eprintln!("Error: {}", e);
for cause in e.iter_causes() {
eprintln!("Caused by: {}", cause);
}
::std::process::exit(1);
}
}
fn run() -> Result<(), failure::Error> {
let wasm_pack_version = background_check_for_updates();
// Deprecate `init`
if let Some("init") = env::args().nth(1).as_ref().map(|arg| arg.as_str()) {
println!("wasm-pack init is deprecated, consider using wasm-pack build");
}
if let Ok(me) = env::current_exe() {
// If we're actually running as the installer then execute our
// self-installation, otherwise just continue as usual.
if me
.file_stem()
.and_then(|s| s.to_str())
.expect("executable should have a filename")
.starts_with("wasm-pack-init")
{
installer::install();
}
}
let args = Cli::from_args();
run_wasm_pack(args.cmd)?;
if let Ok(wasm_pack_version) = wasm_pack_version.try_recv() {
PBAR.warn(&format!("There's a newer version of wasm-pack available, the new version is: {}, you are using: {}. \
To update, navigate to: https://rustwasm.github.io/wasm-pack/installer/", wasm_pack_version.latest, wasm_pack_version.local));
}
Ok(())
}
fn setup_panic_hooks() {
let meta = human_panic::Metadata {
version: env!("CARGO_PKG_VERSION").into(),
name: env!("CARGO_PKG_NAME").into(),
authors: env!("CARGO_PKG_AUTHORS").replace(":", ", ").into(),
homepage: env!("CARGO_PKG_HOMEPAGE").into(),
};
let default_hook = panic::take_hook();
if let Err(_) = env::var("RUST_BACKTRACE") {
panic::set_hook(Box::new(move |info: &panic::PanicInfo| {
// First call the default hook that prints to standard error.
default_hook(info);
// Then call human_panic.
let file_path = human_panic::handle_dump(&meta, info);
human_panic::print_msg(file_path, &meta)
.expect("human-panic: printing error message to console failed");
}));
}
}