Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/fuzzy-kids-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@patricktree/fetch-favicon': minor
---

- add a fetch-favicon CLI for batch URL input
- keep fetching other favicons when one fails, with clearer source logging
2 changes: 2 additions & 0 deletions packages/fetch-favicon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@
"turbo:lint:fix": "pnpm run lint:file . --fix"
},
"dependencies": {
"@commander-js/extra-typings": "^14.0.0",
"@patricktree/commons-ecma": "workspace:^",
"@patricktree/commons-node": "workspace:^",
"commander": "^14.0.2",
"tiny-invariant": "^1.3.3",
"zod": "^3.23.8"
},
Expand Down
45 changes: 45 additions & 0 deletions packages/fetch-favicon/src/fetch-favicon-cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env node
/* eslint-disable n/no-process-exit -- is a CLI */

import * as commander from '@commander-js/extra-typings';
import { writeFile } from 'node:fs/promises';

import { fetchFavicons } from '#pkg/index.js';

const program = new commander.Command()
.name('fetch-favicon')
.addArgument(new commander.Argument('[url...]'))
.addOption(new commander.Option('-o, --output <path>', 'Write JSON output to a file'))
.addOption(new commander.Option('--stdin', 'Read URLs from stdin (whitespace-separated)'))
.addOption(new commander.Option('--no-pretty', 'Minify JSON output'));

program.parse();

const options = program.opts();
const hrefs = program.processedArgs[0];
const shouldReadStdin = options.stdin || (!process.stdin.isTTY && hrefs.length === 0);
const stdinUrls = shouldReadStdin ? await readStdinUrls() : [];
const uniqueHrefs = [...new Set([...hrefs, ...stdinUrls])];

if (uniqueHrefs.length === 0) {
program.outputHelp();
process.exit(1);
}

const normalizedHrefs = uniqueHrefs.map((href) => new URL(href).href);
const result = await fetchFavicons(normalizedHrefs);
const json = JSON.stringify(result, null, options.pretty ? 2 : 0);

if (options.output) {
await writeFile(options.output, `${json}\n`, 'utf8');
} else {
console.log(json);
}

async function readStdinUrls(): Promise<string[]> {
const chunks: string[] = [];
for await (const chunk of process.stdin) {
chunks.push(String(chunk));
}
return chunks.join('').split(/\s+/).filter(Boolean);
}
31 changes: 20 additions & 11 deletions packages/fetch-favicon/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import playwright from 'playwright';
import invariant from 'tiny-invariant';
import { z } from 'zod';

import { arrays } from '@patricktree/commons-ecma/util/arrays';
import { check } from '@patricktree/commons-ecma/util/assert';
import { binaryUtils } from '@patricktree/commons-node/utils/binary';

Expand Down Expand Up @@ -45,29 +44,39 @@ export async function fetchFavicons(hrefs: string[]): Promise<FaviconsForWebsite
}

console.log('Step #2: Gather a list of favicon URLs we need to fetch (with duplicates removed)');
let allIconURLs: URL[] = [];
for (const entry of Object.values(websites)) {
const allIconURLs: Array<{ url: URL; sources: Set<string> }> = [];
function addIconTarget(iconURL: string, websiteHref: string) {
const existing = allIconURLs.find((entry) => entry.url.href === iconURL);
if (existing) {
existing.sources.add(websiteHref);
return;
}
allIconURLs.push({ url: new URL(iconURL), sources: new Set([websiteHref]) });
}
for (const [websiteHref, entry] of Object.entries(websites)) {
invariant(entry);
if (check.isNonEmptyString(entry.iconURLs.light)) {
allIconURLs.push(new URL(entry.iconURLs.light));
addIconTarget(entry.iconURLs.light, websiteHref);
}
if (check.isNonEmptyString(entry.iconURLs.dark)) {
allIconURLs.push(new URL(entry.iconURLs.dark));
addIconTarget(entry.iconURLs.dark, websiteHref);
}
}
allIconURLs = arrays.uniqueValues(allIconURLs);

console.log('Step #3: Go to every favicon URL and store the favicon as a data URL');
const icons: FaviconsForWebsites['icons'] = {};
await Promise.all(
allIconURLs.map(async (url) => {
console.log(`Fetching favicon from ${url.href}`);
for (const { url, sources } of allIconURLs) {
console.log(`Fetching favicon from ${url.href}`);
try {
const response = await fetchUrl(url);
const blob = await response.blob();
const dataURL = await binaryUtils.convertBlobToDataURL(blob);
icons[url.href] = { dataURL };
}),
);
} catch (error) {
const sourcesText = [...sources].join(', ');
console.error(`Failed to fetch favicon from ${url.href} (source: ${sourcesText})`, error);
}
}

console.log('Step #4: Close the puppeteer browser');
await browser.close();
Expand Down
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.