UNPKG

3.29 kBJavaScriptView Raw
1'use strict';
2
3const chalk = require('chalk');
4const pluralize = require('pluralize');
5const prettyMs = require('pretty-ms');
6
7/**
8 * Print given object as JSON.
9 * @param {Object} obj
10 * @return {String}
11 */
12function printJSON(obj) {
13 return console.log(JSON.stringify(obj, null, ' '));
14}
15
16/**
17 * Print module dependency graph as indented text (or JSON).
18 * @param {Object} modules
19 * @param {Object} opts
20 * @return {undefined}
21 */
22module.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 * Print a summary of module dependencies.
39 * @param {Object} modules
40 * @param {Object} opts
41 * @return {undefined}
42 */
43module.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 * Print the result from Madge.circular().
65 * @param {Object} spinner
66 * @param {Object} res
67 * @param {Array} circular
68 * @param {Object} opts
69 * @return {undefined}
70 */
71module.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 * Print the given modules.
97 * @param {Array} modules
98 * @param {Object} opts
99 * @return {undefined}
100 */
101module.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 * Print warnings to the console.
113 * @param {Object} res
114 * @return {undefined}
115 */
116module.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 * Get a summary from the result.
130 * @param {Object} res
131 * @param {Number} startTime
132 * @return {undefined}
133 */
134module.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};