-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to build that detects broken lins
- Loading branch information
1 parent
17486de
commit 5c12bbd
Showing
4 changed files
with
2,887 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const blc = require('broken-link-checker'); | ||
const express = require('express'); | ||
const fs = require('fs'); | ||
|
||
const app = express() | ||
app.use(express.static('build/site')) | ||
|
||
|
||
const server = app.listen(3000, () => { | ||
console.log(`Started serving files on port 3000!`) | ||
}) | ||
|
||
String.prototype.trimUrl = function () { | ||
return this.replaceAll("http://localhost:3000", "") | ||
} | ||
|
||
const brokenLinks = [] | ||
const siteChecker = new blc.SiteChecker({ | ||
excludeExternalLinks: true, | ||
honorRobotExclusions: false, | ||
maxSocketsPerHost: 3, | ||
maxSockets: 10, | ||
}, { | ||
link: (result) => { | ||
if (result.broken && result.url.resolved) { | ||
let statusCode = result.http.response.statusCode; | ||
console.log(`Broken link: ${result.url.resolved?.trimUrl()} (${statusCode}) on page ${result.base.resolved?.trimUrl()}`) | ||
brokenLinks.push(result.url.resolved.trimUrl()) | ||
} | ||
}, | ||
end: () => { | ||
server.close() | ||
if (brokenLinks.length > 0) { | ||
process.exitCode = 1 | ||
} | ||
} | ||
} | ||
) | ||
|
||
siteChecker.enqueue("http://localhost:3000/home/index.html") |
Oops, something went wrong.