Skip to content

Commit f7d273d

Browse files
committed
refactor: use axios for registry ping
1 parent 8a29cda commit f7d273d

File tree

3 files changed

+126
-238
lines changed

3 files changed

+126
-238
lines changed

packages/@vue/cli-service/lib/commands/build/formatStats.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = function formatStats (stats, dir, api) {
22
const fs = require('fs')
33
const zlib = require('zlib')
44
const chalk = require('chalk')
5-
const ui = require('cliui')({ width: 60 })
5+
const ui = require('cliui')({ width: 80 })
66

77
const json = stats.toJson({
88
hash: false,

packages/@vue/cli/lib/util/installDeps.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
const { URL } = require('url')
2-
const https = require('https')
1+
const axios = require('axios')
32
const chalk = require('chalk')
43
const execa = require('execa')
54
const readline = require('readline')
@@ -16,20 +15,18 @@ const registries = {
1615
}
1716
const taobaoDistURL = 'https://npm.taobao.org/dist'
1817

19-
const ping = url => new Promise((resolve, reject) => {
20-
const req = https.request({
21-
hostname: new URL(url).hostname,
22-
path: '/vue/latest'
23-
}, () => {
24-
resolve(url)
25-
})
26-
req.on('error', reject)
27-
req.end()
28-
})
18+
async function ping (registry) {
19+
await axios.get(`${registry}/vue-cli-version-marker/latest`)
20+
return registry
21+
}
22+
23+
function removeSlash (url) {
24+
return url.replace(/\/$/, '')
25+
}
2926

3027
let checked
3128
let result
32-
const shouldUseTaobao = async () => {
29+
async function shouldUseTaobao () {
3330
// ensure this only gets called once.
3431
if (checked) return result
3532
checked = true
@@ -48,14 +45,15 @@ const shouldUseTaobao = async () => {
4845

4946
const userCurrent = (await execa(`npm`, ['config', 'get', 'registry'])).stdout
5047
const defaultRegistry = registries.npm
51-
if (userCurrent !== defaultRegistry) {
48+
if (removeSlash(userCurrent) !== removeSlash(defaultRegistry)) {
5249
// user has configured custom regsitry, respect that
5350
return save(false)
5451
}
5552
const faster = await Promise.race([
5653
ping(defaultRegistry),
5754
ping(registries.taobao)
5855
])
56+
5957
if (faster !== registries.taobao) {
6058
// default is already faster
6159
return save(false)
@@ -75,15 +73,15 @@ const shouldUseTaobao = async () => {
7573
return save(useTaobaoRegistry)
7674
}
7775

78-
const toStartOfLine = stream => {
76+
function toStartOfLine (stream) {
7977
if (!chalk.supportsColor) {
8078
stream.write('\r')
8179
return
8280
}
8381
readline.cursorTo(stream, 0)
8482
}
8583

86-
const renderProgressBar = (curr, total) => {
84+
function renderProgressBar (curr, total) {
8785
const ratio = Math.min(Math.max(curr / total, 0), 1)
8886
const bar = ` ${curr}/${total}`
8987
const availableSpace = Math.max(0, process.stderr.columns - bar.length - 3)

0 commit comments

Comments
 (0)