Skip to content

Commit

Permalink
ci: If icon files change, upadte lastUpdated in package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Oct 23, 2024
1 parent dcff8b4 commit 11e5441
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
11 changes: 8 additions & 3 deletions .github/fetch_icons/index.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,9 +28,13 @@ try {

if (newHash) {
writeFileSync(hashPath, newHash);
const packageJson = JSON.parse(readFileSync("./package.json").toString());
packageJson.lastUpdated = DATE;
writeFileSync("./package.json", JSON.stringify(packageJson, null, 2));
const fileChanges = checkForFileChanges();
if (fileChanges) {
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);
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/on-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions scripts/utils/checkGit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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();
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;
};

0 comments on commit 11e5441

Please sign in to comment.