UNPKG

4.25 kBJavaScriptView Raw
1//--------------------------------------------------------
2//-- Doctor
3//--------------------------------------------------------
4'use strict';
5
6const chalk = require('chalk');
7const figures = require('figures');
8const cli = require('@absolunet/cli');
9const { terminal } = require('@absolunet/terminal');
10const Task = require('~/classes/task');
11const env = require('~/helpers/env');
12const { ow } = cli;
13
14
15const totals = {
16 success: 0,
17 failure: 0
18};
19
20let verbose = false;
21
22
23const reporter = (title, reports) => {
24 let success;
25
26 const titleColor = reports.summary.success ? chalk.green : chalk.red;
27 terminal.echo(`${chalk.cyan(title)} diagnosis ${titleColor(`${reports.summary.success ? '(^_^)' : 'ಠ_ಠ'} ${reports.summary.nb ? ` [${reports.summary.nb.success}/${reports.summary.nb.total}]` : ''}`)}\n`);
28
29 totals.success += reports.summary.nb.success;
30 totals.failure += reports.summary.nb.failure;
31
32 if (reports.summary.success && !verbose) {
33 terminal.echoIndent(chalk.green(`${figures.tick} ${reports.summary.nb.total === 1 ? reports.last.message : 'All tests passed'}`));
34 } else {
35
36 reports.list.forEach((test) => {
37 if (test.success && verbose) {
38 terminal.echoIndent(`${chalk.green(figures.tick)} ${chalk.dim(test.message)}`);
39
40 } else if (!test.success) {
41
42 let extra = '';
43 if (test.differences) {
44 extra += test.differences.superfluous && test.differences.superfluous.length !== 0 ? chalk.yellow(` [+] ${test.differences.superfluous.join(' | ')}`) : '';
45 extra += test.differences.missing && test.differences.missing.length !== 0 ? chalk.red(` [-] ${test.differences.missing.join(' | ')}`) : '';
46 extra += test.differences.mismatched && test.differences.mismatched.length !== 0 ? chalk.red(` [*] ${test.differences.mismatched.join(' | ')}`) : '';
47 }
48
49 if (test.outdated) {
50 extra += ` ${chalk.dim(test.outdated.current)}${chalk.yellow(test.outdated.latest)}`;
51 }
52
53 if (test.linterOutput) {
54 extra += `\n${test.linterOutput}`;
55 }
56
57 terminal.echo(`${chalk.red(`${figures.pointerSmall} ${figures.cross}`)} ${test.message}${extra}`);
58 }
59 });
60 }
61
62 terminal.spacer(2);
63
64 return success;
65};
66
67
68
69
70
71
72class DoctorTask extends Task {
73
74 constructor() {
75 super();
76 this.filename = __filename;
77 }
78
79 async cli(meowCli) {
80 cli.refuseArguments(meowCli);
81
82 const { verbose: flagVerbose } = cli.validateFlags(meowCli, {
83 verbose: ow.boolean
84 });
85 verbose = flagVerbose;
86
87
88 terminal.spacer();
89 const spinner = terminal.startSpinner(`Diagnosing ${chalk.cyan(env.packageConfig.name)}...`);
90
91 //-- Load here to speed up spinner first display
92 /* eslint-disable global-require */
93 const pluralize = require('pluralize');
94 const fss = require('@absolunet/fss');
95 const paths = require('~/helpers/paths');
96 const tester = require('~/helpers/doctor/tester');
97 /* eslint-enable global-require */
98
99
100 const [general, root, bundles, components, workflow, vendors, sync] = await Promise.all([
101 tester.general(),
102 tester.root(),
103 tester.bundles(),
104 tester.components(),
105 tester.workflowUpdates(),
106 tester.vendorsUpdates(),
107 tester.syncWorkflowToolbox()
108 ]);
109
110 spinner.stop();
111
112 //-- Reports
113 reporter('General', general);
114 reporter('Root strucure', root);
115 reporter('Bundles', bundles);
116 reporter('Components', components);
117 reporter('Workflow', workflow);
118 reporter('Vendors', vendors);
119 reporter('Sync between workflow and toolbox', sync);
120
121
122 //-- Totals
123 terminal.spacer(2);
124
125 if (totals.success) {
126 terminal.echo(chalk.green(`${pluralize('test', totals.success, true)} passed`));
127 }
128
129 if (totals.failure) {
130 terminal.echo(chalk.red(`${pluralize('test', totals.failure, true)} failed`));
131 }
132
133 terminal.spacer();
134
135
136 //-- Reward
137 if (totals.failure === 0) {
138 const reward = fss.readFile(`${paths.workflow.ressources}/doctor-reward`, 'utf8');
139 const pink = chalk.hex('#ff69b4');
140 const green = chalk.hex('#198c19');
141
142 terminal.echo(reward
143 .replace(/_.--._/ug, `_${pink('.--.')}_`)
144 .replace(/`--'/ug, pink('`--\''))
145 .replace(/\(\)/ug, pink('()'))
146 .replace(/.==./ug, green('.==.')))
147 ;
148 terminal.spacer();
149 }
150 }
151
152}
153
154
155module.exports = new DoctorTask();