Skip to content
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

refactor: use a plain http request to get package metadata #5045

Merged
merged 2 commits into from Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/@vue/cli-shared-utils/lib/request.js
@@ -1,13 +1,14 @@
exports.request = {
get (uri) {
get (uri, opts) {
// lazy require
const request = require('request-promise-native')
const reqOpts = {
method: 'GET',
timeout: 30000,
resolveWithFullResponse: true,
json: true,
uri
uri,
...opts
}

return request(reqOpts)
Expand Down
28 changes: 17 additions & 11 deletions packages/@vue/cli/lib/util/ProjectPackageManager.js
Expand Up @@ -8,6 +8,7 @@ const {
chalk,
execa,
semver,
request,

hasYarn,
hasProjectYarn,
Expand All @@ -19,7 +20,8 @@ const {
resolvePluginId,

log,
warn
warn,
error
} = require('@vue/cli-shared-utils')

const { loadOptions } = require('../options')
Expand Down Expand Up @@ -174,7 +176,8 @@ class PackageManager {
}
}

async getMetadata (packageName, { field = '' } = {}) {
// https://github.com/npm/registry/blob/master/docs/responses/package-metadata.md
async getMetadata (packageName, { full = false } = {}) {
const registry = await this.getRegistry()

const metadataKey = `${this.bin}-${registry}-${packageName}`
Expand All @@ -184,17 +187,20 @@ class PackageManager {
return metadata
}

const args = await this.addRegistryToArgs(['info', packageName, field, '--json'])
const { stdout } = await execa(this.bin, args)

metadata = JSON.parse(stdout)
if (this.bin === 'yarn') {
// `yarn info` outputs messages in the form of `{"type": "inspect", data: {}}`
metadata = metadata.data
const headers = {}
if (!full) {
headers.Accept = 'application/vnd.npm.install-v1+json'
}

metadataCache.set(metadataKey, metadata)
return metadata
const url = `${registry}/${packageName}`
try {
metadata = (await request.get(url, { headers })).body
metadataCache.set(metadataKey, metadata)
return metadata
} catch (e) {
error(`Failed to get response from ${url}`)
throw e
}
}

async getRemoteVersion (packageName, versionRange = 'latest') {
Expand Down