-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.ts
More file actions
71 lines (66 loc) · 1.88 KB
/
util.ts
File metadata and controls
71 lines (66 loc) · 1.88 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
/**
* @fileoverview SEA (Single Executable Application) binary detection +
* path accessor.
*
* Two responsibilities (mirror of `src/smol/util.ts` shape):
*
* 1. `isSeaBinary()` — memoized boolean detector for whether the
* current process is running as a Node.js Single Executable
* Application. Probes via Node 24+'s `node:sea.isSea()` native
* API; falls back to `false` on older runtimes.
*
* 2. `getSeaBinaryPath()` — returns the path of the SEA binary
* (`process.argv[0]` normalized) when running as SEA, otherwise
* `undefined`.
*
* Defensive across runtimes: returns `false` / `undefined` cleanly on
* stock Node < 24, browsers, Deno, Bun.
*/
import process from 'node:process'
import { normalizePath } from '../paths/normalize'
/**
* Cached SEA detection result.
*/
let _isSea: boolean | undefined
/**
* Detect if the current process is running as a SEA binary.
* Uses Node.js 24+ native API with caching for performance.
*
* @example
* ```typescript
* if (isSeaBinary()) {
* console.log('Running as a Single Executable Application')
* }
* ```
*/
export function isSeaBinary(): boolean {
if (_isSea === undefined) {
try {
// Use Node.js 24+ native SEA detection API.
// eslint-disable-next-line n/no-unsupported-features/node-builtins
const seaModule = require('node:sea')
_isSea = seaModule.isSea()
} catch {
// Node.js < 24 or SEA module not available.
_isSea = false
}
}
return _isSea ?? false
}
/**
* Get the current SEA binary path.
* Only valid when running as a SEA binary.
*
* @example
* ```typescript
* const binPath = getSeaBinaryPath()
* if (binPath) {
* console.log(`Running as SEA binary: ${binPath}`)
* }
* ```
*/
export function getSeaBinaryPath(): string | undefined {
return isSeaBinary() && process.argv[0]
? normalizePath(process.argv[0])
: undefined
}