#!/usr/bin/env node // Lays the finished covers out as followers see them on a profile grid: each cover cropped to its 3:4 middle // (the part Instagram and TikTok show on the grid), three to a row, newest first. // // Usage: // node scripts/grid-preview.mjs --manifest covers/manifest.json --out grid.png // [--webp preview.webp] [--order newest-first|plan] [--tile 360] [--gap 4] [--background #ffffff] // // --order newest-first (default): the set is posted as cover 1 to N, so the grid shows N top left. // --order plan: cover 1 top left, for a set planned to read in plan order. // --tile: the width of one tile in pixels (its height is 4/3 of it). --webp writes a smaller copy for a web page // or a message, at most 2048 px wide. Writes grid.png.json beside the PNG for scripts/check-covers.mjs. // Needs sharp: run `npm install sharp` in the Skill folder (Node 20.9 or newer). import { createHash } from 'node:crypto' import { existsSync, readFileSync, writeFileSync } from 'node:fs' import { dirname, join, relative, resolve } from 'node:path' let sharp try { sharp = (await import('sharp')).default } catch { console.error('This script needs the sharp package. Run `npm install sharp` in the Skill folder (Node 20.9 or newer), then try again.') process.exit(1) } const FLAGS = new Set(['manifest', 'out', 'webp', 'order', 'tile', 'gap', 'background']) function fail(message) { console.error(message) process.exit(1) } const opts = {} const argv = process.argv.slice(2) for (let i = 0; i < argv.length; i++) { const a = argv[i] if (!a.startsWith('--')) fail(`Unexpected argument "${a}". Flags look like --name value.`) const key = a.slice(2) if (!FLAGS.has(key)) fail(`Unknown flag --${key}. Allowed: ${[...FLAGS].map((f) => `--${f}`).join(', ')}`) const value = argv[i + 1] if (value === undefined || value.startsWith('--')) fail(`--${key} needs a value.`) opts[key] = value i++ } if (!opts.manifest || !opts.out) fail('Usage: node scripts/grid-preview.mjs --manifest covers/manifest.json --out grid.png [--webp preview.webp] [--order newest-first|plan] [--tile 360]') if (!/\.png$/i.test(opts.out)) fail('--out must end in .png (use --webp for a small copy).') if (opts.webp && !/\.webp$/i.test(opts.webp)) fail('--webp must end in .webp.') const order = opts.order ?? 'newest-first' if (!['newest-first', 'plan'].includes(order)) fail('--order must be newest-first or plan.') const tile = Number(opts.tile ?? 360) const gap = Number(opts.gap ?? 4) if (!Number.isInteger(tile) || tile < 120 || tile > 1080) fail('--tile must be a whole number of pixels from 120 to 1080.') if (!Number.isInteger(gap) || gap < 0 || gap > 40) fail('--gap must be a whole number of pixels from 0 to 40.') const background = opts.background ?? '#ffffff' if (!/^#[0-9a-fA-F]{6}$/.test(background)) fail('--background must be a #hex colour.') if (!existsSync(opts.manifest)) fail(`Manifest not found: ${opts.manifest}. Run scripts/compose-covers.mjs first.`) const manifest = JSON.parse(readFileSync(opts.manifest, 'utf8')) if (manifest.schema !== 'reel-cover-set-manifest/1') fail(`${opts.manifest} is not a cover manifest (make it with scripts/compose-covers.mjs).`) const zones = JSON.parse(readFileSync(new URL('../assets/safe-zones.json', import.meta.url), 'utf8')) const crop = zones.grid.crop const cols = zones.grid.columns const dir = dirname(resolve(opts.manifest)) const covers = [...manifest.covers].sort((a, b) => a.n - b.n) if (order === 'newest-first') covers.reverse() if (covers.length % cols) fail(`The set has ${covers.length} covers; the grid needs full rows of ${cols}.`) const tileH = Math.round((tile * crop.height) / crop.width) const rows = covers.length / cols const width = cols * tile + (cols - 1) * gap const height = rows * tileH + (rows - 1) * gap const composite = [] for (const [i, c] of covers.entries()) { const file = join(dir, c.file) if (!existsSync(file)) fail(`Cover not found: ${file}. Re-run compose-covers.mjs.`) const bytes = readFileSync(file) if (createHash('sha256').update(bytes).digest('hex') !== c.sha256) fail(`${file} has changed since the manifest was written. Re-run compose-covers.mjs.`) const input = await sharp(bytes).extract({ left: crop.x, top: crop.y, width: crop.width, height: crop.height }).resize(tile, tileH).png().toBuffer() composite.push({ input, left: (i % cols) * (tile + gap), top: Math.floor(i / cols) * (tileH + gap) }) } const png = await sharp({ create: { width, height, channels: 3, background } }).composite(composite).png().toBuffer() writeFileSync(opts.out, png) writeFileSync( `${opts.out}.json`, `${JSON.stringify({ schema: 'reel-cover-set-grid/1', manifest: relative(dirname(resolve(opts.out)), resolve(opts.manifest)), order, tile, gap, width, height, covers: covers.map((c) => ({ n: c.n, sha256: c.sha256 })) }, null, 2)}\n`, ) console.log(`${opts.out} ${width}x${height}, ${rows} row(s) of ${cols}, ${order === 'newest-first' ? `cover ${covers[0].n} top left (posted last)` : 'cover 1 top left'}`) if (opts.webp) { const scale = Math.min(1, 2048 / width) await sharp(png).resize(Math.round(width * scale)).webp({ quality: 82 }).toFile(opts.webp) const info = await sharp(opts.webp).metadata() console.log(`${opts.webp} ${info.width}x${info.height}, ${Math.round(readFileSync(opts.webp).length / 1024)} KB`) }