Skip to content

Commit

Permalink
refactor: avoid version check affecting startup time
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 5, 2018
1 parent 4e92205 commit 1ddeefc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ packages/test
dist
temp
.vuerc
.version
3 changes: 2 additions & 1 deletion packages/@vue/cli/.npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
__tests__/
__mocks__/
__mocks__/
.version
1 change: 1 addition & 0 deletions packages/@vue/cli/lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ async function create (projectName, options) {
if (!action) {
return
} else if (action === 'overwrite') {
console.log(`\nRemoving ${chalk.cyan(targetDir)}...`)
await fs.remove(targetDir)
}
}
Expand Down
62 changes: 47 additions & 15 deletions packages/@vue/cli/lib/util/getVersions.js
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)
}
}
}

0 comments on commit 1ddeefc

Please sign in to comment.