Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bin/oseda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub fn export(opts: ExportOptions) -> Result<(), Box<dyn Error>> {
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));
Expand Down
17 changes: 14 additions & 3 deletions src/cmd/run.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use clap::Args;
use std::{
process::Command,
sync::{
Expand Down Expand Up @@ -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:
Expand All @@ -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<AtomicBool>) -> Result<(), OsedaRunError> {
pub fn run_with_shutdown(port: u16, shutdown_flag: Arc<AtomicBool>) -> Result<(), OsedaRunError> {
// command run failure and command status are considered different, handled accordingly

match Command::new("npx").arg("vite").arg("build").status() {
Expand All @@ -76,6 +85,8 @@ pub fn run_with_shutdown(shutdown_flag: Arc<AtomicBool>) -> Result<(), OsedaRunE

let mut child = Command::new("npx")
.arg("serve")
.arg("-l")
.arg(format!("{}", port))
.arg("dist")
.spawn()
.map_err(|e| {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading