Skip to content

Commit

Permalink
feat(cli): do not enforce src-tauri folder structure, closes #2643 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Sep 27, 2021
1 parent f68603a commit a8cff6b
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 39 deletions.
6 changes: 6 additions & 0 deletions .changes/app-folder-structure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cli.js": patch
"cli.rs": patch
---

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.
2 changes: 2 additions & 0 deletions tooling/cli.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"cross-env": "7.0.3",
"cross-spawn": "7.0.3",
"fs-extra": "10.0.0",
"glob": "7.2.0",
"global-agent": "3.0.0",
"got": "11.8.2",
"imagemin": "8.0.1",
Expand Down Expand Up @@ -81,6 +82,7 @@
"@rollup/plugin-typescript": "8.2.5",
"@types/cross-spawn": "6.0.2",
"@types/fs-extra": "9.0.13",
"@types/glob": "7.1.4",
"@types/global-agent": "2.1.1",
"@types/imagemin": "7.0.1",
"@types/imagemin-optipng": "5.2.1",
Expand Down
51 changes: 34 additions & 17 deletions tooling/cli.js/src/helpers/app-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,53 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

import { existsSync } from 'fs'
import { join, normalize, resolve, sep, isAbsolute } from 'path'
import { join, resolve, isAbsolute, dirname } from 'path'
import logger from './logger'
import chalk from 'chalk'
import { createRequire } from 'module'

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

const require = createRequire(import.meta.url)
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment
const glob = require('glob')

function resolvePath(basePath: string, dir: string): string {
return dir && isAbsolute(dir) ? dir : resolve(basePath, dir)
}

const getAppDir = (): string => {
let dir = process.env.__TAURI_TEST_APP_DIR ?? process.cwd()
let count = 0

// only go up three folders max
while (dir.length > 0 && !dir.endsWith(sep) && count <= 2) {
if (existsSync(join(dir, 'src-tauri', 'tauri.conf.json'))) {
return dir
}
count++
dir = normalize(join(dir, '..'))
const getAppDir = (): string | null => {
const dir = process.env.__TAURI_TEST_APP_DIR ?? process.cwd()
// eslint-disable-next-line
const matches: string[] = glob.sync(join(dir, `**/package.json`), {
ignore: '**/node_modules/**'
})

if (matches.length === 0) {
return null
} else {
return dirname(matches[0])
}
}

warn("Couldn't recognize the current folder as a part of a Tauri project")
process.exit(1)
const getTauriDir = (): string => {
const dir = process.env.__TAURI_TEST_APP_DIR ?? process.cwd()
// eslint-disable-next-line
const matches: string[] = glob.sync(join(dir, `**/tauri.conf.json`))

if (matches.length === 0) {
warn(
"Couldn't recognize the current folder as a part of a Tauri project. It must contain a `tauri.conf.json` file in any subfolder."
)
process.exit(1)
return ''
} else {
return dirname(matches[0])
}
}

const appDir = getAppDir()
const tauriDir = resolve(appDir, 'src-tauri')
const appDir = getAppDir() ?? resolve(getTauriDir(), '..')
const tauriDir = getTauriDir()

const resolveDir = {
app: (dir: string) => resolvePath(appDir, dir),
Expand Down
25 changes: 25 additions & 0 deletions tooling/cli.js/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1631,6 +1631,14 @@
dependencies:
"@types/node" "*"

"@types/glob@7.1.4":
version "7.1.4"
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.4.tgz#ea59e21d2ee5c517914cb4bc8e4153b99e566672"
integrity sha512-w+LsMxKyYQm347Otw+IfBXOv9UWVjpHpCDdbBMt8Kz/xbvCYNjP+0qPh91Km3iKfSRLBB0P7fAMf0KHrPu+MyA==
dependencies:
"@types/minimatch" "*"
"@types/node" "*"

"@types/global-agent@2.1.1":
version "2.1.1"
resolved "https://registry.yarnpkg.com/@types/global-agent/-/global-agent-2.1.1.tgz#3f93185e48a3a36e377a52a8301320cd162a831b"
Expand Down Expand Up @@ -1713,6 +1721,11 @@
dependencies:
"@types/node" "*"

"@types/minimatch@*":
version "3.0.5"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40"
integrity sha512-Klz949h02Gz2uZCMGwDUSDS1YBlTdDDgbWHi+81l29tQALUtvz4rAYi5uoVhE5Lagoq6DeqAUlbrHvW/mXDgdQ==

"@types/ms@0.7.31":
version "0.7.31"
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197"
Expand Down Expand Up @@ -3864,6 +3877,18 @@ glob-parent@^5.1.0, glob-parent@^5.1.2:
dependencies:
is-glob "^4.0.1"

glob@7.2.0:
version "7.2.0"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.2.0.tgz#d15535af7732e02e948f4c41628bd910293f6023"
integrity sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==
dependencies:
fs.realpath "^1.0.0"
inflight "^1.0.4"
inherits "2"
minimatch "^3.0.4"
once "^1.3.0"
path-is-absolute "^1.0.0"

glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6:
version "7.1.7"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.7.tgz#3b193e9233f01d42d0b3f78294bbeeb418f94a90"
Expand Down
52 changes: 30 additions & 22 deletions tooling/cli.rs/src/helpers/app_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,42 @@ use std::{env::current_dir, path::PathBuf};

use once_cell::sync::Lazy;

fn get_app_dir() -> PathBuf {
let mut dir = current_dir().expect("failed to read cwd");

let mut count = 0;

// only go up three folders max
while count <= 2 {
let test_path = dir.join("src-tauri/tauri.conf.json");
if test_path.exists() {
return dir;
}
count += 1;
match dir.parent() {
Some(parent) => {
dir = parent.to_path_buf();
}
None => break,
}
}
fn get_tauri_dir() -> PathBuf {
glob::glob(
&current_dir()
.expect("failed to read cwd")
.join("**/tauri.conf.json")
.to_string_lossy()
.into_owned(),
)
.unwrap()
.filter_map(Result::ok)
.last()
.map(|p| p.parent().unwrap().to_path_buf())
.expect("Couldn't recognize the current folder as a Tauri project. It must contain a `tauri.conf.json` file in any subfolder.")
}

panic!("Couldn't recognize the current folder as a Tauri project.")
fn get_app_dir() -> Option<PathBuf> {
glob::glob(
&current_dir()
.expect("failed to read cwd")
.join("**/package.json")
.to_string_lossy()
.into_owned(),
)
.unwrap()
.filter_map(Result::ok)
.filter(|p| !p.to_string_lossy().into_owned().contains("node_modules"))
.last()
.map(|p| p.parent().unwrap().to_path_buf())
}

pub fn app_dir() -> &'static PathBuf {
static APP_DIR: Lazy<PathBuf> = Lazy::new(get_app_dir);
static APP_DIR: Lazy<PathBuf> =
Lazy::new(|| get_app_dir().unwrap_or_else(|| get_tauri_dir().parent().unwrap().to_path_buf()));
&APP_DIR
}

pub fn tauri_dir() -> PathBuf {
app_dir().join("src-tauri")
get_tauri_dir()
}

0 comments on commit a8cff6b

Please sign in to comment.