use std::path::Path;
use std::process::{Command, Stdio};
use anyhow::{Context, Result, bail};
pub const INSTALL_HINT: &str = "install it with `cargo install dioxus-cli --version ^0.7 --locked`";
pub fn is_dioxus_app(app_dir: &Path) -> bool {
app_dir.join("Dioxus.toml").is_file()
}
pub fn apps(root: &Path) -> Vec<String> {
let mut names: Vec<String> = std::fs::read_dir(root.join("apps"))
.into_iter()
.flatten()
.flatten()
.filter(|entry| is_dioxus_app(&entry.path()))
.filter_map(|entry| entry.file_name().into_string().ok())
.collect();
names.sort();
names
}
pub fn dx_installed() -> bool {
Command::new("dx")
.arg("--version")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.is_ok_and(|status| status.success())
}
pub fn build(root: &Path, app: &str, release: bool) -> Result<()> {
if !dx_installed() {
bail!("`{app}` is a Dioxus app, but `dx` was not found; {INSTALL_HINT}");
}
let mut command = Command::new("dx");
command
.args(["build", "--platform", "web"])
.current_dir(root.join("apps").join(app));
if release {
command.arg("--release");
}
let status = command
.status()
.with_context(|| format!("failed to run `dx build` for {app}"))?;
if !status.success() {
bail!("`dx build` failed for {app}");
}
Ok(())
}