[484] | 1 | [](http://travis-ci.org/jharding/grunt-exec) |
---|
| 2 | grunt-exec |
---|
| 3 | ========== |
---|
| 4 | |
---|
| 5 | Grunt plugin for executing shell commands. |
---|
| 6 | |
---|
| 7 | Installation |
---|
| 8 | ------------ |
---|
| 9 | |
---|
| 10 | Install grunt-exec using npm: |
---|
| 11 | |
---|
| 12 | ``` |
---|
| 13 | $ npm install grunt-exec |
---|
| 14 | ``` |
---|
| 15 | |
---|
| 16 | Then add this line to your project's *Gruntfile.js*: |
---|
| 17 | |
---|
| 18 | ```javascript |
---|
| 19 | grunt.loadNpmTasks('grunt-exec'); |
---|
| 20 | ``` |
---|
| 21 | |
---|
| 22 | Usage |
---|
| 23 | ----- |
---|
| 24 | |
---|
| 25 | 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. |
---|
| 26 | |
---|
| 27 | 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. |
---|
| 28 | |
---|
| 29 | [types_of_tasks]: https://github.com/gruntjs/grunt/blob/master/docs/types_of_tasks.md#multi-tasks |
---|
| 30 | |
---|
| 31 | ### Properties |
---|
| 32 | |
---|
| 33 | * __command__: The shell command to be executed. Must be a string or a function that returns a string. (alias: __cmd__) |
---|
| 34 | * __stdout__: If `true`, stdout will be printed. Defaults to `true`. |
---|
| 35 | * __stderr__: If `true`, stderr will be printed. Defaults to `true`. |
---|
| 36 | * __cwd__: Current working directory of the shell command. Defaults to the directory containing your Gruntfile. |
---|
| 37 | * __exitCode__: The expected exit code, task will fail if the actual exit code doesn't match. Defaults to `0`. |
---|
| 38 | * __callback__: The callback function passed `child_process.exec`. Defaults to a noop. |
---|
| 39 | |
---|
| 40 | ### Command Functions |
---|
| 41 | |
---|
| 42 | 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. |
---|
| 43 | |
---|
| 44 | #### Passing arguments from the command line |
---|
| 45 | |
---|
| 46 | Command functions can be called with arbitrary arguments. Let's say we have the following exec target that echoes a formatted name: |
---|
| 47 | |
---|
| 48 | ```javascript |
---|
| 49 | exec: { |
---|
| 50 | echo_name: { |
---|
| 51 | cmd: function(firstName, lastName) { |
---|
| 52 | var formattedName = [ |
---|
| 53 | lastName.toUpperCase(), |
---|
| 54 | firstName.toUpperCase() |
---|
| 55 | ].join(', '); |
---|
| 56 | |
---|
| 57 | return 'echo ' + formattedName; |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | ``` |
---|
| 62 | |
---|
| 63 | In order to get `SIMPSON, HOMER` echoed, you'd run `grunt exec:echo_name:homer:simpson` from the command line. |
---|
| 64 | |
---|
| 65 | ### Accessing `grunt` object |
---|
| 66 | |
---|
| 67 | 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`. |
---|
| 68 | |
---|
| 69 | ### Example |
---|
| 70 | |
---|
| 71 | The following examples are available in grunt-exec's Gruntfile. |
---|
| 72 | |
---|
| 73 | ```javascript |
---|
| 74 | grunt.initConfig({ |
---|
| 75 | exec: { |
---|
| 76 | remove_logs: { |
---|
| 77 | command: 'rm -f *.log', |
---|
| 78 | stdout: false, |
---|
| 79 | stderr: false |
---|
| 80 | }, |
---|
| 81 | list_files: { |
---|
| 82 | cmd: 'ls -l **' |
---|
| 83 | }, |
---|
| 84 | echo_grunt_version: { |
---|
| 85 | cmd: function() { return 'echo ' + this.version; } |
---|
| 86 | }, |
---|
| 87 | echo_name: { |
---|
| 88 | cmd: function(firstName, lastName) { |
---|
| 89 | var formattedName = [ |
---|
| 90 | lastName.toUpperCase(), |
---|
| 91 | firstName.toUpperCase() |
---|
| 92 | ].join(', '); |
---|
| 93 | |
---|
| 94 | return 'echo ' + formattedName; |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | }); |
---|
| 99 | ``` |
---|
| 100 | |
---|
| 101 | Testing |
---|
| 102 | ------- |
---|
| 103 | |
---|
| 104 | ``` |
---|
| 105 | $ cd grunt-exec |
---|
| 106 | $ npm test |
---|
| 107 | ``` |
---|
| 108 | |
---|
| 109 | Issues |
---|
| 110 | ------ |
---|
| 111 | |
---|
| 112 | Found a bug? Create an issue on GitHub. |
---|
| 113 | |
---|
| 114 | https://github.com/jharding/grunt-exec/issues |
---|
| 115 | |
---|
| 116 | Versioning |
---|
| 117 | ---------- |
---|
| 118 | |
---|
| 119 | For transparency and insight into the release cycle, releases will be numbered with the follow format: |
---|
| 120 | |
---|
| 121 | `<major>.<minor>.<patch>` |
---|
| 122 | |
---|
| 123 | And constructed with the following guidelines: |
---|
| 124 | |
---|
| 125 | * Breaking backwards compatibility bumps the major |
---|
| 126 | * New additions without breaking backwards compatibility bumps the minor |
---|
| 127 | * Bug fixes and misc changes bump the patch |
---|
| 128 | |
---|
| 129 | For more information on semantic versioning, please visit http://semver.org/. |
---|
| 130 | |
---|
| 131 | License |
---|
| 132 | ------- |
---|
| 133 | |
---|
| 134 | Copyright (c) 2012 [Jake Harding](http://thejakeharding.com) |
---|
| 135 | Licensed under the MIT License. |
---|