Skip to content

Commit e7ccbd8

Browse files
authored
feat(cli): detect JSON5 and TOML configuration files in the dev watcher (#5439)
1 parent 9076d5d commit e7ccbd8

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"cli.rs": patch
3+
"cli.js": patch
4+
---
5+
6+
Detect JSON5 and TOML configuration files in the dev watcher.

core/tauri-utils/src/config/parse.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,33 @@ pub enum ConfigError {
141141
},
142142
}
143143

144+
/// Determines if the given folder has a configuration file.
145+
pub fn folder_has_configuration_file(folder: &Path) -> bool {
146+
folder.join(ConfigFormat::Json.into_file_name()).exists()
147+
|| folder.join(ConfigFormat::Json5.into_file_name()).exists()
148+
|| folder.join(ConfigFormat::Toml.into_file_name()).exists()
149+
// platform file names
150+
|| folder.join(ConfigFormat::Json.into_platform_file_name()).exists()
151+
|| folder.join(ConfigFormat::Json5.into_platform_file_name()).exists()
152+
|| folder.join(ConfigFormat::Toml.into_platform_file_name()).exists()
153+
}
154+
155+
/// Determines if the given file path represents a Tauri configuration file.
156+
pub fn is_configuration_file(path: &Path) -> bool {
157+
path
158+
.file_name()
159+
.map(|file_name| {
160+
file_name == OsStr::new(ConfigFormat::Json.into_file_name())
161+
|| file_name == OsStr::new(ConfigFormat::Json5.into_file_name())
162+
|| file_name == OsStr::new(ConfigFormat::Toml.into_file_name())
163+
// platform file names
164+
|| file_name == OsStr::new(ConfigFormat::Json.into_platform_file_name())
165+
|| file_name == OsStr::new(ConfigFormat::Json5.into_platform_file_name())
166+
|| file_name == OsStr::new(ConfigFormat::Toml.into_platform_file_name())
167+
})
168+
.unwrap_or_default()
169+
}
170+
144171
/// Reads the configuration from the given root directory.
145172
///
146173
/// It first looks for a `tauri.conf.json[5]` file on the given directory. The file must exist.

tooling/cli/src/helpers/app_paths.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ use std::{
66
cmp::Ordering,
77
env::current_dir,
88
ffi::OsStr,
9-
fs::FileType,
109
path::{Path, PathBuf},
1110
};
1211

1312
use ignore::WalkBuilder;
1413
use once_cell::sync::Lazy;
1514

16-
use tauri_utils::config::parse::ConfigFormat;
15+
use tauri_utils::config::parse::{
16+
folder_has_configuration_file, is_configuration_file, ConfigFormat,
17+
};
1718

1819
const TAURI_GITIGNORE: &[u8] = include_bytes!("../../tauri.gitignore");
1920

20-
fn lookup<F: Fn(&PathBuf, FileType) -> bool>(dir: &Path, checker: F) -> Option<PathBuf> {
21+
fn lookup<F: Fn(&PathBuf) -> bool>(dir: &Path, checker: F) -> Option<PathBuf> {
2122
let mut default_gitignore = std::env::temp_dir();
2223
default_gitignore.push(".gitignore");
2324
if !default_gitignore.exists() {
@@ -51,7 +52,7 @@ fn lookup<F: Fn(&PathBuf, FileType) -> bool>(dir: &Path, checker: F) -> Option<P
5152

5253
for entry in builder.build().flatten() {
5354
let path = dir.join(entry.path());
54-
if checker(&path, entry.file_type().unwrap()) {
55+
if checker(&path) {
5556
return Some(path);
5657
}
5758
}
@@ -67,13 +68,7 @@ fn get_tauri_dir() -> PathBuf {
6768
return cwd.join("src-tauri/");
6869
}
6970

70-
lookup(&cwd, |path, file_type| if file_type.is_dir() {
71-
path.join(ConfigFormat::Json.into_file_name()).exists() || path.join(ConfigFormat::Json5.into_file_name()).exists() || path.join(ConfigFormat::Toml.into_file_name()).exists()
72-
} else if let Some(file_name) = path.file_name() {
73-
file_name == OsStr::new(ConfigFormat::Json.into_file_name()) || file_name == OsStr::new(ConfigFormat::Json5.into_file_name()) || file_name == OsStr::new(ConfigFormat::Toml.into_file_name())
74-
} else {
75-
false
76-
})
71+
lookup(&cwd, |path| folder_has_configuration_file(path) || is_configuration_file(path))
7772
.map(|p| if p.is_dir() { p } else { p.parent().unwrap().to_path_buf() })
7873
.unwrap_or_else(||
7974
panic!("Couldn't recognize the current folder as a Tauri project. It must contain a `{}`, `{}` or `{}` file in any subfolder.",
@@ -85,7 +80,7 @@ fn get_tauri_dir() -> PathBuf {
8580
}
8681

8782
fn get_app_dir() -> Option<PathBuf> {
88-
lookup(&current_dir().expect("failed to read cwd"), |path, _| {
83+
lookup(&current_dir().expect("failed to read cwd"), |path| {
8984
if let Some(file_name) = path.file_name() {
9085
file_name == OsStr::new("package.json")
9186
} else {

tooling/cli/src/interface/rust.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use std::{
66
collections::HashMap,
7-
ffi::OsStr,
87
fs::{File, FileType},
98
io::{Read, Write},
109
path::{Path, PathBuf},
@@ -30,6 +29,7 @@ use tauri_bundler::{
3029
AppCategory, BundleBinary, BundleSettings, DebianSettings, MacOsSettings, PackageSettings,
3130
UpdaterSettings, WindowsSettings,
3231
};
32+
use tauri_utils::config::parse::is_configuration_file;
3333

3434
use super::{AppSettings, ExitReason, Interface};
3535
use crate::helpers::{
@@ -393,7 +393,7 @@ impl Rust {
393393
let on_exit = on_exit.clone();
394394
let event_path = event.path;
395395

396-
if event_path.file_name() == Some(OsStr::new("tauri.conf.json")) {
396+
if is_configuration_file(&event_path) {
397397
info!("Tauri configuration changed. Rewriting manifest...");
398398
let config = reload_config(options.config.as_deref())?;
399399
self.app_settings.manifest =

0 commit comments

Comments
 (0)