mirror of
				https://github.com/awesome-selfhosted/awesome-selfhosted.git
				synced 2025-11-04 08:42:15 +01:00 
			
		
		
		
	Document test.js (#1024)
* Document test.js Added docmentation and cleaned up some WIP * Follow ESLint AirBNB style guide Cleanup REGEXPs
This commit is contained in:
		
							
								
								
									
										181
									
								
								test.js
									
									
									
									
									
								
							
							
						
						
									
										181
									
								
								test.js
									
									
									
									
									
								
							@@ -1,132 +1,85 @@
 | 
				
			|||||||
const fs = require('fs')
 | 
					// Accepts input of any filename, ie. node test.js README.md
 | 
				
			||||||
var log = '{\n'
 | 
					 | 
				
			||||||
var issuelog = '  "message": "#### Syntax Issues\\n\\n Name | Entry\\n----|----------------------\\n'
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
var file = fs.readFileSync(process.argv[2], 'utf8')
 | 
					const fs = require('fs');
 | 
				
			||||||
entryErrorCheck(file)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
function entryErrorCheck (md) {
 | 
					let log = '{\n';
 | 
				
			||||||
  var namepatt = /\*\s\[(.*?)\]/
 | 
					let issuelog = '  "message": "#### Syntax Issues\\n\\n Name | Entry\\n----|----------------------\\n';
 | 
				
			||||||
  var entries = split(md)
 | 
					 | 
				
			||||||
  var totalFail = 0
 | 
					 | 
				
			||||||
  var totalPass = 0
 | 
					 | 
				
			||||||
  var total = 0
 | 
					 | 
				
			||||||
  var entryArray = []
 | 
					 | 
				
			||||||
  for (var i = 0, len = entries.length; i < len; i++) {
 | 
					 | 
				
			||||||
    entryArray[i] = new Object;
 | 
					 | 
				
			||||||
    entryArray[i].raw = entries[i]
 | 
					 | 
				
			||||||
    if (entryFilter(entries[i]) === true) {
 | 
					 | 
				
			||||||
      total += 1
 | 
					 | 
				
			||||||
      entryArray[i].name = namepatt.exec(entries[i])[1]
 | 
					 | 
				
			||||||
      entryArray[i].pass = findPattern(entries[i])
 | 
					 | 
				
			||||||
      if (entryArray[i].pass === true) {
 | 
					 | 
				
			||||||
        totalPass += 1
 | 
					 | 
				
			||||||
      } else {
 | 
					 | 
				
			||||||
        console.log(entryArray[i].name + 'Failed.')
 | 
					 | 
				
			||||||
        // entryArray[i].error = findError(entries[i]) //WIP
 | 
					 | 
				
			||||||
        totalFail += 1
 | 
					 | 
				
			||||||
        issuelog += entryArray[i].name + ' | ' + entries[i] + '\\n'
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  if (totalFail > 0) {
 | 
					 | 
				
			||||||
    console.log(totalFail + ' Failed, ' + totalPass + ' Passed, of ' + total)
 | 
					 | 
				
			||||||
    log += '  "error": true,\n  "title": "Found ' + totalFail + ' entries with syntax error(s).",\n'
 | 
					 | 
				
			||||||
    fs.writeFileSync('syntaxcheck.json', log + issuelog + '"\n}')
 | 
					 | 
				
			||||||
    process.exit(1)
 | 
					 | 
				
			||||||
  } else {
 | 
					 | 
				
			||||||
    console.log(totalFail + ' Failed, ' + totalPass + ' Passed, of ' + total + '\n')
 | 
					 | 
				
			||||||
    log += '  "error": false\n}'
 | 
					 | 
				
			||||||
    fs.writeFileSync('syntaxcheck.json', log)
 | 
					 | 
				
			||||||
    process.exit(0)
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
function entryFilter (md) {
 | 
					const file = fs.readFileSync(process.argv[2], 'utf8'); // Reads argv into var file
 | 
				
			||||||
  var linepatt = /^\s{0,4}\*\s\[.*`/
 | 
					
 | 
				
			||||||
  return linepatt.test(md)
 | 
					function entryFilter(md) { // Function to find lines with entries
 | 
				
			||||||
 | 
					  const linepatt = /^ {0,4}\* \[.*`/;
 | 
				
			||||||
 | 
					  return linepatt.test(md);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function split (text) {
 | 
					function split(text) { // Function to split lines into array
 | 
				
			||||||
  return text.split(/\r?\n/)
 | 
					  return text.split(/\r?\n/);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// function parseMD(md) {
 | 
					function findPattern(text) { // Test entries against 8 patterns.  If matches pattern returns true
 | 
				
			||||||
  // return split(md).filter(entryFilter);
 | 
					  const nodnospatt = /^ {0,4}\* \[.*?\]\(.*?\) - .{0,249}?\. `.*?` `.*?`/; // Regex for entries with no demo and no source code
 | 
				
			||||||
// }
 | 
					  const slpatt = /^ {0,4}\* \[.*?\]\(.*?\) - .{0,249}?\. \(\[Demo\b\]\(.*?\), \[Source Code\b\]\(.*?\)\) `.*?` `.*?`/; // Regex for entries with demo and source code
 | 
				
			||||||
 | 
					  const nodpatt = / {0,4}\* \[.*?\]\(.*?\) - .{0,249}?\. \(\[Source Code\]\(.*?\)\) `.*?` `.*?`/; // Regex for entries with no demo
 | 
				
			||||||
function findPattern (text) {
 | 
					  const nospatt = / {0,4}\* \[.*?\]\(.*?\) - .{0,249}?\. \(\[Demo\]\(.*?\)\) `.*?` `.*?`/; // Regex for entries with no source code
 | 
				
			||||||
  var nodnospatt = /\s{0,4}\*\s\[(.*?)\]\((.*?)\)\s\-\s(.{0,249}?\.\s)`(.*?)`\s`(.*?)`/ // Regex for entries with no demo and no source code
 | 
					  const pnodnospatt = / {0,4}\* \[.*?\]\(.*?\) `⚠` - .{0,249}?\. `.*?` `.*?`/; // Regex for entries with proprietary with no demo and no source code
 | 
				
			||||||
  var slpatt = /\s{0,4}\*\s\[(.*?)\]\((.*?)\)\s\-\s(.{0,249}?\.\s)\(\[Demo\b\]\((.*?)\),\s\[Source Code\b\]\((.*?)\)\)\s`(.*?)`\s`(.*?)`/ // Regex for entries with demo and source code
 | 
					  const pslpatt = / {0,4}\* \[.*?\]\(.*?\) `⚠` - .{0,249}?\. \(\[Demo\b\]\(.*?\), \[Source Code\b\]\(.*?\)\) `.*?` `.*?`/; // Regex for entries with proprietary with demo and source code
 | 
				
			||||||
  var nodpatt = /\s{0,4}\*\s\[(.*?)\]\((.*?)\)\s\-\s(.{0,249}?\.\s)\(\[Source Code\]\((.*?)\)\)\s`(.*?)`\s`(.*?)`/ // Regex for entries with no demo
 | 
					  const pnodpatt = / {0,4}\* \[.*?\]\(.*?\) `⚠` - .{0,249}?\. \(\[Source Code\]\(.*?\)\) `.*?` `.*?`/; // Regex for entries with proprietary with no demo
 | 
				
			||||||
  var nospatt = /\s{0,4}\*\s\[(.*?)\]\((.*?)\)\s\-\s(.{0,249}?\.\s)\(\[Demo\]\((.*?)\)\)\s`(.*?)`\s`(.*?)`/ // Regex for entries with no source code
 | 
					  const pnospatt = / {0,4}\* \[.*?\]\(.*?\) `⚠` - .{0,249}?\. \(\[Demo\]\(.*?\)\) `.*?` `.*?`/; // Regex for entries with proprietary with no source code
 | 
				
			||||||
  var pnodnospatt = /\s{0,4}\*\s\[(.*?)\]\((.*?)\)\s`(⚠)`\s\-\s(.{0,249}?\.\s)`(.*?)`\s`(.*?)`/ // Regex for entries with proprietary with no demo and no source code
 | 
					 | 
				
			||||||
  var pslpatt = /\s{0,4}\*\s\[(.*?)\]\((.*?)\)\s`(⚠)`\s\-\s(.{0,249}?\.\s)\(\[Demo\b\]\((.*?)\),\s\[Source Code\b\]\((.*?)\)\)\s`(.*?)`\s`(.*?)`/ // Regex for entries with proprietary with demo and source code
 | 
					 | 
				
			||||||
  var pnodpatt = /\s{0,4}\*\s\[(.*?)\]\((.*?)\)\s`(⚠)`\s\-\s(.{0,249}?\.\s)\(\[Source Code\]\((.*?)\)\)\s`(.*?)`\s`(.*?)`/ // Regex for entries with proprietary with no demo
 | 
					 | 
				
			||||||
  var pnospatt = /\s{0,4}\*\s\[(.*?)\]\((.*?)\)\s`(⚠)`\s\-\s(.{0,249}?\.\s)\(\[Demo\]\((.*?)\)\)\s`(.*?)`\s`(.*?)`/ // Regex for entries with proprietary with no source code
 | 
					 | 
				
			||||||
  if (nodnospatt.test(text) === true) {
 | 
					  if (nodnospatt.test(text) === true) {
 | 
				
			||||||
    return true
 | 
					    return true;
 | 
				
			||||||
  } else if (slpatt.test(text) === true) {
 | 
					  } else if (slpatt.test(text) === true) {
 | 
				
			||||||
    return true
 | 
					    return true;
 | 
				
			||||||
  } else if (nodpatt.test(text) === true) {
 | 
					  } else if (nodpatt.test(text) === true) {
 | 
				
			||||||
    return true
 | 
					    return true;
 | 
				
			||||||
  } else if (nospatt.test(text) === true) {
 | 
					  } else if (nospatt.test(text) === true) {
 | 
				
			||||||
    return true
 | 
					    return true;
 | 
				
			||||||
  } else if (pnodnospatt.test(text) === true) {
 | 
					  } else if (pnodnospatt.test(text) === true) {
 | 
				
			||||||
    return true
 | 
					    return true;
 | 
				
			||||||
  } else if (pslpatt.test(text) === true) {
 | 
					  } else if (pslpatt.test(text) === true) {
 | 
				
			||||||
    return true
 | 
					    return true;
 | 
				
			||||||
  } else if (pnodpatt.test(text) === true) {
 | 
					  } else if (pnodpatt.test(text) === true) {
 | 
				
			||||||
    return true
 | 
					    return true;
 | 
				
			||||||
  } else if (pnospatt.test(text) === true) {
 | 
					  } else if (pnospatt.test(text) === true) {
 | 
				
			||||||
    return true
 | 
					    return true;
 | 
				
			||||||
  } else {
 | 
					 | 
				
			||||||
    return false
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
};
 | 
					  return false;
 | 
				
			||||||
// WIP
 | 
					 | 
				
			||||||
function findError (entry) {
 | 
					 | 
				
			||||||
  var fixedEntry = entry
 | 
					 | 
				
			||||||
  if (/\s{0,4}\*\s\[.*?\]\(http.*?\)/.test(entry) === false) { // Fix * site name/URL
 | 
					 | 
				
			||||||
    console.log('marker2')
 | 
					 | 
				
			||||||
    entry.replace(/^(\s*)\**\s*\[*([^()\[\]]*?)\]*\s*\(*(https*:\/\/[^ )]{1,})(\)*\s*`*⚠`*\s*-*\s*|\)*\s*-*\s*)(.*)/gi, function (match, p1, p2, p3, p4, p5) {
 | 
					 | 
				
			||||||
      console.log('Error in [Name](URL)')
 | 
					 | 
				
			||||||
      if (/⚠/.test(entry) === true) {
 | 
					 | 
				
			||||||
        fixedEntry = p1 + '* [' + p2 + '](' + p3 + ') `⚠` - ' + p5
 | 
					 | 
				
			||||||
      } else {
 | 
					 | 
				
			||||||
        fixedEntry = p1 + '* [' + p2 + '](' + p3 + ') - ' + p5
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      console.log('Fixed: ' + fixedEntry)
 | 
					 | 
				
			||||||
    })
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  if (/\s-\s/.test(entry) === false) {
 | 
					 | 
				
			||||||
    console.log('marker3')
 | 
					 | 
				
			||||||
    entry.replace(/(\s{0,3}\*\s\[.*?\]\(.*?\))(\s*`*⚠`*\s*-*\s*|\s*`*⚠`*\s*-*|`*⚠`*\s*-*\s*|`*⚠`*-*|\s*|\s*-*\s*|\s*-*|-*\s*|-*|\s*)(.{0,249}?\.)(.*)/gi, function (match, p1, p2, p3, p4) { // Fix space - space between link and descrition
 | 
					 | 
				
			||||||
      console.log('Missing ` - ` between Site link and description.')
 | 
					 | 
				
			||||||
      if (/⚠/.test(entry) === true) {
 | 
					 | 
				
			||||||
        fixedEntry = p1 + ' `⚠` - ' + p3 + p4
 | 
					 | 
				
			||||||
      } else {
 | 
					 | 
				
			||||||
        fixedEntry = p1 + ' - ' + p3 + p4
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      console.log('Fixed: ' + fixedEntry)
 | 
					 | 
				
			||||||
    })
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  if (/^\s{0,4}\*\s\[.*?\]\(.*?\).*?(\s\.|\)\)\s)`.*?`\s`.*?`/.test(entry) === false) {
 | 
					 | 
				
			||||||
    console.log('marker4')
 | 
					 | 
				
			||||||
    entry.replace(/^(\s*\*\s.*?\)\)|.*?\s\-\s.*?\.)(\s*`*\s*)(.*?)(\s*`\s*`\s*|\s*`\s*`*\s*|\s*`*\s*`\s*|\s)(.*?)`/gi, function (match, p1, p2, p3, p4, p5) { // Fix License and Language
 | 
					 | 
				
			||||||
      console.log('Issue in Language or License')
 | 
					 | 
				
			||||||
      fixedEntry = p1 + ' `' + p3 + '` `' + p5 + '`'
 | 
					 | 
				
			||||||
      console.log('Fixed: ' + fixedEntry)
 | 
					 | 
				
			||||||
    })
 | 
					 | 
				
			||||||
  } else if (/demo/i.test(entry) === true) {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  } else if (/source/i.test(entry) === true) {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  console.log(findPattern(fixedEntry))
 | 
					 | 
				
			||||||
  // if (findPattern(fixedEntry) === false) {
 | 
					 | 
				
			||||||
    // findError(fixedEntry) // If it's not fixed let's try again.
 | 
					 | 
				
			||||||
  // }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// else if (/source/i.test(entry) === true && /demo/i.test(entry) === true) {
 | 
					function entryErrorCheck(md) {
 | 
				
			||||||
 | 
					  const namepatt = /\* \[(.*?)\]/; // regex pattern to find name of entryArray
 | 
				
			||||||
 | 
					  const entries = split(md); // Inserts each line into the entries array
 | 
				
			||||||
 | 
					  let totalFail = 0;
 | 
				
			||||||
 | 
					  let totalPass = 0;
 | 
				
			||||||
 | 
					  let total = 0;
 | 
				
			||||||
 | 
					  const entryArray = [];
 | 
				
			||||||
 | 
					  for (let i = 0, len = entries.length; i < len; i += 1) { // Loop to create array of objects
 | 
				
			||||||
 | 
					    entryArray[i] = new Object;
 | 
				
			||||||
 | 
					    entryArray[i].raw = entries[i];
 | 
				
			||||||
 | 
					    if (entryFilter(entries[i]) === true) { // filter out lines that don't with * [)
 | 
				
			||||||
 | 
					      total += 1;
 | 
				
			||||||
 | 
					      entryArray[i].name = namepatt.exec(entries[i])[1]; // Parses name of entry
 | 
				
			||||||
 | 
					      entryArray[i].pass = findPattern(entries[i]); // Tests against known patterns
 | 
				
			||||||
 | 
					      if (entryArray[i].pass === true) { // If entry passes increment totalPass counter
 | 
				
			||||||
 | 
					        totalPass += 1;
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        console.log(`${entryArray[i].name} Failed.`); // If entry fails increment totalFail counter and append error to issuelog
 | 
				
			||||||
 | 
					        // entryArray[i].error = findError(entries[i]) //WIP
 | 
				
			||||||
 | 
					        totalFail += 1;
 | 
				
			||||||
 | 
					        issuelog += `${entryArray[i].name} | ${entries[i]} \\n`;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  if (totalFail > 0) { // Logs # passed & failed to console, and failures to syntaxcheck.json
 | 
				
			||||||
 | 
					    console.log(`${totalFail} Failed, ${totalPass} Passed, of ${total}`);
 | 
				
			||||||
 | 
					    log += ` "error": true,\n  "title": "Found ${totalFail} entries with syntax error(s).",\n`;
 | 
				
			||||||
 | 
					    fs.writeFileSync('syntaxcheck.json', `${log} ${issuelog} "\n}`);
 | 
				
			||||||
 | 
					    process.exit(1);
 | 
				
			||||||
 | 
					  } else { // Logs # of entries passed to console and error: false to syntaxcheck.json
 | 
				
			||||||
 | 
					    console.log(`${totalFail} Failed, ${totalPass} Passed, of ${total} \n`);
 | 
				
			||||||
 | 
					    log += '  "error": false\n}';
 | 
				
			||||||
 | 
					    fs.writeFileSync('syntaxcheck.json', log);
 | 
				
			||||||
 | 
					    process.exit(0);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					entryErrorCheck(file);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user