Skip to content

Commit

Permalink
better logging for link check
Browse files Browse the repository at this point in the history
  • Loading branch information
vincerubinetti committed Mar 14, 2024
1 parent c03c54c commit 23bd4e2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
8 changes: 5 additions & 3 deletions check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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));
17 changes: 14 additions & 3 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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;
}

Expand Down

0 comments on commit 23bd4e2

Please sign in to comment.