Skip to content
This repository was archived by the owner on Jul 7, 2025. It is now read-only.

Commit 33304e5

Browse files
adding scripts and workflows (#869)
1 parent fc82476 commit 33304e5

File tree

5 files changed

+927
-1066
lines changed

5 files changed

+927
-1066
lines changed

.github/workflows/deploy_preview.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ name: Deploy Preview
22

33
on:
44
pull_request:
5+
workflow_run:
6+
workflows: [Get Figma Images]
7+
types:
8+
- completed
59

610
concurrency:
711
group: ${{ github.workflow }}-${{ github.ref }}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Get Figma Images
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
types:
9+
- labeled
10+
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.ref }}
13+
cancel-in-progress: true
14+
15+
jobs:
16+
findNodeReferences:
17+
if: ${{ github.event.label.name == 'update figma images' || github.event_name == 'push' }}
18+
runs-on: ubuntu-latest
19+
env:
20+
FilesToScan: '**/*.mdx'
21+
ImageUrlFile: figmaImageNodeUrls.json
22+
ImageOutputDir: content/images/figma
23+
FigmaToken: ${{ secrets.FIGMA_TOKEN }}
24+
steps:
25+
- name: Checkout repository
26+
uses: actions/checkout@v4
27+
- name: Set up Node
28+
uses: actions/setup-node@v4
29+
with:
30+
node-version: 20
31+
cache: 'yarn'
32+
- name: Install dependencies
33+
run: yarn
34+
- name: Get Figma Images
35+
run: node scripts/getFigmaImages.js "${{env.FilesToScan}}" > ${{env.ImageUrlFile}}
36+
- name: Log file content
37+
run: cat ${{env.ImageUrlFile}}
38+
- name: Download images from figma
39+
run: npx @primer/figma-images --figmaToken ${{env.FigmaToken}} --nodeURLsFile ${{env.ImageUrlFile}} --outputDir ${{env.ImageOutputDir}}
40+
- name: Log output dir content
41+
run: ls ${{env.ImageOutputDir}}
42+
- uses: stefanzweifel/git-auto-commit-action@v4
43+
with:
44+
commit_message: github-actions[bot] Update figma images
45+
- uses: actions-ecosystem/action-remove-labels@v1
46+
if: always()
47+
with:
48+
labels: 'update figma images'

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
"scripts": {
66
"start": "gatsby develop",
77
"clean": "gatsby clean",
8-
"build": "gatsby build",
8+
"build": "npm run copy:figma-images && gatsby build",
99
"serve": "gatsby serve",
10-
"build:preview": "gatsby build",
10+
"build:preview": "npm run copy:figma-images && gatsby build",
1111
"now-build": "yarn build",
1212
"lint": "eslint .",
13-
"markdownlint": "markdownlint-cli2 \"**/*.{md,mdx}\" \"!.github\" \"!node_modules\""
13+
"markdownlint": "markdownlint-cli2 \"**/*.{md,mdx}\" \"!.github\" \"!node_modules\"",
14+
"copy:figma-images": "mkdir -p public/images && cp -r content/images/figma public/images || true"
1415
},
1516
"dependencies": {
1617
"@github/prettier-config": "^0.0.6",
@@ -50,7 +51,9 @@
5051
},
5152
"devDependencies": {
5253
"@github/markdownlint-github": "^0.3.0",
54+
"@primer/figma-images": "^0.1.2",
5355
"esm": "^3.2.25",
56+
"fast-glob": "^3.3.2",
5457
"markdownlint-cli2": "^0.5.1",
5558
"markdownlint-cli2-formatter-pretty": "^0.0.3",
5659
"mustache": "^4.2.0",

scripts/getFigmaImages.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Import necessary modules
2+
const fs = require('fs').promises;
3+
const fastGlob = require('fast-glob');
4+
5+
const isFigmaLink = (match) => {
6+
return match.startsWith('https://www.figma.com/design/')
7+
}
8+
9+
const findMatches = async (regexPattern, files) => {
10+
const matches = await Promise.all(
11+
files.map(async (filePath) => {
12+
// read file content
13+
const content = await fs.readFile(filePath, { encoding: 'utf8' })
14+
// try to find all matches in the file content
15+
const matches = [...content.matchAll(regexPattern)]
16+
// for each match, return the first group
17+
return matches.map((match) => match[1]).filter(isFigmaLink);
18+
})
19+
)
20+
//
21+
return matches.flat()
22+
}
23+
24+
const run = async () => {
25+
// get arguments
26+
const [fileGlob] = process.argv.slice(2)
27+
if(!fileGlob) {
28+
console.error('❌ Please provide a file glob as the argument. It needs to be wrapped in quotes.')
29+
return
30+
}
31+
// get all files that match the file glob
32+
const files = await fastGlob([fileGlob])
33+
// define the regex pattern to search
34+
const pattern = /<FigmaImage\s+[^>]*src="([^"]+)"[^>]*>/g
35+
// find matches in find
36+
const matches = await findMatches(pattern, files)
37+
// output result
38+
console.log(JSON.stringify(matches, null, 2))
39+
40+
}
41+
42+
run()

0 commit comments

Comments
 (0)