From e7ccbd8573f6b9124e80c0b67fa2365729c3c196 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Wed, 19 Oct 2022 15:37:54 -0300 Subject: [PATCH] feat(cli): detect JSON5 and TOML configuration files in the dev watcher (#5439) --- .changes/cli-improve-config-watcher.md | 6 ++++++ core/tauri-utils/src/config/parse.rs | 27 ++++++++++++++++++++++++++ tooling/cli/src/helpers/app_paths.rs | 19 +++++++----------- tooling/cli/src/interface/rust.rs | 4 ++-- 4 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 .changes/cli-improve-config-watcher.md diff --git a/.changes/cli-improve-config-watcher.md b/.changes/cli-improve-config-watcher.md new file mode 100644 index 000000000..3087512e6 --- /dev/null +++ b/.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. diff --git a/core/tauri-utils/src/config/parse.rs b/core/tauri-utils/src/config/parse.rs index 3f92acd26..0eddafbef 100644 --- a/core/tauri-utils/src/config/parse.rs +++ b/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. diff --git a/tooling/cli/src/helpers/app_paths.rs b/tooling/cli/src/helpers/app_paths.rs index 791b97d95..5c413c113 100644 --- a/tooling/cli/src/helpers/app_paths.rs +++ b/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 bool>(dir: &Path, checker: F) -> Option { +fn lookup bool>(dir: &Path, checker: F) -> Option { let mut default_gitignore = std::env::temp_dir(); default_gitignore.push(".gitignore"); if !default_gitignore.exists() { @@ -51,7 +52,7 @@ fn lookup bool>(dir: &Path, checker: F) -> Option

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 { - lookup(¤t_dir().expect("failed to read cwd"), |path, _| { + lookup(¤t_dir().expect("failed to read cwd"), |path| { if let Some(file_name) = path.file_name() { file_name == OsStr::new("package.json") } else { diff --git a/tooling/cli/src/interface/rust.rs b/tooling/cli/src/interface/rust.rs index be1cd910e..2ed6402f5 100644 --- a/tooling/cli/src/interface/rust.rs +++ b/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 =