|
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 | // SPDX-License-Identifier: MIT |
4 | 4 |
|
5 | | -use std::{env::current_dir, path::PathBuf}; |
| 5 | +use std::{ |
| 6 | + env::current_dir, |
| 7 | + ffi::OsStr, |
| 8 | + path::{Path, PathBuf}, |
| 9 | +}; |
6 | 10 |
|
| 11 | +use ignore::Walk; |
7 | 12 | use once_cell::sync::Lazy; |
8 | 13 |
|
| 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 | + |
9 | 24 | fn get_tauri_dir() -> PathBuf { |
10 | | - glob::glob( |
11 | | - ¤t_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(¤t_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 | + }) |
19 | 30 | .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.") |
21 | 32 | } |
22 | 33 |
|
23 | 34 | fn get_app_dir() -> Option<PathBuf> { |
24 | | - glob::glob( |
25 | | - ¤t_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(¤t_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 | + }) |
34 | 42 | .map(|p| p.parent().unwrap().to_path_buf()) |
35 | 43 | } |
36 | 44 |
|
|
0 commit comments