1
- const fs = require ( 'fs-extra' )
2
- const path = require ( 'path' )
3
- const fsCachePath = path . resolve ( __dirname , '.versions' )
1
+ const semver = require ( 'semver' )
2
+ const { loadOptions, saveOptions } = require ( '../options' )
4
3
5
4
let sessionCached
6
5
@@ -10,31 +9,26 @@ module.exports = async function getVersions () {
10
9
}
11
10
12
11
let latest
13
- const local = require ( 'vue-cli-version-marker' ) . devDependencies
12
+ const local = require ( `../../package.json` ) . version
14
13
if ( process . env . VUE_CLI_TEST || process . env . VUE_CLI_DEBUG ) {
15
14
return ( sessionCached = {
16
15
current : local ,
17
16
latest : local
18
17
} )
19
18
}
20
19
21
- if ( ! fs . existsSync ( fsCachePath ) ) {
22
- // if the cache file doesn't exist, this is likely a fresh install
23
- // then create a cache file with the bundled version map
24
- await fs . writeFile ( fsCachePath , JSON . stringify ( local ) )
25
- }
26
-
27
- const cached = JSON . parse ( await fs . readFile ( fsCachePath , 'utf-8' ) )
28
- const lastChecked = ( await fs . stat ( fsCachePath ) ) . mtimeMs
20
+ const { latestVersion, lastChecked } = loadOptions ( )
21
+ const cached = latestVersion
29
22
const daysPassed = ( Date . now ( ) - lastChecked ) / ( 60 * 60 * 1000 * 24 )
23
+
30
24
if ( daysPassed > 1 ) {
31
25
// if we haven't check for a new version in a day, wait for the check
32
26
// before proceeding
33
- latest = await getAndCacheLatestVersions ( cached )
27
+ latest = await getAndCacheLatestVersion ( cached )
34
28
} else {
35
29
// Otherwise, do a check in the background. If the result was updated,
36
30
// it will be used for the next 24 hours.
37
- getAndCacheLatestVersions ( cached )
31
+ getAndCacheLatestVersion ( cached )
38
32
latest = cached
39
33
}
40
34
@@ -46,13 +40,15 @@ module.exports = async function getVersions () {
46
40
47
41
// fetch the latest version and save it on disk
48
42
// so that it is available immediately next time
49
- async function getAndCacheLatestVersions ( cached ) {
43
+ async function getAndCacheLatestVersion ( cached ) {
50
44
const getPackageVersion = require ( './getPackageVersion' )
51
45
const res = await getPackageVersion ( 'vue-cli-version-marker' , 'latest' )
52
46
if ( res . statusCode === 200 ) {
53
- const versions = res . body . devDependencies
54
- await fs . writeFile ( fsCachePath , JSON . stringify ( versions ) )
55
- return versions
47
+ const { version } = res . body
48
+ if ( semver . valid ( version ) && version !== cached ) {
49
+ saveOptions ( { lastestVersion : version , lastChecked : Date . now ( ) } )
50
+ return version
51
+ }
56
52
}
57
53
return cached
58
54
}
0 commit comments