forked from vuejs/vue-cli
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: avoid version check affecting startup time
- Loading branch information
Showing
4 changed files
with
51 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ packages/test | |
dist | ||
temp | ||
.vuerc | ||
.version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
__tests__/ | ||
__mocks__/ | ||
__mocks__/ | ||
.version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,56 @@ | ||
const fs = require('fs-extra') | ||
const path = require('path') | ||
const fsCachePath = path.resolve(__dirname, '.version') | ||
|
||
module.exports = async function getVersions () { | ||
const current = require(`../../package.json`).version | ||
let latest | ||
if (process.env.VUE_CLI_LATEST_VERSION) { | ||
// cached value | ||
latest = process.env.VUE_CLI_LATEST_VERSION | ||
} else if (process.env.VUE_CLI_TEST || process.env.VUE_CLI_DEBUG) { | ||
// test/debug, use local version | ||
latest = process.env.VUE_CLI_LATEST_VERSION = current | ||
} else { | ||
const getPackageVersion = require('./getPackageVersion') | ||
const res = await getPackageVersion('vue-cli-version-marker', 'latest') | ||
if (res.statusCode === 200) { | ||
latest = process.env.VUE_CLI_LATEST_VERSION = res.body.version | ||
} else { | ||
// fallback to local version | ||
latest = process.env.VUE_CLI_LATEST_VERSION = current | ||
const current = require(`../../package.json`).version | ||
if (process.env.VUE_CLI_TEST || process.env.VUE_CLI_DEBUG) { | ||
return { | ||
latest: current, | ||
current | ||
} | ||
} | ||
|
||
if (fs.existsSync(fsCachePath)) { | ||
// if we haven't check for a new version in a week, force a full check | ||
// before proceeding. | ||
const lastChecked = (await fs.stat(fsCachePath)).mtimeMs | ||
const daysPassed = (Date.now() - lastChecked) / (60 * 60 * 1000 * 24) | ||
if (daysPassed > 7) { | ||
await getAndCacheLatestVersion(current) | ||
} | ||
latest = await fs.readFile(fsCachePath, 'utf-8') | ||
} else { | ||
// if the cache file doesn't exist, this is likely a fresh install | ||
// so no need to check | ||
latest = current | ||
} | ||
|
||
// Do a check in the background. The cached file will be used for the next | ||
// startup within a week. | ||
getAndCacheLatestVersion(current) | ||
|
||
return { | ||
current, | ||
latest | ||
} | ||
} | ||
|
||
// fetch the latest version and save it on disk | ||
// so that it is available immediately next time | ||
let sentCheckRequest = false | ||
async function getAndCacheLatestVersion (current) { | ||
if (sentCheckRequest) { | ||
return | ||
} | ||
sentCheckRequest = true | ||
const getPackageVersion = require('./getPackageVersion') | ||
const res = await getPackageVersion('vue-cli-version-marker', 'latest') | ||
if (res.statusCode === 200) { | ||
const { version } = res.body | ||
if (version !== current) { | ||
await fs.writeFile(fsCachePath, version) | ||
} | ||
} | ||
} |