فهرست منبع

feat(tauri.js) keep running on failures & watch tauri lib on con… (#497)

Lucas Fernandes Nogueira 5 سال پیش
والد
کامیت
a084ccf756
3فایلهای تغییر یافته به همراه57 افزوده شده و 22 حذف شده
  1. 8 3
      cli/tauri.js/bin/tauri-dev.js
  2. 48 19
      cli/tauri.js/src/runner.ts
  3. 1 0
      cli/tauri.js/src/types/config.ts

+ 8 - 3
cli/tauri.js/bin/tauri-dev.js

@@ -2,9 +2,10 @@ const parseArgs = require('minimist')
 
 const argv = parseArgs(process.argv.slice(2), {
   alias: {
-    h: 'help'
+    h: 'help',
+    e: 'exit-on-panic'
   },
-  boolean: ['h']
+  boolean: ['h', 'e']
 })
 
 if (argv.help) {
@@ -21,4 +22,8 @@ if (argv.help) {
 
 const dev = require('../dist/api/dev')
 
-dev()
+dev({
+  ctx: {
+    exitOnPanic: argv['exit-on-panic']
+  }
+})

+ 48 - 19
cli/tauri.js/src/runner.ts

@@ -1,5 +1,5 @@
 import Inliner from '@tauri-apps/tauri-inliner'
-import toml from '@tauri-apps/toml'
+import toml, { JsonMap } from '@tauri-apps/toml'
 import chokidar, { FSWatcher } from 'chokidar'
 import { existsSync, readFileSync, writeFileSync } from 'fs-extra'
 import { JSDOM } from 'jsdom'
@@ -14,7 +14,7 @@ const getTauriConfig = require('./helpers/tauri-config')
 import { TauriConfig } from './types/config'
 
 const log = logger('app:tauri', 'green')
-const warn = logger('app:tauri (template)', 'red')
+const warn = logger('app:tauri (runner)', 'red')
 
 class Runner {
   pid: number
@@ -43,9 +43,9 @@ class Runner {
       }
     }
 
-    this.__manipulateToml(toml => {
-      this.__whitelistApi(cfg, toml)
-    })
+    const tomlContents = this.__getManifest()
+    this.__whitelistApi(cfg, tomlContents)
+    this.__rewriteManifest(tomlContents)
 
     entry.generate(tauriDir, cfg)
 
@@ -67,19 +67,35 @@ class Runner {
         cargoArgs: ['run'].concat(
           features.length ? ['--features', ...features] : []
         ),
-        dev: true
+        dev: true,
+        exitOnPanic: cfg.ctx.exitOnPanic
       })
     }
 
     // Start watching for tauri app changes
     // eslint-disable-next-line security/detect-non-literal-fs-filename
+
+    let tauriPaths: string[] = []
+    // @ts-ignore
+    if (tomlContents.dependencies.tauri.path) {
+      // @ts-ignore
+      const tauriPath = path.resolve(tauriDir, tomlContents.dependencies.tauri.path)
+      tauriPaths = [
+        tauriPath,
+        `${tauriPath}-api`,
+        `${tauriPath}-updater`,
+        `${tauriPath}-utils`
+      ]
+    }
+
     this.tauriWatcher = chokidar
       .watch(
         [
           path.join(tauriDir, 'src'),
           path.join(tauriDir, 'Cargo.toml'),
           path.join(tauriDir, 'build.rs'),
-          path.join(appDir, 'tauri.conf.json')
+          path.join(tauriDir, 'tauri.conf.json'),
+          ...tauriPaths
         ],
         {
           ignoreInitial: true
@@ -88,7 +104,7 @@ class Runner {
       .on(
         'change',
         debounce((path: string) => {
-          this.__stopCargo()
+          (this.pid ? this.__stopCargo() : Promise.resolve())
             .then(() => {
               if (path.includes('tauri.conf.json')) {
                 this.run(getTauriConfig({ ctx: cfg.ctx })).catch(e => {
@@ -111,9 +127,9 @@ class Runner {
   }
 
   async build(cfg: TauriConfig): Promise<void> {
-    this.__manipulateToml(toml => {
-      this.__whitelistApi(cfg, toml)
-    })
+    const tomlContents = this.__getManifest()
+    this.__whitelistApi(cfg, tomlContents)
+    this.__rewriteManifest(tomlContents)
 
     entry.generate(tauriDir, cfg)
 
@@ -243,11 +259,13 @@ class Runner {
   async __runCargoCommand({
     cargoArgs,
     extraArgs,
-    dev = false
+    dev = false,
+    exitOnPanic = true
   }: {
     cargoArgs: string[]
     extraArgs?: string[]
-    dev?: boolean
+    dev?: boolean,
+    exitOnPanic?: boolean
   }): Promise<void> {
     return new Promise((resolve, reject) => {
       this.pid = spawn(
@@ -258,6 +276,12 @@ class Runner {
         tauriDir,
 
         code => {
+          if (dev && !exitOnPanic && code === 101) {
+            this.pid = 0
+            resolve()
+            return
+          }
+
           if (code) {
             warn()
             warn('⚠️  [FAIL] Cargo CLI has failed')
@@ -308,14 +332,19 @@ class Runner {
     })
   }
 
-  __manipulateToml(callback: (tomlContents: object) => void): void {
-    const tomlPath = path.join(tauriDir, 'Cargo.toml')
-    const tomlFile = readFileSync(tomlPath)
-    // @ts-ignore
-    const tomlContents = toml.parse(tomlFile)
+  __getManifestPath(): string {
+    return path.join(tauriDir, 'Cargo.toml')
+  }
 
-    callback(tomlContents)
+  __getManifest(): JsonMap {
+    const tomlPath = this.__getManifestPath()
+    const tomlFile = readFileSync(tomlPath).toString()
+    const tomlContents = toml.parse(tomlFile)
+    return tomlContents
+  }
 
+  __rewriteManifest(tomlContents: JsonMap) {
+    const tomlPath = this.__getManifestPath()
     const output = toml.stringify(tomlContents)
     writeFileSync(tomlPath, output)
   }

+ 1 - 0
cli/tauri.js/src/types/config.ts

@@ -12,6 +12,7 @@ export interface TauriConfig {
     target: string
     debug?: boolean
     targetName: string
+    exitOnPanic?: boolean
   }
   bundle: {}
   tauri: {