From 7a7f5e582893ea3869a9385080d792800d9d164a Mon Sep 17 00:00:00 2001 From: Xaroz Date: Wed, 18 Dec 2024 16:20:39 -0600 Subject: [PATCH] chore: add workflow to check svgs --- .github/workflows/combine.yml | 14 +++++----- .github/workflows/optimize-svg.yml | 41 ++++++++++++++++++++++++++++ scripts/optimize-svg.js | 44 ++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/optimize-svg.yml create mode 100644 scripts/optimize-svg.js diff --git a/.github/workflows/combine.yml b/.github/workflows/combine.yml index ed40c8bea..b35312bb0 100644 --- a/.github/workflows/combine.yml +++ b/.github/workflows/combine.yml @@ -25,13 +25,13 @@ jobs: - name: yarn-install run: | - yarn install - CHANGES=$(git status -s) - if [[ ! -z $CHANGES ]]; then - echo "Changes found: $CHANGES" - git diff - exit 1 - fi + yarn install + CHANGES=$(git status -s) + if [[ ! -z $CHANGES ]]; then + echo "Changes found: $CHANGES" + git diff + exit 1 + fi - name: foundry-install uses: foundry-rs/foundry-toolchain@v1 diff --git a/.github/workflows/optimize-svg.yml b/.github/workflows/optimize-svg.yml new file mode 100644 index 000000000..cb07ba3fd --- /dev/null +++ b/.github/workflows/optimize-svg.yml @@ -0,0 +1,41 @@ +name: optimize-svg + +on: + pull_request: + # Allows you to run this workflow manually + workflow_dispatch: + +jobs: + combine: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref || 'main' }} + token: ${{ secrets.GITHUB_TOKEN }} + + - uses: actions/cache@v4 + with: + path: | + **/node_modules + .yarn/cache + key: ${{ runner.os }}-yarn-cache-${{ hashFiles('./yarn.lock') }} + + - name: yarn-install + run: | + yarn install + CHANGES=$(git status -s) + if [[ ! -z $CHANGES ]]; then + echo "Changes found: $CHANGES" + git diff + exit 1 + fi + + - name: setup-node + uses: actions/setup-node@v3 + with: + node-version: 16 + + - name: validate-optimize-svgs + run: | + node ./scripts/optimize-svg.js diff --git a/scripts/optimize-svg.js b/scripts/optimize-svg.js new file mode 100644 index 000000000..da024e99e --- /dev/null +++ b/scripts/optimize-svg.js @@ -0,0 +1,44 @@ +import fs from 'fs'; +import path from 'path'; + +const directories = ['./chains', './deployments']; +const MAX_FILE_SIZE = 20 * 1024; // 20KBs + +function isValidSvg(filePath) { + const fileName = path.basename(filePath); + const stats = fs.statSync(filePath); + + if (!fileName.endsWith('logo.svg')) { + console.error(`Error: File does not end with 'logo.svg' -> ${filePath}`); + // process.exit(1); // Exit immediately if criteria is not met + } + + if (stats.size > MAX_FILE_SIZE) { + console.error(`Error: File size exceeds 20KBs -> ${filePath}`); + // process.exit(1); // Exit immediately if criteria is not met + } +} + +function findAndProcessSVGs(directory) { + const files = fs.readdirSync(directory); + + files.forEach((file) => { + const fullPath = path.join(directory, file); + const stats = fs.statSync(fullPath); + + if (stats.isDirectory()) { + findAndProcessSVGs(fullPath); // Recurse into subdirectories + } else if (path.extname(fullPath) === '.svg') { + isValidSvg(fullPath); // Validate file, exits on failure + } + }); +} + +directories.forEach((directory) => { + if (fs.existsSync(directory)) { + console.log(`Checking directory: ${directory}`); + findAndProcessSVGs(directory); + } else { + console.log(`Directory does not exist: ${directory}`); + } +});