Skip to content

Commit a8cff6b

Browse files
authored
feat(cli): do not enforce src-tauri folder structure, closes #2643 (#2654)
1 parent f68603a commit a8cff6b

File tree

5 files changed

+97
-39
lines changed

5 files changed

+97
-39
lines changed

.changes/app-folder-structure.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"cli.js": patch
3+
"cli.rs": patch
4+
---
5+
6+
Do not force Tauri application code on `src-tauri` folder and use a glob pattern to look for a subfolder with a `tauri.conf.json` file.

tooling/cli.js/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"cross-env": "7.0.3",
5555
"cross-spawn": "7.0.3",
5656
"fs-extra": "10.0.0",
57+
"glob": "7.2.0",
5758
"global-agent": "3.0.0",
5859
"got": "11.8.2",
5960
"imagemin": "8.0.1",
@@ -81,6 +82,7 @@
8182
"@rollup/plugin-typescript": "8.2.5",
8283
"@types/cross-spawn": "6.0.2",
8384
"@types/fs-extra": "9.0.13",
85+
"@types/glob": "7.1.4",
8486
"@types/global-agent": "2.1.1",
8587
"@types/imagemin": "7.0.1",
8688
"@types/imagemin-optipng": "5.2.1",

tooling/cli.js/src/helpers/app-paths.ts

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,53 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
import { existsSync } from 'fs'
6-
import { join, normalize, resolve, sep, isAbsolute } from 'path'
5+
import { join, resolve, isAbsolute, dirname } from 'path'
76
import logger from './logger'
87
import chalk from 'chalk'
8+
import { createRequire } from 'module'
99

1010
const warn = logger('tauri', chalk.red)
1111

12+
const require = createRequire(import.meta.url)
13+
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment
14+
const glob = require('glob')
15+
1216
function resolvePath(basePath: string, dir: string): string {
1317
return dir && isAbsolute(dir) ? dir : resolve(basePath, dir)
1418
}
1519

16-
const getAppDir = (): string => {
17-
let dir = process.env.__TAURI_TEST_APP_DIR ?? process.cwd()
18-
let count = 0
19-
20-
// only go up three folders max
21-
while (dir.length > 0 && !dir.endsWith(sep) && count <= 2) {
22-
if (existsSync(join(dir, 'src-tauri', 'tauri.conf.json'))) {
23-
return dir
24-
}
25-
count++
26-
dir = normalize(join(dir, '..'))
20+
const getAppDir = (): string | null => {
21+
const dir = process.env.__TAURI_TEST_APP_DIR ?? process.cwd()
22+
// eslint-disable-next-line
23+
const matches: string[] = glob.sync(join(dir, `**/package.json`), {
24+
ignore: '**/node_modules/**'
25+
})
26+
27+
if (matches.length === 0) {
28+
return null
29+
} else {
30+
return dirname(matches[0])
2731
}
32+
}
2833

29-
warn("Couldn't recognize the current folder as a part of a Tauri project")
30-
process.exit(1)
34+
const getTauriDir = (): string => {
35+
const dir = process.env.__TAURI_TEST_APP_DIR ?? process.cwd()
36+
// eslint-disable-next-line
37+
const matches: string[] = glob.sync(join(dir, `**/tauri.conf.json`))
38+
39+
if (matches.length === 0) {
40+
warn(
41+
"Couldn't recognize the current folder as a part of a Tauri project. It must contain a `tauri.conf.json` file in any subfolder."
42+
)
43+
process.exit(1)
44+
return ''
45+
} else {
46+
return dirname(matches[0])
47+
}
3148
}
3249

33-
const appDir = getAppDir()
34-
const tauriDir = resolve(appDir, 'src-tauri')
50+
const appDir = getAppDir() ?? resolve(getTauriDir(), '..')
51+
const tauriDir = getTauriDir()
3552

3653
const resolveDir = {
3754
app: (dir: string) => resolvePath(appDir, dir),

tooling/cli.js/yarn.lock

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,6 +1631,14 @@
16311631
dependencies:
16321632
"@types/node" "*"
16331633

1634+
"@types/glob@7.1.4":
1635+
version "7.1.4"
1636+
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.4.tgz#ea59e21d2ee5c517914cb4bc8e4153b99e566672"
1637+
integrity sha512-w+LsMxKyYQm347Otw+IfBXOv9UWVjpHpCDdbBMt8Kz/xbvCYNjP+0qPh91Km3iKfSRLBB0P7fAMf0KHrPu+MyA==
1638+
dependencies:
1639+
"@types/minimatch" "*"
1640+
"@types/node" "*"
1641+
16341642
"@types/global-agent@2.1.1":
16351643
version "2.1.1"
16361644
resolved "https://registry.yarnpkg.com/@types/global-agent/-/global-agent-2.1.1.tgz#3f93185e48a3a36e377a52a8301320cd162a831b"
@@ -1713,6 +1721,11 @@
17131721
dependencies:
17141722
"@types/node" "*"
17151723

1724+
"@types/minimatch@*":
1725+
version "3.0.5"
1726+
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40"
1727+
integrity sha512-Klz949h02Gz2uZCMGwDUSDS1YBlTdDDgbWHi+81l29tQALUtvz4rAYi5uoVhE5Lagoq6DeqAUlbrHvW/mXDgdQ==
1728+
17161729
"@types/ms@0.7.31":
17171730
version "0.7.31"
17181731
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197"
@@ -3864,6 +3877,18 @@ glob-parent@^5.1.0, glob-parent@^5.1.2:
38643877
dependencies:
38653878
is-glob "^4.0.1"
38663879

3880+
glob@7.2.0:
3881+
version "7.2.0"
3882+
resolved "https://registry.yarnpkg.com/glob/-/glob-7.2.0.tgz#d15535af7732e02e948f4c41628bd910293f6023"
3883+
integrity sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==
3884+
dependencies:
3885+
fs.realpath "^1.0.0"
3886+
inflight "^1.0.4"
3887+
inherits "2"
3888+
minimatch "^3.0.4"
3889+
once "^1.3.0"
3890+
path-is-absolute "^1.0.0"
3891+
38673892
glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6:
38683893
version "7.1.7"
38693894
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.7.tgz#3b193e9233f01d42d0b3f78294bbeeb418f94a90"

tooling/cli.rs/src/helpers/app_paths.rs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,42 @@ use std::{env::current_dir, path::PathBuf};
66

77
use once_cell::sync::Lazy;
88

9-
fn get_app_dir() -> PathBuf {
10-
let mut dir = current_dir().expect("failed to read cwd");
11-
12-
let mut count = 0;
13-
14-
// only go up three folders max
15-
while count <= 2 {
16-
let test_path = dir.join("src-tauri/tauri.conf.json");
17-
if test_path.exists() {
18-
return dir;
19-
}
20-
count += 1;
21-
match dir.parent() {
22-
Some(parent) => {
23-
dir = parent.to_path_buf();
24-
}
25-
None => break,
26-
}
27-
}
9+
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+
.into_owned(),
16+
)
17+
.unwrap()
18+
.filter_map(Result::ok)
19+
.last()
20+
.map(|p| p.parent().unwrap().to_path_buf())
21+
.expect("Couldn't recognize the current folder as a Tauri project. It must contain a `tauri.conf.json` file in any subfolder.")
22+
}
2823

29-
panic!("Couldn't recognize the current folder as a Tauri project.")
24+
fn get_app_dir() -> Option<PathBuf> {
25+
glob::glob(
26+
&current_dir()
27+
.expect("failed to read cwd")
28+
.join("**/package.json")
29+
.to_string_lossy()
30+
.into_owned(),
31+
)
32+
.unwrap()
33+
.filter_map(Result::ok)
34+
.filter(|p| !p.to_string_lossy().into_owned().contains("node_modules"))
35+
.last()
36+
.map(|p| p.parent().unwrap().to_path_buf())
3037
}
3138

3239
pub fn app_dir() -> &'static PathBuf {
33-
static APP_DIR: Lazy<PathBuf> = Lazy::new(get_app_dir);
40+
static APP_DIR: Lazy<PathBuf> =
41+
Lazy::new(|| get_app_dir().unwrap_or_else(|| get_tauri_dir().parent().unwrap().to_path_buf()));
3442
&APP_DIR
3543
}
3644

3745
pub fn tauri_dir() -> PathBuf {
38-
app_dir().join("src-tauri")
46+
get_tauri_dir()
3947
}

0 commit comments

Comments
 (0)