All files / libs/tty isCommandExists.js

100% Statements 8/8
100% Branches 1/1
100% Functions 2/2
100% Lines 8/8
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                      1x 1x   1x 2x       2x 2x       1x   1x                          
/**
 *
 * 基于 npm 模块 command-exists 改写的
 *
 * @module      libs/tty/isCommandExists
 * @createdAt   2017-04-11
 *
 * @copyright   Copyright (c) 2017 Zhonglei Qiu
 * @license     Licensed under the MIT license.
 *
 */
var isWin = require('../sys/isWin')
var execSync = require('child_process').execSync
 
module.exports = function(command) {
  return isWin ? /* istanbul ignore next */ winCommandExistsSync(command) : unixCommandExistsSync(command)
}
 
function unixCommandExistsSync(command) {
  try {
    var stdout = execSync('command -v ' + command
      + ' 2>/dev/null'
      + ' && { echo >&1 \'' + command + ' found\'; exit 0; }'
    )
    return !!stdout
  } catch (e) {
    return false
  }
}
 
/* istanbul ignore next */
function winCommandExistsSync(command) {
  try {
    var stdout = execSync('where ' + command)
    return !!stdout
  } catch (e) {
    return false
  }
}