Skip to content

Commit 9c6fd27

Browse files
committed
update
1 parent 99b5e3d commit 9c6fd27

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+15215
-12
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Sync Figma variables to tokens
2+
on:
3+
workflow_dispatch:
4+
inputs:
5+
file_key:
6+
description: 'The file key of the Figma file to be updated'
7+
required: true
8+
9+
jobs:
10+
sync-figma-to-tokens:
11+
runs-on: ubuntu-latest
12+
permissions:
13+
# Need to be able to create new branches and commits
14+
contents: write
15+
pull-requests: write
16+
steps:
17+
- name: Install Node.js
18+
uses: actions/setup-node@v3
19+
with:
20+
node-version: '18.16.0'
21+
22+
- name: Set NPM version
23+
run: npm install -g [email protected]
24+
25+
- name: Clone repo
26+
uses: actions/checkout@v3
27+
28+
- name: Install dependencies
29+
run: npm install
30+
31+
- name: Sync variables in Figma file to tokens
32+
run: npm run sync-figma-to-tokens -- --output tokens
33+
env:
34+
FILE_KEY: ${{ github.event.inputs.file_key }}
35+
PERSONAL_ACCESS_TOKEN: ${{ secrets.GH_ACTION_VARIABLES_SYNC_FIGMA_TOKEN }}
36+
37+
- name: Create Pull Request
38+
uses: peter-evans/create-pull-request@v5
39+
with:
40+
commit-message: Update tokens from Figma
41+
title: Update tokens from Figma
42+
body: 'Update tokens from Figma from file: https://www.figma.com/file/${{ github.event.inputs.file_key }}'
43+
branch: update-tokens
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Sync tokens to Figma
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
file_key:
7+
description: 'The file key of the Figma file to be updated'
8+
required: true
9+
10+
jobs:
11+
sync-tokens-to-figma:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Install Node.js
15+
uses: actions/setup-node@v3
16+
with:
17+
node-version: '18.16.0'
18+
19+
- name: Set NPM version
20+
run: npm install -g [email protected]
21+
22+
- name: Clone repo
23+
uses: actions/checkout@v3
24+
25+
- name: Install dependencies
26+
run: npm install
27+
28+
- name: 🔍 Check for duplicate token collections
29+
run: npm run check-duplicates
30+
31+
- name: 🚀 Sync tokens to Figma file
32+
run: npm run sync-tokens-to-figma
33+
env:
34+
FILE_KEY: ${{ github.event.inputs.file_key }}
35+
PERSONAL_ACCESS_TOKEN: ${{ secrets.GH_ACTION_VARIABLES_SYNC_FIGMA_TOKEN }}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: CI
2+
on: [push]
3+
jobs:
4+
test:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- name: Check out repository code
8+
uses: actions/checkout@v3
9+
- run: npm install
10+
- run: npm run prettier:check
11+
- run: npm run test

design-tokens/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
.env
3+
design-tokens/ echo design-tokens/
4+
design-tokens/ echo design-tokens/
5+
design-tokens/

design-tokens/.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ignore tokens json files
2+
tokens/*

design-tokens/.prettierrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"trailingComma": "all",
3+
"tabWidth": 2,
4+
"semi": false,
5+
"singleQuote": true,
6+
"printWidth": 100,
7+
"arrowParens": "always"
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"editor.formatOnSave": true
3+
}

design-tokens/LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Figma
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

design-tokens/README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# variables-github-action-example
2+
3+
This repository contains a couple of GitHub Actions workflows:
4+
5+
- Sync tokens to Figma
6+
- Sync Figma variables to tokens
7+
8+
These workflows demonstrate bi-directional syncing between variables in Figma and design tokens in a codebase using Figma's [Variables REST API](https://www.figma.com/developers/api#variables). For more background and a graphical representation of what these workflows do, see our [Syncing design systems using Variables REST API](https://www.figma.com/community/file/1270821372236564565) FigJam file.
9+
10+
To use these workflows, you should copy the code in this repository into your organization and modify it to suit the needs of your design processes.
11+
12+
## Prerequisites
13+
14+
To use the "Sync Figma variables to tokens" workflow, you must be a full member of an Enterprise org in Figma. To use the "Sync tokens to Figma" workflow, you must also have an editor seat.
15+
16+
Both workflows assume that you have a single Figma file with local variable collections, along with one or more tokens json files in the `tokens/` directory that adhere\* to the [draft W3C spec for Design Tokens](https://tr.designtokens.org/format/). For demonstration purposes, this directory contains the tokens exported from the [Get started with variables](https://www.figma.com/community/file/1253086684245880517/Get-started-with-variables) Community file. Have a copy of this file ready if you want to try out the workflow with these existing tokens.
17+
18+
> \*See `src/token_types.ts` for more details on the format of the expected tokens json files, including the deviations from the draft design tokens spec we've had to make. **We expect there to be one tokens file per variable collection and mode.**
19+
20+
In addition, you must also have a [personal access token](https://www.figma.com/developers/api#access-tokens) for the Figma API to allow these workflows to authenticate with the API. For the "Sync Figma variables to tokens" workflow, the token must have at least the Read-only Variables scope selected. For the "Sync tokens to Figma" workflow, the token must have the Read and write Variables scope selected.
21+
22+
## Usage
23+
24+
Before running either of these workflows, you'll need to create an [encrypted secret](https://docs.github.com/en/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository) in your repository named `GH_ACTION_VARIABLES_SYNC_FIGMA_TOKEN` containing your personal access token.
25+
26+
Both workflows are configured to [run manually](https://docs.github.com/en/actions/using-workflows/manually-running-a-workflow) for demonstration purposes, and are designed to be as conservative as possible in their functionality (see details below).
27+
28+
### Sync Figma variables to tokens
29+
30+
To run the "Sync Figma variables to tokens" workflow:
31+
32+
- Open the workflow under the **Actions** tab in your repository and click **Run workflow**
33+
- You will be asked to provide the file key of the Figma file. The file key can be obtained from any Figma file URL: `https://www.figma.com/file/{file_key}/{title}`.
34+
- After the workflow finishes, you should see a new pull request if there are changes to be made to the tokens files in the `tokens/` directory. If there are no changes to be made, then a pull request will not be created.
35+
36+
This workflow has some key behaviors to note:
37+
38+
- After generating the new tokens json files, this workflow creates a pull request for someone on the team to review. If you prefer, you can modify the workflow to commit directly to a designated branch without creating a pull request.
39+
- If a variable collection or mode is removed from the Figma file, the corresponding tokens file will not be removed from the codebase.
40+
41+
### Sync tokens to Figma
42+
43+
To run the "Sync tokens to Figma" workflow:
44+
45+
- Open the workflow under the **Actions** tab in your repository and click **Run workflow**
46+
- You will be asked to provide the file key of the Figma file. The file key can be obtained from any Figma file URL: `https://www.figma.com/file/{file_key}/{title}`. Note: if you are trying out this workflow for the first time, use a file that is separate from your design system to avoid any unintended changes.
47+
- After the workflow finishes, open the file in Figma and observe that the variables should be updated to reflect the tokens in your tokens files.
48+
49+
This workflow has some key behaviors to note:
50+
51+
- Though this workflow is configured to run manually, you're free to modify it to run on code push to a specified branch once you are comfortable with its behavior.
52+
- When syncing to a Figma file that does not have any variable collections, this workflow will add brand-new collections and variables. When syncing to a Figma file that has existing variable collections, this workflow will modify collections and variables **in-place** using name matching. That is, it will look for existing collections and variables by name, modify their properties and values if names match, and create new variables if names do not match.
53+
- The workflow will not remove variables or variable collections that have been removed in your tokens files.
54+
- When mapping aliases to existing local variables, we assume that variable names are unique _across all collections_ in the Figma file. Figma allows duplicate variable names across collections, so you should make sure that aliases don't have naming conflicts in your file.
55+
- For optional Figma variable properties like scopes and code syntax, the workflow will not modify these properties in Figma if the tokens json files do not contain those properties.
56+
- If a string variable is bound to a text node content in the same file, and the text node uses a [shared font in the organization](https://help.figma.com/hc/en-us/articles/360039956774), that variable cannot be updated and will result in a 400 response.
57+
58+
## Local development
59+
60+
You can run the GitHub actions locally by running `npm install` and creating a `.env` file with the following contents:
61+
62+
```
63+
FILE_KEY="your_file_key"
64+
PERSONAL_ACCESS_TOKEN="your_personal_access_token"
65+
```
66+
67+
and then running:
68+
69+
```sh
70+
npm run sync-tokens-to-figma
71+
72+
# or
73+
74+
# Defaults to writing to the tokens_new directory
75+
npm run sync-figma-to-tokens
76+
77+
# Writes to the specified directory
78+
npm run sync-figma-to-tokens -- --output directory_name
79+
```
80+
81+
### Testing
82+
83+
Run the Jest tests:
84+
85+
```sh
86+
npm run test
87+
```

design-tokens/jest.config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { Config } from 'jest'
2+
3+
const config: Config = {
4+
verbose: true,
5+
//transform: {},
6+
preset: 'ts-jest',
7+
testEnvironment: 'node',
8+
moduleNameMapper: {
9+
'^(\\.{1,2}/.*)\\.js$': '$1',
10+
},
11+
}
12+
13+
export default config

0 commit comments

Comments
 (0)