electron 启动时调用exe

app.whenReady().then(() => {
  const { spawn } = require('child_process')
  const fs = require('fs')
  const path = require('path')

  const logStream = fs.createWriteStream('app.log', { flags: 'a' }) // 日志文件
  let exePath = path.resolve('./resources/app.asar.unpacked/resources/plugin-test')
  if (process.env.NODE_ENV === 'development') {
    exePath = path.resolve('./resources/plugin-test')
  }

  const child = spawn('./main.exe', [], { cwd: exePath, stdio: ['ignore', 'pipe', 'pipe'] })

  child.stdout.on('data', (data) => {
    console.log('stdout:', data.toString())
    logStream.write(data)
  })

  child.stderr.on('data', (data) => {
    console.error('stderr:', data.toString())
    logStream.write(data)
  })

  child.on('close', (code) => {
    console.log(`子进程已退出,退出码 ${code}`)
    logStream.end()
  })

  // Set app user model id for windows
  electronApp.setAppUserModelId('com.electron')

  // Default open or close DevTools by F12 in development
  // and ignore CommandOrControl + R in production.
  // see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
  app.on('browser-window-created', (_, window) => {
    optimizer.watchWindowShortcuts(window)
  })

  // IPC test
  ipcMain.on('ping', () => console.log('pong'))

  createWindow()

  app.on('activate', function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
 
})
posted @ 2024-07-03 13:44  风花一世月  阅读(31)  评论(0编辑  收藏  举报