don't offer automatic update if we are running as Windows service
Massimo Melina committed
Feb 28, 2024 at 12:15 UTC
569951fe1cef18fe859f865f9ef1b961388ee8e4
2 files changed
+15
-3
src/update.ts
+3
-2
@@ -11,6 +11,7 @@ import { access, chmod, stat } from 'fs/promises'
11
import { Readable } from 'stream'
12
import open from 'open'
13
import { currentVersion, defineConfig, versionToScalar } from './config'
14
+import { currentServiceName } from './util-os'
15
16
const updateToBeta = defineConfig('update_to_beta', false)
17
@@ -65,7 +66,7 @@ export function localUpdateAvailable() {
66
}
67
68
export function updateSupported() {
68
- return IS_BINARY
69
+ return IS_BINARY && !currentServiceName
70
}
71
72
export async function update(tagOrUrl: string='') {
@@ -116,7 +117,7 @@ export async function update(tagOrUrl: string='') {
117
console.log("launching new version in background", newBinFile)
118
launch(newBin, ['--updating', binFile], { sync: true }) // sync necessary to work on mac by double-click
119
})
119
- console.log('quitting')
120
+ console.log("quitting")
121
setTimeout(() => process.exit()) // give time to return (and caller to complete, eg: rest api to reply)
122
}
123
catch (e: any) {
src/util-os.ts
+12
-1
@@ -1,6 +1,8 @@
1
import { resolve } from 'path'
2
import { exec, execSync } from 'child_process'
3
-import { try_ } from './misc'
3
+import { splitAt, try_ } from './misc'
4
+import _ from 'lodash'
5
+import { pid } from 'node:process'
6
import { promisify } from 'util'
7
import { IS_WINDOWS } from './const'
8
@@ -36,3 +38,12 @@ export async function runCmd(cmd: string, args: string[] = []) {
38
const { stdout, stderr } = await promisify(exec)(`@chcp 65001 >nul & cmd /c ${cmd} ${args.join(' ')}`, { encoding: 'utf-8' })
39
return stderr || stdout
40
}
41
+
42
+function getWindowsServices() {
43
+ const fields = ['PathName', 'DisplayName', 'ProcessId'] as const
44
+ const chunks = execSync(`wmic service get ${fields.join(',')} /value`).toString().replace(/\r/g, '').split(/\n\n+/)
45
+ return chunks.map(chunk =>
46
+ Object.fromEntries(chunk.split('\n').map(line => splitAt('=', line))) as { [k in typeof fields[number]]: string })
47
+}
48
+
49
+export const currentServiceName = IS_WINDOWS && _.find(getWindowsServices(), { ProcessId: String(pid) })?.DisplayName