Bladeren bron

feat(tauri.js) add beforeDevCommand and beforeBuildCommand configs (#500)

Lucas Fernandes Nogueira 5 jaren geleden
bovenliggende
commit
20b70ce38f

+ 1 - 1
cli/tauri.js/src/helpers/spawn.ts

@@ -43,7 +43,7 @@ export const spawnSync = (
   cmd: string,
   params: string[],
   cwd: string,
-  onFail: () => void
+  onFail?: () => void
 ): void => {
   log(`[sync] Running "${cmd} ${params.join(' ')}"`)
   log()

+ 13 - 1
cli/tauri.js/src/runner.ts

@@ -9,7 +9,7 @@ import * as entry from './entry'
 import { appDir, tauriDir } from './helpers/app-paths'
 import logger from './helpers/logger'
 import onShutdown from './helpers/on-shutdown'
-import { spawn } from './helpers/spawn'
+import { spawn, spawnSync } from './helpers/spawn'
 const getTauriConfig = require('./helpers/tauri-config')
 import { TauriConfig } from './types/config'
 
@@ -21,6 +21,7 @@ class Runner {
   tauriWatcher?: FSWatcher
   devPath?: string
   killPromise?: Function
+  ranBeforeDevCommand?: boolean
 
   constructor() {
     this.pid = 0
@@ -43,6 +44,12 @@ class Runner {
       }
     }
 
+    if (!this.ranBeforeDevCommand && cfg.build.beforeDevCommand) {
+      this.ranBeforeDevCommand = true // prevent calling it twice on recursive call on our watcher
+      const [command, ...args] = cfg.build.beforeDevCommand.split(' ')
+      spawnSync(command, args, tauriDir)
+    }
+
     const tomlContents = this.__getManifest()
     this.__whitelistApi(cfg, tomlContents)
     this.__rewriteManifest(tomlContents)
@@ -127,6 +134,11 @@ class Runner {
   }
 
   async build(cfg: TauriConfig): Promise<void> {
+    if (cfg.build.beforeBuildCommand) {
+      const [command, ...args] = cfg.build.beforeBuildCommand.split(' ')
+      spawnSync(command, args, tauriDir)
+    }
+    
     const tomlContents = this.__getManifest()
     this.__whitelistApi(cfg, tomlContents)
     this.__rewriteManifest(tomlContents)

+ 3 - 1
cli/tauri.js/src/template/defaultConfig.ts

@@ -1,7 +1,9 @@
 export default {
   build: {
     distDir: '../dist',
-    devPath: 'http://localhost:4000'
+    devPath: 'http://localhost:4000',
+    beforeDevCommand: '',
+    beforeBuildCommand: ''
   },
   ctx: {},
   tauri: {

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

@@ -5,6 +5,8 @@ export interface TauriConfig {
   build: {
     distDir: string
     devPath: string
+    beforeDevCommand?: string
+    beforeBuildCommand?: string
   }
   ctx: {
     prod?: boolean