Skip to content

Commit

Permalink
feat(cli): detect JSON5 and TOML configuration files in the dev watch…
Browse files Browse the repository at this point in the history
…er (#5439)
  • Loading branch information
lucasfernog committed Oct 19, 2022
1 parent 9076d5d commit e7ccbd8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .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 changes: 27 additions & 0 deletions core/tauri-utils/src/config/parse.rs
Expand Up @@ -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.
Expand Down
19 changes: 7 additions & 12 deletions tooling/cli/src/helpers/app_paths.rs
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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.",
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions tooling/cli/src/interface/rust.rs
Expand Up @@ -4,7 +4,6 @@

use std::{
collections::HashMap,
ffi::OsStr,
fs::{File, FileType},
io::{Read, Write},
path::{Path, PathBuf},
Expand All @@ -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::{
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit e7ccbd8

Please sign in to comment.