utils.js 803 字节
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
const fs = require('fs')
const path = require('path')
fxy060608's avatar
fxy060608 已提交
3 4 5
const {
  normalizePath
} = require('../util')
fxy060608's avatar
fxy060608 已提交
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

function hasProjectYarn (cwd) {
  return fs.existsSync(path.join(cwd, 'yarn.lock'))
}

function hasProjectPnpm (cwd) {
  return fs.existsSync(path.join(cwd, 'pnpm-lock.yaml'))
}

function getInstallCommand (cwd) {
  return hasProjectYarn(cwd)
    ? 'yarn add'
    : hasProjectPnpm(cwd)
      ? 'pnpm i'
      : 'npm i'
}

function installDepTips (
  type,
  module,
  version
) {
  const command =
    `${getInstallCommand(process.cwd())} ${module + (version ? '@' + version : '')}${type === 'devDependencies' ? ' -D' : ''}`
  return `Cannot find module: ${module}
Please run \`${command}\` and try again.`
}

fxy060608's avatar
fxy060608 已提交
34
module.exports = {
fxy060608's avatar
fxy060608 已提交
35 36
  normalizePath,
  installDepTips
fxy060608's avatar
fxy060608 已提交
37
}