Skip to content

Commit 1ae223d

Browse files
committed
refactor: refactor getVersions logic
1 parent fff62f7 commit 1ae223d

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed
+30-26
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,61 @@
11
const fs = require('fs-extra')
22
const path = require('path')
3+
const semver = require('semver')
34
const fsCachePath = path.resolve(__dirname, '.version')
45

6+
let sessionCached
7+
58
module.exports = async function getVersions () {
9+
if (sessionCached) {
10+
return sessionCached
11+
}
12+
613
let latest
7-
const current = require(`../../package.json`).version
14+
const local = require(`../../package.json`).version
815
if (process.env.VUE_CLI_TEST || process.env.VUE_CLI_DEBUG) {
9-
return {
10-
latest: current,
11-
current
12-
}
16+
return (sessionCached = {
17+
current: local,
18+
latest: local
19+
})
1320
}
1421

1522
if (fs.existsSync(fsCachePath)) {
16-
// if we haven't check for a new version in a week, force a full check
17-
// before proceeding.
23+
const cached = await fs.readFile(fsCachePath, 'utf-8')
1824
const lastChecked = (await fs.stat(fsCachePath)).mtimeMs
1925
const daysPassed = (Date.now() - lastChecked) / (60 * 60 * 1000 * 24)
20-
if (daysPassed > 7) {
21-
const cachedCurrent = await fs.readFile(fsCachePath, 'utf-8')
22-
await getAndCacheLatestVersion(cachedCurrent)
26+
if (daysPassed > 1) {
27+
// if we haven't check for a new version in a day, wait for the check
28+
// before proceeding
29+
latest = await getAndCacheLatestVersion(cached)
30+
} else {
31+
// Otherwise, do a check in the background. If the result was updated,
32+
// it will be used for the next 24 hours.
33+
getAndCacheLatestVersion(cached)
34+
latest = cached
2335
}
24-
latest = await fs.readFile(fsCachePath, 'utf-8')
2536
} else {
2637
// if the cache file doesn't exist, this is likely a fresh install
2738
// so no need to check
28-
latest = current
39+
latest = local
2940
}
3041

31-
// Do a check in the background. The cached file will be used for the next
32-
// startup within a week.
33-
getAndCacheLatestVersion(current)
34-
35-
return {
36-
current,
42+
return (sessionCached = {
43+
current: local,
3744
latest
38-
}
45+
})
3946
}
4047

4148
// fetch the latest version and save it on disk
4249
// so that it is available immediately next time
43-
let sentCheckRequest = false
44-
async function getAndCacheLatestVersion (current) {
45-
if (sentCheckRequest) {
46-
return
47-
}
48-
sentCheckRequest = true
50+
async function getAndCacheLatestVersion (cached) {
4951
const getPackageVersion = require('./getPackageVersion')
5052
const res = await getPackageVersion('vue-cli-version-marker', 'latest')
5153
if (res.statusCode === 200) {
5254
const { version } = res.body
53-
if (version !== current) {
55+
if (semver.valid(version) && version !== cached) {
5456
await fs.writeFile(fsCachePath, version)
57+
return version
5558
}
5659
}
60+
return cached
5761
}

0 commit comments

Comments
 (0)