2026-04-22 04:33:38 +02:00
|
|
|
import { execFileSync, spawn } from 'child_process'
|
|
|
|
|
import { dirname, join } from 'path'
|
|
|
|
|
|
|
|
|
|
function getNodeBinDir() {
|
|
|
|
|
return dirname(process.execPath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getNpmBin() {
|
|
|
|
|
return join(getNodeBinDir(), process.platform === 'win32' ? 'npm.cmd' : 'npm')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getCliBin() {
|
|
|
|
|
return join(getNodeBinDir(), process.platform === 'win32' ? 'hermes-web-ui.cmd' : 'hermes-web-ui')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getWindowsShell() {
|
|
|
|
|
return process.env.ComSpec || 'cmd.exe'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function quoteForWindowsCommand(value: string) {
|
|
|
|
|
return `"${value.replace(/"/g, '""')}"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function runUpdateInstall() {
|
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
|
return execFileSync(getWindowsShell(), ['/d', '/s', '/c', `${quoteForWindowsCommand(getNpmBin())} install -g hermes-web-ui@latest`], {
|
|
|
|
|
encoding: 'utf-8',
|
|
|
|
|
timeout: 120000,
|
|
|
|
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
|
|
|
windowsHide: true,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return execFileSync(getNpmBin(), ['install', '-g', 'hermes-web-ui@latest'], {
|
|
|
|
|
encoding: 'utf-8',
|
|
|
|
|
timeout: 120000,
|
|
|
|
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function spawnRestart(port: string) {
|
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
|
return spawn(getWindowsShell(), ['/d', '/s', '/c', `${quoteForWindowsCommand(getCliBin())} restart --port ${port}`], {
|
|
|
|
|
detached: true,
|
|
|
|
|
stdio: 'ignore',
|
|
|
|
|
windowsHide: true,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return spawn(getCliBin(), ['restart', '--port', port], {
|
|
|
|
|
detached: true,
|
|
|
|
|
stdio: 'ignore',
|
|
|
|
|
windowsHide: true,
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-04-21 12:35:48 +08:00
|
|
|
|
|
|
|
|
export async function handleUpdate(ctx: any) {
|
|
|
|
|
try {
|
2026-04-22 04:33:38 +02:00
|
|
|
const output = runUpdateInstall()
|
2026-04-21 12:35:48 +08:00
|
|
|
ctx.body = { success: true, message: output.trim() }
|
|
|
|
|
setTimeout(() => {
|
2026-04-22 04:33:38 +02:00
|
|
|
spawnRestart(process.env.PORT || '8648').unref()
|
2026-04-21 12:35:48 +08:00
|
|
|
process.exit(0)
|
|
|
|
|
}, 2000)
|
|
|
|
|
} catch (err: any) {
|
2026-04-22 04:33:38 +02:00
|
|
|
ctx.status = 500
|
|
|
|
|
ctx.body = { success: false, message: err.stderr || err.message }
|
2026-04-21 12:35:48 +08:00
|
|
|
}
|
|
|
|
|
}
|