archive-org-link-grabber/scripts/build-icons.js

72 lines
2.2 KiB
JavaScript

#!/usr/bin/env node
/*
Generates extension and toolbar PNG icons from the base SVG with transparent
background. Requires `rsvg-convert` (from librsvg).
The SVG defines its own stroke color (set directly in icons/file-search.svg).
To change colors, edit the SVG; the script does not inject or replace colors.
Usage:
node scripts/build-icons.js
*/
const fs = require('fs');
const path = require('path');
const { spawnSync } = require('child_process');
const REPO_ROOT = path.join(__dirname, '..');
const ICONS_DIR = path.join(REPO_ROOT, 'icons');
const SRC_SVG = path.join(ICONS_DIR, 'file-search.svg');
const ADDON_SIZES = [48, 96, 128];
const TOOLBAR_SIZES = [16, 32];
function ensureRsvgConvert() {
try {
const res = spawnSync('rsvg-convert', ['--version'], { stdio: 'ignore' });
if (res && res.status === 0) return 'rsvg-convert';
} catch (_) {}
return null;
}
function assertSvgExists() {
if (!fs.existsSync(SRC_SVG)) {
console.error(`Base SVG not found: ${path.relative(REPO_ROOT, SRC_SVG)}`);
process.exit(1);
}
}
function rasterize(rsvgBin, size, outPath) {
// rsvg-convert preserves transparency by default; specify width/height and PNG output
const args = ['-w', String(size), '-h', String(size), '-f', 'png', '-o', outPath, SRC_SVG];
const proc = spawnSync(rsvgBin, args, { stdio: ['ignore', 'inherit', 'inherit'] });
if (proc.status !== 0) {
console.error(`Failed to generate ${outPath} using ${rsvgBin}.`);
process.exit(proc.status || 1);
}
}
function main() {
const rsvg = ensureRsvgConvert();
if (!rsvg) {
console.error('rsvg-convert not found. Please install librsvg (rsvg-convert) and ensure it is on your PATH.');
process.exit(1);
}
if (!fs.existsSync(ICONS_DIR)) fs.mkdirSync(ICONS_DIR, { recursive: true });
assertSvgExists();
// Add-on icons (use SVG colors as-is)
for (const s of ADDON_SIZES) {
const out = path.join(ICONS_DIR, `icon-${s}.png`);
rasterize(rsvg, s, out);
console.log(`Generated ${path.relative(REPO_ROOT, out)}`);
}
// Toolbar icons (use SVG colors as-is)
for (const s of TOOLBAR_SIZES) {
const out = path.join(ICONS_DIR, `icon-${s}.png`);
rasterize(rsvg, s, out);
console.log(`Generated ${path.relative(REPO_ROOT, out)}`);
}
}
main();