Change color output.

Refactor code.
This commit is contained in:
n8225 2020-02-08 13:15:06 -06:00
parent 50c8c85c65
commit e199778766
1 changed files with 36 additions and 26 deletions

View File

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