diff --git a/src/bin/oseda.rs b/src/bin/oseda.rs index 7bd1729..4abccd8 100644 --- a/src/bin/oseda.rs +++ b/src/bin/oseda.rs @@ -22,7 +22,7 @@ fn main() { Commands::Init(options) => { init::init(options).map(|_| println!("Successfully initialized oseda project")) } - Commands::Run => run::run() + Commands::Run(options) => run::run(options) .map(|_| println!("Successfully ran oseda project")) .map_err(|e| e.into()), Commands::Check(options) => check::check(options) diff --git a/src/cmd/check.rs b/src/cmd/check.rs index e61e604..a886f14 100644 --- a/src/cmd/check.rs +++ b/src/cmd/check.rs @@ -112,7 +112,7 @@ fn verify_project(port_num: u16) -> OsedaProjectStatus { // use shutdown hook and kill once polled as alive let run_handle = std::thread::spawn(move || { - let _ = run::run_with_shutdown(shutdown_flag_clone); + let _ = run::run_with_shutdown(port_num, shutdown_flag_clone); }); let addr = format!("http://localhost:{}", port_num); diff --git a/src/cmd/export.rs b/src/cmd/export.rs index 1f351d7..c43f0bf 100644 --- a/src/cmd/export.rs +++ b/src/cmd/export.rs @@ -79,7 +79,7 @@ pub fn export(opts: ExportOptions) -> Result<(), Box> { let shutdown_flag = Arc::new(AtomicBool::new(false)); let run_flag = shutdown_flag.clone(); - let run_handle = std::thread::spawn(move || run::run_with_shutdown(run_flag)); + let run_handle = std::thread::spawn(move || run::run_with_shutdown(opts.port, run_flag)); // wait a moment for the localhost server to spin up std::thread::sleep(std::time::Duration::from_millis(10000)); diff --git a/src/cmd/run.rs b/src/cmd/run.rs index cc50171..c79043e 100644 --- a/src/cmd/run.rs +++ b/src/cmd/run.rs @@ -1,3 +1,4 @@ +use clap::Args; use std::{ process::Command, sync::{ @@ -32,6 +33,14 @@ impl std::fmt::Display for OsedaRunError { } } +/// Options struct for the run subcommand +#[derive(Args, Debug, Clone)] +pub struct RunOptions { + /// Port the project runs on + #[arg(long, default_value_t = 3000)] + pub port: u16, +} + /// Runs an Oseda project in the working directory /// /// This will: @@ -42,17 +51,17 @@ impl std::fmt::Display for OsedaRunError { /// # Returns /// * `Ok(())` if both the build and serve steps succeed /// * `Err(OsedaRunError)` if any step fails (missing vite isn't installed, or `serve` fails to start) -pub fn run() -> Result<(), OsedaRunError> { +pub fn run(opts: RunOptions) -> Result<(), OsedaRunError> { if !is_cwd_oseda_project() { return Err(OsedaRunError::NotOsedaProjectError( "oseda-config.json not found".to_string(), )); } - run_with_shutdown(Arc::new(AtomicBool::new(false))) + run_with_shutdown(opts.port, Arc::new(AtomicBool::new(false))) } -pub fn run_with_shutdown(shutdown_flag: Arc) -> Result<(), OsedaRunError> { +pub fn run_with_shutdown(port: u16, shutdown_flag: Arc) -> Result<(), OsedaRunError> { // command run failure and command status are considered different, handled accordingly match Command::new("npx").arg("vite").arg("build").status() { @@ -76,6 +85,8 @@ pub fn run_with_shutdown(shutdown_flag: Arc) -> Result<(), OsedaRunE let mut child = Command::new("npx") .arg("serve") + .arg("-l") + .arg(format!("{}", port)) .arg("dist") .spawn() .map_err(|e| { diff --git a/src/lib.rs b/src/lib.rs index b81db09..510ff4f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,7 +28,7 @@ pub enum Commands { /// Initialize a new Oseda project in the working directory Init(cmd::init::InitOptions), /// Run the Oseda project in the working directory - Run, + Run(cmd::run::RunOptions), /// Check the Oseda project in the working directory for common errors Check(cmd::check::CheckOptions), /// Deploy your Oseda project to github to add to oseda.net