source: Dev/trunk/node_modules/grunt-exec/tasks/exec.js @ 484

Last change on this file since 484 was 484, checked in by hendrikvanantwerpen, 11 years ago

Commit node_modules, to make checkouts and builds more deterministic.

File size: 1.9 KB
Line 
1// grunt-exec
2// ==========
3// * GitHub: https://github.com/jharding/grunt-exec
4// * Copyright (c) 2012 Jake Harding
5// * Licensed under the MIT license.
6
7module.exports = function(grunt) {
8  var cp = require('child_process')
9    , f = require('util').format
10    , _ = grunt.util._
11    , log = grunt.log
12    , verbose = grunt.verbose;
13
14  grunt.registerMultiTask('exec', 'Execute shell commands.', function() {
15    var data = this.data
16      , execOptions = {}
17      , stdout = data.stdout !== undefined ? data.stdout : true
18      , stderr = data.stderr !== undefined ? data.stderr : true
19      , callback = _.isFunction(data.callback) ? data.callback : function() {}
20      , exitCode = data.exitCode || 0
21      , command
22      , childProcess
23      , args = [].slice.call(arguments, 0)
24      , done = this.async();
25
26    // allow for command to be specified in either
27    // 'command' or 'cmd' property
28    command = data.command || data.cmd;
29
30    data.cwd && (execOptions.cwd = data.cwd);
31    data.maxBuffer && (execOptions.maxBuffer = data.maxBuffer);
32
33    if (!command) {
34      log.error('Missing command property.');
35      return done(false);
36    }
37
38    if (_.isFunction(command)) {
39      command = command.apply(grunt, args);
40    }
41
42    if (!_.isString(command)) {
43      log.error('Command property must be a string.');
44      return done(false);
45    }
46
47    verbose.subhead(command);
48    verbose.writeln(f('Expecting exit code %d', exitCode));
49
50    childProcess = cp.exec(command, execOptions, callback);
51
52    stdout && childProcess.stdout.on('data', function (d) { log.write(d); });
53    stderr && childProcess.stderr.on('data', function (d) { log.error(d); });
54
55    childProcess.on('exit', function(code) {
56      if (code !== exitCode) {
57        log.error(f('Exited with code: %d.', code));
58        return done(false);
59      }
60
61      verbose.ok(f('Exited with code: %d.', code));
62      done();
63    });
64  });
65};
Note: See TracBrowser for help on using the repository browser.