link-checker-cli
Version:
CLI tool to check for broken links in a website or project
25 lines (24 loc) • 1.28 kB
JavaScript
import chalk from "chalk";
import { CheckLinkView, GetUrlsView } from "../views.js";
import { LinksProcessor } from "./linksProcessor.js";
import { renderWithTask, render } from "hanji";
export class SitemapCommandProcessor {
constructor(options) {
this.options = options;
this.inspectSitemap = async () => {
const { source } = this.options;
const getView = new GetUrlsView();
const links = await renderWithTask(getView, this.linksProcessor.getAllSitemapLinks(source, getView));
render(`Unique links found: ${links.length}`);
const checkLinksView = new CheckLinkView(links.length);
const { brokenLinks, validCount } = await renderWithTask(checkLinksView, this.linksProcessor.checkAllLinks(links, ["internal", "anchor"], checkLinksView));
render(chalk.green("Valid links:", validCount));
render(chalk.red("Unavailable links:", brokenLinks.length));
brokenLinks.map(({ link, statusCode, message }) => {
render(chalk.red(`ERROR ${statusCode}: ${message ? message : ""} `) + chalk.gray(link));
});
};
const { concurrencySize } = options;
this.linksProcessor = new LinksProcessor(concurrencySize);
}
}