forkfeed
is a function that passes answer values to a child process on data. For example, if a fork was spawned which requires user interaction, this module can be used to pass answers to it according to the seen data from stdout
.
yarn add -E forkfeed
- Table Of Contents
- API
forkFeed(readable: Readable, stdin: Writable, inputs: [RegExp, string][], log: Writable)
- Copyright
The package is available by importing its default function:
import forkFeed from 'forkfeed'
Sets up a listener on the Readable stream and writes answers to the Writable stream when data specified in inputs
was detected. The logging stream will receive both data and answers.
import('stream').Writable
stream.Writable
import('stream').Readable
stream.Readable
Given a fork source code as
const rl = require('readline')
const i = rl.createInterface({
input: process.stdin,
output: process.stderr,
})
i.question(
'What was the football coach yelling at the vending machine?\n> ',
() => {
i.question('What do snowmen do in their spare time?\n> ', () => {
process.exit(1)
})
})
The function can be used in the following manner:
/* yarn example/ */
import forkFeed from 'forkFeed'
import { fork } from 'child_process'
(async () => {
const cp = fork('example/fork', [], { stdio: 'pipe' })
forkFeed(cp.stderr, cp.stdin, [
[/coach/, 'Gimme my quarter back!!!'],
[/snowmen/, 'Just chilling.'],
], process.stdout)
})()
What was the football coach yelling at the vending machine?
> Gimme my quarter back!!!
What do snowmen do in their spare time?
> Just chilling.
(c) Context Testing 2019