Skip to content

Commit 5641d5c

Browse files
Merge pull request #90 from mgattozzi/compilation-abort
feat(refactor/early abort): Exit early on failure
2 parents 4bd93cc + cf8d95f commit 5641d5c

File tree

12 files changed

+293
-67
lines changed

12 files changed

+293
-67
lines changed

src/bindgen.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
1+
use PBAR;
12
use console::style;
23
use emoji;
4+
use failure::Error;
35
use std::process::Command;
4-
use PBAR;
56

6-
pub fn cargo_install_wasm_bindgen() {
7+
pub fn cargo_install_wasm_bindgen() -> Result<(), Error> {
78
let step = format!(
89
"{} {}Installing WASM-bindgen...",
910
style("[6/7]").bold().dim(),
1011
emoji::DOWN_ARROW
1112
);
1213
let pb = PBAR.message(&step);
13-
let _output = Command::new("cargo")
14+
let output = Command::new("cargo")
1415
.arg("install")
1516
.arg("wasm-bindgen")
16-
.output()
17-
.unwrap_or_else(|e| panic!("{} failed to execute process: {}", emoji::ERROR, e));
17+
.output()?;
1818
pb.finish();
19+
if !output.status.success() {
20+
let s = String::from_utf8_lossy(&output.stderr);
21+
PBAR.error("Installing wasm-bindgen failed");
22+
bail!(format!("Details:\n{}", s));
23+
} else {
24+
Ok(())
25+
}
1926
}
2027

21-
pub fn wasm_bindgen_build(path: &str, name: &str) {
28+
pub fn wasm_bindgen_build(path: &str, name: &str) -> Result<(), Error> {
2229
let step = format!(
2330
"{} {}Running WASM-bindgen...",
2431
style("[7/7]").bold().dim(),
@@ -27,12 +34,18 @@ pub fn wasm_bindgen_build(path: &str, name: &str) {
2734
let pb = PBAR.message(&step);
2835
let binary_name = name.replace("-", "_");
2936
let wasm_path = format!("target/wasm32-unknown-unknown/release/{}.wasm", binary_name);
30-
let _output = Command::new("wasm-bindgen")
37+
let output = Command::new("wasm-bindgen")
3138
.current_dir(path)
3239
.arg(&wasm_path)
3340
.arg("--out-dir")
3441
.arg("./pkg")
35-
.output()
36-
.unwrap_or_else(|e| panic!("{} failed to execute process: {}", emoji::ERROR, e));
42+
.output()?;
3743
pb.finish();
44+
if !output.status.success() {
45+
let s = String::from_utf8_lossy(&output.stderr);
46+
PBAR.error("wasm-bindgen failed to execute properly");
47+
bail!(format!("Details:\n{}", s));
48+
} else {
49+
Ok(())
50+
}
3851
}

src/build.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
use PBAR;
12
use console::style;
23
use emoji;
4+
use failure::Error;
35
use std::process::Command;
4-
use PBAR;
56

6-
pub fn rustup_add_wasm_target() {
7+
pub fn rustup_add_wasm_target() -> Result<(), Error> {
78
let step = format!(
89
"{} {}Adding WASM target...",
910
style("[1/7]").bold().dim(),
@@ -14,21 +15,18 @@ pub fn rustup_add_wasm_target() {
1415
.arg("target")
1516
.arg("add")
1617
.arg("wasm32-unknown-unknown")
17-
.output()
18-
.unwrap_or_else(|e| panic!("{} failed to execute process: {}", emoji::ERROR, e));
18+
.output()?;
1919
pb.finish();
2020
if !output.status.success() {
2121
let s = String::from_utf8_lossy(&output.stderr);
22-
23-
print!(
24-
"{} rustup_add_wasm_target failed and stderr was:\n{}",
25-
emoji::ERROR,
26-
s
27-
);
22+
PBAR.error("Adding the wasm32-unknown-unknown target failed");
23+
bail!(format!("Details:\n{}", s));
24+
} else {
25+
Ok(())
2826
}
2927
}
3028

31-
pub fn cargo_build_wasm(path: &str) {
29+
pub fn cargo_build_wasm(path: &str) -> Result<(), Error> {
3230
let step = format!(
3331
"{} {}Compiling to WASM...",
3432
style("[2/7]").bold().dim(),
@@ -41,16 +39,13 @@ pub fn cargo_build_wasm(path: &str) {
4139
.arg("--release")
4240
.arg("--target")
4341
.arg("wasm32-unknown-unknown")
44-
.output()
45-
.unwrap_or_else(|e| panic!("{} failed to execute process: {}", emoji::ERROR, e));
42+
.output()?;
4643
pb.finish();
4744
if !output.status.success() {
4845
let s = String::from_utf8_lossy(&output.stderr);
49-
50-
print!(
51-
"{} cargo_build_wasm failed and stderr was:\n{}",
52-
emoji::ERROR,
53-
s
54-
);
46+
PBAR.error("Compilation of your program failed");
47+
bail!(format!("Details:\n{}", s));
48+
} else {
49+
Ok(())
5550
}
5651
}

src/command.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use PBAR;
12
use bindgen;
23
use build;
34
use console::style;
@@ -6,12 +7,12 @@ use failure::Error;
67
use indicatif::HumanDuration;
78
use manifest;
89
use npm;
10+
#[allow(unused)]
911
use quicli::prelude::*;
1012
use readme;
1113
use std::fs;
1214
use std::result;
1315
use std::time::Instant;
14-
use PBAR;
1516

1617
#[derive(Debug, StructOpt)]
1718
pub enum Command {
@@ -30,6 +31,24 @@ pub enum Command {
3031
Publish { path: Option<String> },
3132
}
3233

34+
pub fn run_wasm_pack(command: Command) -> result::Result<(), Error> {
35+
// Run the correct command based off input and store the result of it so that we can clear
36+
// the progress bar then return it
37+
let status = match command {
38+
Command::Init { path, scope } => init(path, scope),
39+
Command::Pack { path } => pack(path),
40+
Command::Publish { path } => publish(path),
41+
};
42+
43+
// Make sure we always clear the progress bar before we abort the program otherwise
44+
// stderr and stdout output get eaten up and nothing will work. If this part fails
45+
// to work and clear the progress bars then you're really having a bad day with your tools.
46+
PBAR.done()?;
47+
48+
// Return the actual status of the program to the main function
49+
status
50+
}
51+
3352
// quicli::prelude::* imports a different result struct which gets
3453
// precedence over the std::result::Result, so have had to specify
3554
// the correct type here.
@@ -46,19 +65,19 @@ pub fn create_pkg_dir(path: &str) -> result::Result<(), Error> {
4665
Ok(())
4766
}
4867

49-
pub fn init(path: Option<String>, scope: Option<String>) -> result::Result<(), Error> {
68+
fn init(path: Option<String>, scope: Option<String>) -> result::Result<(), Error> {
5069
let started = Instant::now();
5170

5271
let crate_path = set_crate_path(path);
5372

54-
build::rustup_add_wasm_target();
55-
build::cargo_build_wasm(&crate_path);
73+
build::rustup_add_wasm_target()?;
74+
build::cargo_build_wasm(&crate_path)?;
5675
create_pkg_dir(&crate_path)?;
5776
manifest::write_package_json(&crate_path, scope)?;
5877
readme::copy_from_crate(&crate_path)?;
59-
bindgen::cargo_install_wasm_bindgen();
78+
bindgen::cargo_install_wasm_bindgen()?;
6079
let name = manifest::get_crate_name(&crate_path)?;
61-
bindgen::wasm_bindgen_build(&crate_path, &name);
80+
bindgen::wasm_bindgen_build(&crate_path, &name)?;
6281
PBAR.one_off_message(&format!(
6382
"{} Done in {}",
6483
emoji::SPARKLE,
@@ -69,23 +88,22 @@ pub fn init(path: Option<String>, scope: Option<String>) -> result::Result<(), E
6988
emoji::PACKAGE,
7089
&crate_path
7190
));
72-
PBAR.done()?;
7391
Ok(())
7492
}
7593

76-
pub fn pack(path: Option<String>) -> result::Result<(), Error> {
94+
fn pack(path: Option<String>) -> result::Result<(), Error> {
7795
let crate_path = set_crate_path(path);
7896

79-
npm::npm_pack(&crate_path);
80-
println!("🎒 packed up your package!");
97+
npm::npm_pack(&crate_path)?;
98+
PBAR.one_off_message("🎒 packed up your package!");
8199
Ok(())
82100
}
83101

84-
pub fn publish(path: Option<String>) -> result::Result<(), Error> {
102+
fn publish(path: Option<String>) -> result::Result<(), Error> {
85103
let crate_path = set_crate_path(path);
86104

87-
npm::npm_publish(&crate_path);
88-
println!("💥 published your package!");
105+
npm::npm_publish(&crate_path)?;
106+
PBAR.one_off_message("💥 published your package!");
89107
Ok(())
90108
}
91109

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
extern crate console;
2+
#[macro_use]
23
extern crate failure;
34
extern crate indicatif;
45
#[macro_use]

src/main.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,9 @@ extern crate indicatif;
55
extern crate quicli;
66

77
use quicli::prelude::*;
8-
use wasm_pack::command::{init, pack, publish, Command};
98
use wasm_pack::Cli;
9+
use wasm_pack::command::run_wasm_pack;
1010

11-
main!(|args: Cli, log_level: verbosity| match args.cmd {
12-
Command::Init { path, scope } => {
13-
init(path, scope)?;
14-
}
15-
Command::Pack { path } => {
16-
pack(path)?;
17-
}
18-
Command::Publish { path } => {
19-
publish(path)?;
20-
}
11+
main!(|args: Cli, log_level: verbosity| {
12+
run_wasm_pack(args.cmd)?;
2113
});

src/manifest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use std::fs::File;
22
use std::io::prelude::*;
33

4+
use PBAR;
45
use console::style;
56
use emoji;
67
use failure::Error;
78
use serde_json;
89
use toml;
9-
use PBAR;
1010

1111
#[derive(Deserialize)]
1212
struct CargoManifest {

src/npm.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1-
use emoji;
1+
use PBAR;
2+
use failure::Error;
23
use std::process::Command;
34

4-
pub fn npm_pack(path: &str) {
5+
pub fn npm_pack(path: &str) -> Result<(), Error> {
56
let pkg_file_path = format!("{}/pkg", path);
67
let output = Command::new("npm")
78
.current_dir(pkg_file_path)
89
.arg("pack")
9-
.output()
10-
.unwrap_or_else(|e| panic!("{} failed to execute process: {}", emoji::ERROR, e));
10+
.output()?;
1111
if !output.status.success() {
1212
let s = String::from_utf8_lossy(&output.stderr);
13-
print!("{} npm_pack failed and stderr was:\n{}", emoji::ERROR, s);
13+
PBAR.error("Packaging up your code failed");
14+
bail!(format!("Details:\n{}", s));
15+
} else {
16+
Ok(())
1417
}
1518
}
1619

17-
pub fn npm_publish(path: &str) {
20+
pub fn npm_publish(path: &str) -> Result<(), Error> {
1821
let pkg_file_path = format!("{}/pkg", path);
1922
let output = Command::new("npm")
2023
.current_dir(pkg_file_path)
2124
.arg("publish")
22-
.output()
23-
.unwrap_or_else(|e| panic!("{} failed to execute process: {}", emoji::ERROR, e));
25+
.output()?;
2426
if !output.status.success() {
2527
let s = String::from_utf8_lossy(&output.stderr);
26-
print!(
27-
"{} npm_publish failed and stderr was:\n{}",
28-
emoji::ERROR,
29-
s
30-
);
28+
PBAR.error("Publishing to npm failed");
29+
bail!(format!("Details:\n{}", s));
30+
} else {
31+
Ok(())
3132
}
3233
}

src/readme.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use console::style;
22
use failure::Error;
33
use std::fs;
44

5-
use emoji;
65
use PBAR;
6+
use emoji;
77

88
pub fn copy_from_crate(path: &str) -> Result<(), Error> {
99
let step = format!(

0 commit comments

Comments
 (0)