From c44803fc2c18d7723a32bec856331779742a581a Mon Sep 17 00:00:00 2001 From: Xaroz Date: Mon, 23 Dec 2024 15:51:41 -0600 Subject: [PATCH 01/10] chore: validate correct file paths script --- scripts/validate-file-path.js | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 scripts/validate-file-path.js diff --git a/scripts/validate-file-path.js b/scripts/validate-file-path.js new file mode 100644 index 00000000..3e36c27f --- /dev/null +++ b/scripts/validate-file-path.js @@ -0,0 +1,51 @@ +import fs from 'fs'; +import path from 'path'; + +const directories = [ + { path: './src', recursive: true }, + { path: './', recursive: false }, +]; + +const invalidFilePath = []; +const invalidExtensions = ['.svg', '.yaml']; + +function findFilesInDirectory(directory, isRecursive = true) { + const files = fs.readdirSync(directory); + + files.forEach((file) => { + const fullPath = path.join(directory, file); + const stats = fs.statSync(fullPath); + + if (stats.isDirectory() && isRecursive) { + // Recurse into subdirectories if allowed + findFilesInDirectory(fullPath, isRecursive); + } else if (invalidExtensions.includes(path.extname(fullPath))) { + invalidFilePath.push(fullPath); + } + }); +} + +function validateFilesPath() { + directories.forEach(({ path, recursive }) => { + if (fs.existsSync(path)) { + console.log(`Checking directory: ${path}`); + findFilesInDirectory(path, recursive); + } else { + console.log(`Directory does not exist: ${path}`); + } + }); + + if (invalidFilePath.length === 0) return; + + console.error( + 'Error: invalid file paths found, make sure they are in the proper directories (chains or deployments):', + invalidFilePath, + ); + process.exit(1); +} + +function main() { + validateFilesPath(); +} + +main(); From 4b43786ba1d0d68b0cd7895aa8d21ea23e86c8e1 Mon Sep 17 00:00:00 2001 From: Xaroz Date: Thu, 26 Dec 2024 10:10:58 -0600 Subject: [PATCH 02/10] ci: separate svg validation and optimization, refactor and add validate file path --- .github/workflows/validate.yaml | 50 ++++++++++++++++++ keke.svg | 0 scripts/common.js | 34 +++++++++++++ scripts/optimize-svg.js | 90 +-------------------------------- scripts/validate-file-path.js | 47 ++++------------- scripts/validate-svg.js | 69 +++++++++++++++++++++++++ 6 files changed, 165 insertions(+), 125 deletions(-) create mode 100644 .github/workflows/validate.yaml create mode 100644 keke.svg create mode 100644 scripts/common.js create mode 100644 scripts/validate-svg.js diff --git a/.github/workflows/validate.yaml b/.github/workflows/validate.yaml new file mode 100644 index 00000000..57770440 --- /dev/null +++ b/.github/workflows/validate.yaml @@ -0,0 +1,50 @@ +name: validate-files + +on: + push: + branches: ["main"] + pull_request: + types: [opened, synchronize, reopened, ready_for_review] + # Allows you to run this workflow manually + workflow_dispatch: + +jobs: + validate-files: + runs-on: ubuntu-latest + permissions: + contents: read + 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: 20 + + - name: validate-file-path + run: | + node ./scripts/validate-file-path.js + + - name: validate-svg + run: | + node ./scripts/validate-svg.js diff --git a/keke.svg b/keke.svg new file mode 100644 index 00000000..e69de29b diff --git a/scripts/common.js b/scripts/common.js new file mode 100644 index 00000000..4314d936 --- /dev/null +++ b/scripts/common.js @@ -0,0 +1,34 @@ +import fs from 'fs'; +import path from 'path'; + +function findFiles(directory, fileTypes = [], isRecursive = true) { + const files = fs.readdirSync(directory); + + return files.flatMap((file) => { + const fullPath = path.join(directory, file); + const stats = fs.statSync(fullPath); + + if (stats.isDirectory() && isRecursive) { + return findFiles(fullPath, fileTypes); // Recurse into subdirectories + } else if (fileTypes.includes(path.extname(fullPath))) { + return fullPath; + } + + return []; + }); +} + +// Get all fille paths for given directorie and given file types +export function getFilePaths(directories = [], fileTypes = [], isRecursive = true) { + return directories + .filter((directory) => { + if (fs.existsSync(directory)) { + console.log(`Checking directory: ${directory}`); + return true; + } else { + console.log(`Directory does not exist: ${directory}`); + return false; + } + }) + .flatMap((directory) => findFiles(directory, fileTypes, isRecursive)); +} diff --git a/scripts/optimize-svg.js b/scripts/optimize-svg.js index c6226e80..1db4da7e 100644 --- a/scripts/optimize-svg.js +++ b/scripts/optimize-svg.js @@ -1,94 +1,9 @@ import fs from 'fs'; -import path from 'path'; import { optimize } from 'svgo'; +import { getFilePaths } from './common.js'; const directories = ['./chains', './deployments']; -const MAX_FILE_SIZE = 100 * 1024; // 100KBs -const RASTER_IMAGE_REGEX = /]*>/i; -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')) { - invalidNameSVGs.push(filePath); - } - - if (stats.size > MAX_FILE_SIZE) { - invalidSizeSVGs.push({ filePath, fileSize: `${fileSize}KBs` }); - } - - const fileContent = fs.readFileSync(filePath, 'utf8'); - if (RASTER_IMAGE_REGEX.test(fileContent)) { - rasterImgSVGs.push(filePath); - } -} - -// Finds all svgs, validates and return all paths found that has svgs -function findAndValidateSVGs(directory) { - const files = fs.readdirSync(directory); - - return files.flatMap((file) => { - const fullPath = path.join(directory, file); - const stats = fs.statSync(fullPath); - - if (stats.isDirectory()) { - return findAndValidateSVGs(fullPath); // Recurse into subdirectories - } else if (path.extname(fullPath) === '.svg') { - isValidSvg(fullPath); - return fullPath; - } - - return []; - }); -} - -// Get all svg paths that are validated -function getSVGPaths() { - return directories - .filter((directory) => { - if (fs.existsSync(directory)) { - console.log(`Checking directory: ${directory}`); - return true; - } else { - console.log(`Directory does not exist: ${directory}`); - return false; - } - }) - .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 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) => { @@ -122,8 +37,7 @@ function optimizeSVGs(svgPaths) { } function main() { - const svgPaths = getSVGPaths(); - validateErrors(); + const svgPaths = getFilePaths(directories, ['.svg']); optimizeSVGs(svgPaths); } diff --git a/scripts/validate-file-path.js b/scripts/validate-file-path.js index 3e36c27f..90e92d84 100644 --- a/scripts/validate-file-path.js +++ b/scripts/validate-file-path.js @@ -1,51 +1,24 @@ -import fs from 'fs'; -import path from 'path'; +import { getFilePaths } from './common.js'; const directories = [ - { path: './src', recursive: true }, - { path: './', recursive: false }, + { paths: ['./src'], recursive: true }, + { paths: ['./'], recursive: false }, ]; -const invalidFilePath = []; -const invalidExtensions = ['.svg', '.yaml']; +const fileExtensions = ['.svg', '.yaml']; -function findFilesInDirectory(directory, isRecursive = true) { - const files = fs.readdirSync(directory); - - files.forEach((file) => { - const fullPath = path.join(directory, file); - const stats = fs.statSync(fullPath); - - if (stats.isDirectory() && isRecursive) { - // Recurse into subdirectories if allowed - findFilesInDirectory(fullPath, isRecursive); - } else if (invalidExtensions.includes(path.extname(fullPath))) { - invalidFilePath.push(fullPath); - } - }); -} - -function validateFilesPath() { - directories.forEach(({ path, recursive }) => { - if (fs.existsSync(path)) { - console.log(`Checking directory: ${path}`); - findFilesInDirectory(path, recursive); - } else { - console.log(`Directory does not exist: ${path}`); - } - }); +function main() { + const invalidFilesPaths = directories.flatMap((directory) => + getFilePaths(directory.paths, fileExtensions, directory.recursive), + ); - if (invalidFilePath.length === 0) return; + if (invalidFilesPaths.length === 0) return; console.error( 'Error: invalid file paths found, make sure they are in the proper directories (chains or deployments):', - invalidFilePath, + invalidFilesPaths, ); process.exit(1); } -function main() { - validateFilesPath(); -} - main(); diff --git a/scripts/validate-svg.js b/scripts/validate-svg.js new file mode 100644 index 00000000..35ddefb7 --- /dev/null +++ b/scripts/validate-svg.js @@ -0,0 +1,69 @@ +import fs from 'fs'; +import path from 'path'; +import { getFilePaths } from './common.js'; + +const directories = ['./chains', './deployments']; +const MAX_FILE_SIZE = 100 * 1024; // 100KBs +const RASTER_IMAGE_REGEX = /]*>/i; + +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')) { + invalidNameSVGs.push(filePath); + } + + if (stats.size > MAX_FILE_SIZE) { + invalidSizeSVGs.push({ filePath, fileSize: `${fileSize}KBs` }); + } + + const fileContent = fs.readFileSync(filePath, 'utf8'); + if (RASTER_IMAGE_REGEX.test(fileContent)) { + rasterImgSVGs.push(filePath); + } +} + +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 tag, likely embedding a raster image in the following paths:', + rasterImgSVGs, + ); + } + + process.exit(1); +} + +function validateSvgs(paths = []) { + paths.forEach((path) => isValidSvg(path)); + validateErrors(); +} + +function main() { + const svgPaths = getFilePaths(directories, ['.svg']); + validateSvgs(svgPaths); +} + +main(); From fec2acf75ec3b6bc39a4e40164479f8e3f73afa4 Mon Sep 17 00:00:00 2001 From: Xaroz Date: Thu, 26 Dec 2024 10:11:21 -0600 Subject: [PATCH 03/10] chore: remove test file --- keke.svg | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 keke.svg diff --git a/keke.svg b/keke.svg deleted file mode 100644 index e69de29b..00000000 From 4d8ef82a981afdfdc35c2ee75c8e4d2a796f1a3c Mon Sep 17 00:00:00 2001 From: Xaroz Date: Thu, 26 Dec 2024 10:14:26 -0600 Subject: [PATCH 04/10] chore: remove need for token --- .github/workflows/validate.yaml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.github/workflows/validate.yaml b/.github/workflows/validate.yaml index 57770440..0c68b61c 100644 --- a/.github/workflows/validate.yaml +++ b/.github/workflows/validate.yaml @@ -14,11 +14,6 @@ jobs: permissions: contents: read steps: - - uses: actions/checkout@v4 - with: - ref: ${{ github.head_ref || 'main' }} - token: ${{ secrets.GITHUB_TOKEN }} - - uses: actions/cache@v4 with: path: | From 515cca861f3ef6c231e5ed9bf073f1dd9aad7e52 Mon Sep 17 00:00:00 2001 From: Xaroz Date: Thu, 26 Dec 2024 10:16:02 -0600 Subject: [PATCH 05/10] fix: typos --- scripts/common.js | 2 +- scripts/optimize-svg.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/common.js b/scripts/common.js index 4314d936..8c04291a 100644 --- a/scripts/common.js +++ b/scripts/common.js @@ -18,7 +18,7 @@ function findFiles(directory, fileTypes = [], isRecursive = true) { }); } -// Get all fille paths for given directorie and given file types +// Get all fille paths for given directories and given file types export function getFilePaths(directories = [], fileTypes = [], isRecursive = true) { return directories .filter((directory) => { diff --git a/scripts/optimize-svg.js b/scripts/optimize-svg.js index 1db4da7e..9975dbb4 100644 --- a/scripts/optimize-svg.js +++ b/scripts/optimize-svg.js @@ -4,7 +4,7 @@ import { getFilePaths } from './common.js'; const directories = ['./chains', './deployments']; -// Optimize svg in given path +// Optimize svg in given paths function optimizeSVGs(svgPaths) { svgPaths.forEach((filePath) => { try { From f2ead8316072c22aa53e30664f07154aeac45879 Mon Sep 17 00:00:00 2001 From: Xaroz Date: Thu, 26 Dec 2024 10:17:16 -0600 Subject: [PATCH 06/10] chore(docs): changeset added --- .changeset/fresh-turkeys-grow.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/fresh-turkeys-grow.md diff --git a/.changeset/fresh-turkeys-grow.md b/.changeset/fresh-turkeys-grow.md new file mode 100644 index 00000000..5fd01407 --- /dev/null +++ b/.changeset/fresh-turkeys-grow.md @@ -0,0 +1,5 @@ +--- +'@hyperlane-xyz/registry': patch +--- + +Improve CI workflow by separating concerns From f9e6c1a2ff8a9198e00984fe1c18f258d3377db8 Mon Sep 17 00:00:00 2001 From: Xaroz Date: Thu, 26 Dec 2024 10:23:11 -0600 Subject: [PATCH 07/10] fix: missing checkout step --- .github/workflows/validate.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/validate.yaml b/.github/workflows/validate.yaml index 0c68b61c..c28413b9 100644 --- a/.github/workflows/validate.yaml +++ b/.github/workflows/validate.yaml @@ -14,6 +14,7 @@ jobs: permissions: contents: read steps: + - uses: actions/checkout@v4 - uses: actions/cache@v4 with: path: | From bad40b8ee4506cb595a5aee824e3cb3feb223d65 Mon Sep 17 00:00:00 2001 From: Xaroz Date: Thu, 26 Dec 2024 14:37:37 -0600 Subject: [PATCH 08/10] chore: rename file --- scripts/{common.js => utils.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename scripts/{common.js => utils.js} (100%) diff --git a/scripts/common.js b/scripts/utils.js similarity index 100% rename from scripts/common.js rename to scripts/utils.js From 8af05f8ac5880690aba71120f82c87948ef35c39 Mon Sep 17 00:00:00 2001 From: Xaroz Date: Thu, 26 Dec 2024 16:55:01 -0600 Subject: [PATCH 09/10] fix: import path --- scripts/optimize-svg.js | 2 +- scripts/validate-file-path.js | 2 +- scripts/validate-svg.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/optimize-svg.js b/scripts/optimize-svg.js index 9975dbb4..80fce693 100644 --- a/scripts/optimize-svg.js +++ b/scripts/optimize-svg.js @@ -1,6 +1,6 @@ import fs from 'fs'; import { optimize } from 'svgo'; -import { getFilePaths } from './common.js'; +import { getFilePaths } from './utils.js'; const directories = ['./chains', './deployments']; diff --git a/scripts/validate-file-path.js b/scripts/validate-file-path.js index 90e92d84..fbd97010 100644 --- a/scripts/validate-file-path.js +++ b/scripts/validate-file-path.js @@ -1,4 +1,4 @@ -import { getFilePaths } from './common.js'; +import { getFilePaths } from './utils.js'; const directories = [ { paths: ['./src'], recursive: true }, diff --git a/scripts/validate-svg.js b/scripts/validate-svg.js index 35ddefb7..aaaaba35 100644 --- a/scripts/validate-svg.js +++ b/scripts/validate-svg.js @@ -1,6 +1,6 @@ import fs from 'fs'; import path from 'path'; -import { getFilePaths } from './common.js'; +import { getFilePaths } from './utils.js'; const directories = ['./chains', './deployments']; const MAX_FILE_SIZE = 100 * 1024; // 100KBs From 248f9266cb4f3f2c8d67f45d8754819f44ad5de4 Mon Sep 17 00:00:00 2001 From: Xaroz Date: Fri, 27 Dec 2024 11:46:53 -0600 Subject: [PATCH 10/10] chore: update setup node to v4 --- .github/workflows/optimize-svg.yml | 2 +- .github/workflows/validate.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/optimize-svg.yml b/.github/workflows/optimize-svg.yml index 5b70e749..e9d20831 100644 --- a/.github/workflows/optimize-svg.yml +++ b/.github/workflows/optimize-svg.yml @@ -32,7 +32,7 @@ jobs: fi - name: setup-node - uses: actions/setup-node@v3 + uses: actions/setup-node@v4 with: node-version: 20 diff --git a/.github/workflows/validate.yaml b/.github/workflows/validate.yaml index c28413b9..b57da932 100644 --- a/.github/workflows/validate.yaml +++ b/.github/workflows/validate.yaml @@ -33,7 +33,7 @@ jobs: fi - name: setup-node - uses: actions/setup-node@v3 + uses: actions/setup-node@v4 with: node-version: 20