feat: create dev environment

This commit is contained in:
Valerio Ageno
2024-05-18 11:46:57 +02:00
parent fc1d9013f8
commit db98e0e2fd
8 changed files with 88 additions and 17 deletions
+1
View File
@@ -31,6 +31,7 @@ async fn main() {
let app = Router::new()
// ROUTE_BUILDER
.nest_service("/__tuono", ServeDir::new(".tuono"))
.fallback_service(ServeDir::new("public").fallback(get(catch_all)));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
+18
View File
@@ -0,0 +1,18 @@
[package]
name = "tuono_bundler"
version = "0.0.1"
edition = "2021"
authors = ["V. Ageno <valerioageno@yahoo.it>"]
[lib]
name = "bundler"
path = "src/lib.rs"
[dependencies]
watchexec = "4.0.0"
miette = "7.2.0"
watchexec-signals = "3.0.0"
tokio = { version = "1", features = ["full"] }
watchexec-supervisor = "2.0.0"
axum_bundler = { path = "../axum_bundler" }
+1
View File
@@ -0,0 +1 @@
# Tuono bundler
+48
View File
@@ -0,0 +1,48 @@
use std::sync::Arc;
use watchexec_supervisor::command::{Command, Program};
use axum_bundler::bundle;
use miette::{IntoDiagnostic, Result};
use watchexec::Watchexec;
use watchexec_signals::Signal;
use watchexec_supervisor::job::start_job;
#[tokio::main]
pub async fn watch() -> Result<()> {
let (job, _) = start_job(Arc::new(Command {
program: Program::Exec {
prog: "cargo".into(),
args: vec!["run".to_string()],
}
.into(),
options: Default::default(),
}));
job.start().await;
let wx = Watchexec::new(move |mut action| {
for event in action.events.iter() {
for path in event.paths() {
if path.0.to_string_lossy().ends_with(".rs") {
job.stop();
println!("Reloading server...");
bundle();
job.start();
}
}
}
// if Ctrl-C is received, quit
if action.signals().any(|sig| sig == Signal::Interrupt) {
action.quit();
}
action
})?;
// watch the current directory
wx.config.pathset(["./src"]);
let _ = wx.main().await.into_diagnostic()?;
Ok(())
}
+2
View File
@@ -0,0 +1,2 @@
pub mod bundler;
+1 -1
View File
@@ -10,6 +10,6 @@ path = "src/lib.rs"
[dependencies]
clap = { version = "4.5.4", features = ["derive"] }
cargo-watch = "8.5.2"
axum_bundler = { path = "../axum_bundler" }
tuono_bundler = { path = "../bundler"}
+14 -15
View File
@@ -1,24 +1,23 @@
use bundler::bundler;
use std::path::Path;
use std::process::{Command, Stdio};
use std::process::Command;
use std::thread;
fn spawn_vite_process() -> std::io::Result<()> {
println!("Spawing vite process");
let current_dir = std::env::current_dir()?;
pub fn run() {
let current_dir = std::env::current_dir().unwrap();
let vite_path = Path::new("node_modules/.bin/vite");
let vite = current_dir.join(vite_path);
dbg!(&vite);
let vite_handler = thread::spawn(move || {
println!("Spawing vite process");
let _ = Command::new(vite).arg("dev").output();
});
let _ = Command::new(vite)
.arg("dev")
.stdout(Stdio::inherit())
.output()?;
let build_rs_handler = thread::spawn(move || {
bundler::watch().unwrap();
});
Ok(())
}
pub fn run() {
println!("Running dev environment");
let _ = spawn_vite_process();
let _ = vite_handler.join();
let _ = build_rs_handler.join();
}