improve sync

This commit is contained in:
Pavel 2024-09-10 16:03:27 +02:00
parent 78ebe7df4a
commit e725c2d415
No known key found for this signature in database
GPG Key ID: 8E4C82D464215E83
1 changed files with 97 additions and 72 deletions

View File

@ -1,10 +1,10 @@
import { isCancel, outro, spinner, text } from '@clack/prompts' import { isCancel, log, outro, spinner, text } from '@clack/prompts'
import { transform } from '@svgr/core' import { transform } from '@svgr/core'
import * as Figma from 'figma-api' import * as Figma from 'figma-api'
import fs from 'fs-extra' import fs from 'fs-extra'
import fetch from 'node-fetch' import fetch from 'node-fetch'
import pMap from 'p-map'
import path from 'path' import path from 'path'
import prettier from 'prettier'
const SVG_DIR = path.resolve(__dirname, '../svg') const SVG_DIR = path.resolve(__dirname, '../svg')
@ -35,14 +35,26 @@ const FILE_KEY = 'qLLuMLfpGxK9OfpIavwsmK'
const NODE_IDS = { const NODE_IDS = {
// currently generates only set of size 20 // currently generates only set of size 20
// https://github.com/status-im/status-web/issues/466 // https://github.com/status-im/status-web/issues/466
'3239:987': 'icons', '3239:987': {
'3227:1083': 'social', name: 'icons-20',
// '942:77': 'reactions', folder: '20',
'942:218': 'love', },
'942:217': 'thumbs-up', '3239:1440': {
'942:216': 'thumbs-down', name: 'icons-16',
'942:215': 'laugh', folder: '16',
'942:213': 'angry', },
'3239:3712': {
name: 'icons-12',
folder: '12',
},
'3227:1083': {
name: 'social',
folder: 'social',
},
'942:77': {
name: 'reactions',
folder: 'reactions',
},
} }
const figma = new Figma.Api({ const figma = new Figma.Api({
@ -50,13 +62,15 @@ const figma = new Figma.Api({
}) })
const s1 = spinner() const s1 = spinner()
s1.start('Fetching nodes from Figma') s1.start('Fetching Figma file nodes')
const { nodes } = await figma.getFileNodes(FILE_KEY, Object.keys(NODE_IDS)) const { nodes } = await figma.getFileNodes(FILE_KEY, Object.keys(NODE_IDS))
// console.log('🚀 ~ nodes:', nodes)
s1.stop('Done!') s1.stop('Done!')
for (const [nodeId, name] of Object.entries(NODE_IDS)) { for (const [nodeId, { name, folder }] of Object.entries(NODE_IDS)) {
const s2 = spinner() const s2 = spinner()
s2.start('Fetching nodes from Figma') s2.start(`Fetching SVG images for ${name}`)
const { components } = nodes[nodeId]! const { components } = nodes[nodeId]!
const nodeIds = Object.keys(components) const nodeIds = Object.keys(components)
@ -75,13 +89,18 @@ for (const [nodeId, name] of Object.entries(NODE_IDS)) {
s2.stop('Done!') s2.stop('Done!')
const s3 = spinner() log.info(`Generating SVGs for ${name}`)
s3.start(`Generating SVGs for ${name}`)
for (const [nodeId, image] of Object.entries(images)) { await pMap(
Object.entries(images),
async ([nodeId, image]) => {
const icon = components[nodeId]! const icon = components[nodeId]!
const svgRaw = await fetch(image!)
const svgRaw = await fetch(image!).then(res => res.text()) .then(res => res.text())
.catch(() => {
log.error(`Failed to fetch SVG for ${icon.name}`)
return
})
// note: probably a wrapper layer for https://www.figma.com/file/qLLuMLfpGxK9OfpIavwsmK/Iconset?type=design&node-id=4408-955&mode=dev, thus skipping // note: probably a wrapper layer for https://www.figma.com/file/qLLuMLfpGxK9OfpIavwsmK/Iconset?type=design&node-id=4408-955&mode=dev, thus skipping
// icon:: { // icon:: {
@ -102,13 +121,18 @@ for (const [nodeId, name] of Object.entries(NODE_IDS)) {
// documentationLinks: [] // documentationLinks: []
// } // }
if (!svgRaw) { if (!svgRaw) {
continue log.error(`Failed to fetch SVG for ${icon.name}`)
return
} }
const svg = await transform(svgRaw, { const svg = await transform(svgRaw, {
plugins: ['@svgr/plugin-svgo'], plugins: ['@svgr/plugin-svgo'],
replaceAttrValues: { replaceAttrValues: {
// '#000': 'currentColor', '#09101C': 'currentColor',
'#2A4AF5': 'var(--customisation-50, #2A4AF5)',
},
svgProps: {
'aria-hidden': '{true}',
}, },
svgoConfig: { svgoConfig: {
plugins: [ plugins: [
@ -125,23 +149,24 @@ for (const [nodeId, name] of Object.entries(NODE_IDS)) {
}, },
}, },
}, },
'prefixIds',
], ],
}, },
}) })
const nameParts = icon.name.replace(' / ', '/').split('/') const nameParts = icon.name.replace(' / ', '/').split('/')
const iconName = nameParts[nameParts.length - 1] const iconName = nameParts.at(-1)!
const fileName = `${toKebabCase(iconName)}-icon.svg` const fileName = `${toKebabCase(iconName)}-icon.svg`
const filePath = path.resolve(SVG_DIR, fileName) const filePath = path.resolve(SVG_DIR, folder, fileName)
const prettierOptions = prettier.resolveConfig.sync(process.cwd()) await fs.ensureDir(path.dirname(filePath))
const formattedSvg = prettier.format(svg, {
...prettierOptions,
parser: 'html',
})
await fs.writeFile(filePath, formattedSvg, { encoding: 'utf8' }) await fs.writeFile(filePath, svg, { encoding: 'utf8' })
}
log.success(filePath)
s3.stop(`${Object.keys(images).length} SVGs generated`) },
{ concurrency: 5 }
)
log.success(`${Object.keys(images).length} SVGs generated`)
} }