Skip to content

Commit

Permalink
refactor: make hasYarn & hasGit lazy to improve boot time
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 2, 2018
1 parent 3df1289 commit 9f25eed
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/@vue/cli-service/lib/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ module.exports = (api, options) => {
isFirstCompile = false

if (!isProduction) {
const buildCommand = hasYarn ? `yarn build` : `npm run build`
const buildCommand = hasYarn() ? `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 {
Expand Down
21 changes: 15 additions & 6 deletions packages/@vue/cli-shared-utils/lib/env.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
const { execSync } = require('child_process')

let _hasYarn
let _hasGit

// env detection
exports.hasYarn = (() => {
exports.hasYarn = () => {
if (process.env.VUE_CLI_TEST) {
return true
}
if (_hasYarn != null) {
return _hasYarn
}
try {
execSync('yarnpkg --version', { stdio: 'ignore' })
return true
return (_hasYarn = true)
} catch (e) {
return false
return (_hasYarn = false)
}
})()
}

exports.hasGit = () => {
if (process.env.VUE_CLI_TEST) {
return true
}
if (_hasGit != null) {
return _hasGit
}
try {
execSync('git --version', { stdio: 'ignore' })
return true
return (_hasGit = true)
} catch (e) {
return false
return (_hasGit = false)
}
}
10 changes: 5 additions & 5 deletions packages/@vue/cli/lib/Creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ module.exports = class Creator {
const packageManager = (
cliOptions.packageManager ||
loadOptions().packageManager ||
(hasYarn ? 'yarn' : 'npm')
(hasYarn() ? 'yarn' : 'npm')
)

await clearConsole()
Expand All @@ -112,7 +112,7 @@ module.exports = class Creator {

// intilaize git repository before installing deps
// so that vue-cli-service can setup git hooks.
if (hasGit) {
if (hasGit()) {
logWithSpinner(`πŸ—ƒ`, `Initializing git repository...`)
await run('git init')
}
Expand Down Expand Up @@ -156,7 +156,7 @@ module.exports = class Creator {
}

// commit initial state
if (hasGit) {
if (hasGit()) {
await run('git add -A')
if (isTestOrDebug) {
await run('git', ['config', 'user.name', 'test'])
Expand All @@ -179,7 +179,7 @@ module.exports = class Creator {

async promptAndResolvePreset () {
// prompt
await clearConsole()
await clearConsole(true)
const answers = await inquirer.prompt(this.resolveFinalPrompts())
debug('vue-cli:answers')(answers)

Expand Down Expand Up @@ -320,7 +320,7 @@ module.exports = class Creator {

// ask for packageManager once
const savedOptions = loadOptions()
if (hasYarn && !savedOptions.packageManager) {
if (!savedOptions.packageManager && hasYarn()) {
outroPrompts.push({
name: 'packageManager',
type: 'list',
Expand Down
4 changes: 2 additions & 2 deletions packages/@vue/cli/lib/invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async function invoke (pluginName, options) {

if (!isTestOrDebug && depsChanged) {
logWithSpinner('πŸ“¦', `Installing additional dependencies...`)
const packageManager = loadOptions().packageManager || (hasYarn ? 'yarn' : 'npm')
const packageManager = loadOptions().packageManager || (hasYarn() ? 'yarn' : 'npm')
await installDeps(context, packageManager)
}

Expand All @@ -93,7 +93,7 @@ async function invoke (pluginName, options) {

log()
log(` Successfully invoked generator for plugin: ${chalk.cyan(id)}`)
if (hasGit) {
if (hasGit()) {
const { stdout } = await execa('git', ['ls-files', '--exclude-standard', '--modified', '--others'])
log(` The following files have been updated / added:\n`)
log(chalk.red(stdout.split(/\r?\n/g).map(line => ` ${line}`).join('\n')))
Expand Down
2 changes: 1 addition & 1 deletion packages/@vue/cli/lib/promptModules/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = cli => {
value: 'save'
},
{
name: 'Lint and fix on commit' + (hasGit ? '' : chalk.red(' (requires Git)')),
name: 'Lint and fix on commit' + (hasGit() ? '' : chalk.red(' (requires Git)')),
value: 'commit'
}
]
Expand Down
4 changes: 2 additions & 2 deletions packages/@vue/cli/lib/util/clearConsole.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const semver = require('semver')
const getVersions = require('./getVersions')
const { clearConsole } = require('@vue/cli-shared-utils')

module.exports = async function clearConsoleWithTitle () {
module.exports = async function clearConsoleWithTitle (checkUpdate) {
const { current, latest } = await getVersions()

let title = chalk.bold.blue(`Vue CLI v${current}`)
Expand All @@ -14,7 +14,7 @@ module.exports = async function clearConsoleWithTitle () {
if (process.env.VUE_CLI_DEBUG) {
title += ' ' + chalk.magenta.bold('DEBUG')
}
if (semver.gt(latest, current)) {
if (checkUpdate && semver.gt(latest, current)) {
title += chalk.green(`
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€${`─`.repeat(latest.length)}─┐
β”‚ ✨ Update available: ${latest} ✨ β”‚
Expand Down
2 changes: 1 addition & 1 deletion packages/@vue/cli/lib/util/loadCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = function loadCommand (commandName, moduleName) {
if (isNotFoundError(err2)) {
const chalk = require('chalk')
const { hasYarn } = require('@vue/cli-shared-utils')
const installCommand = hasYarn ? `yarn global add` : `npm install -g`
const installCommand = hasYarn() ? `yarn global add` : `npm install -g`
console.log()
console.log(
` Command ${chalk.cyan(`vue ${commandName}`)} requires a global addon to be installed.\n` +
Expand Down

0 comments on commit 9f25eed

Please sign in to comment.