|
1 | 1 | const fs = require('fs-extra')
|
2 | 2 | const path = require('path')
|
| 3 | +const semver = require('semver') |
3 | 4 | const fsCachePath = path.resolve(__dirname, '.version')
|
4 | 5 |
|
| 6 | +let sessionCached |
| 7 | + |
5 | 8 | module.exports = async function getVersions () {
|
| 9 | + if (sessionCached) { |
| 10 | + return sessionCached |
| 11 | + } |
| 12 | + |
6 | 13 | let latest
|
7 |
| - const current = require(`../../package.json`).version |
| 14 | + const local = require(`../../package.json`).version |
8 | 15 | 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 | + }) |
13 | 20 | }
|
14 | 21 |
|
15 | 22 | 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') |
18 | 24 | const lastChecked = (await fs.stat(fsCachePath)).mtimeMs
|
19 | 25 | 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 |
23 | 35 | }
|
24 |
| - latest = await fs.readFile(fsCachePath, 'utf-8') |
25 | 36 | } else {
|
26 | 37 | // if the cache file doesn't exist, this is likely a fresh install
|
27 | 38 | // so no need to check
|
28 |
| - latest = current |
| 39 | + latest = local |
29 | 40 | }
|
30 | 41 |
|
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, |
37 | 44 | latest
|
38 |
| - } |
| 45 | + }) |
39 | 46 | }
|
40 | 47 |
|
41 | 48 | // fetch the latest version and save it on disk
|
42 | 49 | // 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) { |
49 | 51 | const getPackageVersion = require('./getPackageVersion')
|
50 | 52 | const res = await getPackageVersion('vue-cli-version-marker', 'latest')
|
51 | 53 | if (res.statusCode === 200) {
|
52 | 54 | const { version } = res.body
|
53 |
| - if (version !== current) { |
| 55 | + if (semver.valid(version) && version !== cached) { |
54 | 56 | await fs.writeFile(fsCachePath, version)
|
| 57 | + return version |
55 | 58 | }
|
56 | 59 | }
|
| 60 | + return cached |
57 | 61 | }
|
0 commit comments