|
| 1 | +import { type ChildProcess, spawn } from 'node:child_process'; |
| 2 | +import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises'; |
| 3 | +import { createServer } from 'node:net'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { setTimeout as delay } from 'node:timers/promises'; |
| 6 | +import { test } from 'rstack/test'; |
| 7 | + |
| 8 | +const RSTACK_BIN_PATH = path.join(import.meta.dirname, '../../../bin/rs.js'); |
| 9 | + |
| 10 | +const getRandomPort = (): Promise<number> => |
| 11 | + new Promise((resolve, reject) => { |
| 12 | + const server = createServer(); |
| 13 | + server.once('error', reject); |
| 14 | + server.listen(0, '127.0.0.1', () => { |
| 15 | + const address = server.address(); |
| 16 | + if (!address || typeof address === 'string') { |
| 17 | + server.close(); |
| 18 | + reject(new Error('Failed to allocate a port.')); |
| 19 | + return; |
| 20 | + } |
| 21 | + server.close((error) => { |
| 22 | + if (error) { |
| 23 | + reject(error); |
| 24 | + } else { |
| 25 | + resolve(address.port); |
| 26 | + } |
| 27 | + }); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | +const closeChild = async (child: ChildProcess): Promise<void> => { |
| 32 | + if (child.exitCode !== null) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + child.kill(); |
| 37 | + await Promise.race([ |
| 38 | + new Promise<void>((resolve) => child.once('exit', () => resolve())), |
| 39 | + delay(5_000), |
| 40 | + ]); |
| 41 | + |
| 42 | + if (child.exitCode === null) { |
| 43 | + child.kill('SIGKILL'); |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +const waitForOutput = async ( |
| 48 | + child: ChildProcess, |
| 49 | + getOutput: () => string, |
| 50 | + predicate: (output: string) => boolean, |
| 51 | +): Promise<void> => { |
| 52 | + const deadline = Date.now() + 15_000; |
| 53 | + |
| 54 | + while (Date.now() < deadline) { |
| 55 | + const output = getOutput(); |
| 56 | + if (predicate(output)) { |
| 57 | + return; |
| 58 | + } |
| 59 | + if (child.exitCode !== null) { |
| 60 | + throw new Error(`Rstack exited unexpectedly.\n${output}`); |
| 61 | + } |
| 62 | + await delay(100); |
| 63 | + } |
| 64 | + |
| 65 | + throw new Error(`Timed out waiting for Rstack output.\n${getOutput()}`); |
| 66 | +}; |
| 67 | + |
| 68 | +const createConfig = (value: string) => `import { define } from 'rstack'; |
| 69 | +
|
| 70 | +console.log('RSTACK_CONFIG_VALUE:${value}'); |
| 71 | +
|
| 72 | +define.app({ |
| 73 | + server: { |
| 74 | + port: Number(process.env.PORT), |
| 75 | + }, |
| 76 | +}); |
| 77 | +`; |
| 78 | + |
| 79 | +test('should restart the dev server when Rstack config changes', async () => { |
| 80 | + const cwd = await mkdtemp(path.join(import.meta.dirname, 'temp-')); |
| 81 | + const configPath = path.join(cwd, 'rstack.config.ts'); |
| 82 | + await mkdir(path.join(cwd, 'src')); |
| 83 | + await writeFile(path.join(cwd, 'src/index.js'), 'console.log("hello");\n'); |
| 84 | + await writeFile(configPath, createConfig('initial')); |
| 85 | + |
| 86 | + const child = spawn(process.execPath, [RSTACK_BIN_PATH, 'dev'], { |
| 87 | + cwd, |
| 88 | + env: { |
| 89 | + ...process.env, |
| 90 | + NO_COLOR: '1', |
| 91 | + PORT: String(await getRandomPort()), |
| 92 | + }, |
| 93 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 94 | + windowsHide: true, |
| 95 | + }); |
| 96 | + let output = ''; |
| 97 | + child.stdout?.on('data', (data) => { |
| 98 | + output += data.toString(); |
| 99 | + }); |
| 100 | + child.stderr?.on('data', (data) => { |
| 101 | + output += data.toString(); |
| 102 | + }); |
| 103 | + child.on('error', (error) => { |
| 104 | + output += error.stack ?? error.message; |
| 105 | + }); |
| 106 | + |
| 107 | + try { |
| 108 | + await waitForOutput( |
| 109 | + child, |
| 110 | + () => output, |
| 111 | + (logs) => logs.includes('built in'), |
| 112 | + ); |
| 113 | + await writeFile(configPath, createConfig('updated')); |
| 114 | + await waitForOutput( |
| 115 | + child, |
| 116 | + () => output, |
| 117 | + (logs) => logs.includes('restarting server as rstack.config.ts changed'), |
| 118 | + ); |
| 119 | + await waitForOutput( |
| 120 | + child, |
| 121 | + () => output, |
| 122 | + (logs) => logs.includes('RSTACK_CONFIG_VALUE:updated'), |
| 123 | + ); |
| 124 | + await waitForOutput( |
| 125 | + child, |
| 126 | + () => output, |
| 127 | + (logs) => (logs.match(/built in/g) ?? []).length >= 2, |
| 128 | + ); |
| 129 | + } finally { |
| 130 | + await closeChild(child); |
| 131 | + await rm(cwd, { recursive: true, force: true }); |
| 132 | + } |
| 133 | +}, 30_000); |
0 commit comments