package-json
Advanced tools
Comparing version 2.4.0 to 3.0.0
39
index.js
'use strict'; | ||
var url = require('url'); | ||
var got = require('got'); | ||
var registryUrl = require('registry-url'); | ||
var registryAuthToken = require('registry-auth-token'); | ||
var semver = require('semver'); | ||
const url = require('url'); | ||
const got = require('got'); | ||
const registryUrl = require('registry-url'); | ||
const registryAuthToken = require('registry-auth-token'); | ||
const semver = require('semver'); | ||
module.exports = function (name, version) { | ||
var scope = name.split('/')[0]; | ||
var regUrl = registryUrl(scope); | ||
var pkgUrl = url.resolve(regUrl, encodeURIComponent(name).replace(/^%40/, '@')); | ||
var authInfo = registryAuthToken(regUrl); | ||
var headers = {}; | ||
module.exports = (name, version) => { | ||
const scope = name.split('/')[0]; | ||
const regUrl = registryUrl(scope); | ||
const pkgUrl = url.resolve(regUrl, encodeURIComponent(name).replace(/^%40/, '@')); | ||
const authInfo = registryAuthToken(regUrl); | ||
const headers = {}; | ||
if (authInfo) { | ||
headers.authorization = authInfo.type + ' ' + authInfo.token; | ||
headers.authorization = `${authInfo.type} ${authInfo.token}`; | ||
} | ||
return got(pkgUrl, { | ||
json: true, | ||
headers: headers | ||
}) | ||
.then(function (res) { | ||
var data = res.body; | ||
return got(pkgUrl, {json: true, headers}) | ||
.then(res => { | ||
let data = res.body; | ||
@@ -30,3 +27,3 @@ if (version === 'latest') { | ||
if (!data.versions[version]) { | ||
var versions = Object.keys(data.versions); | ||
const versions = Object.keys(data.versions); | ||
version = semver.maxSatisfying(versions, version); | ||
@@ -48,5 +45,5 @@ | ||
}) | ||
.catch(function (err) { | ||
.catch(err => { | ||
if (err.statusCode === 404) { | ||
throw new Error('Package `' + name + '` doesn\'t exist'); | ||
throw new Error(`Package \`${name}\` doesn't exist`); | ||
} | ||
@@ -53,0 +50,0 @@ |
{ | ||
"name": "package-json", | ||
"version": "2.4.0", | ||
"version": "3.0.0", | ||
"description": "Get the package.json of a package from the npm registry", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=4" | ||
}, | ||
@@ -34,3 +34,3 @@ "scripts": { | ||
"dependencies": { | ||
"got": "^5.0.0", | ||
"got": "^6.7.1", | ||
"registry-auth-token": "^3.0.1", | ||
@@ -37,0 +37,0 @@ "registry-url": "^3.0.3", |
@@ -23,3 +23,3 @@ # package-json [![Build Status](https://travis-ci.org/sindresorhus/package-json.svg?branch=master)](https://travis-ci.org/sindresorhus/package-json) | ||
// also works with scoped packages | ||
// Also works with scoped packages | ||
packageJson('@company/package', 'latest').then(json => { | ||
@@ -36,4 +36,3 @@ console.log(json); | ||
You can optionally specify a version (e.g. `1.0.0`) or `latest`. | ||
If you don't specify a version you'll get the [main entry](http://registry.npmjs.org/pageres/) containing all versions. | ||
You can optionally specify a version (e.g. `1.0.0`) or `latest`. If you don't specify a version you'll get the [main entry](http://registry.npmjs.org/pageres/) containing all versions. | ||
@@ -65,2 +64,2 @@ The version can also be in any format supported by the [semver](https://www.npmjs.com/package/semver) module. For example: | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
5153
42
63
+ Addedduplexer3@0.1.5(transitive)
+ Addedget-stream@3.0.0(transitive)
+ Addedgot@6.7.1(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedtimed-out@4.0.1(transitive)
+ Addedunzip-response@2.0.1(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removedduplexer2@0.1.4(transitive)
- Removederror-ex@1.3.2(transitive)
- Removedgot@5.7.1(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-arrayish@0.2.1(transitive)
- Removedisarray@1.0.0(transitive)
- Removednode-status-codes@1.0.0(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedparse-json@2.2.0(transitive)
- Removedpinkie@2.0.4(transitive)
- Removedpinkie-promise@2.0.1(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedread-all-stream@3.1.0(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedtimed-out@3.1.3(transitive)
- Removedunzip-response@1.0.2(transitive)
- Removedutil-deprecate@1.0.2(transitive)
Updatedgot@^6.7.1