Skip to content

Commit 9c6c5a8

Browse files
authored
perf(cli.rs): improve performance of tauri dir lookup reading .gitignore (#3405)
1 parent 989ee4e commit 9c6c5a8

4 files changed

Lines changed: 85 additions & 21 deletions

File tree

.changes/perf-cli-dir-lookup.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cli.rs": patch
3+
---
4+
5+
Respect `.gitignore` configuration when looking for the folder with the `tauri.conf.json` file.

tooling/cli/Cargo.lock

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

tooling/cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ heck = "0.4"
6060
dialoguer = "0.9"
6161
url = { version = "2.2", features = [ "serde" ] }
6262
os_pipe = "1"
63+
ignore = "0.4"
6364

6465
[target."cfg(windows)".dependencies]
6566
encode_unicode = "0.3"

tooling/cli/src/helpers/app_paths.rs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,43 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
use std::{env::current_dir, path::PathBuf};
5+
use std::{
6+
env::current_dir,
7+
ffi::OsStr,
8+
path::{Path, PathBuf},
9+
};
610

11+
use ignore::Walk;
712
use once_cell::sync::Lazy;
813

14+
fn lookup<F: Fn(&PathBuf) -> bool>(dir: &Path, checker: F) -> Option<PathBuf> {
15+
for entry in Walk::new(dir).flatten() {
16+
let path = dir.join(entry.path());
17+
if checker(&path) {
18+
return Some(path);
19+
}
20+
}
21+
None
22+
}
23+
924
fn get_tauri_dir() -> PathBuf {
10-
glob::glob(
11-
&current_dir()
12-
.expect("failed to read cwd")
13-
.join("**/tauri.conf.json")
14-
.to_string_lossy(),
15-
)
16-
.unwrap()
17-
.filter_map(Result::ok)
18-
.last()
25+
lookup(&current_dir().expect("failed to read cwd"), |path| if let Some(file_name) = path.file_name() {
26+
file_name == OsStr::new("tauri.conf.json") || file_name == OsStr::new("tauri.conf.json5")
27+
} else {
28+
false
29+
})
1930
.map(|p| p.parent().unwrap().to_path_buf())
20-
.expect("Couldn't recognize the current folder as a Tauri project. It must contain a `tauri.conf.json` file in any subfolder.")
31+
.expect("Couldn't recognize the current folder as a Tauri project. It must contain a `tauri.conf.json` or `tauri.conf.json5` file in any subfolder.")
2132
}
2233

2334
fn get_app_dir() -> Option<PathBuf> {
24-
glob::glob(
25-
&current_dir()
26-
.expect("failed to read cwd")
27-
.join("**/package.json")
28-
.to_string_lossy(),
29-
)
30-
.unwrap()
31-
.filter_map(Result::ok)
32-
.filter(|p| !p.to_string_lossy().into_owned().contains("node_modules"))
33-
.last()
35+
lookup(&current_dir().expect("failed to read cwd"), |path| {
36+
if let Some(file_name) = path.file_name() {
37+
file_name == OsStr::new("package.json")
38+
} else {
39+
false
40+
}
41+
})
3442
.map(|p| p.parent().unwrap().to_path_buf())
3543
}
3644

0 commit comments

Comments
 (0)