Skip to content
Browse files

intitial checkin

  • Loading branch information...
0 parents commit e8bb32798850955331540dbfaa18bf6aea8885e9 @maxogden committed Aug 3, 2012
55 index.js
@@ -0,0 +1,55 @@
+var stream = require('stream')
+var util = require('util')
+var concat = require('concat-stream')
+var spawn = require('child_process').spawn
+
+function Shell(cmd, args, cb) {
+ if (!cb) {
+ cb = args
+ args = null
+ }
+ var self = this
+ stream.Stream.call(this)
+ this.writable = true
+ this.child = spawn(cmd, args)
+ this.stderrDone = false
+ this.stdoutDone = false
+ this.concat('stdout', function(err, stdout) {
+ if (err) return cb(err)
+ self.stdout = stdout
+ self.stdoutDone = true
+ if (self.stderrDone) cb(self.stderr, self.stdout)
+ })
+ this.concat('stderr', function(err, stderr) {
+ if (err) return cb(err)
+ self.stderr = stderr
+ self.stderrDone = true
+ if (self.stdoutDone) cb(self.stderr, self.stdin)
+ })
+ this.on('error', function(e) { this.child.stdin.emit('error', e) })
+}
+
+util.inherits(Shell, stream.Stream)
+
+Shell.prototype.write = function(chunk) {
+ this.child.stdin.write(chunk)
+}
+
+Shell.prototype.end = function() {
+ this.child.stdin.end()
+}
+
+Shell.prototype.concat = function(name, cb) {
+ this.child[name].pipe(
+ concat(function(err, out) {
+ if (err) return cb(err)
+ cb(false, out)
+ })
+ )
+}
+
+module.exports = function(cmd, args, cb) {
+ return new Shell(cmd, args, cb)
+}
+
+module.exports.Shell = Shell
48 node_modules/concat-stream/index.js
@@ -0,0 +1,48 @@
+var stream = require('stream')
+var util = require('util')
+var BufferJoiner = require('bufferjoiner')
+
+function ConcatStream(cb) {
+ stream.Stream.call(this)
+ this.writable = true
+ this.cb = cb
+ this.body = []
+ this.on('error', cb)
+}
+
+util.inherits(ConcatStream, stream.Stream)
+
+ConcatStream.prototype.write = function(chunk) {
+ this.body.push(chunk)
+}
+
+ConcatStream.prototype.getBody = function () {
+ if (this.body.length === 0) return
+ if (typeof(this.body[0]) === "string") return this.body.join('')
+ if (this.body[0].toString().match(/Array/)) {
+ var first = false
+ this.body.forEach(function(ary) {
+ if (!first) return first = ary
+ first.concat(ary)
+ })
+ return first
+ }
+ if (typeof(Buffer) !== "undefined" && Buffer.isBuffer(this.body[0])) {
+ var buffs = new BufferJoiner()
+ this.body.forEach(function(buf) {
+ buffs.add(buf)
+ })
+ return buffs.join()
+ }
+ return this.body
+}
+
+ConcatStream.prototype.end = function() {
+ this.cb(false, this.getBody())
+}
+
+module.exports = function(cb) {
+ return new ConcatStream(cb)
+}
+
+module.exports.ConcatStream = ConcatStream
6 node_modules/concat-stream/node_modules/bufferjoiner/.npmignore
@@ -0,0 +1,6 @@
+docs/
+examples/
+test/
+lib-cov/
+coverage.html
+README.md
5 node_modules/concat-stream/node_modules/bufferjoiner/.travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+ - 0.4
+ - 0.6
+ - 0.8
22 node_modules/concat-stream/node_modules/bufferjoiner/LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2011 Kilian Ciuffolo, me@nailik.org
+
+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.
17 node_modules/concat-stream/node_modules/bufferjoiner/Makefile
@@ -0,0 +1,17 @@
+REPORTER = spec
+
+test:
+ @NODE_ENV=test ./node_modules/.bin/mocha test/*.test.js $(OPT) --reporter $(REPORTER)
+
+test-bail:
+ @rm -rf lib-cov
+ @jscoverage lib lib-cov
+ $(MAKE) test OPT=--bail
+
+test-cov:
+ @rm -rf lib-cov
+ @jscoverage lib lib-cov
+ @BUFFER_JOINER_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html
+ @open -g coverage.html
+
+.PHONY: test test-bail test-cov
3 node_modules/concat-stream/node_modules/bufferjoiner/index.js
@@ -0,0 +1,3 @@
+module.exports = process.env.BUFFER_JOINER_COV
+ ? require('./lib-cov/bufferjoiner')
+ : require('./lib/bufferjoiner')
72 node_modules/concat-stream/node_modules/bufferjoiner/lib/bufferjoiner.js
@@ -0,0 +1,72 @@
+/**
+ * Setup BufferJoiner.
+ *
+ * @api public
+ */
+
+var BufferJoiner = module.exports = function BufferJoiner() {
+ if (!(this instanceof BufferJoiner)) {
+ return new BufferJoiner()
+ }
+
+ Object.defineProperty(this, '_buffersList', {
+ value: [],
+ writable: true,
+ enumerable: false,
+ configurable: false
+ })
+ Object.defineProperty(this, '_length', {
+ value: 0,
+ writable: true,
+ enumerable: false,
+ configurable: false
+ })
+}
+
+/**
+ * Return `this._length`
+ *
+ * @api public
+ */
+
+BufferJoiner.prototype.__defineGetter__('length', function length() {
+ return this._length
+})
+
+/**
+ * Add buffer into the buffers list.
+ *
+ * @param {Buffer} buffer
+ * @api public
+ */
+
+BufferJoiner.prototype.add = function add(buffer) {
+ this._buffersList.push(buffer)
+ this._length += buffer.length
+ return this
+}
+
+/**
+ * Return a joined version of the buffers list
+ * add reset status. If `reAdd` return value
+ * will be readded to the buffers list.
+ *
+ * @param {Boolean} reAdd
+ * @api public
+ */
+
+BufferJoiner.prototype.join = function join(reAdd) {
+ var result = new Buffer(this._length)
+ var lastFreeIndex = 0
+ var buffer
+
+ while (buffer = this._buffersList.shift()) {
+ buffer.copy(result, lastFreeIndex)
+ lastFreeIndex += buffer.length
+ }
+
+ this._length = 0
+ reAdd && this.add(result)
+
+ return result
+}
40 node_modules/concat-stream/node_modules/bufferjoiner/package.json
@@ -0,0 +1,40 @@
+{
+ "author": {
+ "name": "Kilian Ciuffolo",
+ "email": "me@nailik.org",
+ "url": "http://nailik.org"
+ },
+ "name": "bufferjoiner",
+ "main": "./",
+ "description": "A nodejs binary buffer utility",
+ "keywords": [
+ "buffer",
+ "merge",
+ "join",
+ "binary"
+ ],
+ "version": "0.1.3",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/kilianc/node-bufferjoiner.git"
+ },
+ "devDependencies": {
+ "mocha": "1.3.x",
+ "chai": "1.0.x"
+ },
+ "_npmUser": {
+ "name": "maxogden",
+ "email": "max@maxogden.com"
+ },
+ "_id": "bufferjoiner@0.1.3",
+ "dependencies": {},
+ "optionalDependencies": {},
+ "engines": {
+ "node": "*"
+ },
+ "_engineSupported": true,
+ "_npmVersion": "1.1.21",
+ "_nodeVersion": "v0.8.0",
+ "_defaultsLoaded": true,
+ "_from": "bufferjoiner@0.1.3"
+}
40 node_modules/concat-stream/package.json
@@ -0,0 +1,40 @@
+{
+ "name": "concat-stream",
+ "description": "writable stream that concatenates strings or data and calls a callback with the result",
+ "tags": [
+ "stream",
+ "simple",
+ "util",
+ "utility"
+ ],
+ "version": "0.0.6",
+ "author": {
+ "name": "Max Ogden",
+ "email": "max@maxogden.com"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/maxogden/node-concat-stream.git"
+ },
+ "bugs": {
+ "url": "http://github.com/maxogden/node-concat-stream/issues"
+ },
+ "engines": [
+ "node >= 0.4.0"
+ ],
+ "dependencies": {
+ "bufferjoiner": "0.1.3"
+ },
+ "_npmUser": {
+ "name": "maxogden",
+ "email": "max@maxogden.com"
+ },
+ "_id": "concat-stream@0.0.6",
+ "devDependencies": {},
+ "optionalDependencies": {},
+ "_engineSupported": true,
+ "_npmVersion": "1.1.21",
+ "_nodeVersion": "v0.8.0",
+ "_defaultsLoaded": true,
+ "_from": "concat-stream@0.0.6"
+}
31 node_modules/concat-stream/readme.md
@@ -0,0 +1,31 @@
+# concat-stream
+
+ npm install concat-stream
+
+then
+
+ var concat = require('concat-stream')
+ var fs = require('fs')
+
+ var read = fs.createReadStream('readme.md')
+ var write = concat(function(err, data) {})
+
+ read.pipe(write)
+
+works with arrays too!
+
+ var write = concat(function(err, data) {})
+ write.write([1,2,3])
+ write.write([4,5,6])
+ write.end()
+ // data will be [1,2,3,4,5,6] in the above callback
+
+works with buffers too! can't believe the deals!
+
+ var write = concat(function(err, data) {})
+ write.write(new Buffer('hello '))
+ write.write(new Buffer('world'))
+ write.end()
+ // data will be a buffer that toString()s to 'hello world' in the above callback
+
+MIT LICENSE
9 node_modules/concat-stream/test.js
@@ -0,0 +1,9 @@
+var spawn = require('child_process').spawn
+var concat = require('./')
+
+var cmd = spawn('ls')
+cmd.stdout.pipe(
+ concat(function(err, out) {
+ console.log(err, out.toString())
+ })
+)
17 package.json
@@ -0,0 +1,17 @@
+{ "name" : "shellout"
+, "description" : "simple shelling out for spawning commands and concatenating the stderr/stdout in a callback"
+, "tags" : ["cli", "simple", "util", "utility"]
+, "version" : "0.0.1"
+, "author" : "Max Ogden <max@maxogden.com>"
+, "repository" :
+ { "type" : "git"
+ , "url" : "http://github.com/maxogden/node-shellout.git"
+ }
+, "bugs" :
+ { "url" : "http://github.com/maxogden/node-shellout/issues" }
+, "engines" : ["node >= 0.4.0"]
+, "dependencies": {
+ "concat-stream": "0.0.6"
+ }
+, "scripts": { "test": "node test.js ls" }
+}
14 readme.md
@@ -0,0 +1,14 @@
+# node-shellout
+
+ npm install shellout
+
+then
+
+ var concat = require('shellout')
+ shellout('ls', function(err, out) {
+ // undefined 'index.js\nnode_modules\npackage.json\nreadme.md\ntest.js\n'
+ })
+
+see also: [[https://github.com/bahamas10/node-exec]]
+
+MIT LICENSE
7 test.js
@@ -0,0 +1,7 @@
+var shell = require('./')
+
+// e.g. node test.js ls
+var cmd = shell(process.argv[2], function(err, out) {
+ console.log(err, out.toString())
+})
+

0 comments on commit e8bb327

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