Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added summary of broken links at the end of cli #148

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"runtimeVersion": "8.16.0",
"name": "Launch Program",
"program": "${workspaceFolder}/bin/blc",
"args": ["https://preprod.ergosign.de/de/news/2017/event-DKM2017.html", "-ro"]
}
]
}
30 changes: 21 additions & 9 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@ function log()



function logMetrics(brokenLinks, excludedLinks, totalLinks, duration, preBreak, exit)
function logMetrics(brokenLinks, excludedLinks, totalLinks, duration, preBreak, exit, brokenLinksArray)
{
var output = preBreak===true ? "\n" : "";

output += chalk.gray("Finished! "+totalLinks+" links found.");
output += chalk.cyan("Finished! "+totalLinks+" links found.");

if (excludedLinks > 0)
{
Expand All @@ -212,19 +212,29 @@ function logMetrics(brokenLinks, excludedLinks, totalLinks, duration, preBreak,
{
output += chalk.gray(" ");
output += chalk[ brokenLinks>0 ? "red" : "green" ](brokenLinks+" broken");
output += chalk.gray(".");
output += chalk.cyan(".");
}

if (duration != null)
{
output += chalk.gray("\nElapsed time: ");
output += chalk.gray( humanizeDuration(duration, {round:true, largest:2}) );
output += chalk.cyan("\nElapsed time: ");
output += chalk.cyan( humanizeDuration(duration, {round:true, largest:2}) );
}

if (exit === true)
{
for (let i = 0; i < brokenLinksArray.length; i++) {
const link = brokenLinksArray[i];

output += chalk.red(`\n [${link.http.response.statusCode}] ${link.http.response.url}`);
output += chalk.yellow(`\n on ${link.base.original}`);
}
}

log(output);

if (exit === true)
{
{
process.exit(brokenLinks===0 ? 0 : 1);
}
}
Expand All @@ -234,11 +244,11 @@ function logMetrics(brokenLinks, excludedLinks, totalLinks, duration, preBreak,
/*
Ensure that `logMetrics()` is called after `logResults_delayed()`.
*/
function logMetrics_delayed(brokenLinks, excludedLinks, totalLinks, duration, preBreak, exit)
function logMetrics_delayed(brokenLinks, excludedLinks, totalLinks, duration, preBreak, exit, brokenLinksArray)
{
setImmediate( function()
{
logMetrics(brokenLinks, excludedLinks, totalLinks, duration, preBreak, exit);
logMetrics(brokenLinks, excludedLinks, totalLinks, duration, preBreak, exit, brokenLinksArray);
});
}

Expand Down Expand Up @@ -400,6 +410,7 @@ function run(url, checkerOptions, logOptions)
total:
{
brokenLinks: 0,
brokenLinksArray: [],
excludedLinks: 0,
links: 0,
pages: 0,
Expand Down Expand Up @@ -449,6 +460,7 @@ function run(url, checkerOptions, logOptions)
{
data.page.brokenLinks++;
data.total.brokenLinks++;
data.total.brokenLinksArray.push(result);
}

data.page.totalLinks++;
Expand Down Expand Up @@ -486,7 +498,7 @@ function run(url, checkerOptions, logOptions)
}
else if (data.total.pages > 1)
{
logMetrics_delayed(data.total.brokenLinks, data.total.excludedLinks, data.total.links, Date.now()-data.total.startTime, true, true);
logMetrics_delayed(data.total.brokenLinks, data.total.excludedLinks, data.total.links, Date.now()-data.total.startTime, true, true, data.total.brokenLinksArray);
}
}
};
Expand Down
10 changes: 7 additions & 3 deletions lib/internal/checkUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,20 @@ function checkUrl(link, baseUrl, cache, options, retry)
var request = bhttp.request(link.url.resolved, // TODO :: https://github.com/joepie91/node-bhttp/issues/3
{
discardResponse: true,
headers: { "user-agent":options.userAgent },
method: retry!==405 ? options.requestMethod : "get"
headers: {
"user-agent": options.userAgent,
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"
},
method: retry !== 405 ? options.requestMethod : "get"
})
.then( function(response)
{
response = simpleResponse(response);

if (response.statusCode===405 && options.requestMethod==="head" && options.retry405Head===true && retry!==405)
if ((response.statusCode === 405 || response.statusCode === 404 || response.statusCode === 500) && options.requestMethod==="head" && options.retry405Head===true && retry!==405)
{
// Retry possibly broken server with "get"
console.log('RETRYING WITH GET: ' + link.url.resolved);
return checkUrl(link, baseUrl, cache, options, 405);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/internal/defaultOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ var defaultOptions =
requestMethod: "head",
retry405Head: true,
tags: require("./tags"),
userAgent: userAgent(pkg.name, pkg.version)
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
//userAgent: userAgent(pkg.name, pkg.version)
};


Expand Down