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
+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