Skip to content
Merged
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,11 @@ type: `Boolean`
default: `false`

By default, it will print the command output.

#### options.cwd

type: `String`

default: Result of `process.cwd()` (as described [here](http://nodejs.org/api/process.html#process_process_cwd))

Sets the current working directory for the command.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ function shell(commands, options) {
var separator = process.platform.match(/^win/) >= 0 ? ';' : ':'
var path = pathToBin + separator + process.env.PATH
var env = _.extend({}, process.env, {PATH: path})
var cwd = options.cwd || process.cwd()

return through.obj(function (file, _, done) {
var self = this

async.eachSeries(commands, function (command, done) {
command = gutil.template(command, {file: file})

cp.exec(command, {env: env}, function (error, stdout, stderr) {
cp.exec(command, {env: env, cwd: cwd}, function (error, stdout, stderr) {
if (!quiet) {
if (stderr) gutil.log(stderr.trim())
if (stdout) gutil.log(stdout.trim())
Expand Down
32 changes: 32 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,37 @@ describe('gulp-shell(commands, options)', function () {
stream.write(fakeFile)
})
})

describe('cwd', function () {
it('should set the current working directory when `cwd` is a string', function (done) {
var stream = shell(['pwd'], {cwd: '..'})

var write = process.stdout.write
process.stdout.write = function (output) {
process.stdout.write = write
should(output).containEql(join(__dirname, '../..'))
should(output).not.containEql(join(__dirname, '..'))
done()
}

stream.write(fakeFile)
})
})

describe('cwd', function () {
it('should use the process current working directory when `cwd` is not passed', function (done) {
var stream = shell(['pwd'])

var write = process.stdout.write
process.stdout.write = function (output) {
process.stdout.write = write
should(output).containEql(join(__dirname, '..'))
done()
}

stream.write(fakeFile)
})
})

})
})