Cotton code
28 lines · 1.6 KB · TypeScriptRaw
// A small tile (Home's board, a card's object) draws a tool's photograph at// 192 px, so it never loads the master. Export the same photographs at that// size beside them.// Run after regenerating tool objects or stocks: pnpm exec tsx scripts/gen-tools/small-images.tsimport { readdir, readFile, mkdir, writeFile } from "node:fs/promises";import { resolve, join } from "node:path";import sharp from "sharp";const catalogue = resolve(__dirname, "../../apps/loom/Sources/CottonPaper/Media.xcassets");async function main() {  for (const entry of (await readdir(catalogue)).sort()) {    if (!/^(tool|stock)-.+\.imageset$/.test(entry) || entry.endsWith("-small.imageset")) continue;    const name = entry.slice(0, -".imageset".length);    const contents = JSON.parse(await readFile(join(catalogue, entry, "Contents.json"), "utf8"));    const source = contents.images.find((image: { filename?: string }) => image.filename)?.filename;    if (!source) throw new Error(`${entry} has no image`);    const dest = join(catalogue, `${name}-small.imageset`);    await mkdir(dest, { recursive: true });    await sharp(join(catalogue, entry, source))      .resize({ width: 192, height: 192, fit: "inside", withoutEnlargement: true })      .png().toFile(join(dest, `${name}-small.png`));    await writeFile(join(dest, "Contents.json"), JSON.stringify({      images: [{ filename: `${name}-small.png`, idiom: "universal", scale: "2x" }],      info: { author: "xcode", version: 1 },    }, null, 2) + "\n");  }}main().catch(error => { console.error(error); process.exitCode = 1; });