Skip to content

contexttesting/forkfeed

Repository files navigation

forkfeed

npm version

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

The package is available by importing its default function:

import forkFeed from 'forkfeed'

forkFeed(
  readable: Readable,
  stdin: Writable,
  inputs: [RegExp, string][],
  log: Writable,
): void

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.

Copyright

(c) Context Testing 2019

About

Passes answer values to a child process on data.

Resources

License

Stars

Watchers

Forks

Packages

No packages published