| 1 |
# `sync-child-process` |
| 2 |
|
| 3 |
This package exposes a `SyncChildProcess` class that allows Node.js to run a |
| 4 |
subprocess synchronously *and* interactively. |
| 5 |
|
| 6 |
[**API Docs**] |
| 7 |
|
| 8 |
[**API Docs**]: https://sass.github.io/sync-child-process/classes/SyncChildProcess.html |
| 9 |
|
| 10 |
## Usage |
| 11 |
|
| 12 |
Use [`new SyncChildProcess()`] to start running a subprocess. This supports the |
| 13 |
same API as [`child_process.spawn()`] other than a few options. You can send |
| 14 |
input to the process using [`process.stdin`], and receive events from it |
| 15 |
(stdout, stderr, or exit) using [`process.next()`]. This implements [the |
| 16 |
iterator protocol], but *not* the iterable protocol because it's intrinsically |
| 17 |
stateful. |
| 18 |
|
| 19 |
[`new SyncChildProcess()`]: https://sass.github.io/sync-child-process/classes/SyncChildProcess.html#constructor |
| 20 |
[`child_process.spawn()`]: https://nodejs.org/api/child_process.html#child_processspawncommand-args-options |
| 21 |
[`process.stdin`]: https://sass.github.io/sync-child-process/classes/SyncChildProcess.html#stdin |
| 22 |
[`process.next()`]: https://sass.github.io/sync-child-process/classes/SyncChildProcess.html#next |
| 23 |
[the iterator protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterator_protocol |
| 24 |
|
| 25 |
```js |
| 26 |
import {SyncChildProcess} from 'sync-child-process'; |
| 27 |
// or |
| 28 |
// const {SyncChildProcess} = require('sync-child-process'); |
| 29 |
|
| 30 |
const node = new SyncChildProcess('node', ['--interactive']); |
| 31 |
|
| 32 |
for (;;) { |
| 33 |
if (node.next().value.data.toString().endsWith('> ')) break; |
| 34 |
} |
| 35 |
|
| 36 |
node.stdin.write("41 * Math.pow(2, 5)\n"); |
| 37 |
console.log((node.next().value.data.toString().split("\n")[0])); |
| 38 |
node.stdin.write(".exit\n"); |
| 39 |
console.log(`Node exited with exit code ${node.next().value.code}`); |
| 40 |
``` |
| 41 |
|
| 42 |
## Why synchrony? |
| 43 |
|
| 44 |
See [the `sync-message-port` documentation] for an explanation of why running |
| 45 |
code synchronously can be valuable even in an asynchronous ecosystem like |
| 46 |
Node.js |
| 47 |
|
| 48 |
[the `sync-message-port` documentation]: https://github.com/sass/sync-message-port?tab=readme-ov-file#why-synchrony |
| 49 |
|
| 50 |
### Why not `child_process.spawnSync()`? |
| 51 |
|
| 52 |
Although Node's built-in [`child_process.spawnSync()`] function does run |
| 53 |
synchronously, it's not interactive. It only returns once the process has run to |
| 54 |
completion and exited, which means it's not suitable for any long-lived |
| 55 |
subprocess that interleaves sending and receiving data, such as when using the |
| 56 |
[embedded Sass protocol]. |
| 57 |
|
| 58 |
[`child_process.spawnSync()`]: https://nodejs.org/api/child_process.html#child_processspawnsynccommand-args-options |
| 59 |
[embedded Sass protocol]: https://github.com/sass/sass/blob/main/spec/embedded-protocol.md |
| 60 |
|
| 61 |
--- |
| 62 |
|
| 63 |
Disclaimer: this is not an official Google product. |