2020-02-08 14:06:37 +01:00
|
|
|
// USAGE:
|
|
|
|
// node test.js -r README.md (Checks whole file)
|
|
|
|
// node test.js -r README.md -d temp.md (Checks just the diff)
|
2016-12-10 14:46:24 +01:00
|
|
|
|
2017-04-12 01:38:29 +02:00
|
|
|
const fs = require('fs');
|
2020-02-08 14:06:37 +01:00
|
|
|
const chalk = require('chalk');
|
|
|
|
let licenses = new Set();
|
|
|
|
let pr = false;
|
|
|
|
let readme;
|
|
|
|
let diff;
|
2016-12-10 14:46:24 +01:00
|
|
|
|
2020-02-08 14:06:37 +01:00
|
|
|
//Parse the command options and set the pr var
|
|
|
|
function parseArgs(args) {
|
|
|
|
if ( args.indexOf('-r', 2) > 0 ) {
|
|
|
|
readme = fs.readFileSync(args[args.indexOf('-r', 2)+1], 'utf8')
|
|
|
|
}
|
|
|
|
if (args.indexOf('-d', 2) > 0) {
|
|
|
|
pr = true;
|
|
|
|
diff = fs.readFileSync(args[args.indexOf('-d', 2)+1], 'utf8');
|
|
|
|
}
|
|
|
|
if ( pr === true) {
|
|
|
|
console.log(chalk.blue(`Running on PR. README.md: ${args[args.indexOf('-r', 2)+1]} diff: ${args[args.indexOf('-d', 2)+1]}`))
|
|
|
|
}
|
|
|
|
}
|
2016-12-10 14:46:24 +01:00
|
|
|
|
2020-02-08 14:06:37 +01:00
|
|
|
// Function to find lines with entries
|
|
|
|
function entryFilter(md) {
|
2018-07-02 05:29:00 +02:00
|
|
|
const linepatt = /^\s{0,2}-\s\[.*`/;
|
2017-04-12 01:38:29 +02:00
|
|
|
return linepatt.test(md);
|
2016-12-10 14:46:24 +01:00
|
|
|
}
|
|
|
|
|
2020-02-08 14:06:37 +01:00
|
|
|
// Function to find lines with licenses
|
|
|
|
function licenseFilter(md) {
|
|
|
|
const linepatt = /^- `.*` - .*/;
|
|
|
|
return linepatt.test(md)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function to split lines into array
|
|
|
|
function split(text) {
|
2017-04-12 01:38:29 +02:00
|
|
|
return text.split(/\r?\n/);
|
|
|
|
}
|
2016-12-10 14:46:24 +01:00
|
|
|
|
2020-02-08 14:06:37 +01:00
|
|
|
// All entries should match this pattern. If matches pattern returns true.
|
|
|
|
function findPattern(text) {
|
2020-02-08 22:41:06 +01:00
|
|
|
const patt = /^\s{0,2}-\s\[.*?\]\(.*?\) (`⚠` )?- .{0,249}?\.( \(\[(Demo|Source Code|Clients)\]\([^)\]]*\)(, \[(Source Code|Clients)\]\([^)\]]*\))?(, \[(Source Code|Clients)\]\([^)\]]*\))*\))? \`.*?\` \`.*?\`$/;
|
2020-01-28 21:52:19 +01:00
|
|
|
if (patt.test(text) === true) {
|
2017-04-12 01:38:29 +02:00
|
|
|
return true;
|
2020-01-28 21:52:19 +01:00
|
|
|
}
|
2017-04-12 01:38:29 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-08 14:06:37 +01:00
|
|
|
// Parses SPDX identifiers from list of licenses
|
|
|
|
function parseLicense(md) {
|
|
|
|
const patt = /^- `(.*)` - .*/
|
|
|
|
return patt.exec(md)[1]
|
|
|
|
}
|
|
|
|
|
2020-02-08 22:41:06 +01:00
|
|
|
//Test '- [Name](http://homepage/)'
|
2020-02-08 14:06:37 +01:00
|
|
|
function testMainLink(text) {
|
|
|
|
let testA = /(^ {0,2}- \[.*?\]\(.*\))(?=.?-? ?\w)/;
|
2020-02-08 22:41:06 +01:00
|
|
|
const testA1 = /(- \W?\w*\W{0,2}.*?\)?)( .*$)/;
|
2020-02-08 20:15:06 +01:00
|
|
|
if (!testA.test(text)) {
|
2020-02-08 14:06:37 +01:00
|
|
|
let a1 = testA1.exec(text)[2];
|
2020-02-08 20:15:06 +01:00
|
|
|
return chalk.red(text.replace(a1, ''))
|
2020-02-08 14:06:37 +01:00
|
|
|
}
|
|
|
|
return chalk.green(testA.exec(text)[1])
|
|
|
|
}
|
|
|
|
|
2020-02-08 22:41:06 +01:00
|
|
|
//Test '`⚠` - Short description, less than 250 characters.'
|
2020-02-08 14:06:37 +01:00
|
|
|
function testDescription(text) {
|
|
|
|
const testB = /( - .*\. )(?:(\(?\[?|\`))/;
|
2020-02-08 22:41:06 +01:00
|
|
|
const testA1 = /(- \W?\w*\W{0,2}.*?\)?)( .*$)/;
|
2020-02-08 14:06:37 +01:00
|
|
|
const testB2 = /((\(\[|\`).*$)/;
|
2020-02-08 20:15:06 +01:00
|
|
|
if (!testB.test(text)) {
|
2020-02-08 14:06:37 +01:00
|
|
|
let b1 = testA1.exec(text)[1];
|
|
|
|
let b2 = testB2.exec(text)[1];
|
2020-02-08 20:15:06 +01:00
|
|
|
return chalk.red(text.replace(b1, '').replace(b2, ''))
|
2020-02-08 14:06:37 +01:00
|
|
|
}
|
|
|
|
return chalk.green(testB.exec(text)[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
//If present, tests '([Demo](http://url.to/demo), [Source Code](http://url.of/source/code), [Clients](https://url.to/list/of/related/clients-or-apps))'
|
|
|
|
function testSrcDemCli(text) {
|
|
|
|
let testC = text.search(/\(\[|\)\,|\)\)/);
|
2020-02-08 22:41:06 +01:00
|
|
|
let testD = /(?<=\w. )(\(\[(Demo|Source Code|Clients)\]\([^)\]]*\)(, \[(Source Code|Clients)\]\([^)\]]*\))?(, \[(Source Code|Clients)\]\([^)\]]*\))*\))(?= \`?)/;
|
2020-08-16 02:16:50 +02:00
|
|
|
const testD1 = /(^- \W[a-zA-Z0-9-_ .]*\W{0,2}http[^\[]*)(?<= )/;
|
2020-02-08 14:06:37 +01:00
|
|
|
const testD2 = /(\`.*\` \`.*\`$)/;
|
2020-02-08 20:15:06 +01:00
|
|
|
if ((testC > -1) && (!testD.test(text))) {
|
2020-02-08 14:06:37 +01:00
|
|
|
let d1 = testD1.exec(text)[1];
|
|
|
|
let d2 = testD2.exec(text)[1];
|
2020-02-08 22:41:06 +01:00
|
|
|
return chalk.red(text.replace(d1, '').replace(d2, ''))
|
2020-02-08 14:06:37 +01:00
|
|
|
} else if (testC > -1) {
|
|
|
|
return chalk.green(testD.exec(text)[1])
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests '`License` `Language`'
|
|
|
|
function testLangLic(text) {
|
|
|
|
const testD2 = /(\`.*\` \`.*\`$)/;
|
|
|
|
let testE = testD2.test(text);
|
|
|
|
const testE1 = /(^[^`]*)/;
|
2020-02-08 20:15:06 +01:00
|
|
|
if (!testE) {
|
2020-02-08 14:06:37 +01:00
|
|
|
let e1 = testE1.exec(text)[1];
|
2020-02-08 20:15:06 +01:00
|
|
|
return chalk.red(text.replace(e1, ''))
|
2020-02-08 14:06:37 +01:00
|
|
|
}
|
|
|
|
return chalk.green(testD2.exec(text)[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
//Runs all the syntax tests...
|
|
|
|
function findError(text) {
|
|
|
|
let res
|
|
|
|
res = testMainLink(text)
|
|
|
|
res += testDescription(text)
|
|
|
|
res += testSrcDemCli(text)
|
|
|
|
res += testLangLic(text)
|
|
|
|
return res + `\n`
|
|
|
|
}
|
2020-02-08 20:15:06 +01:00
|
|
|
|
2020-02-08 14:06:37 +01:00
|
|
|
//Check if license is in the list of licenses.
|
|
|
|
function testLicense(md) {
|
2020-02-08 20:15:06 +01:00
|
|
|
let pass = true;
|
|
|
|
let lFailed = []
|
|
|
|
let lPassed = []
|
2020-02-08 22:41:06 +01:00
|
|
|
const regex = /.*\`(.*)\` .*$/;
|
|
|
|
try {
|
|
|
|
for (l of regex.exec(md)[1].split("/")) {
|
|
|
|
if (!licenses.has(l)) {
|
|
|
|
pass = false;
|
|
|
|
lPassed.push(l)
|
|
|
|
}
|
|
|
|
lFailed.push(l)
|
2020-02-08 20:15:06 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-08 22:41:06 +01:00
|
|
|
catch(err) {
|
|
|
|
console.log(chalk.yellow("Error in License syntax, license not checked against list."))
|
|
|
|
return [false, "", ""]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-02-08 20:15:06 +01:00
|
|
|
return [pass, lFailed, lPassed]
|
2020-02-08 14:06:37 +01:00
|
|
|
}
|
|
|
|
|
2020-02-08 20:15:06 +01:00
|
|
|
|
2020-02-08 14:06:37 +01:00
|
|
|
//Parses name from entry
|
|
|
|
function parseName(md) {
|
|
|
|
const regex = /^\W*(.*?)\W/
|
|
|
|
return regex.exec(md)[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
function entryErrorCheck() {
|
|
|
|
const lines = split(readme); // Inserts each line into the entries array
|
2017-04-12 01:38:29 +02:00
|
|
|
let totalFail = 0;
|
|
|
|
let totalPass = 0;
|
|
|
|
let total = 0;
|
2020-02-08 14:06:37 +01:00
|
|
|
let entries = [];
|
|
|
|
let diffEntries = [];
|
|
|
|
|
|
|
|
if (lines[0] === "") {
|
2020-02-08 20:15:06 +01:00
|
|
|
console.log(chalk.red("0 Entries Found, check your commandline arguments"))
|
2019-08-22 00:30:28 +02:00
|
|
|
process.exit(0)
|
|
|
|
}
|
2020-02-08 14:06:37 +01:00
|
|
|
for (let i = 0; i < lines.length; i ++) { // Loop through array of lines
|
|
|
|
if (entryFilter(lines[i]) === true) { // filter out lines that don't start with * [)
|
|
|
|
e = {};
|
|
|
|
e.raw = lines[i];
|
2020-02-08 20:45:29 +01:00
|
|
|
e.line = i + 1
|
2020-02-08 14:06:37 +01:00
|
|
|
entries.push(e);
|
|
|
|
} else if (licenseFilter(lines[i]) === true) {
|
|
|
|
licenses.add(parseLicense(lines[i]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pr === true) {
|
2020-02-08 20:15:06 +01:00
|
|
|
console.log(chalk.cyan("Only testing the diff from the PR.\n"))
|
2020-02-08 14:06:37 +01:00
|
|
|
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 * [)
|
|
|
|
e = {};
|
|
|
|
e.raw = l;
|
|
|
|
diffEntries.push(e);
|
|
|
|
} else if (licenseFilter(l) === true) {
|
|
|
|
licenses.add(parseLicense(l))
|
2016-12-10 14:46:24 +01:00
|
|
|
}
|
2017-04-12 01:38:29 +02:00
|
|
|
}
|
2020-02-08 20:15:06 +01:00
|
|
|
if (diffEntries.length === 0) {
|
|
|
|
console.log("No entries changed in README.md, Exiting...")
|
|
|
|
process.exit(0)
|
|
|
|
}
|
2020-02-08 14:06:37 +01:00
|
|
|
total = diffEntries.length
|
|
|
|
for (let e of diffEntries) {
|
|
|
|
e.pass = true
|
|
|
|
e.name = parseName(e.raw)
|
|
|
|
if (!findPattern(e.raw)) {
|
|
|
|
e.highlight = findError(e.raw);
|
|
|
|
e.pass = false;
|
2020-02-08 22:41:06 +01:00
|
|
|
console.log(e.highlight)
|
2020-02-08 14:06:37 +01:00
|
|
|
}
|
|
|
|
e.licenseTest = testLicense(e.raw);
|
2020-02-08 20:15:06 +01:00
|
|
|
if (!e.licenseTest) {
|
2020-02-08 14:06:37 +01:00
|
|
|
e.pass = false;
|
2020-02-08 20:15:06 +01:00
|
|
|
console.log(chalk.red(`${e.name}'s license is not on License list.`))
|
2020-02-08 14:06:37 +01:00
|
|
|
}
|
|
|
|
if (e.pass) {
|
|
|
|
totalPass++
|
|
|
|
} else {
|
|
|
|
totalFail++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-02-08 20:15:06 +01:00
|
|
|
console.log(chalk.cyan("Testing entire README.md\n"))
|
2020-02-08 14:06:37 +01:00
|
|
|
total = entries.length
|
|
|
|
for (let e of entries) {
|
|
|
|
e.pass = true
|
|
|
|
e.name = parseName(e.raw)
|
|
|
|
if (!findPattern(e.raw)) {
|
|
|
|
e.highlight = findError(e.raw);
|
|
|
|
e.pass = false;
|
2020-02-08 20:15:06 +01:00
|
|
|
console.log(`${chalk.yellow(e.line + ": ")}${e.highlight}`);
|
|
|
|
syntax = e.highlight;
|
2020-02-08 14:06:37 +01:00
|
|
|
}
|
|
|
|
e.licenseTest = testLicense(e.raw);
|
2020-02-08 20:15:06 +01:00
|
|
|
if (!e.licenseTest[0]) {
|
2020-02-08 14:06:37 +01:00
|
|
|
e.pass = false;
|
2020-02-08 20:15:06 +01:00
|
|
|
console.log(chalk.yellow(e.line + ": ") + `${e.name}'s license ${chalk.red(`'${e.licenseTest[1]}'`)} is not on the License list.\n`)
|
2020-02-08 14:06:37 +01:00
|
|
|
}
|
|
|
|
if (e.pass) {
|
|
|
|
totalPass++
|
|
|
|
} else {
|
|
|
|
totalFail++
|
|
|
|
}
|
2020-02-08 20:15:06 +01:00
|
|
|
}
|
2016-12-10 14:46:24 +01:00
|
|
|
}
|
2020-02-08 14:06:37 +01:00
|
|
|
if (totalFail > 0) {
|
|
|
|
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`))
|
2017-04-12 01:38:29 +02:00
|
|
|
process.exit(1);
|
2020-02-08 14:06:37 +01:00
|
|
|
} else {
|
|
|
|
console.log(chalk.blue(`\n-----------------------------\n`))
|
|
|
|
console.log(chalk.green(`${totalPass} Passed of ${total}`))
|
|
|
|
console.log(chalk.blue(`\n-----------------------------\n`))
|
|
|
|
process.exit(0)
|
2016-12-10 14:46:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 14:06:37 +01:00
|
|
|
parseArgs(process.argv)
|
|
|
|
entryErrorCheck();
|