Skip to content

Commit fb2b9a5

Browse files
feat(cli.js): allow configuring tauri app path on package.json #2752 (#3035)
Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
1 parent f87c04d commit fb2b9a5

File tree

5 files changed

+80
-27
lines changed

5 files changed

+80
-27
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cli.js": patch
3+
---
4+
5+
Read `package.json` and check for a `tauri` object containing the `appPath` string, which points to the tauri crate path.

tooling/cli.js/bin/tauri.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
import chalk from 'chalk'
77
import updateNotifier from 'update-notifier'
8+
import { findUpSync } from 'find-up'
9+
import { existsSync, readFileSync } from 'fs'
10+
import { resolve as resolvePath, dirname } from 'path'
811
import { createRequire } from 'module'
912
const require = createRequire(import.meta.url)
1013
const pkg = require('../package.json')
@@ -74,10 +77,29 @@ ${chalk.yellow('Options')}
7477
if (process.argv && process.env.NODE_ENV !== 'test') {
7578
process.argv.splice(0, 3)
7679
}
80+
let cwd = null
81+
const pkgJsonPath = findUpSync('package.json')
82+
if (pkgJsonPath) {
83+
const packageJson = JSON.parse(readFileSync(pkgJsonPath).toString())
84+
if ('tauri' in packageJson) {
85+
const { tauri: tauriConfig } = packageJson
86+
if (tauriConfig.appPath) {
87+
cwd = resolvePath(dirname(pkgJsonPath), tauriConfig.appPath)
88+
console.log(cwd)
89+
if (!existsSync(cwd)) {
90+
console.error(
91+
`Configured appPath in package.json '${cwd}' does not exist.`
92+
)
93+
process.exit(1)
94+
}
95+
}
96+
}
97+
}
7798
;(
7899
await runOnRustCli(
79100
command,
80-
(process.argv || []).filter((v) => v !== '--no-update-notifier')
101+
(process.argv || []).filter((v) => v !== '--no-update-notifier'),
102+
{ cwd }
81103
)
82104
).promise
83105
.then(() => {

tooling/cli.js/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"chalk": "4.1.2",
5555
"cross-env": "7.0.3",
5656
"cross-spawn": "7.0.3",
57+
"find-up": "^6.2.0",
5758
"fs-extra": "10.0.0",
5859
"glob": "7.2.0",
5960
"global-agent": "3.0.0",

tooling/cli.js/src/helpers/rust-cli.ts

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@ declare let __RUST_CLI_VERSION__: string
1313

1414
const currentDirName = dirname(fileURLToPath(import.meta.url))
1515

16+
interface Options {
17+
cwd?: string
18+
}
19+
1620
export async function runOnRustCli(
1721
command: string,
18-
args: string[]
22+
args: string[],
23+
options: Options = {}
1924
): Promise<{ pid: number; promise: Promise<void> }> {
25+
const cwd = options.cwd ?? process.cwd()
2026
const targetPath = resolve(currentDirName, '../..')
2127
const targetCliPath = join(
2228
targetPath,
@@ -39,20 +45,10 @@ export async function runOnRustCli(
3945
}
4046

4147
if (existsSync(targetCliPath)) {
42-
pid = spawn(
43-
targetCliPath,
44-
['tauri', command, ...args],
45-
process.cwd(),
46-
onClose
47-
)
48+
pid = spawn(targetCliPath, ['tauri', command, ...args], cwd, onClose)
4849
} else if (process.env.NODE_ENV === 'production') {
4950
await downloadCli()
50-
pid = spawn(
51-
targetCliPath,
52-
['tauri', command, ...args],
53-
process.cwd(),
54-
onClose
55-
)
51+
pid = spawn(targetCliPath, ['tauri', command, ...args], cwd, onClose)
5652
} else {
5753
if (existsSync(resolve(targetPath, 'test'))) {
5854
// running local CLI since test directory exists
@@ -62,12 +58,7 @@ export async function runOnRustCli(
6258
targetPath,
6359
'../cli.rs/target/release/cargo-tauri'
6460
)
65-
pid = spawn(
66-
localCliPath,
67-
['tauri', command, ...args],
68-
process.cwd(),
69-
onClose
70-
)
61+
pid = spawn(localCliPath, ['tauri', command, ...args], cwd, onClose)
7162
} else {
7263
spawnSync(
7364
'cargo',
@@ -79,14 +70,9 @@ export async function runOnRustCli(
7970
'--version',
8071
__RUST_CLI_VERSION__
8172
],
82-
process.cwd()
83-
)
84-
pid = spawn(
85-
targetCliPath,
86-
['tauri', command, ...args],
87-
process.cwd(),
88-
onClose
73+
cwd
8974
)
75+
pid = spawn(targetCliPath, ['tauri', command, ...args], cwd, onClose)
9076
}
9177
}
9278

tooling/cli.js/yarn.lock

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4080,6 +4080,14 @@ find-up@^4.0.0, find-up@^4.1.0:
40804080
locate-path "^5.0.0"
40814081
path-exists "^4.0.0"
40824082

4083+
find-up@^6.2.0:
4084+
version "6.2.0"
4085+
resolved "https://registry.yarnpkg.com/find-up/-/find-up-6.2.0.tgz#f3b81d633fa83bebe64f83a8bab357f86d5914be"
4086+
integrity sha512-yWHzMzXCaFoABSnFTCPKNFlYoq4mSga9QLRRKOCLSJ33hSkzROB14ITbAWW0QDQDyuzsPQ33S1DsOWQb/oW1yA==
4087+
dependencies:
4088+
locate-path "^7.0.0"
4089+
path-exists "^5.0.0"
4090+
40834091
find-versions@^3.0.0:
40844092
version "3.2.0"
40854093
resolved "https://registry.yarnpkg.com/find-versions/-/find-versions-3.2.0.tgz#10297f98030a786829681690545ef659ed1d254e"
@@ -5733,6 +5741,13 @@ locate-path@^5.0.0:
57335741
dependencies:
57345742
p-locate "^4.1.0"
57355743

5744+
locate-path@^7.0.0:
5745+
version "7.0.0"
5746+
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-7.0.0.tgz#f0a60c8dd7ef0f737699eb9461b9567a92bc97da"
5747+
integrity sha512-+cg2yXqDUKfo4hsFxwa3G1cBJeA+gs1vD8FyV9/odWoUlQe/4syxHQ5DPtKjtfm6gnKbZzjCqzX03kXosvZB1w==
5748+
dependencies:
5749+
p-locate "^6.0.0"
5750+
57365751
lockfile-lint-api@^5.1.7:
57375752
version "5.1.7"
57385753
resolved "https://registry.yarnpkg.com/lockfile-lint-api/-/lockfile-lint-api-5.1.7.tgz#052f615ab85ac3ad69f8c90b03eb3b64dbd6ebd9"
@@ -6292,6 +6307,13 @@ p-limit@^2.2.0:
62926307
dependencies:
62936308
p-try "^2.0.0"
62946309

6310+
p-limit@^4.0.0:
6311+
version "4.0.0"
6312+
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-4.0.0.tgz#914af6544ed32bfa54670b061cafcbd04984b644"
6313+
integrity sha512-5b0R4txpzjPWVw/cXXUResoD4hb6U/x9BH08L7nw+GN1sezDzPdxeRvpc9c433fZhBan/wusjbCsqwqm4EIBIQ==
6314+
dependencies:
6315+
yocto-queue "^1.0.0"
6316+
62956317
p-locate@^2.0.0:
62966318
version "2.0.0"
62976319
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-2.0.0.tgz#20a0103b222a70c8fd39cc2e580680f3dde5ec43"
@@ -6306,6 +6328,13 @@ p-locate@^4.1.0:
63066328
dependencies:
63076329
p-limit "^2.2.0"
63086330

6331+
p-locate@^6.0.0:
6332+
version "6.0.0"
6333+
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-6.0.0.tgz#3da9a49d4934b901089dca3302fa65dc5a05c04f"
6334+
integrity sha512-wPrq66Llhl7/4AGC6I+cqxT07LhXvWL08LNXz1fENOw0Ap4sRZZ/gZpTTJ5jpurzzzfS2W/Ge9BY3LgLjCShcw==
6335+
dependencies:
6336+
p-limit "^4.0.0"
6337+
63096338
p-map-series@^1.0.0:
63106339
version "1.0.0"
63116340
resolved "https://registry.yarnpkg.com/p-map-series/-/p-map-series-1.0.0.tgz#bf98fe575705658a9e1351befb85ae4c1f07bdca"
@@ -6431,6 +6460,11 @@ path-exists@^4.0.0:
64316460
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-4.0.0.tgz#513bdbe2d3b95d7762e8c1137efa195c6c61b5b3"
64326461
integrity sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==
64336462

6463+
path-exists@^5.0.0:
6464+
version "5.0.0"
6465+
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-5.0.0.tgz#a6aad9489200b21fab31e49cf09277e5116fb9e7"
6466+
integrity sha512-RjhtfwJOxzcFmNOi6ltcbcu4Iu+FL3zEj83dk4kAS+fVpTxXLO1b38RvJgT/0QwvV/L3aY9TAnyv0EOqW4GoMQ==
6467+
64346468
path-is-absolute@^1.0.0:
64356469
version "1.0.1"
64366470
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
@@ -8160,6 +8194,11 @@ yauzl@^2.4.2:
81608194
buffer-crc32 "~0.2.3"
81618195
fd-slicer "~1.1.0"
81628196

8197+
yocto-queue@^1.0.0:
8198+
version "1.0.0"
8199+
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-1.0.0.tgz#7f816433fb2cbc511ec8bf7d263c3b58a1a3c251"
8200+
integrity sha512-9bnSc/HEW2uRy67wc+T8UwauLuPJVn28jb+GtJY16iiKWyvmYJRXVT4UamsAEGQfPohgr2q4Tq0sQbQlxTfi1g==
8201+
81638202
zopflipng-bin@^6.0.0:
81648203
version "6.0.1"
81658204
resolved "https://registry.yarnpkg.com/zopflipng-bin/-/zopflipng-bin-6.0.1.tgz#a91ef81bdba18e151dda3f34df23d3ecd7d04a1f"

0 commit comments

Comments
 (0)