11//! Run command for development server
22
3+ use super :: watch:: { self , WatchArgs } ;
34use anyhow:: Result ;
45use clap:: Args ;
56use console:: style;
@@ -24,6 +25,10 @@ pub struct RunArgs {
2425 /// Watch for changes and auto-reload (like FastAPI's --reload)
2526 #[ arg( short, long, visible_alias = "reload" , alias = "hot" ) ]
2627 pub watch : bool ,
28+
29+ /// Package to run (for workspace projects)
30+ #[ arg( short = 'P' , long) ]
31+ pub package : Option < String > ,
2732}
2833
2934/// Run the development server
@@ -44,8 +49,31 @@ pub async fn run_dev(args: RunArgs) -> Result<()> {
4449 style( " Changes to source files will trigger automatic rebuild" ) . dim( )
4550 ) ;
4651 println ! ( ) ;
47- // Use cargo-watch if available
48- run_with_watch ( & args) . await
52+
53+ // Delegate to native watcher
54+ let watch_args = WatchArgs {
55+ command : "run" . to_string ( ) ,
56+ clear : false ,
57+ extensions : "rs,toml,html,css,sql" . to_string ( ) ,
58+ watch_paths : vec ! [
59+ "src" . to_string( ) ,
60+ "templates" . to_string( ) ,
61+ "migrations" . to_string( ) ,
62+ ] ,
63+ ignore_paths : vec ! [
64+ ".git" . to_string( ) ,
65+ "target" . to_string( ) ,
66+ "node_modules" . to_string( ) ,
67+ ] ,
68+ delay : 300 ,
69+ quiet : false ,
70+ no_restart_on_fail : false ,
71+ poll : false ,
72+ features : args. features ,
73+ release : args. release ,
74+ package : args. package ,
75+ } ;
76+ watch:: watch ( watch_args) . await
4977 } else {
5078 println ! (
5179 "{}" ,
@@ -64,73 +92,15 @@ async fn run_cargo(args: &RunArgs) -> Result<()> {
6492 cmd. arg ( "--release" ) ;
6593 }
6694
67- if let Some ( features) = & args. features {
68- cmd. arg ( "--features" ) . arg ( features. join ( "," ) ) ;
69- }
70-
71- cmd. stdout ( Stdio :: inherit ( ) )
72- . stderr ( Stdio :: inherit ( ) )
73- . stdin ( Stdio :: inherit ( ) ) ;
74-
75- let status = cmd. status ( ) . await ?;
76-
77- if !status. success ( ) {
78- anyhow:: bail!( "cargo run failed" ) ;
79- }
80-
81- Ok ( ( ) )
82- }
83-
84- async fn run_with_watch ( args : & RunArgs ) -> Result < ( ) > {
85- // Check if cargo-watch is installed
86- let check = Command :: new ( "cargo" )
87- . args ( [ "watch" , "--version" ] )
88- . output ( )
89- . await ;
90-
91- if check. is_err ( ) || !check. unwrap ( ) . status . success ( ) {
92- println ! ( "{}" , style( "cargo-watch not found. Installing..." ) . yellow( ) ) ;
93-
94- let install = Command :: new ( "cargo" )
95- . args ( [ "install" , "cargo-watch" ] )
96- . status ( )
97- . await ?;
98-
99- if !install. success ( ) {
100- println ! (
101- "{}" ,
102- style( "Failed to install cargo-watch. Running without watch mode." ) . yellow( )
103- ) ;
104- return run_cargo ( args) . await ;
105- }
95+ if let Some ( pkg) = & args. package {
96+ cmd. arg ( "-p" ) . arg ( pkg) ;
10697 }
10798
108- let mut cmd = Command :: new ( "cargo" ) ;
109- cmd. arg ( "watch" ) ;
110-
111- // Ignore heavy directories for better performance
112- cmd. args ( [
113- "-i" ,
114- ".git" ,
115- "-i" ,
116- "target" ,
117- "-i" ,
118- "node_modules" ,
119- "-i" ,
120- "assets" ,
121- ] ) ;
122-
123- // Build the run command string
124- let mut run_cmd = String :: from ( "run" ) ;
125- if args. release {
126- run_cmd. push_str ( " --release" ) ;
127- }
12899 if let Some ( features) = & args. features {
129- run_cmd . push_str ( & format ! ( " --features {}" , features. join( "," ) ) ) ;
100+ cmd . arg ( " --features" ) . arg ( features. join ( "," ) ) ;
130101 }
131102
132- // Pass the command with -x flag
133- cmd. arg ( "-x" ) . arg ( & run_cmd) ;
103+ cmd. env ( "RUSTAPI_ENV" , "development" ) ;
134104
135105 cmd. stdout ( Stdio :: inherit ( ) )
136106 . stderr ( Stdio :: inherit ( ) )
@@ -139,7 +109,7 @@ async fn run_with_watch(args: &RunArgs) -> Result<()> {
139109 let status = cmd. status ( ) . await ?;
140110
141111 if !status. success ( ) {
142- anyhow:: bail!( "cargo watch failed" ) ;
112+ anyhow:: bail!( "cargo run failed" ) ;
143113 }
144114
145115 Ok ( ( ) )
0 commit comments