Change color output.

Refactor code.
This commit is contained in:
n8225 2020-02-08 13:15:06 -06:00
parent 50c8c85c65
commit e199778766

View File

@ -3,7 +3,6 @@
// node test.js -r README.md -d temp.md (Checks just the diff) // node test.js -r README.md -d temp.md (Checks just the diff)
const fs = require('fs'); const fs = require('fs');
//let colors = require('colors/safe');
const chalk = require('chalk'); const chalk = require('chalk');
let licenses = new Set(); let licenses = new Set();
let pr = false; let pr = false;
@ -60,9 +59,9 @@ function parseLicense(md) {
function testMainLink(text) { function testMainLink(text) {
let testA = /(^ {0,2}- \[.*?\]\(.*\))(?=.?-? ?\w)/; let testA = /(^ {0,2}- \[.*?\]\(.*\))(?=.?-? ?\w)/;
const testA1 = /(- \[.*?\]?\(?.*?\)?)( .*$)/; const testA1 = /(- \[.*?\]?\(?.*?\)?)( .*$)/;
if (testA.test(text) === false) { if (!testA.test(text)) {
let a1 = testA1.exec(text)[2]; let a1 = testA1.exec(text)[2];
return chalk.red.underline(text.replace(a1, '')) return chalk.red(text.replace(a1, ''))
} }
return chalk.green(testA.exec(text)[1]) return chalk.green(testA.exec(text)[1])
} }
@ -72,10 +71,10 @@ function testDescription(text) {
const testB = /( - .*\. )(?:(\(?\[?|\`))/; const testB = /( - .*\. )(?:(\(?\[?|\`))/;
const testA1 = /(- \[.*?\]?\(?.*?\)?)( .*$)/; const testA1 = /(- \[.*?\]?\(?.*?\)?)( .*$)/;
const testB2 = /((\(\[|\`).*$)/; const testB2 = /((\(\[|\`).*$)/;
if (testB.test(text) === false) { if (!testB.test(text)) {
let b1 = testA1.exec(text)[1]; let b1 = testA1.exec(text)[1];
let b2 = testB2.exec(text)[1]; let b2 = testB2.exec(text)[1];
return chalk.red.underline(text.replace(b1, '').replace(b2, '')) return chalk.red(text.replace(b1, '').replace(b2, ''))
} }
return chalk.green(testB.exec(text)[1]) return chalk.green(testB.exec(text)[1])
} }
@ -86,10 +85,11 @@ function testSrcDemCli(text) {
let testD = /(?<=\w. )(\(\[(Demo|Source Code|Clients)\]\([^)]*\)(, \[(Source Code|Clients)\]\([^)]*\))?(, \[(Source Code|Clients)\]\([^)]*\))*\))(?= \`?)/; let testD = /(?<=\w. )(\(\[(Demo|Source Code|Clients)\]\([^)]*\)(, \[(Source Code|Clients)\]\([^)]*\))?(, \[(Source Code|Clients)\]\([^)]*\))*\))(?= \`?)/;
const testD1 = /(^.*\.)(?= )/; const testD1 = /(^.*\.)(?= )/;
const testD2 = /(\`.*\` \`.*\`$)/; const testD2 = /(\`.*\` \`.*\`$)/;
if ((testC > -1) && (testD.test(text) === false)) { if ((testC > -1) && (!testD.test(text))) {
let d1 = testD1.exec(text)[1]; let d1 = testD1.exec(text)[1];
let d2 = testD2.exec(text)[1]; let d2 = testD2.exec(text)[1];
return chalk.red.underline(text.replace(d1+' ', '').replace(d2, ''))
return chalk.red(text.replace(d1+' ', '').replace(d2, ''))
} else if (testC > -1) { } else if (testC > -1) {
return chalk.green(testD.exec(text)[1]) return chalk.green(testD.exec(text)[1])
} }
@ -101,9 +101,9 @@ function testLangLic(text) {
const testD2 = /(\`.*\` \`.*\`$)/; const testD2 = /(\`.*\` \`.*\`$)/;
let testE = testD2.test(text); let testE = testD2.test(text);
const testE1 = /(^[^`]*)/; const testE1 = /(^[^`]*)/;
if (testE === false) { if (!testE) {
let e1 = testE1.exec(text)[1]; let e1 = testE1.exec(text)[1];
return chalk.red.underline(text.replace(e1, '')) return chalk.red(text.replace(e1, ''))
} }
return chalk.green(testD2.exec(text)[1]) return chalk.green(testD2.exec(text)[1])
} }
@ -117,12 +117,24 @@ function findError(text) {
res += testLangLic(text) res += testLangLic(text)
return res + `\n` return res + `\n`
} }
//Check if license is in the list of licenses. //Check if license is in the list of licenses.
function testLicense(md) { function testLicense(md) {
let pass = true;
let lFailed = []
let lPassed = []
const regex = /.*\`(.*)\` \`.*\`$/; const regex = /.*\`(.*)\` \`.*\`$/;
return licenses.has(regex.exec(md)[1]) for (l of regex.exec(md)[1].split("/")) {
if (!licenses.has(l)) {
pass = false;
lPassed.push(l)
}
lFailed.push(l)
}
return [pass, lFailed, lPassed]
} }
//Parses name from entry //Parses name from entry
function parseName(md) { function parseName(md) {
const regex = /^\W*(.*?)\W/ const regex = /^\W*(.*?)\W/
@ -134,12 +146,11 @@ function entryErrorCheck() {
let totalFail = 0; let totalFail = 0;
let totalPass = 0; let totalPass = 0;
let total = 0; let total = 0;
let failed = [];
let entries = []; let entries = [];
let diffEntries = []; let diffEntries = [];
if (lines[0] === "") { if (lines[0] === "") {
console.log(chalk.red("0 Entries Found")) console.log(chalk.red("0 Entries Found, check your commandline arguments"))
process.exit(0) process.exit(0)
} }
for (let i = 0; i < lines.length; i ++) { // Loop through array of lines for (let i = 0; i < lines.length; i ++) { // Loop through array of lines
@ -154,7 +165,7 @@ function entryErrorCheck() {
} }
if (pr === true) { if (pr === true) {
console.log(chalk.cyan("Only testing the diff from the PR.")) console.log(chalk.cyan("Only testing the diff from the PR.\n"))
const diffLines = split(diff); // Inserts each line of diff into an array const diffLines = split(diff); // Inserts each line of diff into an array
for (let l of diffLines) { for (let l of diffLines) {
if (entryFilter(l) === true) { // filter out lines that don't start with * [) if (entryFilter(l) === true) { // filter out lines that don't start with * [)
@ -165,6 +176,10 @@ function entryErrorCheck() {
licenses.add(parseLicense(l)) licenses.add(parseLicense(l))
} }
} }
if (diffEntries.length === 0) {
console.log("No entries changed in README.md, Exiting...")
process.exit(0)
}
total = diffEntries.length total = diffEntries.length
for (let e of diffEntries) { for (let e of diffEntries) {
e.pass = true e.pass = true
@ -175,9 +190,9 @@ function entryErrorCheck() {
console.log(`${e.highlight}`) console.log(`${e.highlight}`)
} }
e.licenseTest = testLicense(e.raw); e.licenseTest = testLicense(e.raw);
if (e.licenseTest === false) { if (!e.licenseTest) {
e.pass = false; e.pass = false;
console.log(chalk.yellow(`${e.name}'s license is not on License list.`)) console.log(chalk.red(`${e.name}'s license is not on License list.`))
} }
if (e.pass) { if (e.pass) {
totalPass++ totalPass++
@ -186,7 +201,7 @@ function entryErrorCheck() {
} }
} }
} else { } else {
console.log(chalk.cyan("Testing entire README.md")) console.log(chalk.cyan("Testing entire README.md\n"))
total = entries.length total = entries.length
for (let e of entries) { for (let e of entries) {
e.pass = true e.pass = true
@ -194,25 +209,22 @@ function entryErrorCheck() {
if (!findPattern(e.raw)) { if (!findPattern(e.raw)) {
e.highlight = findError(e.raw); e.highlight = findError(e.raw);
e.pass = false; e.pass = false;
console.log(`${chalk.yellow(e.line)} ${e.highlight}`) console.log(`${chalk.yellow(e.line + ": ")}${e.highlight}`);
syntax = e.highlight;
} }
e.licenseTest = testLicense(e.raw); e.licenseTest = testLicense(e.raw);
if (e.licenseTest === false) { if (!e.licenseTest[0]) {
e.pass = false; e.pass = false;
console.log(chalk.yellow(`${e.line} ${e.name}'s license is not on License list.`)) console.log(chalk.yellow(e.line + ": ") + `${e.name}'s license ${chalk.red(`'${e.licenseTest[1]}'`)} is not on the License list.\n`)
} }
if (e.pass) { if (e.pass) {
totalPass++ totalPass++
} else { } else {
totalFail++ totalFail++
} }
} }
} }
if (totalFail > 0) { if (totalFail > 0) {
console.log(chalk.blue(`\n-----------------------------\n`))
console.log(chalk.green("The portion of the entry with an error ") + chalk.underline.red("will be underlined and RED") + `\n`)
console.log(chalk.blue(`\n-----------------------------\n`)) console.log(chalk.blue(`\n-----------------------------\n`))
console.log(chalk.red(`${totalFail} Failed, `) + chalk.green(`${totalPass} Passed, `) + chalk.blue(`of ${total}`)) console.log(chalk.red(`${totalFail} Failed, `) + chalk.green(`${totalPass} Passed, `) + chalk.blue(`of ${total}`))
console.log(chalk.blue(`\n-----------------------------\n`)) console.log(chalk.blue(`\n-----------------------------\n`))
@ -223,8 +235,6 @@ function entryErrorCheck() {
console.log(chalk.blue(`\n-----------------------------\n`)) console.log(chalk.blue(`\n-----------------------------\n`))
process.exit(0) process.exit(0)
} }
} }
parseArgs(process.argv) parseArgs(process.argv)