4.41 kBJavaScriptView Raw
1'use strict';
2
3var chalk = require('chalk');
4var _ = require('lodash');
5var table = require('text-table');
6var emoji = require('./emoji');
7var stripAnsi = require('strip-ansi');
8
9function uppercaseFirstLetter(str) {
10 return str[0].toUpperCase() + str.substr(1);
11}
12
13function render(pkg, currentState) {
14 var packageName = pkg.moduleName;
15 var rows = [];
16
17 var indent = ' ' + emoji(' ');
18
19 var installer = currentState.get('installer');
20 var isYarn = installer === 'yarn';
21
22 var args = [isYarn ? 'add' : 'install'];
23 if (currentState.get('global')) {
24 isYarn ? args.unshift('global') : args.push('--global');
25 }
26
27 var flags = [];
28 if (isYarn) {
29 pkg.devDependency && flags.push('--dev');
30 } else {
31 pkg.devDependency ? flags.push('--save-dev') : flags.push('--save');
32 }
33
34 var upgradeCommand = installer + ' ' + args.join(' ') + ' ' + packageName + '@' + pkg.latest + ' ' + flags.join(' ');
35 var upgradeMessage = chalk.green(upgradeCommand) + ' to go from ' + pkg.installed + ' to ' + pkg.latest;
36 // DYLAN: clean this up
37 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 rc file options to remove unused check, but still monitor for outdated version:', indent + (' .npmcheckrc {"depcheck": {"ignoreMatches": ["' + packageName + '"]}}'), indent + ('Use ' + chalk.green('--skip-unused') + ' to skip this check.'), indent + ('To remove this package: ' + chalk.green(isYarn ? 'yarn remove ' + packageName + ' ' + (pkg.devDependency ? '--dev' : '') : 'npm uninstall ' + packageName + ' --save' + (pkg.devDependency ? '-dev' : '')))] : '', 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();
38
39 if (!status.length) {
40 return false;
41 }
42
43 rows.push([chalk.yellow(packageName), status.shift()]);
44
45 while (status.length) {
46 rows.push([' ', status.shift()]);
47 }
48
49 rows.push([' ']);
50
51 return rows;
52}
53
54function outputConsole(currentState) {
55 var packages = currentState.get('packages');
56
57 var rows = packages.reduce(function (acc, pkg) {
58 return acc.concat(render(pkg, currentState));
59 }, []).filter(Boolean);
60
61 if (rows.length) {
62 var renderedTable = table(rows, {
63 stringLength: function stringLength(s) {
64 return stripAnsi(s).length;
65 }
66 });
67
68 console.log('');
69 console.log(renderedTable);
70 console.log('Use ' + chalk.green('npm-check -' + (currentState.get('global') ? 'g' : '') + 'u') + ' for interactive update.');
71 process.exitCode = 1;
72 } else {
73 console.log(emoji(':heart: ') + 'Your modules look ' + chalk.bold('amazing') + '. Keep up the great work.' + emoji(' :heart:'));
74 process.exitCode = 0;
75 }
76}
77
78module.exports = outputConsole;
\No newline at end of file