Skip to content

feat(cli)!: make the useTaobaoRegistry behavior more explicit #6095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/@vue/cli/lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const schema = createSchema(joi => joi.object().keys({
latestVersion: joi.string().regex(/^\d+\.\d+\.\d+(-(alpha|beta|rc)\.\d+)?$/),
lastChecked: joi.date().timestamp(),
packageManager: joi.string().valid('yarn', 'npm', 'pnpm'),
useTaobaoRegistry: joi.boolean(),
presets: joi.object().pattern(/^/, presetSchema)
}))

Expand All @@ -78,7 +77,6 @@ exports.defaults = {
latestVersion: undefined,

packageManager: undefined,
useTaobaoRegistry: undefined,
presets: {
'default': Object.assign({ vueVersion: '2' }, exports.defaultPreset),
'__default_vue_3__': Object.assign({ vueVersion: '3' }, exports.defaultPreset)
Expand Down
2 changes: 1 addition & 1 deletion packages/@vue/cli/lib/util/ProjectPackageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class PackageManager {
let registry
if (args.registry) {
registry = args.registry
} else if (!process.env.VUE_CLI_TEST && await shouldUseTaobao(this.bin)) {
} else if (!process.env.VUE_CLI_TEST && await shouldUseTaobao(this.bin, this.context)) {
registry = registries.taobao
} else {
try {
Expand Down
37 changes: 37 additions & 0 deletions packages/@vue/cli/lib/util/projectRegistry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const fs = require('fs-extra')
const path = require('path')
const ini = require('ini')

exports.getProjectRegistry = (context) => {
const npmrcPaths = path.resolve(context, '.npmrc')

let registry

if (fs.existsSync(npmrcPaths)) {
try {
registry = ini.parse(fs.readFileSync(npmrcPaths, 'utf-8')).registry
} catch (e) {
// in case of file permission issues, etc.
}
}

return registry
}

exports.setProjectRegistry = (context, registry) => {
const npmrcPaths = path.resolve(context, '.npmrc')

let npmConfig = {
registry
}

if (fs.existsSync(npmrcPaths)) {
try {
npmConfig = Object.assign({}, ini.parse(fs.readFileSync(npmrcPaths, 'utf-8')), npmConfig)
} catch (e) {
// in case of file permission issues, etc.
}
}

fs.outputFileSync(npmrcPaths, ini.stringify(npmConfig))
}
45 changes: 13 additions & 32 deletions packages/@vue/cli/lib/util/shouldUseTaobao.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
const {
chalk,
execa,
request,

hasYarn
} = require('@vue/cli-shared-utils')
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
}
const { getProjectRegistry, setProjectRegistry } = require('./projectRegistry')

function removeSlash (url) {
return url.replace(/\/$/, '')
Expand All @@ -21,7 +14,7 @@ function removeSlash (url) {
let checked
let result

module.exports = async function shouldUseTaobao (command) {
module.exports = async function shouldUseTaobao (command, context) {
if (!command) {
command = hasYarn() ? 'yarn' : 'npm'
}
Expand All @@ -31,14 +24,19 @@ module.exports = async function shouldUseTaobao (command) {
checked = true

// previously saved preference
const saved = loadOptions().useTaobaoRegistry
if (typeof saved === 'boolean') {
return (result = saved)
const saved = getProjectRegistry(context)
if (saved) {
result = saved === registries.taobao
return result
}

const save = val => {
result = val
saveOptions({ useTaobaoRegistry: val })

if (val === true) {
setProjectRegistry(context, registries.taobao)
}

return val
}

Expand All @@ -60,21 +58,6 @@ module.exports = async function shouldUseTaobao (command) {
return save(false)
}

let faster
try {
faster = await Promise.race([
ping(defaultRegistry),
ping(registries.taobao)
])
} catch (e) {
return save(false)
}

if (faster !== registries.taobao) {
// default is already faster
return save(false)
}

if (process.env.VUE_CLI_API_MODE) {
return save(true)
}
Expand All @@ -84,10 +67,8 @@ module.exports = async function shouldUseTaobao (command) {
{
name: 'useTaobaoRegistry',
type: 'confirm',
message: chalk.yellow(
` Your connection to the default ${command} registry seems to be slow.\n` +
` Use ${chalk.cyan(registries.taobao)} for faster installation?`
)
default: false,
message: `Use ${chalk.cyan(registries.taobao)} for faster installation? (Recommend to Chinese users)`
}
])
return save(useTaobaoRegistry)
Expand Down