From 43cf678a33b129a870e4703a9141d764220ebbca Mon Sep 17 00:00:00 2001 From: Luke Walton Date: Wed, 23 Oct 2024 11:46:57 +0100 Subject: [PATCH] ci: If icon files change, upadte lastUpdated in package.json (#51) Co-authored-by: github-actions --- .github/fetch_icons/index.js | 17 ++++++++++++----- .github/workflows/on-pr.yml | 2 ++ scripts/utils/checkGit.ts | 29 +++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 scripts/utils/checkGit.ts diff --git a/.github/fetch_icons/index.js b/.github/fetch_icons/index.js index 961b8630..61164312 100644 --- a/.github/fetch_icons/index.js +++ b/.github/fetch_icons/index.js @@ -1,6 +1,7 @@ import core from "@actions/core"; import { readFileSync, writeFileSync } from "fs"; import fetchIcons from "../../dist/scripts/fetch-icons/fetchIcons.js"; +import { checkForFileChanges, stageAllFiles } from '../../dist/scripts/utils/checkGit.js'; import { ZDS_ASSETS_FILE_ID, ZDS_ASSETS_ICON_PAGE_NAME } from "../../figmaConfig.js"; const FIGMA_ACCESS_TOKEN = core.getInput("figma-access-token") || process.env.FIGMA_ACCESS_TOKEN; @@ -24,15 +25,21 @@ try { "outputs", false, ); + let filesChanged = false; if (newHash) { writeFileSync(hashPath, newHash); - const packageJson = JSON.parse(readFileSync("./package.json").toString()); - packageJson.lastUpdated = DATE; - writeFileSync("./package.json", JSON.stringify(packageJson, null, 2)); + filesChanged = checkForFileChanges(); + if (filesChanged) { + const packageJson = JSON.parse(readFileSync("./package.json").toString()); + packageJson.lastUpdated = DATE; + writeFileSync("./package.json", JSON.stringify(packageJson, null, 2)); + stageAllFiles(); + } } - console.log("Files changed", newHash); - core.setOutput("files_changed", newHash != undefined); + + console.log("Files changed", filesChanged); + core.setOutput("files_changed", filesChanged); } catch (error) { core.setFailed(error.message); } diff --git a/.github/workflows/on-pr.yml b/.github/workflows/on-pr.yml index d67002f4..0e3bfcc1 100644 --- a/.github/workflows/on-pr.yml +++ b/.github/workflows/on-pr.yml @@ -27,6 +27,8 @@ jobs: run: npm ci - name: Run tests run: npm run test + - name: Run tsc + run: tsc release-comment: name: Release Comment diff --git a/scripts/utils/checkGit.ts b/scripts/utils/checkGit.ts new file mode 100644 index 00000000..3545354e --- /dev/null +++ b/scripts/utils/checkGit.ts @@ -0,0 +1,29 @@ +import { execSync } from "child_process"; + +/** + * Gets all files that have changed in the current branch + * @returns string[] - List of files that have changed + */ +const getAllChangedFiles = (): string[] => { + const diffOutput = execSync(`git diff HEAD --name-only`).toString(); + if (diffOutput != "") { + console.log("Files changed:", diffOutput); + } + return diffOutput.toString().split("\n").filter(Boolean); +}; + +/** + * Stages all files in the current branch + */ +export const stageAllFiles = (): void => { + execSync(`git add .`); +}; + +/** + * Checks if any files have changed in the current branch, but removes package.json from the list of changed files + * @returns boolean - Whether files have changed and the action should create a PR + */ +export const checkForFileChanges = (): boolean => { + stageAllFiles(); + return getAllChangedFiles().length > 0; +};