From 2c494024626e4118d5ca5f67fc0ea326c0e86f77 Mon Sep 17 00:00:00 2001 From: Daniel Luxemburg Date: Sun, 23 Feb 2014 16:43:05 -0500 Subject: [PATCH] add options.cwd to set current working directory for command --- README.md | 8 ++++++++ index.js | 3 ++- test/index.js | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f404f60..79b382b 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/index.js b/index.js index 53d884a..5992f0e 100644 --- a/index.js +++ b/index.js @@ -24,6 +24,7 @@ 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 @@ -31,7 +32,7 @@ function shell(commands, options) { 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()) diff --git a/test/index.js b/test/index.js index 3e904d2..2517170 100644 --- a/test/index.js +++ b/test/index.js @@ -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) + }) + }) + }) })