grunt-exec
Advanced tools
Comparing version 0.4.2 to 0.4.5
@@ -12,2 +12,3 @@ module.exports = function(grunt) { | ||
} | ||
, list_all_files: 'ls -la' | ||
, echo_grunt_version: { | ||
@@ -14,0 +15,0 @@ cmd: function() { return 'echo ' + this.version; } |
{ | ||
"name": "grunt-exec", | ||
"description": "Grunt task for executing shell commands.", | ||
"version": "0.4.2", | ||
"version": "0.4.5", | ||
"homepage": "https://github.com/jharding/grunt-exec", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -25,24 +25,44 @@ [](http://travis-ci.org/jharding/grunt-exec) | ||
This plugin is a [multi task][types_of_tasks], meaning that grunt will automatically iterate over all exec targets if a target is not specified. | ||
This plugin is a [multi task][types_of_tasks], meaning that grunt will | ||
automatically iterate over all exec targets if a target is not specified. | ||
If the exit code generated by the specified shell command is greater than 0, grunt-exec will assume an error has occurred and will abort grunt immediately. | ||
If the exit code generated by the specified shell command is greater than 0, | ||
grunt-exec will assume an error has occurred and will abort grunt immediately. | ||
[types_of_tasks]: https://github.com/gruntjs/grunt/blob/master/docs/types_of_tasks.md#multi-tasks | ||
[types_of_tasks]: http://gruntjs.com/configuring-tasks#task-configuration-and-targets | ||
### Properties | ||
* __command__: The shell command to be executed. Must be a string or a function that returns a string. (alias: __cmd__) | ||
* __stdout__: If `true`, stdout will be printed. Defaults to `true`. | ||
* __stderr__: If `true`, stderr will be printed. Defaults to `true`. | ||
* __cwd__: Current working directory of the shell command. Defaults to the directory containing your Gruntfile. | ||
* __exitCode__: The expected exit code, task will fail if the actual exit code doesn't match. Defaults to `0`. | ||
* __callback__: The callback function passed `child_process.exec`. Defaults to a noop. | ||
* __command__ (alias: __cmd__): The shell command to be executed. Must be a | ||
string or a function that returns a string. | ||
* __stdout__: If `true`, stdout will be printed. Defaults to `true`. | ||
* __stderr__: If `true`, stderr will be printed. Defaults to `true`. | ||
* __cwd__: Current working directory of the shell command. Defaults to the | ||
directory containing your Gruntfile. | ||
* __exitCode__ (alias: __exitCodes__): The expected exit code(s), task will | ||
fail if the actual exit code doesn't match. Defaults to `0`. Can be an array | ||
for multiple allowed exit codes. | ||
* __callback__: The callback function passed `child_process.exec`. Defaults to | ||
a noop. | ||
If the configuration is instead a simple `string`, it will be | ||
interpreted as a full command itself: | ||
```javascript | ||
exec: { | ||
echo_something: 'echo "This is something"' | ||
} | ||
``` | ||
### Command Functions | ||
If you plan on doing advanced stuff with grunt-exec, you'll most likely be using functions for the `command` property of your exec targets. This section details a couple of helpful tips about command functions that could help make your life easier. | ||
If you plan on doing advanced stuff with grunt-exec, you'll most likely be using | ||
functions for the `command` property of your exec targets. This section details | ||
a couple of helpful tips about command functions that could help make your life | ||
easier. | ||
#### Passing arguments from the command line | ||
Command functions can be called with arbitrary arguments. Let's say we have the following exec target that echoes a formatted name: | ||
Command functions can be called with arbitrary arguments. Let's say we have the | ||
following exec target that echoes a formatted name: | ||
@@ -64,7 +84,9 @@ ```javascript | ||
In order to get `SIMPSON, HOMER` echoed, you'd run `grunt exec:echo_name:homer:simpson` from the command line. | ||
In order to get `SIMPSON, HOMER` echoed, you'd run | ||
`grunt exec:echo_name:homer:simpson` from the command line. | ||
### Accessing `grunt` object | ||
All command functions are called in the context of the `grunt` object that they are being ran with. This means you can access the `grunt` object through `this`. | ||
All command functions are called in the context of the `grunt` object that they | ||
are being ran with. This means you can access the `grunt` object through `this`. | ||
@@ -86,2 +108,3 @@ ### Example | ||
}, | ||
list_all_files: 'ls -la', | ||
echo_grunt_version: { | ||
@@ -122,3 +145,4 @@ cmd: function() { return 'echo ' + this.version; } | ||
For transparency and insight into the release cycle, releases will be numbered with the follow format: | ||
For transparency and insight into the release cycle, releases will be numbered | ||
with the follow format: | ||
@@ -125,0 +149,0 @@ `<major>.<minor>.<patch>` |
@@ -20,3 +20,3 @@ // grunt-exec | ||
, callback = _.isFunction(data.callback) ? data.callback : function() {} | ||
, exitCode = data.exitCode || 0 | ||
, exitCodes = data.exitCode || data.exitCodes || 0 | ||
, command | ||
@@ -27,5 +27,8 @@ , childProcess | ||
// https://github.com/jharding/grunt-exec/pull/30 | ||
exitCodes = _.isArray(exitCodes) ? exitCodes : [exitCodes]; | ||
// allow for command to be specified in either | ||
// 'command' or 'cmd' property | ||
command = data.command || data.cmd; | ||
// 'command' or 'cmd' property, or as a string. | ||
command = data.command || data.cmd || (_.isString(data) && data); | ||
@@ -50,3 +53,3 @@ data.cwd && (execOptions.cwd = data.cwd); | ||
verbose.subhead(command); | ||
verbose.writeln(f('Expecting exit code %d', exitCode)); | ||
verbose.writeln(f('Expecting exit code %s', exitCodes.join(' or '))); | ||
@@ -59,3 +62,3 @@ childProcess = cp.exec(command, execOptions, callback); | ||
childProcess.on('exit', function(code) { | ||
if (code !== exitCode) { | ||
if (exitCodes.indexOf(code) < 0) { | ||
log.error(f('Exited with code: %d.', code)); | ||
@@ -62,0 +65,0 @@ return done(false); |
@@ -23,7 +23,8 @@ module.exports = function(grunt) { | ||
return 'echo "you can use callback, and error, stdout, stderr can be used as arguments"'; | ||
}, | ||
callback: function(error, stdout, stderr){ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var outputPath = path.resolve(process.cwd(), 'test4'); | ||
} | ||
, callback: function(error, stdout, stderr){ | ||
var fs = require('fs') | ||
, path = require('path') | ||
, outputPath = path.resolve(process.cwd(), 'test4'); | ||
console.log('outputPath : ' + outputPath); | ||
@@ -33,2 +34,11 @@ fs.writeFileSync(outputPath, stdout, 'utf-8'); | ||
} | ||
, test5: { | ||
cmd: 'exit 8' | ||
, exitCodes: 8 | ||
} | ||
, test6: { | ||
cmd: 'exit 9' | ||
, exitCodes: [8, 9] | ||
} | ||
, test7: 'echo "you don\'t even need an object" > test7' | ||
} | ||
@@ -35,0 +45,0 @@ }); |
@@ -7,3 +7,11 @@ var grunt = require('grunt') | ||
, opts = { gruntfile: path.join(testDir, 'Gruntfile.js') } | ||
, tasks = ['exec:test1', 'exec:test2', 'exec:test3:42:love', 'exec:test4']; | ||
, tasks = [ | ||
'exec:test1' | ||
, 'exec:test2' | ||
, 'exec:test3:42:love' | ||
, 'exec:test4' | ||
, 'exec:test5' | ||
, 'exec:test6' | ||
, 'exec:test7' | ||
]; | ||
@@ -24,2 +32,3 @@ grunt.tasks(tasks, opts, function() { | ||
} | ||
, { name: 'test7', expected: 'you don\'t even need an object\n' } | ||
] | ||
@@ -26,0 +35,0 @@ , outputPath; |
Sorry, the diff of this file is not supported yet
12227
55619
203
160