Skip to content

Instantly share code, notes, and snippets.

@humphd
Created September 21, 2020 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save humphd/44b0bf55d4da896affcf6b8ef2074991 to your computer and use it in GitHub Desktop.
Save humphd/44b0bf55d4da896affcf6b8ef2074991 to your computer and use it in GitHub Desktop.
Link Checker
const linkchecker = (link) => {
//processes Links
let status = 200;
fetch(link.url).then(res => {
const status = res.status;
switch (status) {
case 200:
link.status = StatusEnum.good;
console.log(chalk.green(link.toString()));
break;
case 400:
case 404:
link.status = StatusEnum.bad;
console.log(chalk.red(link.toString()));
break;
default:
console.log(chalk.grey(link.toString()));
})
.catch(err => console.log('...')
};
const documentProccessing = async (doc) => {
//processDocument
try {
let data = await fs.readFile(process.cwd() + "/" + doc, "utf8"); // gets the data in the document
new Set(doc.match(link_reg)).forEach((e) => {
// gets all http/https linnks in the document and create a Link and checks it
let checkThis = new Link(e, StatusEnum.unknown);
linkchecker(checkThis);
});
} catch (e) {
console.log(e);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment