Skip to content

Commit

Permalink
add --progress flag to avoid stdout spam
Browse files Browse the repository at this point in the history
  • Loading branch information
a-hariti committed Jul 24, 2024
1 parent 14d7665 commit 0430b83
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 31 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@
"@spotlightjs/spotlight": "^2.0.0-alpha.1",
"@tailwindcss/forms": "^0.5.7",
"@tailwindcss/typography": "^0.5.10",
"@types/cli-progress": "^3.11.6",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
Expand Down
34 changes: 11 additions & 23 deletions scripts/lint-404s/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {readFileSync} from 'fs';
import path, {dirname} from 'path';
import {fileURLToPath} from 'url';

import cliProgress from 'cli-progress';

const baseURL = 'http://localhost:3000/';
type Link = {href: string; innerText: string};

Expand All @@ -14,6 +12,8 @@ const trimSlashes = (s: string) => s.replace(/(^\/|\/$)/g, '');
// @ts-ignore
const ignoreListFile = path.join(dirname(import.meta.url), './ignore-list.txt');

const showProgress = process.argv.includes('--progress');

// Paths to skip
const ignoreList: string[] = readFileSync(fileURLToPath(ignoreListFile), 'utf8')
.split('\n')
Expand Down Expand Up @@ -88,12 +88,9 @@ async function main() {
return false;
}

const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
progressBar.start(slugs.length, 0);

for (let i = 0; i < slugs.length; i++) {
const slug = slugs[i];
for (const slug of slugs) {
const pageUrl = new URL(slug, baseURL);
const now = performance.now();
const html = await fetchWithFollow(pageUrl.href).then(r => r.text());

const linkRegex = /<a[^>]*href="([^"]*)"[^>]*>([^<]*)<\/a>/g;
Expand All @@ -116,7 +113,12 @@ async function main() {
all404s.push({slug, page404s});
}

progressBar.increment();
if (showProgress) {
console.log(
page404s.length ? '❌' : '✅',
`in ${(performance.now() - now).toFixed(1).padStart(4, '0')} ms | ${slug}`
);
}
}

if (all404s.length === 0) {
Expand All @@ -143,22 +145,8 @@ async function main() {
return true;
}
const now = performance.now();

const humanReadableMs = (ms: number) => {
const oneSecond = 1000;
const oneMinute = oneSecond * 60;
if (ms < oneSecond) {
return `${ms.toFixed(1)} ms`;
}
if (ms < oneMinute) {
return `${(ms / 1000).toFixed(1)} s`;
}
// show minutes and seconds
return `${Math.floor(ms / oneMinute)} m ${((ms % oneMinute) / 1000).toFixed(1)} s`;
};

main().then(has404s => {
console.log(`\n Done in ${humanReadableMs(performance.now() - now)}`);
console.log(`\n Done in ${(performance.now() - now).toFixed(1)} ms`);
process.exit(has404s ? 1 : 0);
});

Expand Down
7 changes: 0 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2815,13 +2815,6 @@
resolved "https://registry.yarnpkg.com/@types/caseless/-/caseless-0.12.5.tgz#db9468cb1b1b5a925b8f34822f1669df0c5472f5"
integrity sha512-hWtVTC2q7hc7xZ/RLbxapMvDMgUnDvKvMOpKal4DrMyfGBUfB1oKaZlIRr6mJL+If3bAP6sV/QneGzF6tJjZDg==

"@types/cli-progress@^3.11.6":
version "3.11.6"
resolved "https://registry.yarnpkg.com/@types/cli-progress/-/cli-progress-3.11.6.tgz#94b334ebe4190f710e51c1bf9b4fedb681fa9e45"
integrity sha512-cE3+jb9WRlu+uOSAugewNpITJDt1VF8dHOopPO4IABFc3SXYL5WE/+PTz/FCdZRRfIujiWW3n3aMbv1eIGVRWA==
dependencies:
"@types/node" "*"

"@types/connect@*":
version "3.4.38"
resolved "https://registry.yarnpkg.com/@types/connect/-/connect-3.4.38.tgz#5ba7f3bc4fbbdeaff8dded952e5ff2cc53f8d858"
Expand Down

0 comments on commit 0430b83

Please sign in to comment.