forked from tuono-labs/tuono
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(crates/tuono): feat: add spinner to
tuono build
(tuono-labs#560)
- Loading branch information
1 parent
e3b1c0e
commit d37b655
Showing
4 changed files
with
91 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,6 @@ end_of_line = lf | |
indent_style = space | ||
indent_size = 2 | ||
max_line_length = 80 | ||
|
||
[*.rs] | ||
indent_size = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use fs_extra::dir::{copy, CopyOptions}; | ||
use spinners::{Spinner, Spinners}; | ||
use std::path::PathBuf; | ||
use std::thread::sleep; | ||
use std::time::Duration; | ||
|
||
use crate::app::App; | ||
use crate::mode::Mode; | ||
|
||
pub fn build(mut app: App, ssg: bool, no_js_emit: bool) { | ||
if no_js_emit { | ||
println!("Rust build successfully finished"); | ||
return; | ||
} | ||
|
||
if ssg && app.has_dynamic_routes() { | ||
// TODO: allow dynamic routes static generation | ||
println!("Cannot statically build dynamic routes"); | ||
return; | ||
} | ||
|
||
app.build_tuono_config() | ||
.expect("Failed to build tuono.config.ts"); | ||
|
||
let mut app_build_spinner = Spinner::new(Spinners::Dots, "Building app...".into()); | ||
|
||
app.check_server_availability(Mode::Prod); | ||
|
||
app.build_react_prod(); | ||
|
||
// Remove the spinner | ||
app_build_spinner.stop_with_message("\u{2705}Build completed".into()); | ||
|
||
if ssg { | ||
let mut app_build_static_spinner = | ||
Spinner::new(Spinners::Dots, "Static site generation".into()); | ||
|
||
let static_dir = PathBuf::from("out/static"); | ||
|
||
if static_dir.is_dir() { | ||
std::fs::remove_dir_all(&static_dir).expect("Failed to clear the out/static folder"); | ||
} | ||
|
||
std::fs::create_dir(&static_dir).expect("Failed to create static output dir"); | ||
|
||
copy( | ||
"./out/client", | ||
static_dir, | ||
&CopyOptions::new().overwrite(true).content_only(true), | ||
) | ||
.expect("Failed to clone assets into static output folder"); | ||
|
||
// Start the server | ||
#[allow(clippy::zombie_processes)] | ||
let mut rust_server = app.run_rust_server(); | ||
|
||
let reqwest_client = reqwest::blocking::Client::builder() | ||
.user_agent("") | ||
.build() | ||
.expect("Failed to build reqwest client"); | ||
|
||
// Wait for server | ||
let mut is_server_ready = false; | ||
let config = app.config.as_ref().unwrap(); | ||
|
||
while !is_server_ready { | ||
let server_url = format!("http://{}:{}", config.server.host, config.server.port); | ||
if reqwest_client.get(&server_url).send().is_ok() { | ||
is_server_ready = true; | ||
} | ||
sleep(Duration::from_secs(1)); | ||
} | ||
|
||
for (_, route) in app.route_map { | ||
route.save_ssg_file(&reqwest_client); | ||
} | ||
|
||
// Close server | ||
let _ = rust_server.kill(); | ||
|
||
app_build_static_spinner | ||
.stop_with_message("\u{2705}Static site generation completed".into()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters