1 | 'use strict';
|
2 |
|
3 | const _ = require('lodash');
|
4 | const bestGuessHomepage = require('./best-guess-homepage');
|
5 | const semver = require('semver');
|
6 | const packageJson = require('package-json');
|
7 | const cpuCount = require('os').cpus().length;
|
8 | const throat = require('throat')(cpuCount);
|
9 |
|
10 | function getNpmInfo(packageName) {
|
11 | return throat(() => packageJson(packageName, { fullMetadata: true, allVersions: true }))
|
12 | .then(rawData => {
|
13 | const CRAZY_HIGH_SEMVER = '8000.0.0';
|
14 |
|
15 | const sortedVersions = _(rawData.versions)
|
16 | .keys()
|
17 | .remove(_.partial(semver.gt, CRAZY_HIGH_SEMVER))
|
18 | .sort(semver.compare)
|
19 | .valueOf();
|
20 |
|
21 | const latest = rawData['dist-tags'].latest;
|
22 | const next = rawData['dist-tags'].next;
|
23 | const latestStableRelease = semver.satisfies(latest, '*') ?
|
24 | latest :
|
25 | semver.maxSatisfying(sortedVersions, '*');
|
26 | return {
|
27 | latest: latestStableRelease,
|
28 | next: next,
|
29 | versions: sortedVersions,
|
30 | homepage: bestGuessHomepage(rawData)
|
31 | };
|
32 | }).catch(err => {
|
33 | const errorMessage = `Registry error ${err.message}`;
|
34 | return {
|
35 | error: errorMessage
|
36 | };
|
37 | });
|
38 | }
|
39 |
|
40 | module.exports = getNpmInfo;
|