1 | 'use strict';
|
2 |
|
3 | const chalk = require('chalk');
|
4 | const program = require('commander');
|
5 |
|
6 | const yaspeller = require('../yaspeller');
|
7 |
|
8 | const { uptime } = require('../helpers/uptime');
|
9 | const { hasManyErrors } = require('../helpers/typos');
|
10 | const { packageJson } = require('../helpers/package');
|
11 | const {
|
12 | hasEngRusLetters,
|
13 | replaceRusLettersWithAsterisk,
|
14 | replaceEngLettersWithAsterisk,
|
15 | } = require('../helpers/string');
|
16 | const { isUrl } = require('../helpers/url');
|
17 | const { okSymbol, errorSymbol } = require('../helpers/symbols');
|
18 | const {
|
19 | consoleError,
|
20 | consoleOk,
|
21 | consoleWarn,
|
22 | consoleLog,
|
23 | consoleInfo,
|
24 | } = require('../helpers/console');
|
25 |
|
26 | function getTyposByCode(code, data) {
|
27 | let typos = [];
|
28 | let num = 1;
|
29 |
|
30 | data.forEach(function(el) {
|
31 | if (el.code !== code) {
|
32 | return;
|
33 | }
|
34 |
|
35 | const comment = [];
|
36 | const pos = el.position;
|
37 |
|
38 | if (pos.length) {
|
39 | comment.push(chalk.cyan(pos[0].line + ':' + pos[0].column));
|
40 | }
|
41 |
|
42 | if (el.count > 1) {
|
43 | comment.push(chalk.cyan('count: ' + el.count));
|
44 | }
|
45 |
|
46 | if (hasEngRusLetters(el.word)) {
|
47 | comment.push(chalk.red('en: ' + replaceRusLettersWithAsterisk(el.word)));
|
48 | comment.push(chalk.green('ru: ' + replaceEngLettersWithAsterisk(el.word)));
|
49 | }
|
50 |
|
51 | if (el.suggest) {
|
52 | comment.push(chalk.cyan('suggest: ' + el.suggest.join(', ')));
|
53 | }
|
54 |
|
55 | typos.push(num + '. ' + el.word + (comment.length ? ' (' + comment.join(', ') + ')' : ''));
|
56 | num++;
|
57 | });
|
58 |
|
59 | return typos;
|
60 | }
|
61 |
|
62 | module.exports = {
|
63 | name: 'console',
|
64 | onStart() {
|
65 | consoleLog('Spelling check:');
|
66 | },
|
67 | onResourceComplete(err, data) {
|
68 | const errors = [];
|
69 | if (err) {
|
70 | consoleError(data);
|
71 | } else {
|
72 | if (hasManyErrors(data.data)) {
|
73 | errors.push(chalk.red('Too many errors\n'));
|
74 | }
|
75 |
|
76 | yaspeller.errors.forEach(function(el) {
|
77 | const typos = getTyposByCode(el.code, data.data);
|
78 | if (typos.length) {
|
79 | errors.push(chalk.red(el.title + ': ' +
|
80 | typos.length + '\n') +
|
81 | typos.join('\n') + '\n');
|
82 | }
|
83 | });
|
84 |
|
85 | const time = data.time ? ' ' + chalk.magenta(data.time + ' ms') : '';
|
86 | const separator = chalk.red('-----\n');
|
87 |
|
88 | let res = data.resource;
|
89 |
|
90 | if (isUrl(res)) {
|
91 | res = chalk.underline(res);
|
92 | }
|
93 |
|
94 | if (errors.length) {
|
95 | console.error(chalk.red(errorSymbol) + ' ' + res + time + '\n' +
|
96 | separator + errors.join('\n') + separator);
|
97 | } else {
|
98 | console.log(chalk.green(okSymbol) + ' ' + res + time);
|
99 | }
|
100 | }
|
101 | },
|
102 | onComplete(data, stats, configPath) {
|
103 | if (!program.onlyErrors && stats.total) {
|
104 | if (stats.hasTypos) {
|
105 | let path = configPath + ' (';
|
106 | if (configPath.search(/package\.json/) !== -1) {
|
107 | path += '"yaspeller"→';
|
108 | }
|
109 |
|
110 | path += '"dictionary" property)';
|
111 | consoleWarn(`Fix typo or add word to dictionary at ${path} if you are sure about spelling. Docs: ${packageJson.homepage}#configuration`);
|
112 | }
|
113 |
|
114 | if (!stats.errors) {
|
115 | consoleOk('No errors.');
|
116 | }
|
117 |
|
118 | consoleInfo('Checking finished: ' + uptime());
|
119 | }
|
120 | }
|
121 | };
|