/
build.rs
51 lines (38 loc) · 1.31 KB
/
build.rs
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
use std::{fs, path::PathBuf};
use anyhow::Result;
use crate::utils::{
bundle_function, debug, print_progress, success, validate_code_file, validate_public_dir,
};
pub fn build(file: PathBuf, client: Option<PathBuf>, public_dir: Option<PathBuf>) -> Result<()> {
validate_code_file(&file)?;
let client = match client {
Some(client) => {
validate_code_file(&client)?;
Some(client)
}
None => None,
};
let public_dir = validate_public_dir(public_dir)?;
let (index, assets) = bundle_function(&file, &client, &public_dir)?;
let end_progress = print_progress("Writting index.js...");
fs::create_dir_all(".lagon")?;
fs::write(".lagon/index.js", index.get_ref())?;
end_progress();
for (path, content) in assets {
let message = format!("Writting {}...", path);
let end_progress = print_progress(&message);
let dir = PathBuf::from(".lagon")
.join("public")
.join(PathBuf::from(&path).parent().unwrap());
fs::create_dir_all(dir)?;
fs::write(format!(".lagon/public/{}", path), content.get_ref())?;
end_progress();
}
println!();
println!(
"{} {}",
success("Build successful!"),
debug("You can find it in .lagon folder.")
);
Ok(())
}