发布于 6 个月前 ,更新于 6 个月前 node

NODEJS 判断某个命令是否存在

import os from 'os'
import { execSync } from 'child_process'

function cmdExists(cmd: string) {
  try {
    execSync(
      os.platform() === 'win32'
        ? `cmd /c "(help ${cmd} > nul || exit 0) && where ${cmd} > nul 2> nul"`
        : `command -v ${cmd}`,
    )
    return true
  }
  catch {
    return false
  }
}

代码摘自 https://github.com/antfu/ni/blob/main/src/utils.ts#L16

© 2016 - 2022 BY 禾惠 粤ICP备20027042号