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
use crate::settings::toml::{Target, TargetType};
use crate::terminal::message::{Message, StdErr};
use crate::terminal::styles;
use crate::wranglerjs;
use crate::{commands, install};

use std::path::PathBuf;
use std::process::Command;

// Internal build logic, called by both `build` and `publish`
// TODO: return a struct containing optional build info and construct output at command layer
pub fn build_target(target: &Target) -> Result<String, failure::Error> {
    let target_type = &target.target_type;
    match target_type {
        TargetType::JavaScript => {
            let msg = "JavaScript project found. Skipping unnecessary build!".to_string();
            Ok(msg)
        }
        TargetType::Rust => {
            let _ = which::which("rustc").map_err(|e| {
                failure::format_err!(
                    "'rustc' not found: {}. Installation documentation can be found here: {}",
                    e,
                    styles::url("https://www.rust-lang.org/tools/install")
                )
            })?;

            let binary_path = install::install_wasm_pack()?;
            let args = ["build", "--target", "no-modules"];

            let command = command(&args, &binary_path);
            let command_name = format!("{:?}", command);

            commands::run(command, &command_name)?;
            let msg = "Build succeeded".to_string();
            Ok(msg)
        }
        TargetType::Webpack => match wranglerjs::run_build(target) {
            Ok(output) => {
                let msg = format!(
                    "Built successfully, built project size is {}",
                    output.project_size()
                );
                Ok(msg)
            }
            Err(e) => Err(e),
        },
    }
}

pub fn command(args: &[&str], binary_path: &PathBuf) -> Command {
    StdErr::working("Compiling your project to WebAssembly...");

    let mut c = if cfg!(target_os = "windows") {
        let mut c = Command::new("cmd");
        c.arg("/C");
        c.arg(binary_path);
        c
    } else {
        Command::new(binary_path)
    };

    c.args(args);
    c
}