123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- const fs = require("fs");
- const path = require("path");
- // Получаем путь к активному файлу из аргументов командной строки
- const filePath = process.argv[2];
- const content = fs.readFileSync(filePath, "utf8");
- const Eyo = require("eyo-kernel");
- const safeEyo = new Eyo();
- safeEyo.dictionary.loadSafeSync();
- const unsafeEyo = new Eyo();
- unsafeEyo.dictionary.loadNotSafeSync();
- function mainAct() {
- const errors = [];
- const paragraphs = content
- .split(/\n\s*\n/)
- .filter((p) => p.trim().length > 90);
- if (paragraphs.length > 5)
- errors.push(
- `${path.basename(filePath)}:1:2:error:Не должно быть больше 5 параграфов`
- );
- const segmenter = new Intl.Segmenter("ru", { granularity: "grapheme" });
- const segments = Array.from(segmenter.segment(content));
- const charCount = segments.length;
- if (charCount < 550) {
- errors.push(
- `${path.basename(
- filePath
- )}:1:1:error:Файл содержит ${charCount} символов, что меньше 550.`
- );
- } else if (charCount > 1950) {
- errors.push(
- `${path.basename(
- filePath
- )}:1:1:error:Файл содержит ${charCount} символов, что больше 950.`
- );
- }
- if (errors.length > 0) {
- errors.forEach((error) => console.error(error));
- process.exit(1);
- }
- }
- function you() {
- const youRegex = /(?<!^|\.\s|\?\s|!\s)(Вы|Вас|Вам)\s/g;
- const youMatch = youRegex.exec(content);
- if (!youMatch) return false;
- const line = content.substring(0, youMatch.index).split("\n").length;
- const column = youMatch.index - content.lastIndexOf("\n", youMatch.index) + 1;
- console.error(
- `${path.basename(
- filePath
- )}:${line}:${column}:error:Не используйте "Вы", "Вас" или "Вам" в середине предложения.`
- );
- return true;
- }
- function paragraph() {
- const lines = content.split("\n");
- for (let i = 0; i < lines.length; i++) {}
- for (let i = 0; i < lines.length - 1; i++) {
- const sem = lines[i].indexOf(";");
- if (sem > -1) {
- console.error(
- `${path.basename(filePath)}:${
- i + 1
- }:${sem}:warning:Не желательно использовать ;`
- );
- process.exit(1);
- }
- // Проверяем, если строка не пустая и следующая тоже не пустая
- if (
- lines[i].trim().length > 140 &&
- lines[i].trim() !== "" &&
- lines[i + 1].trim() !== ""
- ) {
- console.error(
- `${path.basename(filePath)}:${
- i + 1
- }:1:error:Абзацы разделены менее чем одной пустой строкой`
- );
- return true;
- }
- }
- return false;
- }
- if (filePath.endsWith(".txt")) {
- mainAct();
- const pRes = paragraph();
- const pYou = you();
- if (pRes || pYou) process.exit(1);
- let isYo = false;
- safeEyo.lint(content).forEach(({ before, after, position }) => {
- isYo = true;
- console.error(
- `${path.basename(filePath)}:${position.line}:${
- position.column
- }:error:Замените "${before}" на "${after}"`
- );
- });
- unsafeEyo.lint(content).forEach(({ before, after, position }) => {
- isYo = true;
- console.error(
- `${path.basename(filePath)}:${position.line}:${
- position.column
- }:warning:Вероятно, "${before}" пишется как "${after}"`
- );
- });
- if (isYo) process.exit(1);
- }
|