checkLength.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const fs = require("fs");
  2. const path = require("path");
  3. // Получаем путь к активному файлу из аргументов командной строки
  4. const filePath = process.argv[2];
  5. const content = fs.readFileSync(filePath, "utf8");
  6. function mainAct() {
  7. const errors = [];
  8. const paragraphs = content
  9. .split(/\n\s*\n/)
  10. .filter((p) => p.trim().length > 90);
  11. if (paragraphs.length > 5)
  12. errors.push(
  13. `${path.basename(filePath)}:1:2:error:Не должно быть больше 5 параграфов`
  14. );
  15. const segmenter = new Intl.Segmenter("ru", { granularity: "grapheme" });
  16. const segments = Array.from(segmenter.segment(content));
  17. const charCount = segments.length;
  18. if (charCount < 550) {
  19. errors.push(
  20. `${path.basename(
  21. filePath
  22. )}:1:1:error:Файл содержит ${charCount} символов, что меньше 550.`
  23. );
  24. } else if (charCount > 1950) {
  25. errors.push(
  26. `${path.basename(
  27. filePath
  28. )}:1:1:error:Файл содержит ${charCount} символов, что больше 950.`
  29. );
  30. }
  31. if (errors.length > 0) {
  32. errors.forEach((error) => console.error(error));
  33. process.exit(1);
  34. }
  35. }
  36. function you() {
  37. const youRegex = /\s(Вы|Вас|Вам)\s/g;
  38. const youMatch = youRegex.exec(content);
  39. if (!youMatch) return false;
  40. const line = content.substring(0, youMatch.index).split("\n").length;
  41. const column = youMatch.index - content.lastIndexOf("\n", youMatch.index) + 1;
  42. console.error(
  43. `${path.basename(
  44. filePath
  45. )}:${line}:${column}:error:Не используйте "Вы", "Вас" или "Вам" в середине предложения.`
  46. );
  47. return true;
  48. }
  49. function paragraph() {
  50. const lines = content.split("\n");
  51. for (let i = 0; i < lines.length; i++) {}
  52. for (let i = 0; i < lines.length - 1; i++) {
  53. const sem = lines[i].indexOf(";");
  54. if (sem > -1) {
  55. console.error(
  56. `${path.basename(filePath)}:${
  57. i + 1
  58. }:${sem}:warning:Не желательно использовать ;`
  59. );
  60. process.exit(1);
  61. }
  62. // Проверяем, если строка не пустая и следующая тоже не пустая
  63. if (
  64. lines[i].trim().length > 140 &&
  65. lines[i].trim() !== "" &&
  66. lines[i + 1].trim() !== ""
  67. ) {
  68. console.error(
  69. `${path.basename(filePath)}:${
  70. i + 1
  71. }:1:error:Абзацы разделены менее чем одной пустой строкой`
  72. );
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. if (filePath.endsWith(".txt")) {
  79. mainAct();
  80. const pRes = paragraph();
  81. const pYou = you();
  82. if (pRes || pYou) process.exit(1);
  83. }