link-checker-cli
Version:
CLI tool to check for broken links in a website or project
36 lines (35 loc) • 1.76 kB
JavaScript
import chalk from "chalk";
import { CheckLinkView, GetUrlsView } from "../views.js";
import { LinksProcessor } from "./linksProcessor.js";
import { render, renderWithTask } from "hanji";
export class CliCommandProcessor {
constructor(options) {
this.options = options;
this.createResult = async () => {
const { image, external, source, style, recursive } = this.options;
const included = ["internal", "anchor"];
if (external)
included.push("external");
if (style)
included.push("style");
if (image)
included.push("image");
const getLinksView = new GetUrlsView();
const links = await renderWithTask(getLinksView, this.linksProcessor.getAllLinks(source, recursive, getLinksView));
render(`Unique links found: ${links.length}`);
const checkLinksView = new CheckLinkView(links.length);
const { brokenLinks, validCount, excluded } = await renderWithTask(checkLinksView, this.linksProcessor.checkAllLinks(links, included, checkLinksView));
render(chalk.green("Valid links:", validCount) +
" " +
chalk.gray(`${included.toString()}`));
render(chalk.red("Unavailable links:", brokenLinks.length));
render(chalk.gray("Excluded links:", excluded));
brokenLinks.map(({ link, statusCode, message, parent }) => {
render(chalk.red(`ERROR ${statusCode}: ${message ? message : ""} `) +
chalk.gray(link) + chalk.red(parent));
});
};
const { concurrencySize } = options;
this.linksProcessor = new LinksProcessor(concurrencySize);
}
}