UNPKG

4.47 kBMarkdownView Raw
1# grunt-shell
2
3> Run shell commands
4
5A good way to interact with other CLI tools. E.g. compiling Compass `compass compile` or get the current git branch `git branch`.
6
7
8## Getting Started
9
10If you haven't used [grunt][] before, be sure to check out the [Getting Started][] guide, as it explains how to create a [gruntfile][Getting Started] as well as install and use grunt plugins. Once you're familiar with that process, install this plugin with this command:
11
12```shell
13npm install --save-dev grunt-shell
14```
15
16Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
17
18```js
19grunt.loadNpmTasks('grunt-shell');
20```
21
22*Tip: the [load-grunt-tasks](https://github.com/sindresorhus/load-grunt-tasks) module makes it easier to load multiple grunt tasks.*
23
24[grunt]: http://gruntjs.com
25[Getting Started]: https://github.com/gruntjs/grunt/wiki/Getting-started
26
27
28## Documentation
29
30
31### Example config
32
33```js
34grunt.initConfig({
35 shell: { // Task
36 listFolders: { // Target
37 options: { // Options
38 stdout: true
39 },
40 command: 'ls'
41 }
42 }
43});
44
45grunt.loadNpmTasks('grunt-shell');
46grunt.registerTask('default', ['shell']);
47```
48
49
50### Example usage
51
52
53#### Run command
54
55Create a folder named `test`.
56
57```js
58grunt.initConfig({
59 shell: {
60 makeDir: {
61 command: 'mkdir test'
62 }
63 }
64});
65```
66
67The `command` property supports templates:
68
69```js
70grunt.initConfig({
71 testDir: 'test',
72 shell: {
73 makeDir: {
74 command: 'mkdir <%= testDir %>'
75 }
76 }
77});
78```
79
80You can also supply a function that returns the command:
81
82```js
83grunt.initConfig({
84 shell: {
85 hello: {
86 command: function () {
87 return 'echo hello';
88 }
89 }
90 }
91});
92```
93Which can also take arguments:
94
95```js
96shell: {
97 hello: {
98 command: function (greeting) {
99 return 'echo ' + greeting;
100 }
101 }
102}
103
104grunt.loadNpmTasks('grunt-shell');
105grunt.registerTask('default', ['shell:hello']);
106```
107
108
109#### Run command and display the output
110
111Output a directory listing in your Terminal.
112
113```js
114grunt.initConfig({
115 shell: {
116 dirListing: {
117 command: 'ls',
118 options: {
119 stdout: true
120 }
121 }
122 }
123});
124```
125
126
127#### Custom callback
128
129Do whatever you want with the output.
130
131```js
132function log(err, stdout, stderr, cb) {
133 console.log(stdout);
134 cb();
135}
136
137grunt.initConfig({
138 shell: {
139 dirListing: {
140 command: 'ls',
141 options: {
142 callback: log
143 }
144 }
145 }
146});
147```
148
149
150#### Option passed to the .exec() method
151
152Run a command in another directory. In this example we run it in a subfolder using the `cwd` (current working directory) option.
153
154```js
155grunt.initConfig({
156 shell: {
157 subfolderLs: {
158 command: 'ls',
159 options: {
160 stdout: true,
161 execOptions: {
162 cwd: 'tasks'
163 }
164 }
165 }
166 }
167});
168```
169
170
171#### Multiple commands
172
173Run multiple commands by placing them in an array which is joined using `&&` or `;`. `&&` means run this only if the previous command succeeded. You can also use `&` to have the commands run concurrently (by executing all commands except the last one in a subshell).
174
175```js
176grunt.initConfig({
177 shell: {
178 multiple: {
179 command: [
180 'mkdir test',
181 'cd test',
182 'ls'
183 ].join('&&')
184 }
185 }
186});
187```
188
189
190### Config
191
192
193#### command
194
195**Required**
196Type: `String|Function`
197
198The command you want to run or a function which returns it. Supports underscore templates.
199
200
201### Options
202
203
204#### stdout
205
206Default: `false`
207Type: `Boolean`
208
209Show stdout in the Terminal.
210
211
212#### stderr
213
214Default: `false`
215Type: `Boolean`
216
217Show stderr in the Terminal.
218
219
220#### stdin
221
222Default: `true`
223Type: `Boolean`
224
225Forward the terminal's stdin to the command.
226
227
228#### failOnError
229
230Default: `false`
231Type: `Boolean`
232
233Fail task if it encounters an error. Does not apply if you specify a `callback`.
234
235
236#### callback(err, stdout, stderr, cb)
237
238Default: `function () {}`
239Type: `Function`
240
241Lets you override the default callback with your own.
242
243**Make sure to call the `cb` method when you're done.**
244
245
246#### execOptions
247
248Default: `undefined`
249Accepts: Object
250
251Specify some options to be passed to the [.exec()](http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback) method:
252
253- `cwd` String *Current working directory of the child process*
254- `env` Object *Environment key-value pairs*
255- `setsid` Boolean
256- `encoding` String *(Default: 'utf8')*
257- `timeout` Number *(Default: 0)*
258- `maxBuffer` Number *(Default: 200\*1024)*
259- `killSignal` String *(Default: 'SIGTERM')*
260
261
262## License
263
264MIT © [Sindre Sorhus](http://sindresorhus.com)