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

server.js

51 lines1.0 KB

1import fs from 'node:fs'
2import path from 'node:path'
3import { spawn } from 'node:child_process'
4import { pathToFileURL } from 'node:url'
5​
6const distDir = process.env.E2E_DIST_DIR || 'dist'
7​
8function resolveDistClientDir() {
9 return path.resolve(distDir, 'client')
11​
12function resolveDistServerEntryPath() {
13 const serverJsPath = path.resolve(distDir, 'server', 'server.js')
14 if (fs.existsSync(serverJsPath)) {
15 return serverJsPath
16 }
17​
18 const indexJsPath = path.resolve(distDir, 'server', 'index.js')
19 if (fs.existsSync(indexJsPath)) {
20 return indexJsPath
21 }
22​
23 return serverJsPath
25​
26export function start() {
27 const child = spawn(
28 'srvx',
29 ['--prod', '-s', resolveDistClientDir(), resolveDistServerEntryPath()],
30 {
31 stdio: 'inherit',
32 shell: process.platform === 'win32',
33 },
34 )
35​
36 child.on('exit', (code, signal) => {
37 if (signal) {
38 process.kill(process.pid, signal)
39 return
40 }
41​
42 process.exit(code ?? 0)
43 })
45​
46if (
47 process.argv[1] &&
48 import.meta.url === pathToFileURL(process.argv[1]).href
49) {
50 start()