mbland/pages-server

View on GitHub
lib/command-runner.js

Summary

Maintainability
A
2 hrs
Test Coverage
'use strict'

var childProcess = require('child_process')

module.exports = CommandRunner

function CommandRunner(sitePath, repoName, logger) {
  this.sitePath = sitePath
  this.repoName = repoName
  this.logger = logger
}

CommandRunner.prototype.run = function(path, args, opts, message) {
  return doRun(this, path, args, opts, message)
}

function doRun(runner, path, args, opts, message) {
  return new Promise(function(resolve, reject) {
    var command,
        options = opts || {cwd: runner.sitePath},
        msg = message || 'rebuild failed for',
        error

    if (options.stdio) {
      runner.logger.error('CommandRunner ignoring stdio option value: '
        + options.stdio)
      delete options.stdio
    }

    command = childProcess.spawn(path, args, options)
    command.on('error', function(err) {
      error = err
      reject(new Error(msg + ' ' + runner.repoName + ' due to failed ' +
        'command: ' + path + ' ' + args.join(' ') + ': ' + err.message))
    })

    command.stdout.setEncoding('utf8')
    command.stdout.on('data', function(data) {
      runner.logger.log(data.trimRight())
    })

    command.stderr.setEncoding('utf8')
    command.stderr.on('data', function(data) {
      runner.logger.error(data.trimRight())
    })

    command.on('close', function(code) {
      if (error) {
        return
      }
      if (code !== 0) {
        reject(new Error(msg + ' ' + runner.repoName + ' with exit code ' +
          code + ' from command: ' + path + ' ' + args.join(' ')))
      } else {
        resolve()
      }
    })
  })
}