mirror of
https://github.com/vuejs/vue-cli.git
synced 2026-05-03 10:32:10 -05:00
refactor: hasProjectYarn & hasProjectGit
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
const { execSync } = require('child_process')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
let _hasYarn
|
||||
const _yarnProjects = new Map()
|
||||
let _hasGit
|
||||
const _gitProjects = new Map()
|
||||
|
||||
// env detection
|
||||
exports.hasYarn = () => {
|
||||
@@ -19,6 +23,22 @@ exports.hasYarn = () => {
|
||||
}
|
||||
}
|
||||
|
||||
exports.hasProjectYarn = (cwd) => {
|
||||
if (_yarnProjects.has(cwd)) {
|
||||
return checkYarn(_yarnProjects.get(cwd))
|
||||
}
|
||||
|
||||
const lockFile = path.join(cwd, 'yarn.lock')
|
||||
const result = fs.existsSync(lockFile)
|
||||
_yarnProjects.set(cwd, result)
|
||||
return checkYarn(result)
|
||||
}
|
||||
|
||||
function checkYarn (result) {
|
||||
if (result && !exports.hasYarn()) throw new Error(`The project seems to require yarn but it's not installed.`)
|
||||
return result
|
||||
}
|
||||
|
||||
exports.hasGit = () => {
|
||||
if (process.env.VUE_CLI_TEST) {
|
||||
return true
|
||||
@@ -33,3 +53,20 @@ exports.hasGit = () => {
|
||||
return (_hasGit = false)
|
||||
}
|
||||
}
|
||||
|
||||
exports.hasProjectGit = (cwd) => {
|
||||
if (_gitProjects.has(cwd)) {
|
||||
return _gitProjects.get(cwd)
|
||||
}
|
||||
|
||||
let result
|
||||
try {
|
||||
execSync('git status', { stdio: 'ignore', cwd })
|
||||
result = true
|
||||
} catch (e) {
|
||||
result = false
|
||||
}
|
||||
console.log('hasProjectGit', cwd, result)
|
||||
_gitProjects.set(cwd, result)
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user