Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 41 additions & 16 deletions procstreams.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 || {};

Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
Expand All @@ -322,4 +347,4 @@ procPromise._prototype = {
}
inherits(procPromise, procStream, procPromise._prototype);

module.exports = procStream;
module.exports = procStream;
10 changes: 10 additions & 0 deletions tests/bin/cwd-test.js
Original file line number Diff line number Diff line change
@@ -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());
}
});
1 change: 1 addition & 0 deletions tests/fixtures/cwd-test-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fixtures dir
1 change: 1 addition & 0 deletions tests/fixtures/subdir/cwd-test-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
subdir
20 changes: 20 additions & 0 deletions tests/test-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})