From 2b74e4ba695f9e0a94367849b7d3485457f2c5bf Mon Sep 17 00:00:00 2001 From: Dotan Nahum Date: Thu, 26 Sep 2024 09:08:34 +0300 Subject: [PATCH 1/3] dx: loco watch reloads on changes --- src/cli.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/src/cli.rs b/src/cli.rs index 7f20e737e..84fbfa5f2 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -27,6 +27,7 @@ cfg_if::cfg_if! { use std::path::PathBuf; use clap::{Parser, Subcommand}; +use duct::cmd; use crate::{ app::{AppContext, Hooks}, @@ -36,7 +37,7 @@ use crate::{ }, environment::{resolve_from_env, Environment, DEFAULT_ENVIRONMENT}, gen::{self, Component, ScaffoldKind}, - logger, task, + logger, task, Error, }; #[derive(Parser)] #[command(author, version, about, long_about = None)] @@ -125,6 +126,16 @@ enum Commands { }, /// Display the app version Version {}, + + /// Watch and restart the app + Watch { + /// start worker + #[arg(short, long, action)] + worker: bool, + /// start same-process server and worker + #[arg(short, long, action)] + server_and_worker: bool, + }, } #[derive(Subcommand)] @@ -399,6 +410,7 @@ pub async fn playground() -> crate::Result { /// } /// ``` #[cfg(feature = "with-db")] +#[allow(clippy::too_many_lines)] pub async fn main() -> crate::Result<()> { let cli: Cli = Cli::parse(); let environment: Environment = cli.environment.unwrap_or_else(resolve_from_env).into(); @@ -485,6 +497,28 @@ pub async fn main() -> crate::Result<()> { Commands::Version {} => { println!("{}", H::app_version(),); } + + Commands::Watch { + worker, + server_and_worker, + } => { + // cargo-watch -s 'cargo loco start' + let mut subcmd = vec!["cargo", "loco", "start"]; + if worker { + subcmd.push("--worker"); + } else if server_and_worker { + subcmd.push("--server-and-worker"); + } + + cmd("cargo-watch", &["-s", &subcmd.join(" ")]) + .run() + .map_err(|err| { + Error::Message(format!( + "failed to start with `cargo-watch`. Did you `cargo install \ + cargo-watch`?. error details: `{err}`", + )) + })?; + } } Ok(()) } @@ -552,6 +586,27 @@ pub async fn main() -> crate::Result<()> { Commands::Version {} => { println!("{}", H::app_version(),); } + Commands::Watch { + worker, + server_and_worker, + } => { + // cargo-watch -s 'cargo loco start' + let mut subcmd = vec!["cargo", "loco", "start"]; + if worker { + subcmd.push("--worker"); + } else if server_and_worker { + subcmd.push("--server-and-worker"); + } + + cmd("cargo-watch", &["-s", &subcmd.join(" ")]) + .run() + .map_err(|err| { + Error::Message(format!( + "failed to start with `cargo-watch`. Did you `cargo install \ + cargo-watch`?. error details: `{err}`", + )) + })?; + } } Ok(()) } From a1a5926510b788186f8634fe3d10f1dd1531e81d Mon Sep 17 00:00:00 2001 From: Dotan Nahum Date: Thu, 26 Sep 2024 09:13:13 +0300 Subject: [PATCH 2/3] dx: add typical command aliases --- src/cli.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/cli.rs b/src/cli.rs index 84fbfa5f2..036a50eae 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -63,6 +63,7 @@ struct Cli { #[derive(Subcommand)] enum Commands { /// Start an app + #[clap(alias("s"))] Start { /// start worker #[arg(short, long, action)] @@ -86,6 +87,7 @@ enum Commands { /// Describe all application endpoints Routes {}, /// Run a custom task + #[clap(alias("t"))] Task { /// Task name (identifier) name: Option, @@ -112,6 +114,7 @@ enum Commands { }, /// code generation creates a set of files and code templates based on a /// predefined set of rules. + #[clap(alias("g"))] Generate { /// What to generate #[command(subcommand)] @@ -128,6 +131,7 @@ enum Commands { Version {}, /// Watch and restart the app + #[clap(alias("w"))] Watch { /// start worker #[arg(short, long, action)] From 050974874029eac5bc5960f92dc206fab976a884 Mon Sep 17 00:00:00 2001 From: Dotan Nahum Date: Thu, 26 Sep 2024 09:42:02 +0300 Subject: [PATCH 3/3] fix: ci --- examples/demo/tests/cmd/cli.trycmd | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/demo/tests/cmd/cli.trycmd b/examples/demo/tests/cmd/cli.trycmd index 3f8f776d6..5c3503ef7 100644 --- a/examples/demo/tests/cmd/cli.trycmd +++ b/examples/demo/tests/cmd/cli.trycmd @@ -14,6 +14,7 @@ Commands: generate code generation creates a set of files and code templates based on a predefined set of rules doctor Validate and diagnose configurations version Display the app version + watch Watch and restart the app help Print this message or the help of the given subcommand(s) Options: