-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement Figma icon import CLI (#1754)
Relates to #939 Implement Figma CLI command to import and optimize SVG icons from Figma. Also added a GitHub action which is run daily to create a PR with updated icons.
- Loading branch information
1 parent
80c36cb
commit b525ca5
Showing
664 changed files
with
3,911 additions
and
2,609 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@sit-onyx/figma-utils": minor | ||
--- | ||
|
||
feat: add `import-icons` CLI command | ||
|
||
Add new command to import SVG icons from Figma. | ||
For further information, see [our docs](https://onyx.schwarz/development/packages/figma-utils.html#import-icons) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
"@sit-onyx/icons": major | ||
--- | ||
|
||
feat: update latest icons | ||
|
||
All icons are updated to use the latest icons from | ||
|
||
#### New icons | ||
|
||
- anchor | ||
- arrow-small-up-right-top | ||
- broom | ||
- can | ||
- cheese | ||
- chocolate | ||
- company-plus | ||
- computer-eye | ||
- container-large | ||
- container-small | ||
- containers | ||
- controller | ||
- crane | ||
- dolphin | ||
- git | ||
- globe-network | ||
- globe-shield | ||
- icecream | ||
- parking-search | ||
- prawn | ||
- ship-container | ||
- store-test | ||
- tie | ||
- truck-attention | ||
- truck-empty | ||
|
||
#### Deleted icons | ||
|
||
- computer-argus (renamed to computer-eye) | ||
|
||
#### Other breaking changes | ||
|
||
- removed `ICON_CATEGORIES`, use `groupIconsByCategory(ICON_METADATA)` instead | ||
- removed `optimizeSvg`, `isDirectory` and `readAllIconPaths` | ||
- moved exported types from `/utils` to `/types` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
name: Import Figma icons | ||
on: | ||
workflow_dispatch: | ||
schedule: | ||
- cron: "0 6 * * *" # run daily at 6 am | ||
|
||
jobs: | ||
import: | ||
name: Import Figma icons | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: pnpm/action-setup@v4 | ||
|
||
- name: Setup Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version-file: .node-version | ||
cache: pnpm | ||
|
||
- name: 📦 Install dependencies | ||
run: pnpm install | ||
|
||
- name: 🛠️ Build Figma utils | ||
run: pnpm run build | ||
working-directory: packages/figma-utils | ||
|
||
# delete all icons before importing them so no longer existing or renamed icons are removed | ||
- name: Clear icons | ||
run: rm -rfv src/assets/* | ||
working-directory: packages/icons | ||
|
||
- name: Import icons | ||
run: | | ||
pnpm run @sit-onyx/figma-utils import-icons -k "${{ vars.FIGMA_FILE_KEY_ICONS }}" -t "${{ secrets.FIGMA_TOKEN }}" -p "${{ vars.FIGMA_ICON_PAGE_ID }}" -d "../icons/src/assets" -m "../icons/src/metadata.json" | ||
working-directory: packages/figma-utils | ||
|
||
- name: Create pull request | ||
uses: peter-evans/create-pull-request@v6 | ||
with: | ||
commit-message: "feat: update Figma icons" | ||
title: "feat: update Figma icons" | ||
body: This is an auto-generated pull request. All Figma icons have been updated. | ||
branch-suffix: short-commit-hash # needed to not override other pull requests created via workflows | ||
team-reviewers: "@SchwarzIT/onyx-ux" | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
BRANCH_NAME: ${{ github.ref_name }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,5 +38,8 @@ | |
}, | ||
"dependencies": { | ||
"commander": "^12.1.0" | ||
}, | ||
"optionalDependencies": { | ||
"svgo": "^3.3.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { Command } from "commander"; | ||
import { mkdir, writeFile } from "node:fs/promises"; | ||
import path from "node:path"; | ||
import { writeIconMetadata } from "../icons/generate.js"; | ||
import { optimizeSvg } from "../icons/optimize.js"; | ||
import { parseComponentsToIcons } from "../icons/parse.js"; | ||
import { fetchFigmaComponents, fetchFigmaSVGs } from "../index.js"; | ||
import { isDirectory } from "../utils/fs.js"; | ||
|
||
export type ImportIconsCommandOptions = { | ||
fileKey: string; | ||
token: string; | ||
pageId: string; | ||
aliasSeparator: string; | ||
dir?: string; | ||
metaFile?: string; | ||
}; | ||
|
||
export const importIconsCommand = new Command("import-icons") | ||
.description("CLI tool to import SVG icons from Figma.") | ||
.requiredOption("-k, --file-key <string>", "Figma file key (required)") | ||
.requiredOption( | ||
"-t, --token <string>", | ||
"Figma access token with scope `file_read` or `files:read` (required)", | ||
) | ||
.requiredOption("-p, --page-id <string>", "Figma page ID that contains the icons (required)") | ||
.option( | ||
"-d, --dir <string>", | ||
"Directory to save the icons to. Defaults to current working directory of the script.", | ||
) | ||
.option( | ||
"-m, --meta-file <string>", | ||
'JSON filename/path to write icon metadata to (categories, alias names etc.). Must end with ".json". If unset, no metadata will be generated.', | ||
) | ||
.option( | ||
"-s, --alias-separator <string>", | ||
"Separator for icon alias names (which can be set to the component description in Figma).", | ||
"|", | ||
) | ||
.action(importIconsCommandAction); | ||
|
||
/** | ||
* Action to run when executing the import action. Only intended to be called manually for testing. | ||
*/ | ||
export async function importIconsCommandAction(options: ImportIconsCommandOptions) { | ||
console.log("Fetching components from Figma API..."); | ||
const data = await fetchFigmaComponents(options.fileKey, options.token); | ||
|
||
console.log("Parsing Figma icons..."); | ||
const parsedIcons = parseComponentsToIcons({ | ||
components: data.meta.components, | ||
pageId: options.pageId, | ||
aliasSeparator: options.aliasSeparator, | ||
}); | ||
const outputDirectory = options.dir ?? process.cwd(); | ||
|
||
console.log(`Fetching SVG content for ${parsedIcons.length} icons...`); | ||
|
||
const svgContents = await fetchFigmaSVGs( | ||
options.fileKey, | ||
parsedIcons.map(({ id }) => id), | ||
options.token, | ||
); | ||
|
||
console.log("Optimizing and writing icon files..."); | ||
|
||
if (!(await isDirectory(outputDirectory))) { | ||
await mkdir(outputDirectory, { recursive: true }); | ||
} | ||
|
||
await Promise.all( | ||
parsedIcons.map((icon) => { | ||
const content = optimizeSvg(svgContents[icon.id]); | ||
const fullPath = path.join(outputDirectory, `${icon.name}.svg`); | ||
return writeFile(fullPath, content, "utf-8"); | ||
}), | ||
); | ||
|
||
if (options.metaFile) { | ||
console.log("Writing icon metadata..."); | ||
await writeIconMetadata(options.metaFile, parsedIcons); | ||
} | ||
|
||
console.log("Done."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.