From db98e0e2fd29585a8aefbf4e8233f0cca121e171 Mon Sep 17 00:00:00 2001 From: Valerio Ageno Date: Sat, 18 May 2024 11:46:57 +0200 Subject: [PATCH] feat: create dev environment --- Cargo.toml | 4 ++- crates/axum_bundler/src/bundle.rs | 1 + crates/bundler/Cargo.toml | 18 ++++++++++++ crates/bundler/README.md | 1 + crates/bundler/src/bundler.rs | 48 +++++++++++++++++++++++++++++++ crates/bundler/src/lib.rs | 2 ++ crates/cli/Cargo.toml | 2 +- crates/cli/src/actions/dev.rs | 29 +++++++++---------- 8 files changed, 88 insertions(+), 17 deletions(-) create mode 100644 crates/bundler/Cargo.toml create mode 100644 crates/bundler/README.md create mode 100644 crates/bundler/src/bundler.rs create mode 100644 crates/bundler/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 6f787263..a4161eab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,9 @@ [workspace] +resolver = "2" members = [ "crates/cli", - "crates/axum_bundler" + "crates/axum_bundler", + "crates/bundler" ] exclude = [ "examples/playground" diff --git a/crates/axum_bundler/src/bundle.rs b/crates/axum_bundler/src/bundle.rs index 67fd4a1f..9cceb54e 100644 --- a/crates/axum_bundler/src/bundle.rs +++ b/crates/axum_bundler/src/bundle.rs @@ -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(); diff --git a/crates/bundler/Cargo.toml b/crates/bundler/Cargo.toml new file mode 100644 index 00000000..aa267ac4 --- /dev/null +++ b/crates/bundler/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "tuono_bundler" +version = "0.0.1" +edition = "2021" +authors = ["V. Ageno "] + +[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" } diff --git a/crates/bundler/README.md b/crates/bundler/README.md new file mode 100644 index 00000000..ff3793ec --- /dev/null +++ b/crates/bundler/README.md @@ -0,0 +1 @@ +# Tuono bundler diff --git a/crates/bundler/src/bundler.rs b/crates/bundler/src/bundler.rs new file mode 100644 index 00000000..5089986c --- /dev/null +++ b/crates/bundler/src/bundler.rs @@ -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(()) +} diff --git a/crates/bundler/src/lib.rs b/crates/bundler/src/lib.rs new file mode 100644 index 00000000..58536ce3 --- /dev/null +++ b/crates/bundler/src/lib.rs @@ -0,0 +1,2 @@ +pub mod bundler; + diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 7a12a64f..31218b9d 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -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"} diff --git a/crates/cli/src/actions/dev.rs b/crates/cli/src/actions/dev.rs index 42b100b1..53367676 100644 --- a/crates/cli/src/actions/dev.rs +++ b/crates/cli/src/actions/dev.rs @@ -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(); }