all files / node-mysql-dbdeploy/lib/ helpers.js

17.39% Statements 4/23
0% Branches 0/12
0% Functions 0/3
17.39% Lines 4/23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44                                                                                
var util = require('util');
 
module.exports = {
  fileExists: fileExists,
  exec: exec
};
 
/*** Helper functions **/
function fileExists(path) {
  try {
    return require('fs').statSync(path).isFile();
  } catch (e) {
    return false;
  }
}
 
function exec(cmd, next, successMsg, failMsg) {
  if (typeof successMsg === 'undefined') {
    successMsg = cmd;
  }
  if (typeof failMsg === 'undefined') {
    failMsg = cmd;
  }
  util.log(util.format("exec: %s", cmd));
  require('child_process').exec(cmd, function(error, stdout, stderr) {
    if (stdout.length) {
      util.log('stdout: ' + stdout);
    }
    if (stderr.length) {
      util.log('stderr: ' + stderr);
    }
    if (error !== null) {
      util.log(util.format("Failed %s", failMsg));
      util.log(util.format('exec error: ', error));
    }
    else {
      util.log(util.format("Success %s", successMsg));
    }
    if (typeof next === 'function') {
      next(error, stdout);
    }
  });
}