Files
dioxus/packages/cli/src/main.rs
T
Brian Donovan 9cef71b6b9 fix(cli): improve error message for invalid config (#1200)
Previously an invalid `Dioxus.toml` file would yield this error when running e.g. `dx serve`:

```
Error: Failed to load `Dioxus.toml` because: Dioxus.toml parse failed
```

This doesn't give any indication why it failed to parse. This commit updates it to include why the parsing failed, e.g.:

```
Error: Failed to load Dioxus config because: Dioxus.toml missing field `name` for key `application` at line 38 column 1
```

I initially had it format the message to include `Dioxus.toml:38:1` to be clickable in some terminals, but the location specified didn't always seem particularly relevant to the actual problem so I left it as-is.

This also fixes what I believe would be an issue on case-sensitive file systems where if `dioxus.toml` existed it would try to read `Dioxus.toml`.

I'm still fairly new to Rust, so the `.as_path()` calls may not be the best way to deal with the fact that the borrow checker wouldn't let me re-use `crate_dir` and `dioxus_conf_file`. I'm open to suggestions!
2023-07-15 11:13:22 -05:00

72 lines
2.0 KiB
Rust

use anyhow::anyhow;
use clap::Parser;
use dioxus_cli::*;
#[cfg(feature = "plugin")]
use dioxus_cli::plugin::PluginManager;
use Commands::*;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let args = Cli::parse();
set_up_logging();
let _dioxus_config = DioxusConfig::load()
.map_err(|e| anyhow!("Failed to load Dioxus config because: {e}"))?
.unwrap_or_else(|| {
log::warn!("You appear to be creating a Dioxus project from scratch; we will use the default config");
DioxusConfig::default()
});
#[cfg(feature = "plugin")]
PluginManager::init(_dioxus_config.plugin)
.map_err(|e| anyhow!("🚫 Plugin system initialization failed: {e}"))?;
match args.action {
Translate(opts) => opts
.translate()
.map_err(|e| anyhow!("🚫 Translation of HTML into RSX failed: {}", e)),
Build(opts) => opts
.build()
.map_err(|e| anyhow!("🚫 Building project failed: {}", e)),
Clean(opts) => opts
.clean()
.map_err(|e| anyhow!("🚫 Cleaning project failed: {}", e)),
Serve(opts) => opts
.serve()
.await
.map_err(|e| anyhow!("🚫 Serving project failed: {}", e)),
Create(opts) => opts
.create()
.map_err(|e| anyhow!("🚫 Creating new project failed: {}", e)),
Config(opts) => opts
.config()
.map_err(|e| anyhow!("🚫 Configuring new project failed: {}", e)),
#[cfg(feature = "plugin")]
Plugin(opts) => opts
.plugin()
.await
.map_err(|e| anyhow!("🚫 Error with plugin: {}", e)),
Autoformat(opts) => opts
.autoformat()
.await
.map_err(|e| anyhow!("🚫 Error autoformatting RSX: {}", e)),
Version(opt) => {
let version = opt.version();
println!("{}", version);
Ok(())
}
}
}