• Stars
    star
    290
  • Rank 142,981 (Top 3 %)
  • Language
    JavaScript
  • License
    Other
  • Created about 14 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

spawn and control child processes in node.js with ease

nexpect

nexpect is a node.js module for spawning child applications (such as ssh) and seamlessly controlling them using javascript callbacks. nexpect is based on the ideas of the expect library by Don Libes and the pexpect library by Noah Spurrier.

Motivation

node.js has good built in control for spawning child processes. nexpect builds on these core methods and allows developers to easily pipe data to child processes and assert the expected response. nexpect also chains, so you can compose complex terminal interactions.

Installation

  $ npm install --save nexpect

Usage

require('nexpect')

The module exposes a single function, .spawn.

function spawn (command, [params], [options])

  • command {string|Array} The command that you wish to spawn, a string will be split on ' ' to find the params if params not provided (so do not use the string variant if any arguments have spaces in them)
  • params {Array} Optional Argv to pass to the child process
  • options {Object} Optional An object literal which may contain
    • cwd: Current working directory of the child process.
    • env: Environment variables for the child process.
    • ignoreCase: Ignores the case of any output from the child process.
    • stripColors: Strips any ANSI colors from the output for .expect() and .wait() statements.
    • stream: Expectations can be written against 'stdout', 'stderr', or 'all', which runs expectations against both stdout and stderr (defaults to 'stdout')
    • verbose: Writes the stdout for the child process to process.stdout of the current process, and any data sent with sendline to the process.stdout of the current process.

Top-level entry point for nexpect that liberally parses the arguments and then returns a new chain with the specified command, params, and options.

function expect (expectation)

  • expectation {string|RegExp} Output to assert on the target stream

Expect that the next line of output matches the expectation. Throw an error if it does not.

The expectation can be a string (the line should contain the expected value as a substring) or a RegExp (the line should match the expression).

function wait (expectation, callback)

  • expectation {string|RegExp} Output to assert on the target stream
  • callback {Function} Optional Callback to be called when output matches stream

Wait for a line of output that matches the expectation, discarding lines that do not match.

Throw an error if no such line was found.

The expectation can be a string (the line should contain the expected value as a substring) or a RegExp (the line should match the expression).

The callback will be called for every line that matches the expectation.

function sendline (line)

  • line {string} Output to write to the child process.

Adds a write line to context.process.stdin to the context.queue for the current chain.

function sendEof ()

Close child's stdin stream, let the child know there are no more data coming.

This is useful for testing apps that are using inquirer, as inquirer.prompt() calls stdin.resume() at some point, which causes the app to block on input when the input stream is a pipe.

function run (callback)

  • callback {function} Called when child process closes, with arguments
    • err {Error|null} Error if any occurred
    • output {Array} Array of lines of output examined
    • exit {Number|String} Numeric exit code, or String name of signal

Runs the context against the specified context.command and context.params.

Example

Lets take a look at some sample usage:

  var nexpect = require('nexpect');

  nexpect.spawn("echo", ["hello"])
         .expect("hello")
         .run(function (err, stdout, exitcode) {
           if (!err) {
             console.log("hello was echoed");
           }
         });

  nexpect.spawn("ls -la /tmp/undefined", { stream: 'stderr' })
         .expect("No such file or directory")
         .run(function (err) {
           if (!err) {
             console.log("checked that file doesn't exists");
           }
         });

  nexpect.spawn("node --interactive")
         .expect(">")
         .sendline("console.log('testing')")
         .expect("testing")
         .sendline("process.exit()")
         .run(function (err) {
           if (!err) {
             console.log("node process started, console logged, process exited");
           }
           else {
             console.log(err)
           }
         });

If you are looking for more examples take a look at the examples, and tests.

Tests

All tests are written with vows:

  $ npm test

Authors

Elijah Insua Marak Squires, and Charlie Robbins.

More Repositories

1

haibu

[Deprecated] a node.js application server - spawn your own node.js clouds, on your own hardware
JavaScript
722
star
2

jitsu

Flawless command line deployment of your Node.js apps to the cloud
JavaScript
648
star
3

handbook

A gentle introduction to the art of Nodejitsu
JavaScript
473
star
4

godot

Godot is a streaming real-time event processor based on Riemann written in Node.js
JavaScript
265
star
5

docs

Community powered rocket fuel for node.js
JavaScript
194
star
6

node-cloudservers

A client implementation for Rackspace CloudServers in node.js
JavaScript
159
star
7

require-analyzer

Determine the set of requirements for a given node.js file, directory tree, or module
JavaScript
152
star
8

node-cloudfiles

A client implementation for Rackspace CloudFIles in node.js
JavaScript
151
star
9

kohai

I am kohai. I am a pluggable irc bot for managing real-time data events.
JavaScript
91
star
10

prenup

A collaborative bdd project planning tool for node. uses kyuri and VowsJS
JavaScript
83
star
11

module-foundry

A web service for building node.js modules that runs on Linux, SmartOS and Windows.
JavaScript
80
star
12

mock-request

A simple testing tool for mocking HTTP sequences of request / response pairs in node.js
JavaScript
77
star
13

haibu-carapace

The application host used by the haibu node.js application deployment / management server.
JavaScript
69
star
14

txn

Process and update CouchDB data in atomic, all-or-nothing transactions
JavaScript
65
star
15

nodejitsu-api

a collection of client wrappers for nodejitsu's core api
JavaScript
47
star
16

browsenpm.org

Browse packages, users, code, stats and more the public npm registry in style.
JavaScript
44
star
17

forza

A lightweight agent for Godot
C
36
star
18

jitsu-ui

A terminal interface for jitsu.
JavaScript
34
star
19

aeternum

A process monitor in libuv
C
31
star
20

module-smith

A simple extensible npm build bot that works on Linux, SmartOS, and Windows.
JavaScript
19
star
21

solenoid

Jump start an application
JavaScript
19
star
22

overwatch

A deterministic couchdb replication watcher
JavaScript
19
star
23

persistent-ghost

Wrapper to deploy the Ghost blog on Nodejitsu
JavaScript
18
star
24

haibu-api

A collection of client wrappers for haibu's core api
JavaScript
11
star
25

defaultable

Transparent, drop-in helper for overridable, inheritable defaults in CommonJS modules
JavaScript
9
star
26

contour

Collection of BigPipe Pagelets for fast prototyping and scaffold of templates
CSS
8
star
27

npm-pkg-top

Lists the top binary packages by npm stars and github stars
JavaScript
7
star
28

npm-ev-source

The transform module or daemon that takes a `skimdb` based couch and turns it into an `event-sourced` complete npm
JavaScript
7
star
29

packages-pagelet

A pagelet for rendering a npm package page.
JavaScript
4
star
30

npm-package-json-pagelet

An interactive guide for package.json
HTML
3
star
31

npm-search-pagelet

Search the npm registry.
JavaScript
2
star
32

nodejitsu-npm

A simple command line utility to get started with a Nodejitsu Private npm!
JavaScript
2
star
33

npm-dependencies-pagelet

Pagelet for the dependencies UI for a single package
JavaScript
1
star
34

registry-status-pagelet

Pagelet for visual overview of npm registry statuses
JavaScript
1
star
35

service-select

Service selector pagelet for selecting
JavaScript
1
star
36

npm-documentation-pagelet

Display documentation of npm
CSS
1
star