| 848 | } |
| 849 | |
| 850 | async function runTelemetryCrashScript(body: string): Promise<number> { |
| 851 | const dir = await tempHome(); |
| 852 | const scriptPath = join(dir, 'crash-worker.ts'); |
| 853 | const testDir = import.meta.dirname; |
| 854 | const tsxCli = join( |
| 855 | dirname(fileURLToPath(import.meta.resolve('tsx/package.json'))), |
| 856 | 'dist/cli.mjs', |
| 857 | ); |
| 858 | const crashModuleUrl = pathToFileURL(join(testDir, '../src/crash.ts')).href; |
| 859 | const clientModuleUrl = pathToFileURL(join(testDir, '../src/client.ts')).href; |
| 860 | writeFileSync( |
| 861 | scriptPath, |
| 862 | ` |
| 863 | import { TelemetryClient } from ${JSON.stringify(clientModuleUrl)}; |
| 864 | import { installCrashHandlersForClient } from ${JSON.stringify(crashModuleUrl)}; |
| 865 | |
| 866 | ${body} |
| 867 | `, |
| 868 | ); |
| 869 | |
| 870 | return new Promise((resolve, reject) => { |
| 871 | const child = spawn(process.execPath, [tsxCli, scriptPath], { |
| 872 | cwd: join(testDir, '../../..'), |
| 873 | stdio: ['ignore', 'ignore', 'pipe'], |
| 874 | }); |
| 875 | let stderr = ''; |
| 876 | child.stderr.setEncoding('utf8'); |
| 877 | child.stderr.on('data', (chunk: string) => { |
| 878 | stderr += chunk; |
| 879 | }); |
| 880 | child.on('error', reject); |
| 881 | child.on('close', (code) => { |
| 882 | if (code === null) { |
| 883 | reject(new Error(`Crash script exited without a code: ${stderr}`)); |
| 884 | return; |
| 885 | } |
| 886 | resolve(code); |
| 887 | }); |
| 888 | }); |
| 889 | } |