Skip to content

Commit 9f25eed

Browse files
committed
refactor: make hasYarn & hasGit lazy to improve boot time
1 parent 3df1289 commit 9f25eed

File tree

7 files changed

+27
-18
lines changed

7 files changed

+27
-18
lines changed

β€Žpackages/@vue/cli-service/lib/commands/serve.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ module.exports = (api, options) => {
9797
isFirstCompile = false
9898

9999
if (!isProduction) {
100-
const buildCommand = hasYarn ? `yarn build` : `npm run build`
100+
const buildCommand = hasYarn() ? `yarn build` : `npm run build`
101101
console.log(` Note that the development build is not optimized.`)
102102
console.log(` To create a production build, run ${chalk.cyan(buildCommand)}.`)
103103
} else {
+15-6
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
11
const { execSync } = require('child_process')
22

3+
let _hasYarn
4+
let _hasGit
5+
36
// env detection
4-
exports.hasYarn = (() => {
7+
exports.hasYarn = () => {
58
if (process.env.VUE_CLI_TEST) {
69
return true
710
}
11+
if (_hasYarn != null) {
12+
return _hasYarn
13+
}
814
try {
915
execSync('yarnpkg --version', { stdio: 'ignore' })
10-
return true
16+
return (_hasYarn = true)
1117
} catch (e) {
12-
return false
18+
return (_hasYarn = false)
1319
}
14-
})()
20+
}
1521

1622
exports.hasGit = () => {
1723
if (process.env.VUE_CLI_TEST) {
1824
return true
1925
}
26+
if (_hasGit != null) {
27+
return _hasGit
28+
}
2029
try {
2130
execSync('git --version', { stdio: 'ignore' })
22-
return true
31+
return (_hasGit = true)
2332
} catch (e) {
24-
return false
33+
return (_hasGit = false)
2534
}
2635
}

β€Žpackages/@vue/cli/lib/Creator.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ module.exports = class Creator {
8686
const packageManager = (
8787
cliOptions.packageManager ||
8888
loadOptions().packageManager ||
89-
(hasYarn ? 'yarn' : 'npm')
89+
(hasYarn() ? 'yarn' : 'npm')
9090
)
9191

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

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

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

180180
async promptAndResolvePreset () {
181181
// prompt
182-
await clearConsole()
182+
await clearConsole(true)
183183
const answers = await inquirer.prompt(this.resolveFinalPrompts())
184184
debug('vue-cli:answers')(answers)
185185

@@ -320,7 +320,7 @@ module.exports = class Creator {
320320

321321
// ask for packageManager once
322322
const savedOptions = loadOptions()
323-
if (hasYarn && !savedOptions.packageManager) {
323+
if (!savedOptions.packageManager && hasYarn()) {
324324
outroPrompts.push({
325325
name: 'packageManager',
326326
type: 'list',

β€Žpackages/@vue/cli/lib/invoke.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ async function invoke (pluginName, options) {
7878

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

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

9494
log()
9595
log(` Successfully invoked generator for plugin: ${chalk.cyan(id)}`)
96-
if (hasGit) {
96+
if (hasGit()) {
9797
const { stdout } = await execa('git', ['ls-files', '--exclude-standard', '--modified', '--others'])
9898
log(` The following files have been updated / added:\n`)
9999
log(chalk.red(stdout.split(/\r?\n/g).map(line => ` ${line}`).join('\n')))

β€Žpackages/@vue/cli/lib/promptModules/linter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module.exports = cli => {
5151
value: 'save'
5252
},
5353
{
54-
name: 'Lint and fix on commit' + (hasGit ? '' : chalk.red(' (requires Git)')),
54+
name: 'Lint and fix on commit' + (hasGit() ? '' : chalk.red(' (requires Git)')),
5555
value: 'commit'
5656
}
5757
]

β€Žpackages/@vue/cli/lib/util/clearConsole.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const semver = require('semver')
33
const getVersions = require('./getVersions')
44
const { clearConsole } = require('@vue/cli-shared-utils')
55

6-
module.exports = async function clearConsoleWithTitle () {
6+
module.exports = async function clearConsoleWithTitle (checkUpdate) {
77
const { current, latest } = await getVersions()
88

99
let title = chalk.bold.blue(`Vue CLI v${current}`)
@@ -14,7 +14,7 @@ module.exports = async function clearConsoleWithTitle () {
1414
if (process.env.VUE_CLI_DEBUG) {
1515
title += ' ' + chalk.magenta.bold('DEBUG')
1616
}
17-
if (semver.gt(latest, current)) {
17+
if (checkUpdate && semver.gt(latest, current)) {
1818
title += chalk.green(`
1919
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€${`─`.repeat(latest.length)}─┐
2020
β”‚ ✨ Update available: ${latest} ✨ β”‚

β€Žpackages/@vue/cli/lib/util/loadCommand.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = function loadCommand (commandName, moduleName) {
1212
if (isNotFoundError(err2)) {
1313
const chalk = require('chalk')
1414
const { hasYarn } = require('@vue/cli-shared-utils')
15-
const installCommand = hasYarn ? `yarn global add` : `npm install -g`
15+
const installCommand = hasYarn() ? `yarn global add` : `npm install -g`
1616
console.log()
1717
console.log(
1818
` Command ${chalk.cyan(`vue ${commandName}`)} requires a global addon to be installed.\n` +

0 commit comments

Comments
Β (0)