Skip to content

Commit

Permalink
chore: better error messages, changed node version in workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Xaroz committed Dec 23, 2024
1 parent 434ce06 commit c07cc34
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/optimize-svg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: setup-node
uses: actions/setup-node@v3
with:
node-version: 16
node-version: 20

- name: validate-optimize-svgs
run: |
Expand Down
52 changes: 35 additions & 17 deletions scripts/optimize-svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,26 @@ const directories = ['./chains', './deployments'];
const MAX_FILE_SIZE = 100 * 1024; // 100KBs
const RASTER_IMAGE_REGEX = /<image[^>]*>/i;

let isValid = true;
const invalidNameSVGs = [];
const invalidSizeSVGs = [];
const rasterImgSVGs = [];

function isValidSvg(filePath) {
const fileName = path.basename(filePath);
const stats = fs.statSync(filePath);
const fileSize = (stats.size / 1024).toFixed(2);

if (!fileName.endsWith('logo.svg')) {
console.error(`Error: File does not end with 'logo.svg' -> ${filePath}`);
isValid = false;
invalidNameSVGs.push(filePath);
}

if (stats.size > MAX_FILE_SIZE) {
console.error(`Error: File size exceeds 100KBs (Current size: ${fileSize}KB) -> ${filePath}`);
isValid = false;
invalidSizeSVGs.push({ filePath, fileSize: `${fileSize}KBs` });
}

const fileContent = fs.readFileSync(filePath, 'utf8');
if (RASTER_IMAGE_REGEX.test(fileContent)) {
console.error(
`Error: File contains an <image> tag, likely embedding a raster image -> ${filePath}`,
);
isValid = false;
rasterImgSVGs.push(filePath);
}
}

Expand All @@ -43,7 +40,7 @@ function findAndValidateSVGs(directory) {
if (stats.isDirectory()) {
return findAndValidateSVGs(fullPath); // Recurse into subdirectories
} else if (path.extname(fullPath) === '.svg') {
isValidSvg(fullPath); // Validate file, exits on failure
isValidSvg(fullPath);
return fullPath;
}

Expand All @@ -66,6 +63,32 @@ function getSVGPaths() {
.flatMap((directory) => findAndValidateSVGs(directory));
}

function validateErrors() {
const errorCount = invalidNameSVGs.length + invalidSizeSVGs.length + rasterImgSVGs.length;
if (errorCount === 0) return;

console.error(`Number of errors found: ${errorCount}`);

if (invalidNameSVGs.length > 0) {
console.error(
"Error: Files do not end with 'logo.svg' in the following paths:",
invalidNameSVGs,
);
}

if (invalidSizeSVGs.length > 0) {
console.error('Error: Files size exceed 100KBs in:', invalidSizeSVGs);
}

if (rasterImgSVGs.length > 0) {
console.error(
'Error: Files contain an <image> tag, likely embedding a raster image in the following paths:',
rasterImgSVGs,
);
}

process.exit(1);
}
// Optimize svg in given path
function optimizeSVGs(svgPaths) {
svgPaths.forEach((filePath) => {
Expand All @@ -88,24 +111,19 @@ function optimizeSVGs(svgPaths) {
});

if (result.error) {
console.error(`Error optimizing ${filePath}: ${result.error}`);
return; // Log the error and continue with the next file
throw new Error(result.error);
}
fs.writeFileSync(filePath, result.data, 'utf8');
console.log(`Optimized: ${filePath}`);
} catch (error) {
console.error(`Error processing ${filePath}: ${error.message}`);
// Log the error and continue with the next file
}
});
}

function main() {
const svgPaths = getSVGPaths();
if (!isValid) {
console.error('SVGs have errors, exiting.');
process.exit(1);
}
validateErrors();
optimizeSVGs(svgPaths);
}

Expand Down

0 comments on commit c07cc34

Please sign in to comment.