UNPKG

3.51 kBJavaScriptView Raw
1var colors = require('cli-color');
2var check = require('check-more-types');
3var formInstallCommand = require('./report-install-command');
4var _ = require('lodash');
5var changedLog = require('changed-log');
6var Q = require('q');
7
8var colorAvailable = process.stdout.isTTY;
9
10function report(updates, options) {
11 options = options || {};
12 var useColors = Boolean(options.useColors) && colorAvailable;
13 check.verify.array(updates, 'expected array of updates');
14 // sets latest working version for each module too
15 var cmd = formInstallCommand(updates);
16
17 console.log('\n> next updates:');
18 updates.forEach(function (moduleVersions) {
19 reportModule(moduleVersions, useColors);
20 });
21
22 var reportChanges = updates.map(function (moduleVersions) {
23 return _.partial(printChangedLog, moduleVersions, useColors);
24 });
25
26 function printInstallCommand() {
27 if (_.isUndefined(cmd)) {
28 console.log('> nothing can be updated :(');
29 } else {
30 if (options.keptUpdates) {
31 console.log('> kept working updates');
32 } else {
33 cmd = cmd.trim();
34 var lines = cmd.split('\n').length;
35 if (lines === 1) {
36 console.log('> use the following command to install working versions');
37 } else {
38 console.log('> use the following commands to install working versions');
39 }
40 console.log(cmd);
41 }
42 }
43 }
44
45 function printError(err) {
46 console.error('Error reporting changes');
47 console.error(err.message);
48 }
49 var start = options.changedLog ?
50 reportChanges.reduce(Q.when, Q()).catch(printError) :
51 Q();
52 return start.then(printInstallCommand);
53}
54
55function reportModule(moduleVersions, useColors) {
56 check.verify.array(moduleVersions, 'expected module / versions array');
57 if (!moduleVersions.length) {
58 return;
59 }
60 var name = moduleVersions[0].name;
61 check.verify.unemptyString(name, 'missing module name from ' + JSON.stringify(moduleVersions));
62 var fromVersion = moduleVersions[0].from;
63 check.verify.unemptyString(fromVersion, 'missing from version from ' + JSON.stringify(moduleVersions));
64
65 if (useColors) {
66 var colorVersions = moduleVersions.map(function (info) {
67 return (info.works ? colors.greenBright : colors.redBright)(info.version);
68 });
69 var str = colorVersions.join(', ');
70 console.log(name + ' ' + fromVersion + ' -> ' + str);
71 } else {
72 console.log(name + '@' + fromVersion);
73 moduleVersions.forEach(function (info) {
74 console.log(' ' + info.version + ' ' + (info.works ? 'PASS' : 'FAIL'));
75 });
76 }
77}
78
79function printChangedLog(moduleVersions, useColors) {
80 var info = moduleVersions[0];
81
82 if (!info.works) {
83 return;
84 }
85 return changedLog({
86 name: info.name,
87 from: info.from,
88 to: info.version
89 });
90}
91
92function reportSuccess(text, useColors) {
93 if (colorAvailable && useColors) {
94 console.log(colors.greenBright(text));
95 } else {
96 console.log('PASS', text);
97 }
98}
99
100function reportFailure(text, useColors) {
101 if (colorAvailable && useColors) {
102 console.log(colors.redBright(text));
103 } else {
104 console.log('FAIL', text);
105 }
106}
107
108module.exports = {
109 report: report,
110 reportSuccess: reportSuccess,
111 reportFailure: reportFailure
112};