Files
openstapps/frontend/app/scripts/check-icon-correctness.mjs
2024-03-11 15:12:02 +01:00

75 lines
2.2 KiB
JavaScript

// @ts-check
/*
* Copyright (C) 2022 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {openSync} from 'fontkit';
import config from '../icons.config.mjs';
import {existsSync} from 'fs';
import {getUsedIconsHtml, getUsedIconsTS} from './gather-used-icons.mjs';
const commandName = '"npm run minify-icons"';
const originalFont = openSync(config.inputPath);
if (!existsSync(config.outputPath)) {
console.error(`Minified font not found. Run ${commandName} first.`);
process.exit(-1);
}
const modifiedFont = openSync(config.outputPath);
let success = true;
// eslint-disable-next-line unicorn/prefer-top-level-await
checkAll().then(() => {
console.log();
if (success) {
console.log('All icons are present in both fonts.');
} else {
console.error('Errors occurred.');
process.exit(-1);
}
});
/**
*
*/
async function checkAll() {
check(config.additionalIcons || {});
check(await getUsedIconsTS(config.scriptGlob));
check(await getUsedIconsHtml(config.htmlGlob));
}
/**
* @param {Record<string, string[]>} icons
*/
function check(icons) {
for (const [purpose, iconSet] of Object.entries(icons)) {
for (const icon of iconSet) {
if (!hasIcon(originalFont, icon)) {
success = false;
console.error(`${purpose}: ${icon} does not exist. Typo?`);
} else if (!hasIcon(modifiedFont, icon)) {
success = false;
console.error(`${purpose}: ${icon} not found in minified font. Run ${commandName} to regenerate it.`);
}
}
}
}
/**
* @param {import('fontkit').Font} font
* @param {string} icon
*/
function hasIcon(font, icon) {
return font.layout(icon).glyphs.some(it => it.isLigature);
}