| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import {fork} from 'child_process'; |
| 9 | import invariant from 'invariant'; |
| 10 | import process from 'process'; |
| 11 | import * as readline from 'readline'; |
| 12 | import {hideBin} from 'yargs/helpers'; |
| 13 | |
| 14 | readline.emitKeypressEvents(process.stdin); |
| 15 | |
| 16 | if (process.stdin.isTTY) { |
| 17 | process.stdin.setRawMode(true); |
| 18 | } |
| 19 | |
| 20 | process.stdin.on('keypress', function (_, key) { |
| 21 | if (key && key.name === 'c' && key.ctrl) { |
| 22 | // handle sigint |
| 23 | if (childProc) { |
| 24 | console.log('Interrupted!!'); |
| 25 | childProc.kill('SIGINT'); |
| 26 | childProc.unref(); |
| 27 | process.exit(-1); |
| 28 | } |
| 29 | } |
| 30 | }); |
| 31 | |
| 32 | const childProc = fork(require.resolve('./runner.js'), hideBin(process.argv), { |
| 33 | // for some reason, keypress events aren't sent to handlers in both processes |
| 34 | // when we `inherit` stdin. |
| 35 | // pipe stdout and stderr so we can silence child process after parent exits |
| 36 | stdio: ['pipe', 'pipe', 'pipe', 'ipc'], |
| 37 | // forward existing env variables, like `NODE_OPTIONS` which VSCode uses to attach |
| 38 | // its debugger |
| 39 | env: {...process.env, FORCE_COLOR: 'true'}, |
| 40 | }); |
| 41 | |
| 42 | invariant( |
| 43 | childProc.stdin && childProc.stdout && childProc.stderr, |
| 44 | 'Expected forked process to have piped stdio', |
| 45 | ); |
| 46 | process.stdin.pipe(childProc.stdin); |
| 47 | childProc.stdout.pipe(process.stdout); |
| 48 | childProc.stderr.pipe(process.stderr); |
| 49 | |
| 50 | childProc.on('exit', code => { |
| 51 | process.exit(code ?? -1); |
| 52 | }); |