diff --git a/procstreams.js b/procstreams.js index 6a6c78e..9cb5f4d 100644 --- a/procstreams.js +++ b/procstreams.js @@ -127,19 +127,33 @@ function collect() { function procStream(cmd, args, opts, callback) { if(!cmd) { throw new Error('Missing command'); } - var proc = null, o = null; + var proc = null, o = null, cwd = null; // get the args to create a new procstream - o = normalizeArguments(cmd, args, opts, callback); - cmd = o.cmd; - args = o.args; - opts = o.opts; - callback = o.callback; + if(cmd.cmd) { + o = cmd; + cmd = o.cmd; + args = o.args; + opts = o.opts; + callback = o.callback; + } + else { + o = normalizeArguments(cmd, args, opts, callback); + cmd = o.cmd; + args = o.args; + opts = o.opts; + callback = o.callback; + } + + cwd = opts.cwd; // this is a process object if(isProcess(cmd)) { // this is already a procstream if(procStream.is(cmd)) { + if(cwd && !cmd._cwd) { + cmd._cwd = cwd; + } cmd.on('close', callback); return cmd; } else { @@ -156,6 +170,10 @@ function procStream(cmd, args, opts, callback) { proc._args = o; } + if(cwd && !proc._cwd) { + proc._cwd = cwd; + } + var onExit = function(errCode, signal) { var err = this._err || {}; @@ -240,19 +258,19 @@ procStream._prototype = { } , and: function and() { var args = slice.call(arguments) - , dest = new procPromise(args); + , dest = new procPromise(args, this._cwd); this.on('close', function(code, signal) { if(code === 0) { - dest.resolve(args); + dest.resolve(); } }); return dest; } - , or: function or() { + , or: function or(cmd, args, opts, cwd, callback) { var args = slice.call(arguments) - , dest = new procPromise(args); + , dest = new procPromise(args, this._cwd); this.on('close', function(code, signal) { if(code !== 0) { @@ -262,9 +280,9 @@ procStream._prototype = { return dest; } - , then: function then() { + , then: function then(cmd, args, opts, callback) { var args = slice.call(arguments) - , dest = new procPromise(args); + , dest = new procPromise(args, this._cwd); this.on('close', function(code, signal) { dest.resolve(); @@ -300,11 +318,18 @@ procStream._prototype = { } inherits(procStream, EventEmitter, procStream._prototype); -function procPromise(args) { - this._args = args; +function procPromise(args, cwd) { + this._args = normalizeArguments.apply(null, args); this._resolved = false; this._proc = null; + if(this._args.opts.cwd) { + this._cwd = this._args.opts.cwd; + } + else { + this._args.opts.cwd = this._cwd = cwd; + } + this.resolve = procPromise.prototype.resolve.bind(this); this.reject = procPromise.prototype.reject.bind(this); } @@ -313,7 +338,7 @@ procPromise._prototype = { if(this._resolved) { return this._proc; } this._resolved = true; - this._proc = procStream.apply(null, this._args); + this._proc = procStream.call(null, this._args); this._proc._events = utils.mixin({}, this._proc._events, this._events); return this._proc; @@ -322,4 +347,4 @@ procPromise._prototype = { } inherits(procPromise, procStream, procPromise._prototype); -module.exports = procStream; +module.exports = procStream; \ No newline at end of file diff --git a/tests/bin/cwd-test.js b/tests/bin/cwd-test.js new file mode 100644 index 0000000..4f0329a --- /dev/null +++ b/tests/bin/cwd-test.js @@ -0,0 +1,10 @@ +var fs = require('fs'); + +fs.readFile('./cwd-test-file.txt', function (err, file) { + if(err) { + console.error(err); + } + else { + console.log(file.toString()); + } +}); \ No newline at end of file diff --git a/tests/fixtures/cwd-test-file.txt b/tests/fixtures/cwd-test-file.txt new file mode 100644 index 0000000..5a85bae --- /dev/null +++ b/tests/fixtures/cwd-test-file.txt @@ -0,0 +1 @@ +fixtures dir \ No newline at end of file diff --git a/tests/fixtures/subdir/cwd-test-file.txt b/tests/fixtures/subdir/cwd-test-file.txt new file mode 100644 index 0000000..8bbe8a5 --- /dev/null +++ b/tests/fixtures/subdir/cwd-test-file.txt @@ -0,0 +1 @@ +subdir \ No newline at end of file diff --git a/tests/test-options.js b/tests/test-options.js index 35f9705..39e16d5 100644 --- a/tests/test-options.js +++ b/tests/test-options.js @@ -47,3 +47,23 @@ test('stderr option sends stderr to provided stream', function(assert) { }) $p('node tests/bin/err-test.js').pipe('cat', { stderr: collector }) }) + +test('cwd option carries over from command to command', function(assert) { + var t = timers.timer() + + var cwd = __dirname + '/fixtures/subdir' + + $p('node ' + __dirname + '/bin/cwd-test.js', { cwd: cwd }) + .data(function(err, stdout) { + assert.ifError(err) + assert.equal('subdir', stdout.toString().trim()) + }) + .and('node ' + __dirname + '/bin/cwd-test.js') + .data(function(err, stdout, stderr) { + assert.ifError(err) + + t.stop() + assert.equal('subdir', stdout.toString().trim()) + assert.end() + }) +})