Skip to content

Commit

Permalink
Create separate dist dir for backend
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Oct 29, 2023
1 parent cd49723 commit 2021efe
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,5 +267,9 @@ fn print_project_info(info: &ProjectInfo) {
println!("[Web project dir] {}", info.web_project_path.display());
println!("[Core project dir] {}", info.core_project_path.display());
println!("[Wasm project dir] {}", info.wasm_project_path.display());
println!(
"[Cloudflare project dir] {}",
info.cloudflare_project_path.display()
);
println!("");
}
6 changes: 6 additions & 0 deletions src/project_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub struct ProjectInfo {
pub web_project_path: PathBuf,
pub core_project_path: PathBuf,
pub wasm_project_path: PathBuf,
pub cloudflare_project_path: PathBuf,
pub backend_dist_path: PathBuf,
}

impl ProjectInfo {
Expand All @@ -35,6 +37,8 @@ impl ProjectInfo {
let web_project_path = current_dir.join(format!("{}_web", project_name));
let core_project_path = current_dir.join(format!("{}_core", project_name));
let wasm_project_path = current_dir.join(format!("{}_wasm", project_name));
let cloudflare_project_path = current_dir.join(format!("{}_cloudflare", project_name));
let backend_dist_path = cloudflare_project_path.join("dist_backend");

Path::new(&web_project_path)
.exists()
Expand All @@ -52,6 +56,8 @@ impl ProjectInfo {
web_project_path,
core_project_path,
wasm_project_path,
cloudflare_project_path,
backend_dist_path,
})
}

Expand Down
29 changes: 21 additions & 8 deletions src/rust_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@ use std::path::PathBuf;
pub struct Config {
pub env: Env,
pub project_name: String,
pub dist_path: PathBuf,
pub frontend_dist_path: PathBuf,
pub backend_dist_path: PathBuf,
pub web_project_path: PathBuf,
pub wasm_project_path: PathBuf,
pub cloudflare_project_path: PathBuf,
}

impl Config {
pub fn from_project_info(env: &Env, project_info: &ProjectInfo) -> Self {
Self {
env: env.clone(),
project_name: project_info.project_name.clone(),
dist_path: project_info.dist_path.clone(),
frontend_dist_path: project_info.dist_path.clone(),
backend_dist_path: project_info.backend_dist_path.clone(),
web_project_path: project_info.web_project_path.clone(),
wasm_project_path: project_info.wasm_project_path.clone(),
cloudflare_project_path: project_info.cloudflare_project_path.clone(),
}
}

Expand Down Expand Up @@ -113,6 +117,8 @@ impl RustBuilder {
})
.map_err(Error::WasmPack)?;

self.copy_wasm_to_frontend_dist()?;

exec::run(&exec::Config {
work_dir: self.config.wasm_project_path.clone(),
cmd: "wasm-pack".into(),
Expand All @@ -133,7 +139,7 @@ impl RustBuilder {
.map_err(Error::WasmPack)?;

self.patch_backend_wasm_glue()?;
self.copy_wasm_to_dist()?;
self.copy_wasm_to_backend_dist()?;

Ok(())
}
Expand Down Expand Up @@ -167,6 +173,8 @@ impl RustBuilder {
})
.map_err(Error::WasmPack)?;

self.copy_wasm_to_frontend_dist()?;

exec::run(&exec::Config {
work_dir: self.config.wasm_project_path.clone(),
cmd: "wasm-pack".into(),
Expand All @@ -187,13 +195,14 @@ impl RustBuilder {
.map_err(Error::WasmPack)?;

self.patch_backend_wasm_glue()?;
self.copy_wasm_to_dist()?;
self.copy_wasm_to_backend_dist()?;

Ok(())
}

fn prepare_dirs(&self) -> Result<(), Error> {
fs::create_dir_all(&self.config.dist_path).map_err(Error::CreateDistDir)?;
fs::create_dir_all(&self.config.frontend_dist_path).map_err(Error::CreateDistDir)?;
fs::create_dir_all(&self.config.backend_dist_path).map_err(Error::CreateDistDir)?;
fs::create_dir_all(&self.config.web_project_wasm_frontend_path())
.map_err(Error::CreateWebWasmDir)?;
fs::create_dir_all(&self.config.web_project_wasm_backend_path())
Expand All @@ -202,20 +211,24 @@ impl RustBuilder {
Ok(())
}

fn copy_wasm_to_dist(&self) -> Result<(), Error> {
fn copy_wasm_to_frontend_dist(&self) -> Result<(), Error> {
fs_extra::dir::copy(
&self.config.web_project_wasm_frontend_path(),
&self.config.dist_path,
&self.config.frontend_dist_path,
&fs_extra::dir::CopyOptions {
overwrite: true,
..fs_extra::dir::CopyOptions::default()
},
)
.map_err(Error::CopyWasmToDist)?;

Ok(())
}

fn copy_wasm_to_backend_dist(&self) -> Result<(), Error> {
fs_extra::dir::copy(
&self.config.web_project_wasm_backend_path(),
&self.config.dist_path,
&self.config.backend_dist_path,
&fs_extra::dir::CopyOptions {
overwrite: true,
..fs_extra::dir::CopyOptions::default()
Expand Down

0 comments on commit 2021efe

Please sign in to comment.