2026-04-22 04:33:38 +02:00
|
|
|
import { execFileSync, spawn } from 'child_process'
|
2026-05-10 14:18:52 +02:00
|
|
|
import { existsSync } from 'fs'
|
|
|
|
|
import { delimiter, dirname, join } from 'path'
|
2026-04-22 04:33:38 +02:00
|
|
|
|
2026-05-10 14:18:52 +02:00
|
|
|
function getNodeBinDir() {
|
|
|
|
|
return dirname(process.execPath)
|
2026-04-22 04:33:38 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-10 14:18:52 +02:00
|
|
|
function getNodePrefix() {
|
|
|
|
|
return process.platform === 'win32' ? getNodeBinDir() : dirname(getNodeBinDir())
|
2026-04-22 04:33:38 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-10 14:18:52 +02:00
|
|
|
function getNpmCliPath() {
|
|
|
|
|
const prefix = getNodePrefix()
|
|
|
|
|
const candidates = process.platform === 'win32'
|
|
|
|
|
? [
|
|
|
|
|
join(prefix, 'node_modules', 'npm', 'bin', 'npm-cli.js'),
|
|
|
|
|
join(getNodeBinDir(), 'node_modules', 'npm', 'bin', 'npm-cli.js'),
|
|
|
|
|
]
|
|
|
|
|
: [join(prefix, 'lib', 'node_modules', 'npm', 'bin', 'npm-cli.js')]
|
|
|
|
|
const npmCli = candidates.find(existsSync)
|
2026-04-22 04:33:38 +02:00
|
|
|
|
2026-05-10 14:18:52 +02:00
|
|
|
if (!npmCli) {
|
|
|
|
|
throw new Error(`Unable to locate npm CLI for ${process.execPath}; checked ${candidates.join(', ')}`)
|
2026-04-22 04:33:38 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-10 14:18:52 +02:00
|
|
|
return npmCli
|
2026-05-06 16:15:42 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-10 14:18:52 +02:00
|
|
|
function getGlobalPackageBin(prefix: string) {
|
|
|
|
|
return process.platform === 'win32'
|
|
|
|
|
? join(prefix, 'node_modules', 'hermes-web-ui', 'bin', 'hermes-web-ui.mjs')
|
|
|
|
|
: join(prefix, 'lib', 'node_modules', 'hermes-web-ui', 'bin', 'hermes-web-ui.mjs')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getCurrentNodeEnv() {
|
|
|
|
|
return {
|
|
|
|
|
...process.env,
|
|
|
|
|
PATH: [getNodeBinDir(), process.env.PATH].filter(Boolean).join(delimiter),
|
|
|
|
|
npm_node_execpath: process.execPath,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function runNpm(args: string[], options: { timeout?: number } = {}) {
|
|
|
|
|
return execFileSync(process.execPath, [getNpmCliPath(), ...args], {
|
2026-04-22 04:33:38 +02:00
|
|
|
encoding: 'utf-8',
|
2026-05-10 14:18:52 +02:00
|
|
|
timeout: options.timeout,
|
2026-04-22 04:33:38 +02:00
|
|
|
stdio: ['pipe', 'pipe', 'pipe'],
|
2026-05-10 14:18:52 +02:00
|
|
|
env: getCurrentNodeEnv(),
|
|
|
|
|
}).trim()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getGlobalPrefix() {
|
|
|
|
|
return runNpm(['prefix', '-g'])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getGlobalCliScript() {
|
|
|
|
|
return getGlobalPackageBin(getGlobalPrefix())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function runUpdateInstall() {
|
|
|
|
|
return runNpm(['install', '-g', 'hermes-web-ui@latest'], { timeout: 10 * 60 * 1000 })
|
2026-04-22 04:33:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function spawnRestart(port: string) {
|
2026-05-10 14:18:52 +02:00
|
|
|
const cli = getGlobalCliScript()
|
2026-04-22 04:33:38 +02:00
|
|
|
|
2026-05-10 14:18:52 +02:00
|
|
|
return spawn(process.execPath, [cli, 'restart', '--port', port], {
|
2026-04-22 04:33:38 +02:00
|
|
|
detached: true,
|
|
|
|
|
stdio: 'ignore',
|
|
|
|
|
windowsHide: true,
|
2026-05-10 14:18:52 +02:00
|
|
|
env: getCurrentNodeEnv(),
|
2026-04-22 04:33:38 +02:00
|
|
|
})
|
|
|
|
|
}
|
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-05-06 16:15:42 +08:00
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
|
success: true,
|
|
|
|
|
message: output.trim() || 'hermes-web-ui updated successfully',
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 12:35:48 +08:00
|
|
|
setTimeout(() => {
|
2026-05-06 16:15:42 +08:00
|
|
|
try {
|
|
|
|
|
spawnRestart(process.env.PORT || '8648').unref()
|
|
|
|
|
} finally {
|
|
|
|
|
process.exit(0)
|
|
|
|
|
}
|
|
|
|
|
}, 3000)
|
2026-04-21 12:35:48 +08:00
|
|
|
} catch (err: any) {
|
2026-04-22 04:33:38 +02:00
|
|
|
ctx.status = 500
|
2026-05-06 16:15:42 +08:00
|
|
|
ctx.body = {
|
|
|
|
|
success: false,
|
|
|
|
|
message: err.stderr?.toString() || err.message || String(err),
|
|
|
|
|
}
|
2026-04-21 12:35:48 +08:00
|
|
|
}
|
|
|
|
|
}
|