Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add figma.ts with bun #36

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FIGMA_TOKEN=
FIGMA_FILE_KEY=6dWoWbMuEHiVN5qi1ybDAa
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.lockb binary diff=lockb
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,27 @@ export const TwitterButton = () => (

`currentColor` is set for the icon's path, so you can change the color using CSS.

## Downloading SVGs from Figma

You need to install [Bun](https://bun.sh/).

```bash
brew install oven-sh/bun/bun
```

Create `.env` file and add `FIGMA_TOKEN`.
You can generate Figma Personal access token from [Figma settings page](https://www.figma.com/settings).

```bash
cp .env.example .env
```

You can download SVGs from Figma using the following command:

```bash
npm run figma
```

## License

This icon set is licensed under the [MIT License](https://github.com/ubie-oss/ubie-icons/blob/main/LICENSE).
Binary file added bun.lockb
Binary file not shown.
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"native"
],
"scripts": {
"figma": "node ./scripts/figma.js",
"figma": "bun scripts/figma.ts",
"build:svg": "run-p build:svg:*",
"build:svg:web": "svgr -d src assets",
"build:svg:native": "svgr --native --template native-template.js -d src/native assets",
Expand All @@ -39,6 +39,7 @@
},
"devDependencies": {
"@babel/core": "^7.24.4",
"@figma/rest-api-spec": "^0.13.0",
"@storybook/addon-actions": "^8.0.9",
"@storybook/addon-essentials": "^8.0.9",
"@storybook/addon-interactions": "^8.0.9",
Expand All @@ -56,7 +57,8 @@
"react-native-svg": "^15.1.0",
"storybook": "^8.0.9",
"tsup": "^8.0.2",
"typescript": "^5.4.5"
"typescript": "^5.4.5",
"@types/bun": "latest"
},
"peerDependencies": {
"@types/react": "^17 || ^18",
Expand Down
33 changes: 21 additions & 12 deletions scripts/figma.js → scripts/figma.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
const { resolve } = require('path');
const { createWriteStream } = require('fs');
const { pipeline } = require('stream');
const { promisify } = require('util');
import { resolve } from 'path';
import { createWriteStream } from 'fs';
import { pipeline } from 'stream';
import { promisify } from 'util';
import { GetFileComponentsResponse, GetImagesResponse } from '@figma/rest-api-spec';
import { Readable } from 'stream'; // Add this import

const TOKEN = process.env.FIGMA_TOKEN;
const FIGMA_FILE_KEY = process.env.FIGMA_FILE_KEY;

const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));

const download = async (url, path, callback) => {
const download = async (url: string, path: string, callback: () => void) => {
const streamPipeline = promisify(pipeline);
const response = await fetch(url);
await streamPipeline(response.body, createWriteStream(path));
if (!response.body) {
throw new Error(`unexpected response ${response.statusText}`);
}
const readableStream = response.body as unknown as Readable; // Convert response.body to ReadableStream
await streamPipeline(readableStream, createWriteStream(path));
callback();
};

const main = async () => {
if (!TOKEN || !FIGMA_FILE_KEY) {
throw new Error('FIGMA_TOKEN and FIGMA_FILE_KEY must be provided');
}

const response = await fetch(`https://api.figma.com/v1/files/${FIGMA_FILE_KEY}/components`, {
headers: {
'X-FIGMA-TOKEN': TOKEN,
},
});
const { meta } = await response.json();
const { meta }: GetFileComponentsResponse = await response.json();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typed with @figma/rest-api-spec

const results = meta.components;
const ids = results.map((r) => r.node_id).join(',');

Expand All @@ -29,13 +37,14 @@ const main = async () => {
'X-FIGMA-TOKEN': TOKEN,
},
});
const { images } = await svgResponse.json();
const { images }: GetImagesResponse = await svgResponse.json();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typed with @figma/rest-api-spec

const nodeIds = Object.keys(images);

for (const nodeId of nodeIds) {
const url = images[nodeId];
if (!url) continue;
const result = results.find((r) => r.node_id === nodeId);
const name = result.name;
const name = result?.name;
const path = resolve(__dirname, `../assets/${name}-icon.svg`);
download(url, path, () => {
console.log(path, 'done!');
Expand Down