stateroom_cli/commands/
build.rs

1use crate::build_util::{do_build, locate_config};
2use anyhow::Context;
3use fs_extra::dir::CopyOptions;
4use std::{
5    fs::{copy, create_dir, remove_dir_all},
6    path::Path,
7};
8
9const OUTPUT_DIR: &str = "dist";
10const STATIC_DIR: &str = "static";
11
12pub fn build() -> anyhow::Result<()> {
13    let config = locate_config()?; // TODO: default to a configuration if file not found.
14
15    let build_result = do_build(&config)?;
16
17    if Path::new(OUTPUT_DIR).exists() {
18        remove_dir_all(OUTPUT_DIR).context("Couldn't delete dist directory.")?;
19    }
20    create_dir(OUTPUT_DIR).context("Couldn't create dist directory.")?;
21
22    copy(
23        build_result.server_wasm,
24        Path::new(OUTPUT_DIR).join("server.wasm"),
25    )
26    .context("Couldn't copy server.wasm.")?;
27
28    create_dir(Path::new(OUTPUT_DIR).join(STATIC_DIR))
29        .context("Couldn't create empty static directory.")?;
30
31    if let Some(static_dir) = config.static_files {
32        fs_extra::dir::copy(
33            static_dir,
34            Path::new(OUTPUT_DIR).join(STATIC_DIR),
35            &CopyOptions {
36                content_only: true,
37                ..CopyOptions::default()
38            },
39        )
40        .context("Couldn't copy static items.")?;
41    }
42
43    if let Some(client_wasm) = build_result.client_wasm {
44        fs_extra::dir::copy(
45            client_wasm,
46            Path::new(OUTPUT_DIR).join(STATIC_DIR).join("client"),
47            &CopyOptions {
48                copy_inside: true,
49                ..CopyOptions::default()
50            },
51        )
52        .context("Couldn't copy client wasm.")?;
53    }
54
55    Ok(())
56}