refactor: hasProjectYarn & hasProjectGit

This commit is contained in:
Guillaume Chau
2018-07-05 11:11:48 +02:00
parent 841b470e89
commit 8e5448c2dc
11 changed files with 90 additions and 36 deletions

View File

@@ -17,6 +17,13 @@ class PluginAPI {
this.service = service
}
/**
* Current working directory.
*/
getCwd () {
return this.service.context
}
/**
* Resolve path for a project.
*

View File

@@ -1,6 +1,6 @@
const {
info,
hasYarn,
hasProjectYarn,
openBrowser,
IpcMessenger
} = require('@vue/cli-shared-utils')
@@ -193,7 +193,7 @@ module.exports = (api, options) => {
isFirstCompile = false
if (!isProduction) {
const buildCommand = hasYarn() ? `yarn build` : `npm run build`
const buildCommand = hasProjectYarn(api.getCwd()) ? `yarn build` : `npm run build`
console.log(` Note that the development build is not optimized.`)
console.log(` To create a production build, run ${chalk.cyan(buildCommand)}.`)
} else {

View File

@@ -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
}

View File

@@ -150,7 +150,7 @@ function install ({ id, type }, context) {
status: 'dependency-install',
args: [id]
})
await installPackage(cwd.get(), getCommand(), null, id, type === 'devDependencies')
await installPackage(cwd.get(), getCommand(cwd.get()), null, id, type === 'devDependencies')
logs.add({
message: `Dependency ${id} installed`,
@@ -178,7 +178,7 @@ function uninstall ({ id }, context) {
const dep = findOne(id, context)
await uninstallPackage(cwd.get(), getCommand(), null, id)
await uninstallPackage(cwd.get(), getCommand(cwd.get()), null, id)
logs.add({
message: `Dependency ${id} uninstalled`,
@@ -204,7 +204,7 @@ function update ({ id }, context) {
const dep = findOne(id, context)
const { current, wanted } = await getVersion(dep, context)
await updatePackage(cwd.get(), getCommand(), null, id)
await updatePackage(cwd.get(), getCommand(cwd.get()), null, id)
logs.add({
message: `Dependency ${id} updated from ${current} to ${wanted}`,
@@ -249,7 +249,7 @@ function updateAll (context) {
args: [updatedDeps.length]
})
await updatePackage(cwd.get(), getCommand(), null, updatedDeps.map(
await updatePackage(cwd.get(), getCommand(cwd.get()), null, updatedDeps.map(
p => p.id
).join(' '))

View File

@@ -3,8 +3,12 @@ const path = require('path')
const parseDiff = require('../util/parse-diff')
// Connectors
const cwd = require('./cwd')
// Utils
const { hasProjectGit } = require('@vue/cli-shared-utils')
async function getNewFiles (context) {
if (!hasProjectGit(cwd.get())) return []
const { stdout } = await execa('git', [
'ls-files',
'-o',
@@ -20,6 +24,8 @@ async function getNewFiles (context) {
}
async function getDiffs (context) {
if (!hasProjectGit(cwd.get())) return []
const newFiles = await getNewFiles(context)
await execa('git', ['add', '-N', '*'], {
cwd: cwd.get()
@@ -40,6 +46,8 @@ async function getDiffs (context) {
}
async function commit (message, context) {
if (!hasProjectGit(cwd.get())) return false
await execa('git', ['add', '*'], {
cwd: cwd.get()
})
@@ -50,6 +58,8 @@ async function commit (message, context) {
}
async function reset (context) {
if (!hasProjectGit(cwd.get())) return false
await execa('git', ['reset'], {
cwd: cwd.get()
})
@@ -57,6 +67,8 @@ async function reset (context) {
}
async function getRoot (context) {
if (!hasProjectGit(cwd.get())) return cwd.get()
const { stdout } = await execa('git', [
'rev-parse',
'--show-toplevel'

View File

@@ -217,7 +217,7 @@ function install (id, context) {
if (process.env.VUE_CLI_DEBUG && isOfficialPlugin(id)) {
mockInstall(id, context)
} else {
await installPackage(cwd.get(), getCommand(), null, id)
await installPackage(cwd.get(), getCommand(cwd.get()), null, id)
}
await initPrompts(id, context)
installationStep = 'config'
@@ -250,7 +250,7 @@ function uninstall (id, context) {
if (process.env.VUE_CLI_DEBUG && isOfficialPlugin(id)) {
mockUninstall(id, context)
} else {
await uninstallPackage(cwd.get(), getCommand(), null, id)
await uninstallPackage(cwd.get(), getCommand(cwd.get()), null, id)
}
currentPluginId = null
installationStep = null
@@ -330,7 +330,7 @@ function update (id, context) {
const plugin = findOne(id, context)
const { current, wanted } = await dependencies.getVersion(plugin, context)
await updatePackage(cwd.get(), getCommand(), null, id)
await updatePackage(cwd.get(), getCommand(cwd.get()), null, id)
logs.add({
message: `Plugin ${id} updated from ${current} to ${wanted}`,
@@ -377,7 +377,7 @@ async function updateAll (context) {
args: [updatedPlugins.length]
})
await updatePackage(cwd.get(), getCommand(), null, updatedPlugins.map(
await updatePackage(cwd.get(), getCommand(cwd.get()), null, updatedPlugins.map(
p => p.id
).join(' '))

View File

@@ -1,8 +1,12 @@
const {
hasYarn
hasYarn,
hasProjectYarn
} = require('@vue/cli-shared-utils')
const { loadOptions } = require('@vue/cli/lib/options')
exports.getCommand = function () {
return loadOptions().packageManager || (hasYarn() ? 'yarn' : 'npm')
exports.getCommand = function (cwd = undefined) {
if (!cwd) {
return loadOptions().packageManager || (hasYarn() ? 'yarn' : 'npm')
}
return hasProjectYarn(cwd) ? 'yarn' : 'npm'
}

View File

@@ -16,6 +16,7 @@
"files-changed": "Files changed",
"search-file": "Search file",
"empty": "No change found",
"error": "Couldn't get file changes",
"modals": {
"commit": {
"title": "Commit changes",

View File

@@ -30,6 +30,7 @@ const {
warn,
error,
hasGit,
hasProjectGit,
hasYarn,
logWithSpinner,
stopSpinner,
@@ -443,17 +444,9 @@ module.exports = class Creator extends EventEmitter {
return false
}
if (cliOptions.git) {
return cliOptions.git !== 'false'
return cliOptions.git !== 'false' && cliOptions.git !== false
}
// check if we are in a git repo already
try {
await this.run('git', ['status'])
} catch (e) {
// if git status failed, let's create a fresh repo
return true
}
// if git status worked, it means we are already in a git repo
// so don't init again.
return false
return !hasProjectGit(this.context)
}
}

View File

@@ -6,7 +6,7 @@ const { resolveModule, loadModule } = require('./util/module')
const {
log,
error,
hasYarn,
hasProjectYarn,
stopSpinner,
resolvePluginId
} = require('@vue/cli-shared-utils')
@@ -26,7 +26,7 @@ async function add (pluginName, options = {}, context = process.cwd()) {
log(`📦 Installing ${chalk.cyan(packageName)}...`)
log()
const packageManager = loadOptions().packageManager || (hasYarn() ? 'yarn' : 'npm')
const packageManager = loadOptions().packageManager || (hasProjectYarn(context) ? 'yarn' : 'npm')
await installPackage(context, packageManager, null, packageName)
stopSpinner()

View File

@@ -13,8 +13,8 @@ const normalizeFilePaths = require('./util/normalizeFilePaths')
const {
log,
error,
hasYarn,
hasGit,
hasProjectYarn,
hasProjectGit,
logWithSpinner,
stopSpinner,
resolvePluginId
@@ -122,7 +122,7 @@ async function runGenerator (context, plugin, pkg = getPkg(context)) {
if (!isTestOrDebug && depsChanged) {
log(`📦 Installing additional dependencies...`)
const packageManager =
loadOptions().packageManager || (hasYarn() ? 'yarn' : 'npm')
loadOptions().packageManager || (hasProjectYarn(context) ? 'yarn' : 'npm')
await installDeps(context, packageManager)
}
@@ -137,7 +137,7 @@ async function runGenerator (context, plugin, pkg = getPkg(context)) {
log()
log(` Successfully invoked generator for plugin: ${chalk.cyan(plugin.id)}`)
if (!process.env.VUE_CLI_TEST && hasGit()) {
if (!process.env.VUE_CLI_TEST && hasProjectGit(context)) {
const { stdout } = await execa('git', [
'ls-files',
'--exclude-standard',
@@ -157,14 +157,14 @@ async function runGenerator (context, plugin, pkg = getPkg(context)) {
)
)
log()
log(
` You should review these changes with ${chalk.cyan(
`git diff`
)} and commit them.`
)
log()
}
}
log(
` You should review these changes with ${chalk.cyan(
`git diff`
)} and commit them.`
)
log()
generator.printExitLogs()
}