diff --git a/packages/@vue/cli-service/lib/PluginAPI.js b/packages/@vue/cli-service/lib/PluginAPI.js index 34232a054..25457b626 100644 --- a/packages/@vue/cli-service/lib/PluginAPI.js +++ b/packages/@vue/cli-service/lib/PluginAPI.js @@ -17,6 +17,13 @@ class PluginAPI { this.service = service } + /** + * Current working directory. + */ + getCwd () { + return this.service.context + } + /** * Resolve path for a project. * diff --git a/packages/@vue/cli-service/lib/commands/serve.js b/packages/@vue/cli-service/lib/commands/serve.js index c339d9791..759da33a7 100644 --- a/packages/@vue/cli-service/lib/commands/serve.js +++ b/packages/@vue/cli-service/lib/commands/serve.js @@ -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 { diff --git a/packages/@vue/cli-shared-utils/lib/env.js b/packages/@vue/cli-shared-utils/lib/env.js index 96c2aed11..7c14bde29 100644 --- a/packages/@vue/cli-shared-utils/lib/env.js +++ b/packages/@vue/cli-shared-utils/lib/env.js @@ -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 +} diff --git a/packages/@vue/cli-ui/apollo-server/connectors/dependencies.js b/packages/@vue/cli-ui/apollo-server/connectors/dependencies.js index e7c1494da..249ace300 100644 --- a/packages/@vue/cli-ui/apollo-server/connectors/dependencies.js +++ b/packages/@vue/cli-ui/apollo-server/connectors/dependencies.js @@ -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(' ')) diff --git a/packages/@vue/cli-ui/apollo-server/connectors/git.js b/packages/@vue/cli-ui/apollo-server/connectors/git.js index 3d72e7bc8..07011150e 100644 --- a/packages/@vue/cli-ui/apollo-server/connectors/git.js +++ b/packages/@vue/cli-ui/apollo-server/connectors/git.js @@ -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' diff --git a/packages/@vue/cli-ui/apollo-server/connectors/plugins.js b/packages/@vue/cli-ui/apollo-server/connectors/plugins.js index f88562928..ec34687b0 100644 --- a/packages/@vue/cli-ui/apollo-server/connectors/plugins.js +++ b/packages/@vue/cli-ui/apollo-server/connectors/plugins.js @@ -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(' ')) diff --git a/packages/@vue/cli-ui/apollo-server/util/command.js b/packages/@vue/cli-ui/apollo-server/util/command.js index aa94cbe3c..64e37dcef 100644 --- a/packages/@vue/cli-ui/apollo-server/util/command.js +++ b/packages/@vue/cli-ui/apollo-server/util/command.js @@ -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' } diff --git a/packages/@vue/cli-ui/locales/en.json b/packages/@vue/cli-ui/locales/en.json index 6323eb840..506c3196f 100644 --- a/packages/@vue/cli-ui/locales/en.json +++ b/packages/@vue/cli-ui/locales/en.json @@ -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", diff --git a/packages/@vue/cli/lib/Creator.js b/packages/@vue/cli/lib/Creator.js index cfa32e02f..1f7ed99fc 100644 --- a/packages/@vue/cli/lib/Creator.js +++ b/packages/@vue/cli/lib/Creator.js @@ -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) } } diff --git a/packages/@vue/cli/lib/add.js b/packages/@vue/cli/lib/add.js index 6ed310633..08f54b23a 100644 --- a/packages/@vue/cli/lib/add.js +++ b/packages/@vue/cli/lib/add.js @@ -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() diff --git a/packages/@vue/cli/lib/invoke.js b/packages/@vue/cli/lib/invoke.js index 1f93db647..bda2fdf91 100644 --- a/packages/@vue/cli/lib/invoke.js +++ b/packages/@vue/cli/lib/invoke.js @@ -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() }