From 6fa7f89c613937df8c1d11aa091fa93fa72019bc Mon Sep 17 00:00:00 2001 From: Brian Sperlongano Date: Wed, 13 Dec 2023 21:03:16 -0500 Subject: [PATCH] Convert folder_diff to typescript --- .github/workflows/build-preview.yml | 2 +- scripts/folder_diff.sh | 60 ---------------------------- scripts/folder_diff.ts | 61 +++++++++++++++++++++++++++++ shieldlib/src/shield.js | 6 +-- 4 files changed, 63 insertions(+), 66 deletions(-) delete mode 100755 scripts/folder_diff.sh create mode 100644 scripts/folder_diff.ts diff --git a/.github/workflows/build-preview.yml b/.github/workflows/build-preview.yml index f0646165d..6f3608e6f 100644 --- a/.github/workflows/build-preview.yml +++ b/.github/workflows/build-preview.yml @@ -104,7 +104,7 @@ jobs: - name: Generate map diff sample clips id: map-diff-samples run: | - ./scripts/folder_diff.sh ../samples-main ../samples-pr https://preview.ourmap.us/pr/${{ github.event.pull_request.number }}/ ${{ github.event.pull_request.head.sha }} + npm exec tsx scripts/folder_diff ../samples-main ../samples-pr https://preview.ourmap.us/pr/${{ github.event.pull_request.number }}/ ${{ github.event.pull_request.head.sha }} mv pr_preview-extra.md ../pr/ cat ../pr/pr_preview-extra.md mv samples-diff ../dist/ diff --git a/scripts/folder_diff.sh b/scripts/folder_diff.sh deleted file mode 100755 index 0c306c61a..000000000 --- a/scripts/folder_diff.sh +++ /dev/null @@ -1,60 +0,0 @@ -#!/bin/bash -# Compare two folders of images and produce an .md file of differences - -# Check if the right number of arguments are passed -if [ "$#" -ne 4 ]; then - echo "Usage: $0 " - exit 1 -fi - -FOLDER1="$1" -FOLDER2="$2" -OUTPUT_FOLDER="samples-diff" -URL_BASE="$3" - -# Ensure that image URLs are unique to this commit because GitHub caches identical URLs -SHA="$4" - -# Create the output folder if it doesn't exist -mkdir -p "$OUTPUT_FOLDER" - -# Loop through files in FOLDER1 -for file in "$FOLDER1"/*.png; do - # Extract just the base filename without the path - basefile=$(basename "$file") - - # Check if file exists in FOLDER2 - if [ -e "$FOLDER2/$basefile" ]; then - # Compare the files - cmp -s "$FOLDER1/$basefile" "$FOLDER2/$basefile" - - # If files are different ($? is the exit status of last command, 1 means different for cmp) - if [ $? -eq 1 ]; then - # Copy the files to the output folder with the appropriate naming scheme - cp "$FOLDER1/$basefile" "$OUTPUT_FOLDER/${basefile%.*}_${SHA}_before.png" - cp "$FOLDER2/$basefile" "$OUTPUT_FOLDER/${basefile%.*}_${SHA}_after.png" - fi - fi -done - -OUTPUT_MD="pr_preview-extra.md" - -# Start the table with headers -echo "## Map Changes" > $OUTPUT_MD -echo "| Sample Name | Before | After |" >> $OUTPUT_MD -echo "|-------------|--------|-------|" >> $OUTPUT_MD - -# Loop through *_before.png files in the output folder -for before_file in "$OUTPUT_FOLDER"/*_before.png; do - # Extract just the base filename without the path and the '_before' suffix - basefile=$(basename "$before_file" _${SHA}_before.png) - - # Construct the path to the corresponding after file - after_file="$OUTPUT_FOLDER/${basefile}_${SHA}_after.png" - - # Check if the after file exists - if [ -e "$after_file" ]; then - # Add an entry to the markdown table - echo "| $basefile | ![before]($URL_BASE$before_file) | ![after]($URL_BASE$after_file) |" >> $OUTPUT_MD - fi -done diff --git a/scripts/folder_diff.ts b/scripts/folder_diff.ts new file mode 100644 index 000000000..5f27b2d99 --- /dev/null +++ b/scripts/folder_diff.ts @@ -0,0 +1,61 @@ +import fs from "fs"; +import { basename } from "path"; +import { execSync } from "child_process"; + +// Check if the right number of arguments are passed +if (process.argv.length !== 6) { + console.log("Usage: "); + process.exit(1); +} + +const [folder1, folder2, urlBase, sha] = process.argv.slice(2); +const outputFolder = "samples-diff"; + +// Create the output folder if it doesn't exist +if (!fs.existsSync(outputFolder)) { + fs.mkdirSync(outputFolder); +} + +// Loop through files in folder1 +fs.readdirSync(folder1) + .filter((file) => file.endsWith(".png")) + .forEach((file) => { + const basefile = basename(file); + + // Check if file exists in folder2 + if (fs.existsSync(`${folder2}/${basefile}`)) { + // Compare the files + try { + execSync(`cmp -s "${folder1}/${basefile}" "${folder2}/${basefile}"`); + } catch (e) { + // If files are different + fs.copyFileSync( + `${folder1}/${basefile}`, + `${outputFolder}/${basefile.split(".")[0]}_${sha}_before.png` + ); + fs.copyFileSync( + `${folder2}/${basefile}`, + `${outputFolder}/${basefile.split(".")[0]}_${sha}_after.png` + ); + } + } + }); + +const outputMD = "pr_preview-extra.md"; +let mdContent = + "## Map Changes\n| Sample Name | Before | After |\n|-------------|--------|-------|\n"; + +// Loop through *_before.png files in the output folder +fs.readdirSync(outputFolder) + .filter((file) => file.endsWith("_before.png")) + .forEach((before_file) => { + const basefile = basename(before_file, `_${sha}_before.png`); + + // Check if the after file exists + if (fs.existsSync(`${outputFolder}/${basefile}_${sha}_after.png`)) { + // Add an entry to the markdown table + mdContent += `| ${basefile} | ![before](${urlBase}${before_file}) | ![after](${urlBase}${outputFolder}/${basefile}_${sha}_after.png) |\n`; + } + }); + +fs.writeFileSync(outputMD, mdContent); diff --git a/shieldlib/src/shield.js b/shieldlib/src/shield.js index 698cc254b..ba31ed75c 100644 --- a/shieldlib/src/shield.js +++ b/shieldlib/src/shield.js @@ -3,11 +3,7 @@ import * as ShieldText from "./shield_text"; import * as ShieldDraw from "./shield_canvas_draw"; import * as Gfx from "./screen_gfx"; -import { - drawBanners, - drawBannerHalos, - getBannerCount, -} from "./shield_banner"; +import { drawBanners, drawBannerHalos, getBannerCount } from "./shield_banner"; function compoundShieldSize(r, dimension, bannerCount) { return {