feat(cli): allow for toml and json5 files in --config arg (#13079)

This commit is contained in:
Pietagorh
2025-03-30 18:11:47 +02:00
committed by GitHub
parent 30beb6fee7
commit f805061d11
11 changed files with 31 additions and 11 deletions

View File

@@ -0,0 +1,6 @@
---
tauri-cli: minor:enhance
"@tauri-apps/cli": minor:enhance
---
Add support for passing TOML and JSON5 config files to `--config` arg

1
Cargo.lock generated
View File

@@ -8537,6 +8537,7 @@ dependencies = [
"insta",
"itertools 0.13.0",
"json-patch 3.0.1",
"json5",
"jsonrpsee",
"jsonrpsee-client-transport",
"jsonrpsee-core",

View File

@@ -51,6 +51,7 @@ tauri-bundler = { version = "2.3.0", default-features = false, path = "../tauri-
colored = "2"
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1", features = ["preserve_order"] }
json5 = "0.4"
notify = "8"
notify-debouncer-full = "0.5"
shared_child = "1"

View File

@@ -45,7 +45,7 @@ pub struct Options {
/// Skip the bundling step even if `bundle > active` is `true` in tauri config.
#[clap(long)]
pub no_bundle: bool,
/// JSON strings or path to JSON files to merge with the default configuration file
/// JSON strings or paths to JSON, JSON5 or TOML files to merge with the default configuration file
///
/// Configurations are merged in the order they are provided, which means a particular value overwrites previous values when a config key-value pair conflicts.
///

View File

@@ -60,7 +60,7 @@ pub struct Options {
/// Space or comma separated list of bundles to package.
#[clap(short, long, action = ArgAction::Append, num_args(0..), value_delimiter = ',')]
pub bundles: Option<Vec<BundleFormat>>,
/// JSON strings or path to JSON files to merge with the default configuration file
/// JSON strings or paths to JSON, JSON5 or TOML files to merge with the default configuration file
///
/// Configurations are merged in the order they are provided, which means a particular value overwrites previous values when a config key-value pair conflicts.
///

View File

@@ -59,7 +59,7 @@ pub struct Options {
/// Exit on panic
#[clap(short, long)]
pub exit_on_panic: bool,
/// JSON strings or path to JSON files to merge with the default configuration file
/// JSON strings or paths to JSON, JSON5 or TOML files to merge with the default configuration file
///
/// Configurations are merged in the order they are provided, which means a particular value overwrites previous values when a config key-value pair conflicts.
///

View File

@@ -63,10 +63,22 @@ impl FromStr for ConfigValue {
} else {
let path = PathBuf::from(config);
if path.exists() {
Ok(Self(serde_json::from_str(
&read_to_string(&path)
.with_context(|| format!("invalid configuration at file {config}"))?,
)?))
let raw = &read_to_string(&path)
.with_context(|| format!("invalid configuration at file {config}"))?;
match path.extension() {
Some(ext) if ext == "toml" => Ok(Self(::toml::from_str(raw)?)),
Some(ext) if ext == "json5" => Ok(Self(::json5::from_str(raw)?)),
// treat all other extensions as json
_ => Ok(Self(
// from tauri-utils/src/config/parse.rs:
// we also want to support **valid** json5 in the .json extension
// if the json5 is not valid the serde_json error for regular json will be returned.
match ::json5::from_str(raw) {
Ok(json5) => json5,
Err(_) => serde_json::from_str(raw)?,
},
)),
}
} else {
anyhow::bail!("provided configuration path does not exist")
}

View File

@@ -49,7 +49,7 @@ pub struct Options {
/// List of cargo features to activate
#[clap(short, long, action = ArgAction::Append, num_args(0..))]
pub features: Option<Vec<String>>,
/// JSON strings or path to JSON files to merge with the default configuration file
/// JSON strings or paths to JSON, JSON5 or TOML files to merge with the default configuration file
///
/// Configurations are merged in the order they are provided, which means a particular value overwrites previous values when a config key-value pair conflicts.
///

View File

@@ -48,7 +48,7 @@ pub struct Options {
/// Exit on panic
#[clap(short, long)]
exit_on_panic: bool,
/// JSON strings or path to JSON files to merge with the default configuration file
/// JSON strings or paths to JSON, JSON5 or TOML files to merge with the default configuration file
///
/// Configurations are merged in the order they are provided, which means a particular value overwrites previous values when a config key-value pair conflicts.
///

View File

@@ -60,7 +60,7 @@ pub struct Options {
/// List of cargo features to activate
#[clap(short, long, action = ArgAction::Append, num_args(0..))]
pub features: Option<Vec<String>>,
/// JSON strings or path to JSON files to merge with the default configuration file
/// JSON strings or paths to JSON, JSON5 or TOML files to merge with the default configuration file
///
/// Configurations are merged in the order they are provided, which means a particular value overwrites previous values when a config key-value pair conflicts.
///

View File

@@ -54,7 +54,7 @@ pub struct Options {
/// Exit on panic
#[clap(short, long)]
exit_on_panic: bool,
/// JSON strings or path to JSON files to merge with the default configuration file
/// JSON strings or paths to JSON, JSON5 or TOML files to merge with the default configuration file
///
/// Configurations are merged in the order they are provided, which means a particular value overwrites previous values when a config key-value pair conflicts.
///