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; +};