Skip to content

Commit

Permalink
Support for html file validation
Browse files Browse the repository at this point in the history
  • Loading branch information
achmurali committed Jun 27, 2021
1 parent 8b2543b commit c4b965a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { validator } = require('./lib/validator');
const { validator, validatorFs } = require('./lib/validator');

module.exports = {
validator
validator,
validatorFs
};
17 changes: 17 additions & 0 deletions lib/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const colors = require('colors');

const log = {
warning:(str) => {
console.log(colors.yellow(str))
},
info:(str) => {
console.log(str)
},
error:(str) => {
console.log(colors.red(str))
}
};

module.exports = {
log
};
50 changes: 36 additions & 14 deletions lib/validator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
const cheerio = require('cheerio');
const colors = require('colors');
const fs = require('fs').promises;
const { statSync } = require('fs');

const { log } = require("./log");
const tagConfig = require("./config.json");


const validatorFs = async (pathToFile,tagstoValidate) => {
try{
if(!checkFile(pathToFile))
throw new Error(`ERROR: ${pathToFile} is not a valid HTML file`)

const stat = await statSync(pathToFile);

if(!stat.isFile())
throw new Error(`ERROR: ${pathToFile} is not a file`)

const data = await fs.readFile(pathToFile,'utf-8');

validator(data,tagstoValidate);
}
catch(err){
if(err.code === 'ENOENT')
log.error(`ERROR: ${pathToFile} doesn't exists \n ${err.stack}`);
else
log.error(`ERROR : ${err.message} \n ${err.stack}`);

}
}

const checkFile = (pathToFile) => {
let names = pathToFile.split('.');
if(names[names.length - 1] !== 'html')
return false;
return true;
}

const validator = (requestBody, tagstoValidate) => {
try {
if (!tagstoValidate || tagstoValidate.length === 0)
Expand Down Expand Up @@ -101,19 +134,8 @@ const checkTagCount = ($, tagName, tagCount) => {
log.info(`INFO : No <${tagName}> tag(s)`);
}

const log = {
warning:(str) => {
console.log(colors.yellow(str))
},
info:(str) => {
console.log(str)
},
error:(str) => {
console.log(colors.red(str))
}
}


module.exports = {
validator
validator,
validatorFs
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "seo-tags-validator",
"version": "1.0.0",
"version": "1.1.0",
"description": "A library to validate SEO tags with customisation",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit c4b965a

Please sign in to comment.