()
| 19 | server: { out: string[]; err: string[] } |
| 20 | } |
| 21 | export async function serve() { |
| 22 | // collect stdout and stderr streams from child processes here to avoid interfering with regular vitest output |
| 23 | Object.assign(streams, { |
| 24 | build: { out: [], err: [] }, |
| 25 | server: { out: [], err: [] }, |
| 26 | }) |
| 27 | // helpers to collect streams |
| 28 | const collectStreams = (name, process) => { |
| 29 | process.stdout.on('data', (d) => streams[name].out.push(d.toString())) |
| 30 | process.stderr.on('data', (d) => streams[name].err.push(d.toString())) |
| 31 | } |
| 32 | const collectErrorStreams = (name, e) => { |
| 33 | e.stdout && streams[name].out.push(e.stdout) |
| 34 | e.stderr && streams[name].err.push(e.stderr) |
| 35 | } |
| 36 | |
| 37 | // helper to output stream content on error |
| 38 | const printStreamsToConsole = async (name) => { |
| 39 | const std = streams[name] |
| 40 | if (std.out && std.out.length > 0) { |
| 41 | console.log(`stdout of ${name}\n${std.out.join('\n')}\n`) |
| 42 | } |
| 43 | if (std.err && std.err.length > 0) { |
| 44 | console.log(`stderr of ${name}\n${std.err.join('\n')}\n`) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // only run `vite build` when needed |
| 49 | if (isBuild) { |
| 50 | const buildCommand = `${viteBinPath} build` |
| 51 | try { |
| 52 | const buildProcess = execaCommand(buildCommand, { |
| 53 | cwd: rootDir, |
| 54 | stdio: 'pipe', |
| 55 | }) |
| 56 | collectStreams('build', buildProcess) |
| 57 | await buildProcess |
| 58 | } catch (e) { |
| 59 | console.error(`error while executing cli command "${buildCommand}":`, e) |
| 60 | collectErrorStreams('build', e) |
| 61 | await printStreamsToConsole('build') |
| 62 | throw e |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | await kill(port) |
| 67 | |
| 68 | // run `vite --port x` or `vite preview --port x` to start server |
| 69 | const viteServerArgs = ['--port', `${port}`, '--strict-port'] |
| 70 | if (isBuild) { |
| 71 | viteServerArgs.unshift('preview') |
| 72 | } |
| 73 | const serverCommand = `${viteBinPath} ${viteServerArgs.join(' ')}` |
| 74 | const serverProcess = execaCommand(serverCommand, { |
| 75 | cwd: rootDir, |
| 76 | stdio: 'pipe', |
| 77 | forceKillAfterDelay: 3000, |
| 78 | }) |
nothing calls this directly
no test coverage detected