1 | require('lazy-ass');
|
2 | var check = require('check-more-types');
|
3 | require('console.json');
|
4 | var verify = check.verify;
|
5 | var print = require('./print-modules-table');
|
6 | var stats = require('./stats');
|
7 | var clc = require('cli-color');
|
8 | var getSuccess = stats.getSuccessStats;
|
9 | var q = require('q');
|
10 |
|
11 | function ignore() {}
|
12 |
|
13 | function report(available, currentVersions, options) {
|
14 | la(check.array(available), 'expect an array of info objects', available);
|
15 |
|
16 | if (!available.length) {
|
17 | console.log('nothing new is available');
|
18 | return;
|
19 | }
|
20 |
|
21 |
|
22 | la(check.maybe.object(currentVersions),
|
23 | 'expected current versions object', currentVersions);
|
24 |
|
25 | var chain = q();
|
26 | var updateStats = {};
|
27 |
|
28 | available.forEach(function (info) {
|
29 | la(check.unemptyString(info.name), 'missing module name', info);
|
30 | la(check.array(info.versions), 'missing module versions', info);
|
31 |
|
32 | var currentVersion = currentVersions && currentVersions[info.name] || null;
|
33 |
|
34 | if (currentVersion) {
|
35 | la(check.unemptyString(currentVersion.version), 'missing version', currentVersion);
|
36 | updateStats[info.name] = {
|
37 | name: info.name,
|
38 | from: currentVersion.version
|
39 | };
|
40 | }
|
41 |
|
42 | if (info.versions.length === 1) {
|
43 | if (currentVersion) {
|
44 | chain = chain.then(function () {
|
45 | return getSuccess({
|
46 | name: info.name,
|
47 | from: currentVersion.version,
|
48 | to: info.versions[0]
|
49 | }).then(function (stats) {
|
50 | updateStats[info.name] = stats;
|
51 | }).catch(ignore);
|
52 | });
|
53 | }
|
54 | }
|
55 | });
|
56 |
|
57 | return chain.then(function () {
|
58 | var modules = [];
|
59 |
|
60 | available.forEach(function (info) {
|
61 | verify.string(info.name, 'missing module name ' + info);
|
62 | verify.array(info.versions, 'missing module versions ' + info);
|
63 |
|
64 | var sep = ', ', versions;
|
65 |
|
66 | if (info.versions.length < 5) {
|
67 | versions = info.versions.join(sep);
|
68 | } else {
|
69 | versions = info.versions.slice(0, 2)
|
70 | .concat('...')
|
71 | .concat(info.versions.slice(info.versions.length - 2))
|
72 | .join(sep);
|
73 | }
|
74 |
|
75 | var stats = updateStats[info.name];
|
76 | modules.push({
|
77 | name: info.name,
|
78 | version: versions,
|
79 | from: stats.from,
|
80 | stats: stats
|
81 | });
|
82 | });
|
83 | console.log('\navailable updates:');
|
84 | print(modules, options);
|
85 | console.log('update stats from', clc.underline(stats.url));
|
86 | });
|
87 | }
|
88 |
|
89 | module.exports = report;
|