Mirror of Awesome Self Hosted
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
4.2 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
5 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. // Accepts input of any filename, ie. node test.js README.md
  2. const fs = require('fs');
  3. let log = '{\n';
  4. let issuelog = ' "message": "#### Syntax Issues\\n\\n Name | Entry\\n----|----------------------\\n';
  5. let fails = ''
  6. const file = fs.readFileSync(process.argv[2], 'utf8'); // Reads argv into var file
  7. function entryFilter(md) { // Function to find lines with entries
  8. const linepatt = /^\s{0,2}-\s\[.*`/;
  9. return linepatt.test(md);
  10. }
  11. function split(text) { // Function to split lines into array
  12. return text.split(/\r?\n/);
  13. }
  14. function findPattern(text) { // Test entries against 8 patterns. If matches pattern returns true
  15. const nodnospatt = /^\s{0,2}-\s\[.*?\]\(.*?\) - .{0,249}?\. `.*?` `.*?`/; // Regex for entries with no demo and no source code
  16. const slpatt = /^\s{0,2}-\s\[.*?\]\(.*?\) - .{0,249}?\. \(\[Demo\b\]\(.*?\), \[Source Code\b\]\(.*?\)\) `.*?` `.*?`/; // Regex for entries with demo and source code
  17. const nodpatt = /^\s{0,2}-\s\[.*?\]\(.*?\) - .{0,249}?\. \(\[Source Code\]\(.*?\)\) `.*?` `.*?`/; // Regex for entries with no demo
  18. const nospatt = /^\s{0,2}-\s\[.*?\]\(.*?\) - .{0,249}?\. \(\[Demo\]\(.*?\)\) `.*?` `.*?`/; // Regex for entries with no source code
  19. const pnodnospatt = /^\s{0,2}-\s\[.*?\]\(.*?\) `⚠` - .{0,249}?\. `.*?` `.*?`/; // Regex for entries with proprietary with no demo and no source code
  20. const pslpatt = /^\s{0,2}-\s\[.*?\]\(.*?\) `⚠` - .{0,249}?\. \(\[Demo\b\]\(.*?\), \[Source Code\b\]\(.*?\)\) `.*?` `.*?`/; // Regex for entries with proprietary with demo and source code
  21. const pnodpatt = /^\s{0,2}-\s\[.*?\]\(.*?\) `⚠` - .{0,249}?\. \(\[Source Code\]\(.*?\)\) `.*?` `.*?`/; // Regex for entries with proprietary with no demo
  22. const pnospatt = /^\s{0,2}-\s\[.*?\]\(.*?\) `⚠` - .{0,249}?\. \(\[Demo\]\(.*?\)\) `.*?` `.*?`/; // Regex for entries with proprietary with no source code
  23. if (nodnospatt.test(text) === true) {
  24. return true;
  25. } else if (slpatt.test(text) === true) {
  26. return true;
  27. } else if (nodpatt.test(text) === true) {
  28. return true;
  29. } else if (nospatt.test(text) === true) {
  30. return true;
  31. } else if (pnodnospatt.test(text) === true) {
  32. return true;
  33. } else if (pslpatt.test(text) === true) {
  34. return true;
  35. } else if (pnodpatt.test(text) === true) {
  36. return true;
  37. } else if (pnospatt.test(text) === true) {
  38. return true;
  39. }
  40. return false;
  41. }
  42. function entryErrorCheck(md) {
  43. const namepatt = /^\s{0,2}-\s\[(.*?)\]/; // regex pattern to find name of entryArray
  44. const entries = split(md); // Inserts each line into the entries array
  45. let totalFail = 0;
  46. let totalPass = 0;
  47. let total = 0;
  48. const entryArray = [];
  49. if (entries[0] === "") {
  50. console.log("0 Entries")
  51. process.exit(0)
  52. }
  53. for (let i = 0, len = entries.length; i < len; i += 1) { // Loop to create array of objects
  54. entryArray[i] = new Object;
  55. entryArray[i].raw = entries[i];
  56. if (entryFilter(entries[i]) === true) { // filter out lines that don't with * [)
  57. total += 1;
  58. entryArray[i].name = namepatt.exec(entries[i])[1]; // Parses name of entry
  59. entryArray[i].pass = findPattern(entries[i]); // Tests against known patterns
  60. if (entryArray[i].pass === true) { // If entry passes increment totalPass counter
  61. totalPass += 1;
  62. } else {
  63. console.log(`${entryArray[i].name} Failed.`); // If entry fails increment totalFail counter and append error to issuelog
  64. // entryArray[i].error = findError(entries[i]) //WIP
  65. totalFail += 1;
  66. issuelog += `${entryArray[i].name} | ${entries[i]} \\n`;
  67. fails += `${entries[i]} \n\n`;
  68. }
  69. }
  70. }
  71. if (totalFail > 0) { // Logs # passed & failed to console, and failures to syntaxcheck.json
  72. console.log(`${totalFail} Failed, ${totalPass} Passed, of ${total}\n-----------------------------`);
  73. console.log(fails)
  74. log += ` "error": true,\n "title": "Found ${totalFail} entries with syntax error(s).",\n`;
  75. fs.writeFileSync('syntaxcheck.json', `${log} ${issuelog} "\n}`);
  76. process.exit(1);
  77. } else { // Logs # of entries passed to console and error: false to syntaxcheck.json
  78. console.log(`${totalFail} Failed, ${totalPass} Passed, of ${total} \n`);
  79. log += ' "error": false\n}';
  80. fs.writeFileSync('syntaxcheck.json', log);
  81. process.exit(0);
  82. }
  83. }
  84. entryErrorCheck(file);