mirinjs 0.1.0-alpha.7 → 0.1.0-alpha.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/updater.ts +32 -19
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mirinjs",
3
- "version": "0.1.0-alpha.7",
3
+ "version": "0.1.0-alpha.8",
4
4
  "description": "Build desktop apps with Bun, TypeScript, and Chromium (CEF). The Bun-native Electron alternative.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/updater.ts CHANGED
@@ -15,7 +15,15 @@
15
15
 
16
16
  import { homedir, tmpdir } from "node:os";
17
17
  import { join } from "node:path";
18
- import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync } from "node:fs";
18
+ import {
19
+ existsSync,
20
+ mkdirSync,
21
+ readFileSync,
22
+ readdirSync,
23
+ renameSync,
24
+ rmSync,
25
+ writeFileSync,
26
+ } from "node:fs";
19
27
  import { $ } from "bun";
20
28
  import { runtime } from "./runtime.ts";
21
29
  import { loadCodec } from "./codec.ts";
@@ -214,31 +222,36 @@ export class Updater {
214
222
  if (IS_WINDOWS) {
215
223
  const runningApp = join(this.#resourcesDir()!, "..");
216
224
  const exe = join(runningApp, `${this.#version()!.name}.exe`);
225
+ const script = join(tmpdir(), `mirin-apply-${this.#version()!.name}.ps1`);
217
226
  const ps = [
218
227
  `$ErrorActionPreference='SilentlyContinue'`,
219
- // Wait for the host process to exit (it locks the exe).
228
+ // Never let the app dir be this process's cwd (Windows can't delete a cwd).
229
+ `Set-Location $env:TEMP`,
230
+ // Wait for the host to exit (it locks the exe), then a beat for CEF's
231
+ // helper subprocesses to release their file locks (libcef.dll etc.).
220
232
  `while (Get-Process -Id ${process.pid} -ErrorAction SilentlyContinue) { Start-Sleep -Milliseconds 200 }`,
221
- // Give CEF's helper subprocesses a beat to exit and release their file
222
- // locks (libcef.dll etc.) — they go just after the host.
223
233
  `Start-Sleep -Milliseconds 700`,
224
- // Replace the app folder, retrying while any lingering lock clears
225
- // (~10s max). Unlike Unix, Windows can't delete a dir that's still a
226
- // process's cwd or holds an open handle — hence the neutral cwd below.
234
+ // Replace the app folder, retrying ~10s while any lingering lock clears.
227
235
  `for ($i=0; $i -lt 50; $i++) { Remove-Item -Recurse -Force ${psq(runningApp)}; if (-not (Test-Path ${psq(runningApp)})) { break }; Start-Sleep -Milliseconds 200 }`,
228
236
  `Move-Item -Force ${psq(this.#staged)} ${psq(runningApp)}`,
229
237
  `Start-Process -FilePath ${psq(exe)} -WorkingDirectory ${psq(runningApp)}`,
230
- ].join("; ");
231
- Bun.spawn(
232
- ["powershell", "-NoProfile", "-NonInteractive", "-WindowStyle", "Hidden", "-Command", ps],
233
- {
234
- // Run from a neutral dir, NOT the inherited install dir — otherwise the
235
- // app folder is this PowerShell's cwd and Windows refuses to delete it.
236
- cwd: tmpdir(),
237
- stdin: "ignore",
238
- stdout: "ignore",
239
- stderr: "ignore",
240
- },
241
- ).unref();
238
+ `Remove-Item -Force ${psq(script)}`, // self-clean
239
+ ].join("\n");
240
+ writeFileSync(script, ps, "utf8");
241
+ // The swap MUST outlive this process. A Bun.spawn child is in our job object
242
+ // and is killed when we quit — even with unref/detached (verified). So spawn
243
+ // the swap via WMI Win32_Process.Create: the WMI service launches it, outside
244
+ // our job, so it survives. `await` the short launcher so the swap process
245
+ // exists before core.quit() (no race), then quit.
246
+ const wmiPath = script.replace(/'/g, "''");
247
+ const wmi =
248
+ `([wmiclass]'Win32_Process').Create('powershell -NoProfile -NonInteractive ` +
249
+ `-WindowStyle Hidden -ExecutionPolicy Bypass -File "${wmiPath}"') | Out-Null`;
250
+ const launcher = Bun.spawn(
251
+ ["powershell", "-NoProfile", "-NonInteractive", "-Command", wmi],
252
+ { stdin: "ignore", stdout: "ignore", stderr: "ignore" },
253
+ );
254
+ await launcher.exited;
242
255
  } else {
243
256
  const runningApp = join(this.#resourcesDir()!, "..", "..");
244
257
  const script = [