Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kenotron/capturing-inherit
https://github.com/kenotron/capturing-inherit
Last synced: 10 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/kenotron/capturing-inherit
- Owner: kenotron
- Created: 2022-06-18T17:27:29.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-06-18T17:45:18.000Z (over 2 years ago)
- Last Synced: 2024-11-07T21:11:48.327Z (2 months ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# How to capture a child_process's inherit outputs
https://twitter.com/kenneth_chau/status/1538216216572985346?s=20&t=95R6BkA_EqXiN52s06x7ig## 1. fork a `runner.js` w/ `stdio: pipe` + add `on('data')` handler
```js
const forked = fork("runner.js", {
stdio: ["ignore", "pipe", "pipe", "ipc"],
});
```## 2. `runner.js`, in turn executes a command (even `child_process` spawn or exec with inherit)
```js
execSync(`npm run test`, {
stdio: ["inherit", "inherit", "inherit"],
env: process.env,
});
```## 3. Capture output from pipe: `index.js`
```js
let msgs = [];forked.stdout.on("data", (data) => {
msgs.push(data);
});forked.on("exit", (code, signal) => {
console.log(`${msgs.join("")}`);
});
```