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

server.js

47 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)) return serverJsPath
15​
16 const indexJsPath = path.resolve(distDir, 'server', 'index.js')
17 if (fs.existsSync(indexJsPath)) return indexJsPath
18​
19 return serverJsPath
21​
22export function start() {
23 const child = spawn(
24 'srvx',
25 ['--prod', '-s', resolveDistClientDir(), resolveDistServerEntryPath()],
26 {
27 stdio: 'inherit',
28 shell: process.platform === 'win32',
29 },
30 )
31​
32 child.on('exit', (code, signal) => {
33 if (signal) {
34 process.kill(process.pid, signal)
35 return
36 }
37​
38 process.exit(code ?? 0)
39 })
41​
42if (
43 process.argv[1] &&
44 import.meta.url === pathToFileURL(process.argv[1]).href
45) {
46 start()