gulp-shell
Advanced tools
Comparing version 0.2.0 to 0.2.1
41
index.js
var _ = require('lodash') | ||
var async = require('async') | ||
var cp = require('child_process') | ||
@@ -9,3 +10,11 @@ var gutil = require('gulp-util') | ||
function shell(command, options) { | ||
function shell(commands, options) { | ||
if (typeof commands === 'string') { | ||
commands = [commands] | ||
} | ||
if (!Array.isArray(commands)) { | ||
throw new gutil.PluginError(PLUGIN_NAME, 'Missing commands') | ||
} | ||
if (!options) options = {} | ||
@@ -21,23 +30,29 @@ var ignoreErrors = !!options.ignoreErrors | ||
return through.obj(function (file, _, done) { | ||
command = gutil.template(command, {file: file}) | ||
var self = this | ||
cp.exec(command, {env: env}, function (error, stdout, stderr) { | ||
if (!quiet) { | ||
if (stderr) gutil.log(stderr.trim()) | ||
if (stdout) gutil.log(stdout.trim()) | ||
} | ||
async.eachSeries(commands, function (command, done) { | ||
command = gutil.template(command, {file: file}) | ||
if (error && !ignoreErrors) { | ||
this.emit('error', new gutil.PluginError(PLUGIN_NAME, error)) | ||
cp.exec(command, {env: env}, function (error, stdout, stderr) { | ||
if (!quiet) { | ||
if (stderr) gutil.log(stderr.trim()) | ||
if (stdout) gutil.log(stdout.trim()) | ||
} | ||
done(ignoreErrors ? null : error) | ||
}) | ||
}, function (error) { | ||
if (error) { | ||
self.emit('error', new gutil.PluginError(PLUGIN_NAME, error)) | ||
} else { | ||
this.push(file) | ||
self.push(file) | ||
} | ||
done() | ||
}.bind(this)) | ||
}) | ||
}) | ||
} | ||
shell.task = function (command, options) { | ||
shell.task = function (commands, options) { | ||
return function () { | ||
var stream = shell(command, options) | ||
var stream = shell(commands, options) | ||
@@ -44,0 +59,0 @@ stream.write(new gutil.File()) |
{ | ||
"name": "gulp-shell", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "A handy command line interface for gulp", | ||
@@ -37,2 +37,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"async": "~0.2.10", | ||
"gulp-util": "~2.2.14", | ||
@@ -39,0 +40,0 @@ "lodash": "~2.4.1", |
@@ -33,7 +33,10 @@ # gulp-shell | ||
return gulp.src('*.js') | ||
.pipe(shell('echo <%= file.path %>')) | ||
.pipe(shell([ | ||
'echo <%= file.path %>', | ||
'ls -l <%= file.path %>' | ||
])) | ||
}) | ||
``` | ||
If you just want to execute the command only once, starting the stream with `gulp.src('')` should do the trick. | ||
If you just want to execute a series of commands only once, starting the stream with `gulp.src('')` should do the trick. | ||
@@ -43,22 +46,18 @@ Or you can use this shorthand: | ||
```js | ||
gulp.task('shorthand', shell.task('echo hello')) | ||
``` | ||
To run multiple commands, join them by `&&` or `;`: | ||
```js | ||
gulp.task('multiple', shell.task([ | ||
gulp.task('shorthand', shell.task([ | ||
'echo hello', | ||
'echo world' | ||
].join(' && '))) | ||
])) | ||
``` | ||
Note: The command will be executed in an environment where `PATH` prepended by `./node_modules/.bin`, allowing you to run executables in your Node's dependencies. | ||
Note: All the commands will be executed in an environment where `PATH` prepended by `./node_modules/.bin`, allowing you to run executables in your Node's dependencies. | ||
## API | ||
### shell(command, options) or shell.task(command, options) | ||
### shell(commands, options) or shell.task(commands, options) | ||
#### template | ||
#### commands | ||
type: `Array` or `String` | ||
A command can be a [template][] which can be interpolated by some [file][] info (e.g. `file.path`). | ||
@@ -65,0 +64,0 @@ |
@@ -7,3 +7,3 @@ var gutil = require('gulp-util') | ||
describe('gulp-shell(command, options)', function () { | ||
describe('gulp-shell(commands, options)', function () { | ||
var fakeFile = new gutil.File({ | ||
@@ -15,4 +15,12 @@ cwd: __dirname, | ||
it('should throw when `commands` is missing', function () { | ||
shell.should.throw('Missing commands') | ||
}) | ||
it('should be ok when `commands` is a string', function () { | ||
shell.bind(null, 'true').should.not.throw() | ||
}) | ||
it('should pass file through', function (done) { | ||
var stream = shell('true') | ||
var stream = shell(['true']) | ||
@@ -28,3 +36,3 @@ stream.on('data', function (file) { | ||
it('should execute command after interpolation', function (done) { | ||
var stream = shell('echo <%= file.path %>') | ||
var stream = shell(['echo <%= file.path %>']) | ||
@@ -42,3 +50,3 @@ var write = process.stdout.write | ||
it('should prepend `./node_modules/.bin` to `PATH`', function (done) { | ||
var stream = shell('echo $PATH') | ||
var stream = shell(['echo $PATH']) | ||
@@ -55,5 +63,5 @@ var write = process.stdout.write | ||
describe('.task(command, options)', function () { | ||
describe('.task(commands, options)', function () { | ||
it('should return a function which returns a stream', function (done) { | ||
var task = shell.task('true') | ||
var task = shell.task(['true']) | ||
should(task).be.type('function') | ||
@@ -71,3 +79,3 @@ | ||
it('should emit error by default', function (done) { | ||
var stream = shell('false') | ||
var stream = shell(['false']) | ||
@@ -82,3 +90,3 @@ stream.on('error', function () { | ||
it('should not emit error when `ignoreErrors` == true', function (done) { | ||
var stream = shell('false', {ignoreErrors: true}) | ||
var stream = shell(['false'], {ignoreErrors: true}) | ||
@@ -99,3 +107,3 @@ stream.on('error', function () { | ||
it('should not output anything when `quiet` == true', function (done) { | ||
var stream = shell('echo cannot see me!', {quiet: true}) | ||
var stream = shell(['echo cannot see me!'], {quiet: true}) | ||
@@ -102,0 +110,0 @@ var write = process.stdout.write |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9165
142
4
81
+ Addedasync@~0.2.10
+ Addedasync@0.2.10(transitive)