1 | 'use strict';
|
2 |
|
3 | const chalk = require('chalk');
|
4 | const pluralize = require('pluralize');
|
5 | const prettyMs = require('pretty-ms');
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | function printJSON(obj) {
|
13 | return console.log(JSON.stringify(obj, null, ' '));
|
14 | }
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | module.exports.list = function (modules, opts) {
|
23 | opts = opts || {};
|
24 |
|
25 | if (opts.json) {
|
26 | return printJSON(modules);
|
27 | }
|
28 |
|
29 | Object.keys(modules).forEach((id) => {
|
30 | console.log(chalk.cyan.bold(id));
|
31 | modules[id].forEach((depId) => {
|
32 | console.log(chalk.grey(` ${depId}`));
|
33 | });
|
34 | });
|
35 | };
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | module.exports.summary = function (modules, opts) {
|
44 | const o = {};
|
45 |
|
46 | opts = opts || {};
|
47 |
|
48 | Object.keys(modules).sort((a, b) => {
|
49 | return modules[b].length - modules[a].length;
|
50 | }).forEach((id) => {
|
51 | if (opts.json) {
|
52 | o[id] = modules[id].length;
|
53 | } else {
|
54 | console.log('%s %s', chalk.cyan.bold(modules[id].length), chalk.grey(id));
|
55 | }
|
56 | });
|
57 |
|
58 | if (opts.json) {
|
59 | return printJSON(o);
|
60 | }
|
61 | };
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | module.exports.circular = function (spinner, res, circular, opts) {
|
72 | if (opts.json) {
|
73 | return printJSON(circular);
|
74 | }
|
75 |
|
76 | const cyclicCount = Object.keys(circular).length;
|
77 |
|
78 | if (!circular.length) {
|
79 | spinner.succeed(chalk.bold('No circular dependency found!'));
|
80 | } else {
|
81 | spinner.fail(chalk.red.bold(`Found ${pluralize('circular dependency', cyclicCount, true)}!\n`));
|
82 | circular.forEach((path, idx) => {
|
83 | process.stdout.write(chalk.dim(idx + 1 + ') '));
|
84 | path.forEach((module, idx) => {
|
85 | if (idx) {
|
86 | process.stdout.write(chalk.dim(' > '));
|
87 | }
|
88 | process.stdout.write(chalk.cyan.bold(module));
|
89 | });
|
90 | process.stdout.write('\n');
|
91 | });
|
92 | }
|
93 | };
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | module.exports.modules = function (modules, opts) {
|
102 | if (opts.json) {
|
103 | return printJSON(modules);
|
104 | }
|
105 |
|
106 | modules.forEach((id) => {
|
107 | console.log(chalk.cyan.bold(id));
|
108 | });
|
109 | };
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | module.exports.warnings = function (res) {
|
117 | const skipped = res.warnings().skipped;
|
118 |
|
119 | if (skipped.length) {
|
120 | console.log(chalk.yellow.bold(`\n✖ Skipped ${pluralize('file', skipped.length, true)}\n`));
|
121 |
|
122 | skipped.forEach((file) => {
|
123 | console.log(chalk.yellow(file));
|
124 | });
|
125 | }
|
126 | };
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | module.exports.getResultSummary = function (res, startTime) {
|
135 | const warningCount = res.warnings().skipped.length;
|
136 | const fileCount = Object.keys(res.obj()).length;
|
137 |
|
138 | console.log('Processed %s %s %s %s\n',
|
139 | chalk.bold(fileCount),
|
140 | pluralize('file', fileCount),
|
141 | chalk.dim(`(${prettyMs(Date.now() - startTime)})`),
|
142 | warningCount ? '(' + chalk.yellow.bold(pluralize('warning', warningCount, true)) + ')' : ''
|
143 | );
|
144 | };
|