1- const { URL } = require ( 'url' )
2- const https = require ( 'https' )
1+ const axios = require ( 'axios' )
32const chalk = require ( 'chalk' )
43const execa = require ( 'execa' )
54const readline = require ( 'readline' )
@@ -16,20 +15,18 @@ const registries = {
1615}
1716const 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
3027let checked
3128let 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