diff --git a/src/index.js b/src/index.js index 178e660..9609d2d 100755 --- a/src/index.js +++ b/src/index.js @@ -20,9 +20,6 @@ module.exports.exec = ( }, ] ) => { - //right now URL is just string - //TODO : Check the type of URL, if string then below code, if list i.e multiple URLs then handle that - // Resolution will be a list of list in that case or Better have a dictionary and iterate over that return (() => { console.log("Running...") var auditState = webReview.auditState; @@ -32,19 +29,26 @@ module.exports.exec = ( auditState.url = -1; return; } - webReview.lh( - urlList[auditState.url].url, - urlList[auditState.url].category - ); + urlList[auditState.url].url.forEach(url => { + webReview.lh( + url, + urlList[auditState.url].category + ); + }); }); for (const key in urlList) { const urlObj = urlList[key]; - var dir = new URL(urlObj.url).hostname; - if (!fs.existsSync(dir)) { - fs.mkdirSync(dir); - } - webReview.ss(urlObj.url, urlObj.resolution); + urlObj.url.forEach(url =>{ + var urlNewObj = new URL(url); + var dir = urlNewObj.hostname + "/" + urlNewObj.pathname; + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir,{recursive:true},(err) => { + if(err) throw new err; + }); + } + webReview.ss(url, urlObj.resolution); + }); } //begin auditing if (auditState.url === -1) auditState.url = 0; diff --git a/src/main.js b/src/main.js index e424b6f..c82a125 100755 --- a/src/main.js +++ b/src/main.js @@ -53,9 +53,15 @@ module.exports.ss = ( //TODO : Check the type of URL, if string then below code, if list i.e multiple URLs then handle that // Resolution will be a list of list in that case or Better have a dictionary and iterate over that return (async () => { - var dir = "./" + new URL(url).hostname + "/screenshots"; + var urlObj = new URL(url); + const hostname = urlObj.hostname; + const path = urlObj.pathname + var dir = "./" + if(hostname && hostname != "") dir = dir + hostname; + if(path && path != "") dir = dir + "/" + path; + dir = dir + "/screenshots" if (!fs.existsSync(dir)) { - fs.mkdirSync(dir); + fs.mkdirSync(dir,{recursive:true}); } await new Pageres({ delay: 2 }).src(url, resolution).dest(dir).run(); @@ -80,11 +86,18 @@ module.exports.lh = (url = "https://github.com", categories = []) => { // console.log(opts); launchChromeAndRunLighthouse(url, opts).then((results) => { const html = ReportGenerator.generateReport(results, "html"); - var filename = new URL(url).hostname; + var urlObj = new URL(url); + const hostname = urlObj.hostname + const path = urlObj.pathname + var dir = "./" + var filename = "" + if(hostname && hostname != "") {dir = dir + hostname; filename = filename + hostname} + if(path && path!="") {dir = dir + "/" + path; filename = filename + "." + path} + dir = dir+"/reports" filename += ".html"; - var dir = "./" + new URL(url).hostname + "/reports"; + filename = filename.replace(/\//g,'') if (!fs.existsSync(dir)) { - fs.mkdirSync(dir); + fs.mkdirSync(dir,{recursive:true}); } fs.writeFile(Path.join(dir, filename), html, function (err) { if (err) { @@ -93,6 +106,9 @@ module.exports.lh = (url = "https://github.com", categories = []) => { console.log("Finished Generating Audit Report for " + url); auditState.url += 1; }); + }).catch((err) => { + console.log("Unable to launch chrome and run lighthouse for url",url) + console.log(err); }); }; diff --git a/test.js b/test.js index 1a9263d..cb7cbfc 100644 --- a/test.js +++ b/test.js @@ -1,7 +1,7 @@ const webReview = require("./src/index"); webReview.exec([ { - url: "https://www.github.com/", + url: ["https://www.github.com/marketplace"], resolution: [ "480x320", "1024x768", @@ -11,7 +11,7 @@ webReview.exec([ ], }, { - url: "https://www.google.com", + url: ["https://www.google.com"], resolution: [ "480x320", "1024x768",