Skip to content
Browse files

[refactor] Begin work on nexpect 0.2.x

  • Loading branch information...
1 parent 5608113 commit de8cab21598b352cd881baba3b21546c6daf7822 @indexzero indexzero committed Aug 9, 2011
Showing with 133 additions and 136 deletions.
  1. +1 −1 NEXPECT.LIC → LICENSE
  2. +66 −0 README.md
  3. +0 −86 ReadMe.md
  4. +66 −49 lib/nexpect.js
View
2 NEXPECT.LIC → LICENSE
@@ -1,6 +1,6 @@
nexpect
-Copyright (c) 2010 Elijah Insua, Marak Squires http://github.com/nodejitsu/nexpect
+Copyright (c) 2010 Elijah Insua, Marak Squires, Charlie Robbins http://github.com/nodejitsu/nexpect
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
66 README.md
@@ -0,0 +1,66 @@
+# 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][0] library by Don Libes and the [pexpect][1] 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
+
+### Installing npm (node package manager)
+``` bash
+ curl http://npmjs.org/install.sh | sh
+```
+
+### Installing nexpect
+``` bash
+ $ npm install nexpect
+```
+
+## Usage
+
+``` js
+ var nexpect = require('nexpect');
+
+ nexpect.spawn("echo hello")
+ .expect("hello")
+ .run(function(err) {
+ if (!err) {
+ console.log("hello was echoed");
+ }
+ });
+
+ nexpect.spawn("ls -al /tmp/undefined")
+ .expect("No such file or directory")
+ .run(function(err) {
+ if (!err) {
+ console.log("checked that file doesn't exists");
+ }
+ });
+
+ nexpect.spawn("node")
+ .expect("Type '.help' for options.")
+ .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)
+ }
+ });
+```
+
+# Authors
+[Elijah Insua][2] [Marak Squires][3], and [Charlie Robbins][4].
+
+
+[0]: http://search.cpan.org/~rgiersig/Expect-1.21/Expect.pod "expect"
+[1]: http://pexpect.sourceforge.net/pexpect.html "pexpect"
+[2]: http://github.com/tmpvar "Elijah Insua"
+[3]: http://github.com/marak "Marak Squires"
+[4]: http://github.com/indexzero "Charlie Robbins"
+
+
View
86 ReadMe.md
@@ -1,86 +0,0 @@
-#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][0] library by Don Libes and the [pexpect][1] library by Noah Spurrier.
-
-## why
-
-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
-
-### installing npm (node package manager)
-<pre>
- curl http://npmjs.org/install.sh | sh
-</pre>
-
-### installing nexpect
-<pre>
- npm install nexpect
-</pre>
-
-## usage
-
- var nexpect = require('./lib/nexpect').nspawn;
-
- nexpect.spawn("echo hello")
- .expect("hello")
- .run(function(err) {
- if (!err) {
- console.log("hello was echoed");
- }
- });
-
- nexpect.spawn("ls -al /tmp/undefined")
- .expect("No such file or directory")
- .run(function(err) {
- if (!err) {
- console.log("checked that file doesn't exists");
- }
- });
-
- nexpect.spawn("node")
- .expect("Type '.help' for options.")
- .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)
- }
- });
-
-
-# authors
-
-[Elijah Insua][2] and [Marak Squires][3]
-
-Copyright (c) 2010 Elijah Insua, Marak Squires http://github.com/nodejitsu/nexpect
-
-Permission is hereby granted, free of charge, to any person obtaining
-a copy of this software and associated documentation files (the
-"Software"), to deal in the Software without restriction, including
-without limitation the rights to use, copy, modify, merge, publish,
-distribute, sublicense, and/or sell copies of the Software, and to
-permit persons to whom the Software is furnished to do so, subject to
-the following conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
-
-[0]: http://search.cpan.org/~rgiersig/Expect-1.21/Expect.pod "expect"
-[1]: http://pexpect.sourceforge.net/pexpect.html "pexpect"
-[2]: http://github.com/tmpvar "Elijah Insua"
-[3]: http://github.com/marak "Marak Squires"
-
-
View
115 lib/nexpect.js
@@ -1,56 +1,73 @@
-var spawn = require("child_process").spawn,
- chain = function(ctx) {
- return {
- expect : function(str) {
- ctx.queue.push(function(data) {
- if (data.indexOf(str) > -1) {
- return true
- }
- });
- return chain(ctx);
- },
- sendline : function(str) {
- ctx.queue.push(function() {
- ctx.process.stdin.write(str + "\n");
- });
- return chain(ctx);
- },
- run : function(fn) {
- var err = null,
- out = function(data) {
- data = data.toString();
- var expect = ctx.queue[0];
+/*
+ * nexpect.js: Top-level include for the `nexpect` module.
+ *
+ * (C) 2011, Elijah Insua, Marak Squires, Charlie Robbins.
+ *
+ */
+
+var spawn = require('child_process').spawn;
- if (typeof expect !== "function" || expect(data) !== true) {
- err = data + " was not expected..";
- } else {
- ctx.queue.shift();
- var sendline = ctx.queue.shift();
- if (typeof sendline === "function") {
- sendline();
- }
- }
- };
+function chain (context) {
+ return {
+ expect: function(str) {
+ context.queue.push(function (data) {
+ return data.indexOf(str) > -1;
+ });
+
+ return chain(context);
+ },
+ sendline: function (str) {
+ context.queue.push(function () {
+ context.process.stdin.write(str + '\n');
+ });
+
+ return chain(context);
+ },
+ run: function(fn) {
+ var err = null;
+
+ function out (data) {
+ data = data.toString();
+ var expect = context.queue[0];
- ctx.process = spawn(ctx.app, ctx.args);
- ctx.process.stdout.on("data", out);
- ctx.process.stdout.on("end", function() {
- fn(err);
- });
+ if (typeof expect !== 'function' || expect(data) !== true) {
+ err = data + ' was not expected..';
+ }
+ else {
+ context.queue.shift();
+ var sendline = context.queue.shift();
+ if (typeof sendline === 'function') {
+ sendline();
+ }
}
};
- };
-var nspawn = exports.nspawn = {
- spawn : function(cmd) {
- var args = cmd.split(" "),
- app = args.shift();
+ context.process = spawn(context.app, context.args);
+ context.process.stdout.on('data', out);
+ context.process.stdout.on('end', function () {
+ fn(err);
+ });
+ }
+ };
+};
+
+function nspawn (cmd) {
+ var args = Array.isArray(cmd) ? cmd : cmd.split(' '),
+ app = args.shift(),
+ context;
- var context = {
- app: app,
- args : args,
- queue : [],
- };
- return chain(context);
- }
+ context = {
+ app: app,
+ args: args,
+ queue: [],
+ };
+
+ return chain(context);
};
+
+//
+// Export the core `nspawn` function as well as `nexpect.nspawn` for
+// backwards compatibility.
+//
+module.exports = nspawn;
+module.exports.nspawn = nspawn;

0 comments on commit de8cab2

Please sign in to comment.
Something went wrong with that request. Please try again.