Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

dev.mjs

51 lines1.2 KB

1import { spawn, spawnSync } from 'node:child_process'
2import { once } from 'node:events'
3import { dirname, resolve } from 'node:path'
4import { fileURLToPath } from 'node:url'
5​
6const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm'
7const tsxCommand = process.platform === 'win32' ? 'tsx.cmd' : 'tsx'
8const cwd = resolve(dirname(fileURLToPath(import.meta.url)), '..')
9​
10function runBuild() {
11 const result = spawnSync(npmCommand, ['run', 'build'], {
12 cwd,
13 stdio: 'inherit',
14 })
15​
16 if (result.status !== 0) {
17 process.exit(result.status ?? 1)
18 }
20​
21async function run() {
22 runBuild()
23​
24 const server = spawn(tsxCommand, ['./server/index.mjs'], {
25 cwd,
26 stdio: 'inherit',
27 })
28​
29 const stopServer = (signal) => {
30 if (server.exitCode === null) {
31 server.kill(signal)
32 }
33 }
34​
35 process.on('SIGINT', () => stopServer('SIGINT'))
36 process.on('SIGTERM', () => stopServer('SIGTERM'))
37​
38 const outcome = await Promise.race([
39 once(server, 'error').then(([error]) => {
40 throw error
41 }),
42 once(server, 'exit').then(([code]) => ({ code })),
43 ])
44​
45 process.exitCode = outcome.code ?? 0
47​
48run().catch((error) => {
49 console.error(error)
50 process.exitCode = 1
51})