lib/runner.js

Maintainability

76.63

Lines of code

69

Created with Raphaël 2.1.002550751002017-7-22

2017-7-22
Maintainability: 76.63

Created with Raphaël 2.1.0017.53552.5702017-7-22

2017-7-22
Lines of Code: 69

Difficulty

10.94

Estimated Errors

0.42

Function weight

By Complexity

Created with Raphaël 2.1.0<anonymous>2

By SLOC

Created with Raphaël 2.1.0exec22
1
const async  = require('async');
2
const child  = require('child_process');
3
const colors = require('colors');
4
const fs     = require('fs');
5
const mkdirp = require('mkdirp');
6
const p      = require('path');
7
const util   = require('util');
8
 
9
/**
10
 * Execute a single command.
11
 *
12
 * @param {Object} command: the command to execute
13
 * @param {Object} opts:
14
 *   - cwd: base directory where the commands should be executed from
15
 *   - quiet: don't display command output if quiet
16
 *   - task: Bob task name, used to create output directory
17
 *   - type: Bob task type name, used to create output directory
18
 *   - dir: report directory where process output will be written into a file
19
 * @param {Function} cb: standard cb(err, result) callback
20
 */
21
function exec(command, opts, cb) {
22
 
23
  console.log('%s | %s', command.meta.task.cyan, command.exec);
24
 
25
  var logFile = p.join(opts.logDir, util.format('%s.log', command.meta.type));
26
  var cproc   = child.exec(command.exec, opts, cb);
27
  var wstream = fs.createWriteStream(logFile);
28
 
29
  cproc.stdout.on('data', function (data) {
30
    if (!opts.quiet) {
31
      process.stdout.write(data);
32
    }
33
    wstream.write(data);
34
  });
35
 
36
  cproc.stderr.on('data', function (data) {
37
    if (!opts.quiet) {
38
      process.stderr.write(data);
39
    }
40
    wstream.write(data);
41
  });
42
}
43
 
44
/**
45
 * Execute multiple commands in series.
46
 *
47
 * @param {Array} commands: an array of commands, each command contains:
48
 *   - exec: executable shell command line
49
 *   - meta: command metadata, language and type
50
 * @param {Object} opts:
51
 *   - logDir: directory where log files will be written to
52
 *   - quiet: don't display command output if quiet is set
53
 * @param {Function} cb: standard cb(err, result) callback
54
 */
55
function execSeries(commands, opts, cb) {
56
 
57
  function _exec(command, cb) {
58
    opts.logDir = opts.logDir.replace(/\{lang\}/g, command.meta.language);
59
    mkdirp(opts.logDir, function (err) {
60
      if (err) { cb(err); } else {
61
        exec(command, opts, cb);
62
      }
63
    });
64
  }
65
  async.eachSeries(commands, _exec, cb);
66
}
67
 
68
exports.exec = exec;
69
exports.execSeries = execSeries;