use tokio::process::Command;
use tracing::info;
use colored::Colorize;
pub async fn pm2_start_wrapper(args: Vec<String>, debug: bool) -> Result<(), String> {
if args.is_empty() {
info!("{}", "Usage: xbp start <command> [pm2-options]".yellow());
info!("");
info!("{}", "Examples:".bright_blue());
info!(" xbp start \"npm start\" --name my-app");
info!(" xbp start \"./binary --port 3050\" --name my-service");
info!(" xbp start \"cargo run\" --name rust-app --watch");
info!("");
info!("{}", "Common PM2 options:".bright_blue());
info!(" {} - Process name", "--name".cyan());
info!(" {} - Watch for file changes", "--watch".cyan());
info!(" {} - Max memory before restart", "--max-memory-restart".cyan());
info!(" {} - Number of instances", "--instances".cyan());
info!(" {} - Cron pattern for restart", "--cron-restart".cyan());
return Ok(());
}
let mut cmd = Command::new("pm2");
cmd.arg("start");
for arg in args {
cmd.arg(arg);
}
if debug {
tracing::debug!("Executing: pm2 start {:?}", cmd);
}
let output = cmd
.output()
.await
.map_err(|e| format!("Failed to execute pm2 start: {}", e))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(format!("PM2 start failed: {}", stderr));
}
let stdout = String::from_utf8_lossy(&output.stdout);
info!("{}", stdout);
Ok(())
}