checkLength.js 3.5 KB

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