-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathlocal-build.js
More file actions
44 lines (35 loc) · 1005 Bytes
/
local-build.js
File metadata and controls
44 lines (35 loc) · 1005 Bytes
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
import { spawn } from 'node:child_process'
import { setTimeout as delay } from 'node:timers/promises'
const PORT = '8080'
const url = `http://localhost:${PORT}/build.html`
const serveProcess = spawn(
'serve',
['-l', PORT, '--no-port-switching', '.'],
{ stdio: 'inherit' }
)
const openUrl = (targetUrl) => {
if (process.platform === 'darwin') {
return spawn('open', [targetUrl], { stdio: 'ignore', detached: true })
}
if (process.platform === 'win32') {
return spawn('cmd', ['/c', 'start', '', targetUrl], { stdio: 'ignore', detached: true })
}
return spawn('xdg-open', [targetUrl], { stdio: 'ignore', detached: true })
}
const shutdown = (signal) => {
if (!serveProcess.killed) {
serveProcess.kill(signal)
}
}
process.on('SIGINT', () => shutdown('SIGINT'))
process.on('SIGTERM', () => shutdown('SIGTERM'))
try {
await delay(1000)
const opener = openUrl(url)
opener.unref()
} catch {
console.log(`Open ${url}`)
}
serveProcess.on('exit', (code) => {
process.exit(code ?? 0)
})