UNPKG

3.75 kBJavaScriptView Raw
1'use strict';
2
3var chalk = require('chalk');
4var _ = require('lodash');
5var table = require('text-table');
6var emoji = require('./emoji');
7
8function uppercaseFirstLetter(str) {
9 return str[0].toUpperCase() + str.substr(1);
10}
11
12function render(pkg, currentState) {
13 var packageName = pkg.moduleName;
14 var rows = [];
15
16 var indent = ' ' + emoji(' ');
17 var flags = currentState.get('global') ? '--global' : '--save' + (pkg.devDependency ? '-dev' : '');
18 var upgradeCommand = 'npm install ' + flags + ' ' + packageName + '@' + pkg.latest;
19 var upgradeMessage = chalk.green(upgradeCommand) + ' to go from ' + pkg.installed + ' to ' + pkg.latest;
20 // DYLAN: clean this up
21 var status = _([pkg.notInstalled ? chalk.bgRed.white.bold(emoji(' :worried: ') + ' MISSING! ') + ' Not installed.' : '', pkg.notInPackageJson ? chalk.bgRed.white.bold(emoji(' :worried: ') + ' PKG ERR! ') + ' Not in the package.json. ' + pkg.notInPackageJson : '', pkg.pkgError && !pkg.notInstalled ? chalk.bgGreen.white.bold(emoji(' :worried: ') + ' PKG ERR! ') + ' ' + chalk.red(pkg.pkgError.message) : '', pkg.bump && pkg.easyUpgrade ? [chalk.bgGreen.white.bold(emoji(' :heart_eyes: ') + ' UPDATE! ') + ' Your local install is out of date. ' + chalk.blue.underline(pkg.homepage || ''), indent + upgradeMessage] : '', pkg.bump && !pkg.easyUpgrade ? [chalk.white.bold.bgGreen(pkg.bump === 'nonSemver' ? emoji(' :sunglasses: ') + ' new ver! '.toUpperCase() : emoji(' :sunglasses: ') + ' ' + pkg.bump.toUpperCase() + ' UP ') + ' ' + uppercaseFirstLetter(pkg.bump) + ' update available. ' + chalk.blue.underline(pkg.homepage || ''), indent + upgradeMessage] : '', pkg.unused ? [chalk.black.bold.bgWhite(emoji(' :confused: ') + ' NOTUSED? ') + (' ' + chalk.yellow('Still using ' + packageName + '?')), indent + ('Depcheck did not find code similar to ' + chalk.green('require(\'' + packageName + '\')') + ' or ' + chalk.green('import from \'' + packageName + '\'') + '.'), indent + 'Check your code before removing as depcheck isn\'t able to foresee all ways dependencies can be used.', indent + ('Use ' + chalk.green('--skip-unused') + ' to skip this check.'), indent + ('To remove this package: ' + chalk.green('npm uninstall --save' + (pkg.devDependency ? '-dev' : '') + ' ' + packageName))] : '', pkg.mismatch && !pkg.bump ? chalk.bgRed.yellow.bold(emoji(' :interrobang: ') + ' MISMATCH ') + ' Installed version does not match package.json. ' + pkg.installed + ' ≠ ' + pkg.packageJson : '', pkg.regError ? chalk.bgRed.white.bold(emoji(' :no_entry: ') + ' NPM ERR! ') + ' ' + chalk.red(pkg.regError) : '']).flatten().compact().valueOf();
22
23 if (!status.length) {
24 return false;
25 }
26
27 rows.push([chalk.yellow(packageName), status.shift()]);
28
29 while (status.length) {
30 rows.push([' ', status.shift()]);
31 }
32
33 rows.push([' ']);
34
35 return rows;
36}
37
38function outputConsole(currentState) {
39 var packages = currentState.get('packages');
40
41 var rows = packages.reduce(function (acc, pkg) {
42 return acc.concat(render(pkg, currentState));
43 }, []).filter(Boolean);
44
45 if (rows.length) {
46 var renderedTable = table(rows, {
47 stringLength: function stringLength(s) {
48 return chalk.stripColor(s).length;
49 }
50 });
51
52 console.log('');
53 console.log(renderedTable);
54 console.log('Use ' + chalk.green('npm-check -' + (currentState.get('global') ? 'g' : '') + 'u') + ' for interactive update.');
55 process.exitCode = 1;
56 } else {
57 console.log(emoji(':heart: ') + 'Your modules look ' + chalk.bold('amazing') + '. Keep up the great work.' + emoji(' :heart:'));
58 process.exitCode = 0;
59 }
60}
61
62module.exports = outputConsole;
\No newline at end of file