[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 | |
---|
[516] | 25 | This plugin is a [multi task][types_of_tasks], meaning that grunt will |
---|
| 26 | automatically iterate over all exec targets if a target is not specified. |
---|
[484] | 27 | |
---|
[516] | 28 | If the exit code generated by the specified shell command is greater than 0, |
---|
| 29 | grunt-exec will assume an error has occurred and will abort grunt immediately. |
---|
[484] | 30 | |
---|
[516] | 31 | [types_of_tasks]: http://gruntjs.com/configuring-tasks#task-configuration-and-targets |
---|
[484] | 32 | |
---|
| 33 | ### Properties |
---|
| 34 | |
---|
[516] | 35 | * __command__ (alias: __cmd__): The shell command to be executed. Must be a |
---|
| 36 | string or a function that returns a string. |
---|
| 37 | * __stdout__: If `true`, stdout will be printed. Defaults to `true`. |
---|
| 38 | * __stderr__: If `true`, stderr will be printed. Defaults to `true`. |
---|
| 39 | * __cwd__: Current working directory of the shell command. Defaults to the |
---|
| 40 | directory containing your Gruntfile. |
---|
| 41 | * __exitCode__ (alias: __exitCodes__): The expected exit code(s), task will |
---|
| 42 | fail if the actual exit code doesn't match. Defaults to `0`. Can be an array |
---|
| 43 | for multiple allowed exit codes. |
---|
| 44 | * __callback__: The callback function passed `child_process.exec`. Defaults to |
---|
| 45 | a noop. |
---|
[484] | 46 | |
---|
[516] | 47 | If the configuration is instead a simple `string`, it will be |
---|
| 48 | interpreted as a full command itself: |
---|
| 49 | |
---|
| 50 | ```javascript |
---|
| 51 | exec: { |
---|
| 52 | echo_something: 'echo "This is something"' |
---|
| 53 | } |
---|
| 54 | ``` |
---|
| 55 | |
---|
[484] | 56 | ### Command Functions |
---|
| 57 | |
---|
[516] | 58 | If you plan on doing advanced stuff with grunt-exec, you'll most likely be using |
---|
| 59 | functions for the `command` property of your exec targets. This section details |
---|
| 60 | a couple of helpful tips about command functions that could help make your life |
---|
| 61 | easier. |
---|
[484] | 62 | |
---|
| 63 | #### Passing arguments from the command line |
---|
| 64 | |
---|
[516] | 65 | Command functions can be called with arbitrary arguments. Let's say we have the |
---|
| 66 | following exec target that echoes a formatted name: |
---|
[484] | 67 | |
---|
| 68 | ```javascript |
---|
| 69 | exec: { |
---|
| 70 | echo_name: { |
---|
| 71 | cmd: function(firstName, lastName) { |
---|
| 72 | var formattedName = [ |
---|
| 73 | lastName.toUpperCase(), |
---|
| 74 | firstName.toUpperCase() |
---|
| 75 | ].join(', '); |
---|
| 76 | |
---|
| 77 | return 'echo ' + formattedName; |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | ``` |
---|
| 82 | |
---|
[516] | 83 | In order to get `SIMPSON, HOMER` echoed, you'd run |
---|
| 84 | `grunt exec:echo_name:homer:simpson` from the command line. |
---|
[484] | 85 | |
---|
| 86 | ### Accessing `grunt` object |
---|
| 87 | |
---|
[516] | 88 | All command functions are called in the context of the `grunt` object that they |
---|
| 89 | are being ran with. This means you can access the `grunt` object through `this`. |
---|
[484] | 90 | |
---|
| 91 | ### Example |
---|
| 92 | |
---|
| 93 | The following examples are available in grunt-exec's Gruntfile. |
---|
| 94 | |
---|
| 95 | ```javascript |
---|
| 96 | grunt.initConfig({ |
---|
| 97 | exec: { |
---|
| 98 | remove_logs: { |
---|
| 99 | command: 'rm -f *.log', |
---|
| 100 | stdout: false, |
---|
| 101 | stderr: false |
---|
| 102 | }, |
---|
| 103 | list_files: { |
---|
| 104 | cmd: 'ls -l **' |
---|
| 105 | }, |
---|
[516] | 106 | list_all_files: 'ls -la', |
---|
[484] | 107 | echo_grunt_version: { |
---|
| 108 | cmd: function() { return 'echo ' + this.version; } |
---|
| 109 | }, |
---|
| 110 | echo_name: { |
---|
| 111 | cmd: function(firstName, lastName) { |
---|
| 112 | var formattedName = [ |
---|
| 113 | lastName.toUpperCase(), |
---|
| 114 | firstName.toUpperCase() |
---|
| 115 | ].join(', '); |
---|
| 116 | |
---|
| 117 | return 'echo ' + formattedName; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | }); |
---|
| 122 | ``` |
---|
| 123 | |
---|
| 124 | Testing |
---|
| 125 | ------- |
---|
| 126 | |
---|
| 127 | ``` |
---|
| 128 | $ cd grunt-exec |
---|
| 129 | $ npm test |
---|
| 130 | ``` |
---|
| 131 | |
---|
| 132 | Issues |
---|
| 133 | ------ |
---|
| 134 | |
---|
| 135 | Found a bug? Create an issue on GitHub. |
---|
| 136 | |
---|
| 137 | https://github.com/jharding/grunt-exec/issues |
---|
| 138 | |
---|
| 139 | Versioning |
---|
| 140 | ---------- |
---|
| 141 | |
---|
[516] | 142 | For transparency and insight into the release cycle, releases will be numbered |
---|
| 143 | with the follow format: |
---|
[484] | 144 | |
---|
| 145 | `<major>.<minor>.<patch>` |
---|
| 146 | |
---|
| 147 | And constructed with the following guidelines: |
---|
| 148 | |
---|
| 149 | * Breaking backwards compatibility bumps the major |
---|
| 150 | * New additions without breaking backwards compatibility bumps the minor |
---|
| 151 | * Bug fixes and misc changes bump the patch |
---|
| 152 | |
---|
| 153 | For more information on semantic versioning, please visit http://semver.org/. |
---|
| 154 | |
---|
| 155 | License |
---|
| 156 | ------- |
---|
| 157 | |
---|
| 158 | Copyright (c) 2012 [Jake Harding](http://thejakeharding.com) |
---|
| 159 | Licensed under the MIT License. |
---|