1
- const { URL } = require ( 'url' )
2
- const https = require ( 'https' )
1
+ const axios = require ( 'axios' )
3
2
const chalk = require ( 'chalk' )
4
3
const execa = require ( 'execa' )
5
4
const readline = require ( 'readline' )
@@ -16,20 +15,18 @@ const registries = {
16
15
}
17
16
const taobaoDistURL = 'https://npm.taobao.org/dist'
18
17
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
+ }
29
26
30
27
let checked
31
28
let result
32
- const shouldUseTaobao = async ( ) => {
29
+ async function shouldUseTaobao ( ) {
33
30
// ensure this only gets called once.
34
31
if ( checked ) return result
35
32
checked = true
@@ -48,14 +45,15 @@ const shouldUseTaobao = async () => {
48
45
49
46
const userCurrent = ( await execa ( `npm` , [ 'config' , 'get' , 'registry' ] ) ) . stdout
50
47
const defaultRegistry = registries . npm
51
- if ( userCurrent !== defaultRegistry ) {
48
+ if ( removeSlash ( userCurrent ) !== removeSlash ( defaultRegistry ) ) {
52
49
// user has configured custom regsitry, respect that
53
50
return save ( false )
54
51
}
55
52
const faster = await Promise . race ( [
56
53
ping ( defaultRegistry ) ,
57
54
ping ( registries . taobao )
58
55
] )
56
+
59
57
if ( faster !== registries . taobao ) {
60
58
// default is already faster
61
59
return save ( false )
@@ -75,15 +73,15 @@ const shouldUseTaobao = async () => {
75
73
return save ( useTaobaoRegistry )
76
74
}
77
75
78
- const toStartOfLine = stream => {
76
+ function toStartOfLine ( stream ) {
79
77
if ( ! chalk . supportsColor ) {
80
78
stream . write ( '\r' )
81
79
return
82
80
}
83
81
readline . cursorTo ( stream , 0 )
84
82
}
85
83
86
- const renderProgressBar = ( curr , total ) => {
84
+ function renderProgressBar ( curr , total ) {
87
85
const ratio = Math . min ( Math . max ( curr / total , 0 ) , 1 )
88
86
const bar = ` ${ curr } /${ total } `
89
87
const availableSpace = Math . max ( 0 , process . stderr . columns - bar . length - 3 )
0 commit comments