From 23bd4e2f9d7c7c6eb54fe0377a2af7f34982f53e Mon Sep 17 00:00:00 2001 From: Vincent Rubinetti Date: Wed, 13 Mar 2024 21:19:17 -0400 Subject: [PATCH] better logging for link check --- check.js | 8 +++++--- core.js | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/check.js b/check.js index a6a2160..0ca7667 100644 --- a/check.js +++ b/check.js @@ -6,7 +6,7 @@ onExit(); async function checkList(list) { return await Promise.all( // for each redirect - list.map(async ({ to }) => { + list.map(async ({ to, file, index }) => { try { // do simple request to target url const response = await fetch(to); @@ -19,10 +19,12 @@ async function checkList(list) { ) throw Error(response.status); } catch (error) { - addError(`"to: ${to}" may be a broken link\n(${error})`); + addError( + `${file} entry ${index} "to: ${to}" may be a broken link\n (${error})` + ); } }) ); } -await checkList(getList()); +await checkList(getList(true)); diff --git a/core.js b/core.js index e0f0388..8e17743 100644 --- a/core.js +++ b/core.js @@ -7,7 +7,7 @@ import { parse } from "yaml"; export const verbose = !!process.env.RUNNER_DEBUG; // get full list of redirects -export function getList() { +export function getList(meta) { // get yaml files that match glob pattern const files = globSync("*.y?(a)ml", { cwd: __dirname }); @@ -63,12 +63,16 @@ export function getList() { // normalize "from" field. lower case, remove leading slashes. entry.from = entry.from.toLowerCase().replace(/^(\/+)/, ""); + // record meta for logging + entry.file = file; + entry.index = index; + // add to combined list list.push(entry); - // add to duplicate list. record source file and entry number for logging. + // add to duplicate list duplicates[entry.from] ??= []; - duplicates[entry.from].push({ ...entry, file, index }); + duplicates[entry.from].push(entry); } } @@ -87,6 +91,13 @@ export function getList() { addError(`"from: ${from}" appears ${count} time(s): ${duplicates}`); } + // if meta not requested, clean up + if (!meta) + list.forEach((entry) => { + delete entry.file; + delete entry.index; + }); + return list; }