automatic-update when running as windows service

Massimo Melina committed Apr 9, 2026 at 00:15 UTC a73b64483b6ef839b3cc7ba6d1220812c90e91db
1 file changed +8 -6
src/update.ts
+8 -6
@@ -108,15 +108,14 @@ export function previousAvailable() {
108 return exists(PREVIOUS_FN)
109 }
110
111 -export async function updateSupported() {
112 - return !process.env.DISABLE_UPDATE && (argv.forceupdate || IS_BINARY && !await runningAsWindowsService)
111 +export function updateSupported() {
112 + return !process.env.DISABLE_UPDATE && (argv.forceupdate || IS_BINARY)
113 }
114
115 export async function update(tagOrUrl: string='') {
116 - if (!await updateSupported())
116 + if (!updateSupported())
117 throw process.env.DISABLE_UPDATE ? "Automatic updates are disabled"
118 - : !IS_BINARY ? "Only binary versions support automatic updates"
119 - : "Automatic updates are not available running as a service"
118 + : "Only binary versions support automatic updates"
119 let url = tagOrUrl.includes('://') && tagOrUrl
120 if (tagOrUrl === PREVIOUS_TAG)
121 await rename(PREVIOUS_FN, LOCAL_UPDATE)
@@ -175,17 +174,20 @@ export async function update(tagOrUrl: string='') {
174 }
175 await rename(INSTALLED_FN, PREVIOUS_FN).catch(e => e?.code !== 'ENOENT' && console.warn(String(e)))
176 await rename(LOCAL_UPDATE, INSTALLED_FN).catch(e => console.warn(String(e)))
177 + // the bridge process exists to preserve terminal access; skip it when there's no terminal
178 + const preserveTerminal = process.stdin.isTTY && !await runningAsWindowsService // NSSM fakes a TTY, so we need explicit detection on Windows
179 onProcessExit(() => {
180 const oldBinFile = 'old-' + binFile
181 const oldBin = join(binPath, oldBinFile)
182 try { unlinkSync(oldBin) }
183 catch {}
184 renameSync(bin, oldBin)
184 - if (!IS_WINDOWS && !process.stdin.isTTY) { // non-interactive (service): rename in place and let the supervisor restart
185 + if (!preserveTerminal) {
186 renameSync(newBin, join(binPath, binFile))
187 console.log("Updated binary in place, exiting for process supervisor to restart")
188 return
189 }
190 + // preserving the terminal requires a longer trip
191 console.log("Launching new version in background", newBinFile)
192 spawnSync(cmdEscape(newBin), ['--updating', binFile, '--cwd .'], { shell: true, stdio: [0,1,2] }) // sync necessary to work on Mac by double-click
193 })