Browse Source

feat(cli.js): allow configuring tauri app path on package.json #2752 (#3035)

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
Lucas Fernandes Nogueira 3 năm trước cách đây
mục cha
commit
fb2b9a52f5

+ 5 - 0
.changes/package-json-configure-app-path.md

@@ -0,0 +1,5 @@
+---
+"cli.js": patch
+---
+
+Read `package.json` and check for a `tauri` object containing the `appPath` string, which points to the tauri crate path.

+ 23 - 1
tooling/cli.js/bin/tauri.js

@@ -5,6 +5,9 @@
 
 import chalk from 'chalk'
 import updateNotifier from 'update-notifier'
+import { findUpSync } from 'find-up'
+import { existsSync, readFileSync } from 'fs'
+import { resolve as resolvePath, dirname } from 'path'
 import { createRequire } from 'module'
 const require = createRequire(import.meta.url)
 const pkg = require('../package.json')
@@ -74,10 +77,29 @@ ${chalk.yellow('Options')}
     if (process.argv && process.env.NODE_ENV !== 'test') {
       process.argv.splice(0, 3)
     }
+    let cwd = null
+    const pkgJsonPath = findUpSync('package.json')
+    if (pkgJsonPath) {
+      const packageJson = JSON.parse(readFileSync(pkgJsonPath).toString())
+      if ('tauri' in packageJson) {
+        const { tauri: tauriConfig } = packageJson
+        if (tauriConfig.appPath) {
+          cwd = resolvePath(dirname(pkgJsonPath), tauriConfig.appPath)
+          console.log(cwd)
+          if (!existsSync(cwd)) {
+            console.error(
+              `Configured appPath in package.json '${cwd}' does not exist.`
+            )
+            process.exit(1)
+          }
+        }
+      }
+    }
     ;(
       await runOnRustCli(
         command,
-        (process.argv || []).filter((v) => v !== '--no-update-notifier')
+        (process.argv || []).filter((v) => v !== '--no-update-notifier'),
+        { cwd }
       )
     ).promise
       .then(() => {

+ 1 - 0
tooling/cli.js/package.json

@@ -54,6 +54,7 @@
     "chalk": "4.1.2",
     "cross-env": "7.0.3",
     "cross-spawn": "7.0.3",
+    "find-up": "^6.2.0",
     "fs-extra": "10.0.0",
     "glob": "7.2.0",
     "global-agent": "3.0.0",

+ 12 - 26
tooling/cli.js/src/helpers/rust-cli.ts

@@ -13,10 +13,16 @@ declare let __RUST_CLI_VERSION__: string
 
 const currentDirName = dirname(fileURLToPath(import.meta.url))
 
+interface Options {
+  cwd?: string
+}
+
 export async function runOnRustCli(
   command: string,
-  args: string[]
+  args: string[],
+  options: Options = {}
 ): Promise<{ pid: number; promise: Promise<void> }> {
+  const cwd = options.cwd ?? process.cwd()
   const targetPath = resolve(currentDirName, '../..')
   const targetCliPath = join(
     targetPath,
@@ -39,20 +45,10 @@ export async function runOnRustCli(
   }
 
   if (existsSync(targetCliPath)) {
-    pid = spawn(
-      targetCliPath,
-      ['tauri', command, ...args],
-      process.cwd(),
-      onClose
-    )
+    pid = spawn(targetCliPath, ['tauri', command, ...args], cwd, onClose)
   } else if (process.env.NODE_ENV === 'production') {
     await downloadCli()
-    pid = spawn(
-      targetCliPath,
-      ['tauri', command, ...args],
-      process.cwd(),
-      onClose
-    )
+    pid = spawn(targetCliPath, ['tauri', command, ...args], cwd, onClose)
   } else {
     if (existsSync(resolve(targetPath, 'test'))) {
       // running local CLI since test directory exists
@@ -62,12 +58,7 @@ export async function runOnRustCli(
         targetPath,
         '../cli.rs/target/release/cargo-tauri'
       )
-      pid = spawn(
-        localCliPath,
-        ['tauri', command, ...args],
-        process.cwd(),
-        onClose
-      )
+      pid = spawn(localCliPath, ['tauri', command, ...args], cwd, onClose)
     } else {
       spawnSync(
         'cargo',
@@ -79,14 +70,9 @@ export async function runOnRustCli(
           '--version',
           __RUST_CLI_VERSION__
         ],
-        process.cwd()
-      )
-      pid = spawn(
-        targetCliPath,
-        ['tauri', command, ...args],
-        process.cwd(),
-        onClose
+        cwd
       )
+      pid = spawn(targetCliPath, ['tauri', command, ...args], cwd, onClose)
     }
   }
 

+ 39 - 0
tooling/cli.js/yarn.lock

@@ -4080,6 +4080,14 @@ find-up@^4.0.0, find-up@^4.1.0:
     locate-path "^5.0.0"
     path-exists "^4.0.0"
 
+find-up@^6.2.0:
+  version "6.2.0"
+  resolved "https://registry.yarnpkg.com/find-up/-/find-up-6.2.0.tgz#f3b81d633fa83bebe64f83a8bab357f86d5914be"
+  integrity sha512-yWHzMzXCaFoABSnFTCPKNFlYoq4mSga9QLRRKOCLSJ33hSkzROB14ITbAWW0QDQDyuzsPQ33S1DsOWQb/oW1yA==
+  dependencies:
+    locate-path "^7.0.0"
+    path-exists "^5.0.0"
+
 find-versions@^3.0.0:
   version "3.2.0"
   resolved "https://registry.yarnpkg.com/find-versions/-/find-versions-3.2.0.tgz#10297f98030a786829681690545ef659ed1d254e"
@@ -5733,6 +5741,13 @@ locate-path@^5.0.0:
   dependencies:
     p-locate "^4.1.0"
 
+locate-path@^7.0.0:
+  version "7.0.0"
+  resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-7.0.0.tgz#f0a60c8dd7ef0f737699eb9461b9567a92bc97da"
+  integrity sha512-+cg2yXqDUKfo4hsFxwa3G1cBJeA+gs1vD8FyV9/odWoUlQe/4syxHQ5DPtKjtfm6gnKbZzjCqzX03kXosvZB1w==
+  dependencies:
+    p-locate "^6.0.0"
+
 lockfile-lint-api@^5.1.7:
   version "5.1.7"
   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:
   dependencies:
     p-try "^2.0.0"
 
+p-limit@^4.0.0:
+  version "4.0.0"
+  resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-4.0.0.tgz#914af6544ed32bfa54670b061cafcbd04984b644"
+  integrity sha512-5b0R4txpzjPWVw/cXXUResoD4hb6U/x9BH08L7nw+GN1sezDzPdxeRvpc9c433fZhBan/wusjbCsqwqm4EIBIQ==
+  dependencies:
+    yocto-queue "^1.0.0"
+
 p-locate@^2.0.0:
   version "2.0.0"
   resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-2.0.0.tgz#20a0103b222a70c8fd39cc2e580680f3dde5ec43"
@@ -6306,6 +6328,13 @@ p-locate@^4.1.0:
   dependencies:
     p-limit "^2.2.0"
 
+p-locate@^6.0.0:
+  version "6.0.0"
+  resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-6.0.0.tgz#3da9a49d4934b901089dca3302fa65dc5a05c04f"
+  integrity sha512-wPrq66Llhl7/4AGC6I+cqxT07LhXvWL08LNXz1fENOw0Ap4sRZZ/gZpTTJ5jpurzzzfS2W/Ge9BY3LgLjCShcw==
+  dependencies:
+    p-limit "^4.0.0"
+
 p-map-series@^1.0.0:
   version "1.0.0"
   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:
   resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-4.0.0.tgz#513bdbe2d3b95d7762e8c1137efa195c6c61b5b3"
   integrity sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==
 
+path-exists@^5.0.0:
+  version "5.0.0"
+  resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-5.0.0.tgz#a6aad9489200b21fab31e49cf09277e5116fb9e7"
+  integrity sha512-RjhtfwJOxzcFmNOi6ltcbcu4Iu+FL3zEj83dk4kAS+fVpTxXLO1b38RvJgT/0QwvV/L3aY9TAnyv0EOqW4GoMQ==
+
 path-is-absolute@^1.0.0:
   version "1.0.1"
   resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
@@ -8160,6 +8194,11 @@ yauzl@^2.4.2:
     buffer-crc32 "~0.2.3"
     fd-slicer "~1.1.0"
 
+yocto-queue@^1.0.0:
+  version "1.0.0"
+  resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-1.0.0.tgz#7f816433fb2cbc511ec8bf7d263c3b58a1a3c251"
+  integrity sha512-9bnSc/HEW2uRy67wc+T8UwauLuPJVn28jb+GtJY16iiKWyvmYJRXVT4UamsAEGQfPohgr2q4Tq0sQbQlxTfi1g==
+
 zopflipng-bin@^6.0.0:
   version "6.0.1"
   resolved "https://registry.yarnpkg.com/zopflipng-bin/-/zopflipng-bin-6.0.1.tgz#a91ef81bdba18e151dda3f34df23d3ecd7d04a1f"