瀏覽代碼

fix(cli.js): handle cli.rs promise rejection (#1689)

Lucas Fernandes Nogueira 4 年之前
父節點
當前提交
8845487f9d

+ 5 - 0
.changes/cli.js-error-propagation.md

@@ -0,0 +1,5 @@
+---
+"cli.js": patch
+---
+
+Fixes `UnhandledPromiseRejectionWarning` when the Rust CLI call fails.

文件差異過大導致無法顯示
+ 0 - 0
examples/api/public/build/bundle.js


文件差異過大導致無法顯示
+ 0 - 0
examples/api/public/build/bundle.js.map


+ 18 - 13
tooling/cli.js/bin/tauri.js

@@ -62,14 +62,14 @@ ${chalk.yellow('Options')}
     console.log(`${pkg.version}`)
     return false // do this for node consumers and tests
   } else if (cmds.includes(command)) {
-    if (process.argv && !process.env.test) {
+    if (process.argv && process.env.NODE_ENV !== 'test') {
       process.argv.splice(2, 1)
     }
     console.log(`[tauri]: running ${command}`)
     require(`./tauri-${command}`)
   } else {
     const { runOnRustCli } = require('../dist/helpers/rust-cli')
-    if (process.argv && !process.env.test) {
+    if (process.argv && process.env.NODE_ENV !== 'test') {
       process.argv.splice(0, 3)
     }
     ;(
@@ -77,14 +77,16 @@ ${chalk.yellow('Options')}
         command,
         (process.argv || []).filter((v) => v !== '--no-update-notifier')
       )
-    ).promise.then(() => {
-      if (command === 'init' && !process.argv.some((arg) => arg === '--ci')) {
-        const {
-          installDependencies
-        } = require('../dist/api/dependency-manager')
-        return installDependencies()
-      }
-    })
+    ).promise
+      .then(() => {
+        if (command === 'init' && !process.argv.some((arg) => arg === '--ci')) {
+          const {
+            installDependencies
+          } = require('../dist/api/dependency-manager')
+          return installDependencies()
+        }
+      })
+      .catch(() => process.exit(1))
   }
 }
 
@@ -92,6 +94,9 @@ module.exports = {
   tauri
 }
 
-tauri(cmd).catch((e) => {
-  throw e
-})
+// on test we use the module.exports
+if (process.env.NODE_ENV !== 'test') {
+  tauri(cmd).catch((e) => {
+    throw e
+  })
+}

+ 1 - 1
tooling/cli.js/test/jest/__tests__/tauri.spec.js

@@ -19,7 +19,7 @@ describe('[CLI] cli.js', () => {
 
   it('gets you help', async () => {
     jest.spyOn(console, 'log')
-    const tests = ['--help', '-h', 'invalid command']
+    const tests = ['--help', '-h']
     for (const test of tests) {
       tauri([test])
       expect(!!console.log.mock.calls[0][0]).toBe(true)

部分文件因文件數量過多而無法顯示