-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathglimpse-ui.ts
More file actions
80 lines (68 loc) · 1.99 KB
/
glimpse-ui.ts
File metadata and controls
80 lines (68 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { existsSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { join, dirname } from "node:path";
import { platform } from "node:os";
import { createRequire } from "node:module";
let glimpseAvailable: boolean | null = null;
let resolvedBinaryPath: string | null = null;
export function isGlimpseAvailable(): boolean {
if (glimpseAvailable !== null) return glimpseAvailable;
if (platform() !== "darwin") {
glimpseAvailable = false;
return false;
}
resolvedBinaryPath = getGlimpseBinaryPath();
glimpseAvailable = resolvedBinaryPath !== null;
return glimpseAvailable;
}
function getGlimpseBinaryPath(): string | null {
if (process.env.GLIMPSE_BINARY && existsSync(process.env.GLIMPSE_BINARY)) {
return process.env.GLIMPSE_BINARY;
}
// Local node_modules
try {
const require = createRequire(import.meta.url);
const glimpseuiPath = require.resolve("glimpseui");
const binaryPath = join(dirname(glimpseuiPath), "glimpse");
if (existsSync(binaryPath)) return binaryPath;
} catch {}
// Global npm install
try {
const globalRoot = execFileSync("npm", ["root", "-g"], { encoding: "utf-8" }).trim();
const binaryPath = join(globalRoot, "glimpseui", "src", "glimpse");
if (existsSync(binaryPath)) return binaryPath;
} catch {}
return null;
}
export async function openGlimpseWindow(
html: string,
options: {
title: string;
width?: number;
height?: number;
onClosed: () => void;
},
) {
const modulePath = resolvedBinaryPath
? join(dirname(resolvedBinaryPath), "glimpse.mjs")
: "glimpseui";
const glimpse = await import(modulePath);
let active = true;
const win = glimpse.open(html, {
width: options.width ?? 900,
height: options.height ?? 700,
title: options.title,
});
win.on("closed", () => {
if (!active) return;
active = false;
options.onClosed();
});
return {
close: () => {
if (!active) return;
active = false;
win.close();
},
};
}