env.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. const { execSync } = require('child_process')
  2. const fs = require('fs')
  3. const path = require('path')
  4. const LRU = require('lru-cache')
  5. const semver = require('semver')
  6. let _hasYarn
  7. const _yarnProjects = new LRU({
  8. max: 10,
  9. maxAge: 1000
  10. })
  11. let _hasGit
  12. const _gitProjects = new LRU({
  13. max: 10,
  14. maxAge: 1000
  15. })
  16. // env detection
  17. exports.hasYarn = () => {
  18. if (process.env.VUE_CLI_TEST) {
  19. return true
  20. }
  21. if (_hasYarn != null) {
  22. return _hasYarn
  23. }
  24. try {
  25. execSync('yarnpkg --version', { stdio: 'ignore' })
  26. return (_hasYarn = true)
  27. } catch (e) {
  28. return (_hasYarn = false)
  29. }
  30. }
  31. exports.hasProjectYarn = (cwd) => {
  32. if (_yarnProjects.has(cwd)) {
  33. return checkYarn(_yarnProjects.get(cwd))
  34. }
  35. const lockFile = path.join(cwd, 'yarn.lock')
  36. const result = fs.existsSync(lockFile)
  37. _yarnProjects.set(cwd, result)
  38. return checkYarn(result)
  39. }
  40. function checkYarn (result) {
  41. if (result && !exports.hasYarn()) throw new Error(`The project seems to require yarn but it's not installed.`)
  42. return result
  43. }
  44. exports.hasGit = () => {
  45. if (process.env.VUE_CLI_TEST) {
  46. return true
  47. }
  48. if (_hasGit != null) {
  49. return _hasGit
  50. }
  51. try {
  52. execSync('git --version', { stdio: 'ignore' })
  53. return (_hasGit = true)
  54. } catch (e) {
  55. return (_hasGit = false)
  56. }
  57. }
  58. exports.hasProjectGit = (cwd) => {
  59. if (_gitProjects.has(cwd)) {
  60. return _gitProjects.get(cwd)
  61. }
  62. let result
  63. try {
  64. execSync('git status', { stdio: 'ignore', cwd })
  65. result = true
  66. } catch (e) {
  67. result = false
  68. }
  69. _gitProjects.set(cwd, result)
  70. return result
  71. }
  72. let _hasPnpm
  73. let _hasPnpm3orLater
  74. const _pnpmProjects = new LRU({
  75. max: 10,
  76. maxAge: 1000
  77. })
  78. exports.hasPnpm3OrLater = () => {
  79. if (process.env.VUE_CLI_TEST) {
  80. return true
  81. }
  82. if (_hasPnpm3orLater != null) {
  83. return _hasPnpm3orLater
  84. }
  85. try {
  86. const pnpmVersion = execSync('pnpm --version', {
  87. stdio: ['pipe', 'pipe', 'ignore']
  88. }).toString()
  89. // there's a critical bug in pnpm 2
  90. // https://github.com/pnpm/pnpm/issues/1678#issuecomment-469981972
  91. // so we only support pnpm >= 3.0.0
  92. _hasPnpm = true
  93. _hasPnpm3orLater = semver.gte(pnpmVersion, '3.0.0')
  94. return _hasPnpm3orLater
  95. } catch (e) {
  96. return (_hasPnpm3orLater = false)
  97. }
  98. }
  99. exports.hasProjectPnpm = (cwd) => {
  100. if (_pnpmProjects.has(cwd)) {
  101. return checkPnpm(_pnpmProjects.get(cwd))
  102. }
  103. const lockFile = path.join(cwd, 'pnpm-lock.yaml')
  104. const result = fs.existsSync(lockFile)
  105. _pnpmProjects.set(cwd, result)
  106. return checkPnpm(result)
  107. }
  108. function checkPnpm (result) {
  109. if (result && !exports.hasPnpm3OrLater()) {
  110. throw new Error(`The project seems to require pnpm${_hasPnpm ? ' >= 3' : ''} but it's not installed.`)
  111. }
  112. return result
  113. }
  114. // OS
  115. exports.isWindows = process.platform === 'win32'
  116. exports.isMacintosh = process.platform === 'darwin'
  117. exports.isLinux = process.platform === 'linux'