Browse Source

feat(cli): detect JSON5 and TOML configuration files in the dev watcher (#5439)

Lucas Fernandes Nogueira 2 years ago
parent
commit
e7ccbd8573

+ 6 - 0
.changes/cli-improve-config-watcher.md

@@ -0,0 +1,6 @@
+---
+"cli.rs": patch
+"cli.js": patch
+---
+
+Detect JSON5 and TOML configuration files in the dev watcher.

+ 27 - 0
core/tauri-utils/src/config/parse.rs

@@ -141,6 +141,33 @@ pub enum ConfigError {
   },
 }
 
+/// Determines if the given folder has a configuration file.
+pub fn folder_has_configuration_file(folder: &Path) -> bool {
+  folder.join(ConfigFormat::Json.into_file_name()).exists()
+      || folder.join(ConfigFormat::Json5.into_file_name()).exists()
+      || folder.join(ConfigFormat::Toml.into_file_name()).exists()
+       // platform file names
+       || folder.join(ConfigFormat::Json.into_platform_file_name()).exists()
+      || folder.join(ConfigFormat::Json5.into_platform_file_name()).exists()
+      || folder.join(ConfigFormat::Toml.into_platform_file_name()).exists()
+}
+
+/// Determines if the given file path represents a Tauri configuration file.
+pub fn is_configuration_file(path: &Path) -> bool {
+  path
+    .file_name()
+    .map(|file_name| {
+      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())
+      // platform file names
+      || file_name == OsStr::new(ConfigFormat::Json.into_platform_file_name())
+        || file_name == OsStr::new(ConfigFormat::Json5.into_platform_file_name())
+        || file_name == OsStr::new(ConfigFormat::Toml.into_platform_file_name())
+    })
+    .unwrap_or_default()
+}
+
 /// Reads the configuration from the given root directory.
 ///
 /// It first looks for a `tauri.conf.json[5]` file on the given directory. The file must exist.

+ 7 - 12
tooling/cli/src/helpers/app_paths.rs

@@ -6,18 +6,19 @@ use std::{
   cmp::Ordering,
   env::current_dir,
   ffi::OsStr,
-  fs::FileType,
   path::{Path, PathBuf},
 };
 
 use ignore::WalkBuilder;
 use once_cell::sync::Lazy;
 
-use tauri_utils::config::parse::ConfigFormat;
+use tauri_utils::config::parse::{
+  folder_has_configuration_file, is_configuration_file, ConfigFormat,
+};
 
 const TAURI_GITIGNORE: &[u8] = include_bytes!("../../tauri.gitignore");
 
-fn lookup<F: Fn(&PathBuf, FileType) -> bool>(dir: &Path, checker: F) -> Option<PathBuf> {
+fn lookup<F: Fn(&PathBuf) -> bool>(dir: &Path, checker: F) -> Option<PathBuf> {
   let mut default_gitignore = std::env::temp_dir();
   default_gitignore.push(".gitignore");
   if !default_gitignore.exists() {
@@ -51,7 +52,7 @@ fn lookup<F: Fn(&PathBuf, FileType) -> bool>(dir: &Path, checker: F) -> Option<P
 
   for entry in builder.build().flatten() {
     let path = dir.join(entry.path());
-    if checker(&path, entry.file_type().unwrap()) {
+    if checker(&path) {
       return Some(path);
     }
   }
@@ -67,13 +68,7 @@ fn get_tauri_dir() -> PathBuf {
     return cwd.join("src-tauri/");
   }
 
-  lookup(&cwd, |path, file_type| if file_type.is_dir() {
-    path.join(ConfigFormat::Json.into_file_name()).exists() || path.join(ConfigFormat::Json5.into_file_name()).exists() || path.join(ConfigFormat::Toml.into_file_name()).exists()
-  } else if let Some(file_name) = path.file_name() {
-    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())
-  } else {
-    false
-  })
+  lookup(&cwd, |path| folder_has_configuration_file(path) || is_configuration_file(path))
   .map(|p| if p.is_dir() { p } else {  p.parent().unwrap().to_path_buf() })
   .unwrap_or_else(||
     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 {
 }
 
 fn get_app_dir() -> Option<PathBuf> {
-  lookup(&current_dir().expect("failed to read cwd"), |path, _| {
+  lookup(&current_dir().expect("failed to read cwd"), |path| {
     if let Some(file_name) = path.file_name() {
       file_name == OsStr::new("package.json")
     } else {

+ 2 - 2
tooling/cli/src/interface/rust.rs

@@ -4,7 +4,6 @@
 
 use std::{
   collections::HashMap,
-  ffi::OsStr,
   fs::{File, FileType},
   io::{Read, Write},
   path::{Path, PathBuf},
@@ -30,6 +29,7 @@ use tauri_bundler::{
   AppCategory, BundleBinary, BundleSettings, DebianSettings, MacOsSettings, PackageSettings,
   UpdaterSettings, WindowsSettings,
 };
+use tauri_utils::config::parse::is_configuration_file;
 
 use super::{AppSettings, ExitReason, Interface};
 use crate::helpers::{
@@ -393,7 +393,7 @@ impl Rust {
           let on_exit = on_exit.clone();
           let event_path = event.path;
 
-          if event_path.file_name() == Some(OsStr::new("tauri.conf.json")) {
+          if is_configuration_file(&event_path) {
             info!("Tauri configuration changed. Rewriting manifest...");
             let config = reload_config(options.config.as_deref())?;
             self.app_settings.manifest =