diff --git a/.changes/toml-json5-configs-in-cli.md b/.changes/toml-json5-configs-in-cli.md new file mode 100644 index 000000000..92eb9a335 --- /dev/null +++ b/.changes/toml-json5-configs-in-cli.md @@ -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 diff --git a/Cargo.lock b/Cargo.lock index 201828a39..e55041e48 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8537,6 +8537,7 @@ dependencies = [ "insta", "itertools 0.13.0", "json-patch 3.0.1", + "json5", "jsonrpsee", "jsonrpsee-client-transport", "jsonrpsee-core", diff --git a/crates/tauri-cli/Cargo.toml b/crates/tauri-cli/Cargo.toml index 84ba33f4b..432d90238 100644 --- a/crates/tauri-cli/Cargo.toml +++ b/crates/tauri-cli/Cargo.toml @@ -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" diff --git a/crates/tauri-cli/src/build.rs b/crates/tauri-cli/src/build.rs index 2f369a3e4..87c81b314 100644 --- a/crates/tauri-cli/src/build.rs +++ b/crates/tauri-cli/src/build.rs @@ -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. /// diff --git a/crates/tauri-cli/src/bundle.rs b/crates/tauri-cli/src/bundle.rs index 53fd1e6d6..507dbe006 100644 --- a/crates/tauri-cli/src/bundle.rs +++ b/crates/tauri-cli/src/bundle.rs @@ -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>, - /// 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. /// diff --git a/crates/tauri-cli/src/dev.rs b/crates/tauri-cli/src/dev.rs index de1fee5e2..3d7ea098e 100644 --- a/crates/tauri-cli/src/dev.rs +++ b/crates/tauri-cli/src/dev.rs @@ -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. /// diff --git a/crates/tauri-cli/src/lib.rs b/crates/tauri-cli/src/lib.rs index 59088fa13..4e91f12f4 100644 --- a/crates/tauri-cli/src/lib.rs +++ b/crates/tauri-cli/src/lib.rs @@ -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") } diff --git a/crates/tauri-cli/src/mobile/android/build.rs b/crates/tauri-cli/src/mobile/android/build.rs index 26e27399d..d2d744170 100644 --- a/crates/tauri-cli/src/mobile/android/build.rs +++ b/crates/tauri-cli/src/mobile/android/build.rs @@ -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>, - /// 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. /// diff --git a/crates/tauri-cli/src/mobile/android/dev.rs b/crates/tauri-cli/src/mobile/android/dev.rs index 0a8b13050..4a7a98f06 100644 --- a/crates/tauri-cli/src/mobile/android/dev.rs +++ b/crates/tauri-cli/src/mobile/android/dev.rs @@ -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. /// diff --git a/crates/tauri-cli/src/mobile/ios/build.rs b/crates/tauri-cli/src/mobile/ios/build.rs index f33b5b81d..529d59faf 100644 --- a/crates/tauri-cli/src/mobile/ios/build.rs +++ b/crates/tauri-cli/src/mobile/ios/build.rs @@ -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>, - /// 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. /// diff --git a/crates/tauri-cli/src/mobile/ios/dev.rs b/crates/tauri-cli/src/mobile/ios/dev.rs index 22f336110..3e971c3bc 100644 --- a/crates/tauri-cli/src/mobile/ios/dev.rs +++ b/crates/tauri-cli/src/mobile/ios/dev.rs @@ -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. ///