Skip to content

Commit f805061

Browse files
authored
feat(cli): allow for toml and json5 files in --config arg (#13079)
1 parent 30beb6f commit f805061

11 files changed

Lines changed: 31 additions & 11 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
tauri-cli: minor:enhance
3+
"@tauri-apps/cli": minor:enhance
4+
---
5+
6+
Add support for passing TOML and JSON5 config files to `--config` arg

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/tauri-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ tauri-bundler = { version = "2.3.0", default-features = false, path = "../tauri-
5151
colored = "2"
5252
serde = { version = "1", features = ["derive"] }
5353
serde_json = { version = "1", features = ["preserve_order"] }
54+
json5 = "0.4"
5455
notify = "8"
5556
notify-debouncer-full = "0.5"
5657
shared_child = "1"

crates/tauri-cli/src/build.rs

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

crates/tauri-cli/src/bundle.rs

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

crates/tauri-cli/src/dev.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub struct Options {
5959
/// Exit on panic
6060
#[clap(short, long)]
6161
pub exit_on_panic: bool,
62-
/// JSON strings or path to JSON files to merge with the default configuration file
62+
/// JSON strings or paths to JSON, JSON5 or TOML files to merge with the default configuration file
6363
///
6464
/// Configurations are merged in the order they are provided, which means a particular value overwrites previous values when a config key-value pair conflicts.
6565
///

crates/tauri-cli/src/lib.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,22 @@ impl FromStr for ConfigValue {
6363
} else {
6464
let path = PathBuf::from(config);
6565
if path.exists() {
66-
Ok(Self(serde_json::from_str(
67-
&read_to_string(&path)
68-
.with_context(|| format!("invalid configuration at file {config}"))?,
69-
)?))
66+
let raw = &read_to_string(&path)
67+
.with_context(|| format!("invalid configuration at file {config}"))?;
68+
match path.extension() {
69+
Some(ext) if ext == "toml" => Ok(Self(::toml::from_str(raw)?)),
70+
Some(ext) if ext == "json5" => Ok(Self(::json5::from_str(raw)?)),
71+
// treat all other extensions as json
72+
_ => Ok(Self(
73+
// from tauri-utils/src/config/parse.rs:
74+
// we also want to support **valid** json5 in the .json extension
75+
// if the json5 is not valid the serde_json error for regular json will be returned.
76+
match ::json5::from_str(raw) {
77+
Ok(json5) => json5,
78+
Err(_) => serde_json::from_str(raw)?,
79+
},
80+
)),
81+
}
7082
} else {
7183
anyhow::bail!("provided configuration path does not exist")
7284
}

crates/tauri-cli/src/mobile/android/build.rs

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

crates/tauri-cli/src/mobile/android/dev.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub struct Options {
4848
/// Exit on panic
4949
#[clap(short, long)]
5050
exit_on_panic: bool,
51-
/// JSON strings or path to JSON files to merge with the default configuration file
51+
/// JSON strings or paths to JSON, JSON5 or TOML files to merge with the default configuration file
5252
///
5353
/// Configurations are merged in the order they are provided, which means a particular value overwrites previous values when a config key-value pair conflicts.
5454
///

crates/tauri-cli/src/mobile/ios/build.rs

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

0 commit comments

Comments
 (0)