refactor: use env variables to set registry for package managers (#5110)

also fixes compatibility with Yarn 2
This commit is contained in:
Haoqun Jiang
2020-01-27 20:03:50 +08:00
committed by GitHub
parent be06858bc6
commit b049e999d7

View File

@@ -131,11 +131,13 @@ class PackageManager {
return this._registry
}
async addRegistryToArgs (args) {
async setRegistryEnvs () {
const registry = await this.getRegistry()
args.push(`--registry=${registry}`)
return args
process.env.npm_config_registry = registry
process.env.YARN_NPM_REGISTRY_SERVER = registry
this.setBinaryMirrors()
}
// set mirror urls for users in china
@@ -224,20 +226,28 @@ class PackageManager {
}
}
async runCommand (args) {
await this.setRegistryEnvs()
await executeCommand(this.bin, args, this.context)
}
async install () {
await this.setBinaryMirrors()
const args = await this.addRegistryToArgs(PACKAGE_MANAGER_CONFIG[this.bin].install)
return executeCommand(this.bin, args, this.context)
return this.runCommand([PACKAGE_MANAGER_CONFIG[this.bin].install])
}
async add (packageName, isDev = true) {
await this.setBinaryMirrors()
const args = await this.addRegistryToArgs([
return this.runCommand([
...PACKAGE_MANAGER_CONFIG[this.bin].add,
packageName,
...(isDev ? ['-D'] : [])
])
return executeCommand(this.bin, args, this.context)
}
async remove (packageName) {
return this.runCommand([
...PACKAGE_MANAGER_CONFIG[this.bin].remove,
packageName
])
}
async upgrade (packageName) {
@@ -254,20 +264,10 @@ class PackageManager {
return
}
await this.setBinaryMirrors()
const args = await this.addRegistryToArgs([
return this.runCommand([
...PACKAGE_MANAGER_CONFIG[this.bin].add,
packageName
])
return executeCommand(this.bin, args, this.context)
}
async remove (packageName) {
const args = [
...PACKAGE_MANAGER_CONFIG[this.bin].remove,
packageName
]
return executeCommand(this.bin, args, this.context)
}
}