simplify: pure JS tray launcher, spawn npm run start
This commit is contained in:
1 parent
1ad9a59f76
commit
a1edf16c58
4 files changed
+45
-148
No files matched your search
@@ -0,0 +1,42 @@
|
||||
const { app, Tray, Menu, nativeImage } = require('electron')
|
||||
const { spawn } = require('child_process')
|
||||
const path = require('path')
|
||||
|
||||
const PROJECT_DIR = path.resolve(__dirname, '../../project')
|
||||
let backend = null
|
||||
let tray = null
|
||||
|
||||
const ICON = nativeImage.createFromBuffer(Buffer.from(
|
||||
'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAL0lEQVR4nO3OIQEAAAgDMHJg6Ul1iHEzMb/q2UsqAQEBAQEBAQEBAQEBAQGBdOABvyXoW2Q1L3wAAAAASUVORK5CYII=',
|
||||
'base64'
|
||||
))
|
||||
|
||||
function startBackend() {
|
||||
backend = spawn('npm.cmd', ['run', 'start'], {
|
||||
cwd: PROJECT_DIR,
|
||||
stdio: 'ignore',
|
||||
env: { ...process.env },
|
||||
shell: true,
|
||||
windowsHide: true,
|
||||
})
|
||||
backend.on('exit', () => { backend = null })
|
||||
}
|
||||
|
||||
function stopBackend() {
|
||||
if (backend) { backend.kill('SIGTERM'); backend = null }
|
||||
}
|
||||
|
||||
app.whenReady().then(() => {
|
||||
tray = new Tray(ICON.resize({ width: 32, height: 32 }))
|
||||
tray.setToolTip('质量追溯系统')
|
||||
tray.setContextMenu(Menu.buildFromTemplate([
|
||||
{ label: '打开页面', click: () => require('child_process').exec('start http://localhost:3000') },
|
||||
{ type: 'separator' },
|
||||
{ label: '退出', click: () => { stopBackend(); app.quit() } },
|
||||
]))
|
||||
|
||||
startBackend()
|
||||
})
|
||||
|
||||
app.on('window-all-closed', () => {})
|
||||
app.on('before-quit', () => stopBackend())
|
||||
Reference in new issue
Block a user