package-json
Advanced tools
Comparing version 1.1.0 to 1.2.0
37
index.js
@@ -5,4 +5,20 @@ 'use strict'; | ||
function get(url, cb) { | ||
got(url, {json: true}, function (err, data) { | ||
if (err && err.code === 404) { | ||
cb(new Error('Package or version doesn\'t exist')); | ||
return; | ||
} | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
cb(null, data); | ||
}); | ||
} | ||
module.exports = function (name, version, cb) { | ||
var url = registryUrl(name.split('/')[0]); | ||
var url = registryUrl(name.split('/')[0]) + name + '/'; | ||
@@ -14,8 +30,10 @@ if (typeof version !== 'string') { | ||
got(url + encodeURIComponent(name) + '/' + version, function (err, data) { | ||
if (err && err.code === 404) { | ||
cb(new Error('Package or version doesn\'t exist')); | ||
return; | ||
} | ||
get(url + version, cb); | ||
}; | ||
module.exports.field = function (name, field, cb) { | ||
var url = registryUrl(name.split('/')[0]) + | ||
'-/by-field/?key=%22' + name + '%22&field=' + field; | ||
get(url, function (err, res) { | ||
if (err) { | ||
@@ -26,4 +44,9 @@ cb(err); | ||
cb(null, JSON.parse(data)); | ||
if (Object.keys(res).length === 0) { | ||
cb(new Error('Field `' + field + '` doesn\'t exist')); | ||
return; | ||
} | ||
cb(null, res[name][field]); | ||
}); | ||
}; |
{ | ||
"name": "package-json", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Get the package.json of a package from the npm registry", | ||
@@ -33,3 +33,3 @@ "license": "MIT", | ||
"dependencies": { | ||
"got": "^2.4.0", | ||
"got": "^3.2.0", | ||
"registry-url": "^3.0.0" | ||
@@ -36,0 +36,0 @@ }, |
@@ -28,2 +28,7 @@ # package-json [![Build Status](https://travis-ci.org/sindresorhus/package-json.svg?branch=master)](https://travis-ci.org/sindresorhus/package-json) | ||
}); | ||
packageJson.field('pageres', 'description', function (err, field) { | ||
console.log(field); | ||
//=> 'Capture website screenshots' | ||
}); | ||
``` | ||
@@ -39,6 +44,12 @@ | ||
### packageJson.field(name, field, callback) | ||
Get a specific field in the `package.json`. | ||
## Related | ||
- [`npm-keyword`](https://github.com/sindresorhus/npm-keyword) - Get a list of npm packages with a certain keyword | ||
- [npm-keyword](https://github.com/sindresorhus/npm-keyword) - Get a list of npm packages with a certain keyword | ||
- [npm-user](https://github.com/sindresorhus/npm-user) - Get user info of a npm user | ||
- [npm-email](https://github.com/sindresorhus/npm-email) - Get the email of a npm user | ||
@@ -45,0 +56,0 @@ |
4140
39
58
+ Addedgot@3.3.1(transitive)
+ Addedis-redirect@1.0.0(transitive)
+ Addedobject-assign@3.0.0(transitive)
+ Addedpinkie@2.0.4(transitive)
+ Addedpinkie-promise@2.0.1(transitive)
+ Addedread-all-stream@3.1.0(transitive)
- Removedgot@2.9.2(transitive)
- Removedobject-assign@2.1.1(transitive)
- Removedread-all-stream@2.2.0(transitive)
- Removedstatuses@1.5.0(transitive)
Updatedgot@^3.2.0