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
use std::io::{Error, ErrorKind};
use wasm_pack::{
    command::{
        build::{BuildOptions, Target},
        run_wasm_pack, Command,
    },
    install::InstallMode,
};

#[derive(Debug, Copy, Clone)]
pub enum BuildProfile {
    Debug,
    Release,
}

impl Default for BuildProfile {
    fn default() -> Self {
        Self::Debug
    }
}

pub fn build_project(
    profile: BuildProfile,
    crate_dir: Option<String>,
    out_dir: Option<String>,
    extra_options: Vec<String>,
) -> Result<(), Error> {
    let options = BuildOptions {
        path: crate_dir.map(|p| p.into()),
        scope: None,
        mode: InstallMode::Normal,
        disable_dts: true,
        target: Target::Web,
        debug: false,
        dev: matches!(profile, BuildProfile::Debug),
        release: matches!(profile, BuildProfile::Release),
        profiling: false,
        out_dir: out_dir.unwrap_or_else(|| "bin".to_owned()),
        out_name: Some("index".to_owned()),
        extra_options,
    };
    match run_wasm_pack(Command::Build(options)) {
        Ok(_) => Ok(()),
        Err(error) => Err(Error::new(
            ErrorKind::NotFound,
            format!("Build error: {:?}", error),
        )),
    }
}