-
Notifications
You must be signed in to change notification settings - Fork 477
Expand file tree
/
Copy pathmain.rs
More file actions
124 lines (101 loc) · 3.44 KB
/
main.rs
File metadata and controls
124 lines (101 loc) · 3.44 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#![allow(clippy::redundant_closure, clippy::redundant_pattern_matching)]
extern crate anyhow;
extern crate clap;
extern crate env_logger;
extern crate human_panic;
extern crate log;
extern crate wasm_pack;
extern crate which;
use anyhow::Result;
use clap::Parser;
use std::env;
use std::panic;
use std::sync::mpsc;
use std::thread;
use wasm_pack::{
build::{self, WasmPackVersion},
command::run_wasm_pack,
Cli, PBAR,
};
mod installer;
fn background_check_for_updates() -> mpsc::Receiver<Result<WasmPackVersion>> {
let (sender, receiver) = mpsc::channel();
let _detached_thread = thread::spawn(move || {
let wasm_pack_version = build::check_wasm_pack_versions();
if let Ok(wasm_pack_version) = wasm_pack_version {
if !wasm_pack_version.local.is_empty()
&& !wasm_pack_version.latest.is_empty()
&& wasm_pack_version.local != wasm_pack_version.latest
{
let _ = sender.send(Ok(wasm_pack_version));
}
} else {
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.chain() {
eprintln!("Caused by: {}", cause);
}
::std::process::exit(1);
}
}
fn run() -> Result<()> {
let wasm_pack_version = background_check_for_updates();
// Deprecate `init`
if let Some("init") = env::args().nth(1).as_deref() {
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::parse();
PBAR.set_log_level(args.log_level);
if args.quiet {
PBAR.set_quiet(true);
}
run_wasm_pack(args.cmd)?;
if let Ok(wasm_pack_version) = wasm_pack_version.try_recv() {
match wasm_pack_version {
Ok(wasm_pack_version) =>
PBAR.warn(&format!("There's a newer version of wasm-pack available, the new version is: {}, you are using: {}. \
To update, navigate to: https://drager.github.io/wasm-pack/installer/", wasm_pack_version.latest, wasm_pack_version.local)),
Err(err) => PBAR.warn(&format!("{}", err))
}
}
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");
}));
}
}