Fixes pre-commit git hook to ensure the optimisation of staged icons (#958)

* testing

* Fixes pre-commit hook

* Removing checkIcons from pre commit hook because it might result in false positives and negatives on an unclean local repository

* Added checkIcons Github action

---------

Co-authored-by: Karsa <karsa@karsa.org>
This commit is contained in:
Karsa
2023-02-28 17:24:17 +01:00
committed by GitHub
co-authored by Karsa
parent 2485f6117a
commit 1479a9dbd8
9 changed files with 69 additions and 25 deletions
+3 -2
View File
@@ -7,7 +7,7 @@ const icons = readAllMetadata(ICONS_DIR);
const CATEGORIES_DIR = path.resolve(currentDir, '../categories');
const categories = readAllMetadata(CATEGORIES_DIR);
console.log(`Read all icons`);
console.log('Reading all icons')
const svgFiles = readSvgDirectory(ICONS_DIR);
const iconNames = svgFiles.map(icon => icon.split('.')[0]);
@@ -47,5 +47,6 @@ Object.keys(categories).forEach(categoryName => {
});
if (error) {
throw new Error('At least one error in icon JSONs prevents from committing changes.');
console.error('At least one error in icon JSONs prevents from committing changes.');
process.exit(1);
}
+3 -2
View File
@@ -1,10 +1,11 @@
import fs from 'fs';
import processSvg from './render/processSvg.mjs';
const svgFiles = process.argv.slice(4);
const svgFiles = process.argv.slice(2);
svgFiles.forEach(async (svgFile) => {
console.log('Optimizing staged SVG file:', svgFile)
const content = fs.readFileSync(svgFile);
const svg = await processSvg(content);
const svg = await processSvg(content, svgFile);
fs.writeFileSync(svgFile, svg, 'utf-8');
});
+1 -1
View File
@@ -11,5 +11,5 @@ const svgFiles = readSvgDirectory(ICONS_DIR);
svgFiles.forEach((svgFile) => {
const content = fs.readFileSync(path.join(ICONS_DIR, svgFile));
processSvg(content).then((svg) => writeSvgFile(svgFile, ICONS_DIR, svg));
processSvg(content, svgFile).then((svg) => writeSvgFile(svgFile, ICONS_DIR, svg));
});
+13 -9
View File
@@ -1,6 +1,6 @@
import { optimize } from 'svgo';
import {optimize} from 'svgo';
import prettier from 'prettier';
import { parseSync, stringify } from 'svgson';
import {parseSync, stringify} from 'svgson';
import DEFAULT_ATTRS from './default-attrs.json' assert { type: 'json' };
/**
@@ -8,8 +8,9 @@ import DEFAULT_ATTRS from './default-attrs.json' assert { type: 'json' };
* @param {string} svg - An SVG string.
* @returns {Promise<string>} An optimized svg
*/
async function optimizeSvg(svg) {
async function optimizeSvg(svg, path) {
const result = optimize(svg, {
path,
plugins: [
{
name: 'preset-default',
@@ -17,12 +18,15 @@ async function optimizeSvg(svg) {
overrides: {
convertShapeToPath: false,
mergePaths: false,
removeAttrs: {
attrs: '(fill|stroke.*)',
},
},
},
},
{
name: 'removeAttrs',
params: {
attrs: '(fill|stroke.*)',
}
}
],
});
@@ -47,12 +51,12 @@ function setAttrs(svg) {
* @param {string} svg An SVG string.
* @returns {Promise<string>} An optimized svg
*/
function processSvg(svg) {
function processSvg(svg, path) {
return (
optimizeSvg(svg)
optimizeSvg(svg, path)
.then(setAttrs)
.then((optimizedSvg) =>
prettier.format(optimizedSvg, { parser: 'babel' }),
prettier.format(optimizedSvg, {parser: 'babel'}),
)
// remove semicolon inserted by prettier
// because prettier thinks it's formatting JSX not HTML