Skip to content

Curated website design inspiration gallery, open zero-auth MCP server for AI agents, and shareable moodboard lists.

3.5 KB
Raw Blame
import { readFile, writeFile, mkdir, unlink } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import { resolve, join } from "node:path";
import { createHash } from "node:crypto";
import sharp from "sharp";
import { safeWebsite, CATEGORIES, validateSites } from "./gallery-catalog.mjs";
const args = process.argv.slice(2);
if (args.includes("--help") || !args.length) {
  console.log(
    `Add a website from a local screenshot (no network capture).\n\nnpm run site:add -- --id my-site --name "My Site" --url https://example.com --category "AI tools" --image /path/to/screenshot.png --description "A concise introduction." --observation "What caught your eye." --credit "Designer or site owner" --date YYYY-MM-DD\n\nOptional: --tags "Minimal,Light"\nCategories: ${CATEGORIES.join(", ")}\nUse the actual screenshot date. Requires a screenshot at least 1000px wide and 600px high. Then run npm test and open a PR.`,
  );
  process.exit(0);
}
const a = {};
for (let i = 0; i < args.length; i += 2) {
  if (!args[i].startsWith("--") || args[i + 1] === undefined)
    throw Error("Expected --option value pairs.");
  a[args[i].slice(2)] = args[i + 1];
}
for (const key of [
  "id",
  "name",
  "url",
  "category",
  "image",
  "description",
  "observation",
  "credit",
  "date",
])
  if (!a[key]) throw Error(`Missing --${key}`);
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(a.id))
  throw Error("Use a lowercase slug for --id");
safeWebsite(a.url);
if (!CATEGORIES.includes(a.category)) throw Error("Unknown category.");
if (
  !/^\d{4}-\d{2}-\d{2}$/.test(a.date) ||
  !Number.isFinite(Date.parse(a.date)) ||
  new Date(a.date).toISOString().slice(0, 10) !== a.date
)
  throw Error("Use a real capture date.");
const root = fileURLToPath(new URL("../apps/gallery/", import.meta.url)),
  path = join(root, "sites.json"),
  sites = JSON.parse(await readFile(path, "utf8"));
if (
  sites.some(
    (s) =>
      s.id === a.id || s.url.replace(/\/$/, "") === a.url.replace(/\/$/, ""),
  )
)
  throw Error("Site is already in the collection.");
const input = resolve(a.image),
  meta = await sharp(input, { limitInputPixels: 40000000 }).metadata();
if (
  !["jpeg", "png", "webp"].includes(meta.format) ||
  meta.width < 1000 ||
  meta.height < 600
)
  throw Error("Use a JPEG, PNG, or WebP screenshot at least 1000 × 600.");
const data = await sharp(input, { limitInputPixels: 40000000 })
  .resize({ width: 1440, withoutEnlargement: true })
  .jpeg({ quality: 82, mozjpeg: true })
  .toBuffer();
const m = await sharp(data).metadata();
if (data.length > 3 * 1024 * 1024 || m.height > 16000)
  throw Error("Screenshot is too long or large. Capture a shorter section.");
const entry = {
  id: a.id,
  name: a.name,
  url: a.url,
  category: a.category,
  tags: (a.tags || "")
    .split(",")
    .map((x) => x.trim())
    .filter(Boolean),
  description: a.description,
  observation: a.observation,
  image: `assets/${a.id}.jpg`,
  capturedAt: a.date,
  credit: a.credit,
  imageDimensions: { width: m.width, height: m.height },
  captureSha256: createHash("sha256").update(data).digest("hex"),
  rank: sites.length,
};
await mkdir(join(root, "assets"), { recursive: true });
await writeFile(join(root, entry.image), data, { flag: "wx" });
sites.push(entry);
try {
  await validateSites(sites, root);
} catch (error) {
  await unlink(join(root, entry.image));
  throw error;
}
await writeFile(path, JSON.stringify(sites, null, 2) + "\n");
console.log(
  `Added ${a.name}. Run npm test and review the image before opening a PR.`,
);