GithubHelp home page GithubHelp logo

willcode2surf / nexpect Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nodejitsu/nexpect

0.0 2.0 0.0 302 KB

spawn and control child processes in node.js with ease

Home Page: http://github.com/nodejitsu/nexpect

License: Other

nexpect's Introduction

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('nspawn')

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, or stderr, but not both (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 (str)

  • str {string} Output to assert on the target stream

Expect that the next line of output contains str as a sub-string, fails if it does not.

function wait (str)

  • str {string} Output to assert on the target stream

Wait for a line of output that contains str as a sub-string, discarding lines that do not match, fails if str is not found.

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 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.

nexpect's People

Contributors

binchensjtu avatar chenglou avatar coderarity avatar indexzero avatar marak avatar sam-github avatar tmpvar avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.