Skip to content

Commit 1ddeefc

Browse files
committed
refactor: avoid version check affecting startup time
1 parent 4e92205 commit 1ddeefc

File tree

4 files changed

+51
-16
lines changed

4 files changed

+51
-16
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ packages/test
66
dist
77
temp
88
.vuerc
9+
.version

packages/@vue/cli/.npmignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
__tests__/
2-
__mocks__/
2+
__mocks__/
3+
.version

packages/@vue/cli/lib/create.js

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ async function create (projectName, options) {
5858
if (!action) {
5959
return
6060
} else if (action === 'overwrite') {
61+
console.log(`\nRemoving ${chalk.cyan(targetDir)}...`)
6162
await fs.remove(targetDir)
6263
}
6364
}
+47-15
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,56 @@
1+
const fs = require('fs-extra')
2+
const path = require('path')
3+
const fsCachePath = path.resolve(__dirname, '.version')
4+
15
module.exports = async function getVersions () {
2-
const current = require(`../../package.json`).version
36
let latest
4-
if (process.env.VUE_CLI_LATEST_VERSION) {
5-
// cached value
6-
latest = process.env.VUE_CLI_LATEST_VERSION
7-
} else if (process.env.VUE_CLI_TEST || process.env.VUE_CLI_DEBUG) {
8-
// test/debug, use local version
9-
latest = process.env.VUE_CLI_LATEST_VERSION = current
10-
} else {
11-
const getPackageVersion = require('./getPackageVersion')
12-
const res = await getPackageVersion('vue-cli-version-marker', 'latest')
13-
if (res.statusCode === 200) {
14-
latest = process.env.VUE_CLI_LATEST_VERSION = res.body.version
15-
} else {
16-
// fallback to local version
17-
latest = process.env.VUE_CLI_LATEST_VERSION = current
7+
const current = require(`../../package.json`).version
8+
if (process.env.VUE_CLI_TEST || process.env.VUE_CLI_DEBUG) {
9+
return {
10+
latest: current,
11+
current
12+
}
13+
}
14+
15+
if (fs.existsSync(fsCachePath)) {
16+
// if we haven't check for a new version in a week, force a full check
17+
// before proceeding.
18+
const lastChecked = (await fs.stat(fsCachePath)).mtimeMs
19+
const daysPassed = (Date.now() - lastChecked) / (60 * 60 * 1000 * 24)
20+
if (daysPassed > 7) {
21+
await getAndCacheLatestVersion(current)
1822
}
23+
latest = await fs.readFile(fsCachePath, 'utf-8')
24+
} else {
25+
// if the cache file doesn't exist, this is likely a fresh install
26+
// so no need to check
27+
latest = current
1928
}
29+
30+
// Do a check in the background. The cached file will be used for the next
31+
// startup within a week.
32+
getAndCacheLatestVersion(current)
33+
2034
return {
2135
current,
2236
latest
2337
}
2438
}
39+
40+
// fetch the latest version and save it on disk
41+
// so that it is available immediately next time
42+
let sentCheckRequest = false
43+
async function getAndCacheLatestVersion (current) {
44+
if (sentCheckRequest) {
45+
return
46+
}
47+
sentCheckRequest = true
48+
const getPackageVersion = require('./getPackageVersion')
49+
const res = await getPackageVersion('vue-cli-version-marker', 'latest')
50+
if (res.statusCode === 200) {
51+
const { version } = res.body
52+
if (version !== current) {
53+
await fs.writeFile(fsCachePath, version)
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)