@@ -31,7 +31,6 @@ const metadataCache = new LRU({
3131
3232const isTestOrDebug = process . env . VUE_CLI_TEST || process . env . VUE_CLI_DEBUG
3333
34- const TAOBAO_DIST_URL = 'https://npm.taobao.org/dist'
3534const SUPPORTED_PACKAGE_MANAGERS = [ 'yarn' , 'pnpm' , 'npm' ]
3635const PACKAGE_MANAGER_PNPM4_CONFIG = {
3736 install : [ 'install' , '--reporter' , 'silent' , '--shamefully-hoist' ] ,
@@ -114,8 +113,12 @@ class PackageManager {
114113 } else if ( await shouldUseTaobao ( this . bin ) ) {
115114 this . _registry = registries . taobao
116115 } else {
117- const { stdout } = await execa ( this . bin , [ 'config' , 'get' , 'registry' ] )
118- this . _registry = stdout
116+ try {
117+ this . _registry = ( await execa ( this . bin , [ 'config' , 'get' , 'registry' ] ) ) . stdout
118+ } catch ( e ) {
119+ // Yarn 2 uses `npmRegistryServer` instead of `registry`
120+ this . _registry = ( await execa ( this . bin , [ 'config' , 'get' , 'npmRegistryServer' ] ) ) . stdout
121+ }
119122 }
120123
121124 return this . _registry
@@ -125,12 +128,45 @@ class PackageManager {
125128 const registry = await this . getRegistry ( )
126129 args . push ( `--registry=${ registry } ` )
127130
128- if ( registry === registries . taobao ) {
129- // for node-gyp
130- process . env . NODEJS_ORG_MIRROR = TAOBAO_DIST_URL
131+ return args
132+ }
133+
134+ // set mirror urls for users in china
135+ async setBinaryMirrors ( ) {
136+ const registry = await this . getRegistry ( )
137+
138+ if ( registry !== registries . taobao ) {
139+ return
131140 }
132141
133- return args
142+ try {
143+ // node-sass, chromedriver, etc.
144+ const binaryMirrorConfig = await this . getMetadata ( 'binary-mirror-config' )
145+ const mirrors = binaryMirrorConfig . mirrors . china
146+ for ( const key in mirrors . ENVS ) {
147+ process . env [ key ] = mirrors . ENVS [ key ]
148+ }
149+
150+ // Cypress
151+ const cypressMirror = mirrors . cypress
152+ const defaultPlatforms = {
153+ darwin : 'osx64' ,
154+ linux : 'linux64' ,
155+ win32 : 'win64'
156+ }
157+ const platforms = cypressMirror . newPlatforms || defaultPlatforms
158+ const targetPlatform = platforms [ require ( 'os' ) . platform ( ) ]
159+ // Do not override user-defined env variable
160+ // Because we may construct a wrong download url and an escape hatch is necessary
161+ if ( targetPlatform && ! process . env . CYPRESS_INSTALL_BINARY ) {
162+ // We only support cypress 3 for the current major version
163+ const latestCypressVersion = await this . getRemoteVersion ( 'cypress' , '^3' )
164+ process . env . CYPRESS_INSTALL_BINARY =
165+ `${ cypressMirror . host } /${ latestCypressVersion } /${ targetPlatform } /cypress.zip`
166+ }
167+ } catch ( e ) {
168+ // get binary mirror config failed
169+ }
134170 }
135171
136172 async getMetadata ( packageName , { field = '' } = { } ) {
@@ -178,11 +214,13 @@ class PackageManager {
178214 }
179215
180216 async install ( ) {
217+ await this . setBinaryMirrors ( )
181218 const args = await this . addRegistryToArgs ( PACKAGE_MANAGER_CONFIG [ this . bin ] . install )
182219 return executeCommand ( this . bin , args , this . context )
183220 }
184221
185222 async add ( packageName , isDev = true ) {
223+ await this . setBinaryMirrors ( )
186224 const args = await this . addRegistryToArgs ( [
187225 ...PACKAGE_MANAGER_CONFIG [ this . bin ] . add ,
188226 packageName ,
@@ -205,6 +243,7 @@ class PackageManager {
205243 return
206244 }
207245
246+ await this . setBinaryMirrors ( )
208247 const args = await this . addRegistryToArgs ( [
209248 ...PACKAGE_MANAGER_CONFIG [ this . bin ] . add ,
210249 packageName
0 commit comments