-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
33 lines (26 loc) · 811 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
'use strict';
const semver = require('semver');
const fetch = require('node-fetch');
const url = require('url');
module.exports = pkg => {
if (typeof pkg === 'string') {
pkg = {
name: pkg
}
}
if (!pkg || !pkg.name) {
throw new Error('You must provide a package name');
}
pkg.version = pkg.version || '*';
if (!semver.validRange(pkg.version)) {
throw new Error('That is not a valid package version range');
}
pkg.registry = pkg.registry || 'http://registry.npmjs.com/';
return fetch(url.resolve(pkg.registry, pkg.name))
.then(response => response.json())
.then(data => {
let v = semver.maxSatisfying(Object.keys(data.versions || {}), pkg.version);
return (pkg.full) ? data.versions[v] : v;
})
.catch(err => { throw new Error(err); });
}