-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ask for whether to use taobao registry when getting versions (#…
- Loading branch information
Showing
4 changed files
with
75 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const registries = { | ||
npm: 'https://registry.npmjs.org', | ||
yarn: 'https://registry.yarnpkg.com', | ||
taobao: 'https://registry.npm.taobao.org' | ||
} | ||
|
||
module.exports = registries |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const chalk = require('chalk') | ||
const execa = require('execa') | ||
const request = require('./request') | ||
const inquirer = require('inquirer') | ||
const registries = require('./registries') | ||
const { loadOptions, saveOptions } = require('../options') | ||
|
||
async function ping (registry) { | ||
await request.get(`${registry}/vue-cli-version-marker/latest`) | ||
return registry | ||
} | ||
|
||
function removeSlash (url) { | ||
return url.replace(/\/$/, '') | ||
} | ||
|
||
let checked | ||
let result | ||
|
||
module.exports = async function shouldUseTaobao () { | ||
// ensure this only gets called once. | ||
if (checked) return result | ||
checked = true | ||
|
||
// previously saved preference | ||
const saved = loadOptions().useTaobaoRegistry | ||
if (typeof saved === 'boolean') { | ||
return (result = saved) | ||
} | ||
|
||
const save = val => { | ||
result = val | ||
saveOptions({ useTaobaoRegistry: val }) | ||
return val | ||
} | ||
|
||
const userCurrent = (await execa(`npm`, ['config', 'get', 'registry'])).stdout | ||
const defaultRegistry = registries.npm | ||
if (removeSlash(userCurrent) !== removeSlash(defaultRegistry)) { | ||
// user has configured custom regsitry, respect that | ||
return save(false) | ||
} | ||
const faster = await Promise.race([ | ||
ping(defaultRegistry), | ||
ping(registries.taobao) | ||
]) | ||
|
||
if (faster !== registries.taobao) { | ||
// default is already faster | ||
return save(false) | ||
} | ||
|
||
// ask and save preference | ||
const { useTaobaoRegistry } = await inquirer.prompt([ | ||
{ | ||
name: 'useTaobaoRegistry', | ||
type: 'confirm', | ||
message: chalk.yellow( | ||
` Your connection to the the default npm registry seems to be slow.\n` + | ||
` Use ${chalk.cyan(registries.taobao)} for faster installation?` | ||
) | ||
} | ||
]) | ||
return save(useTaobaoRegistry) | ||
} |