Skip to content

Commit

Permalink
ci: If icon files change, upadte lastUpdated in package.json (#51)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions <[email protected]>
  • Loading branch information
thelukewalton and github-actions committed Oct 23, 2024
1 parent dcff8b4 commit 43cf678
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
17 changes: 12 additions & 5 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 @@ -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);
}
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
29 changes: 29 additions & 0 deletions scripts/utils/checkGit.ts
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 43cf678

Please sign in to comment.