-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path.figmaexportrc.js
167 lines (153 loc) · 4.55 KB
/
.figmaexportrc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const transformSVGO = require('@figma-export/transform-svg-with-svgo')
const outputAsSVG = require('@figma-export/output-components-as-svg')
const outputAsReact = require('@figma-export/output-components-as-svgr')
const dedent = require('dedent')
const svgoConfig = require('./svgo.config')
const { writeFile } = require('node:fs/promises')
/**
* @typedef {import('@figma-export/types').ComponentNode} ComponentNode
* @typedef {import('@figma-export/types').ComponentOutputter} ComponentOutputter
* @typedef {import('@figma-export/types').FigmaExportRC} FigmaExportRC
* @typedef {import('@figma-export/types').PageNode} PageNode
*/
const fileId = process.env.FIGMA_ICONS_FILE
const svgDir = './icons/svg'
const reactDir = './react/src'
/** @type {FigmaExportRC} */
module.exports = {
commands: [
['components', {
fileId,
onlyFromPages: ['Icons'],
transformers: [
transformSVGO(svgoConfig)
],
outputters: [
outputAsSVG({
output: svgDir,
getDirname: ({ dirname }) => dirname,
getBasename: ({ componentName }) => `${normalizeIconName(componentName)}.svg`
}),
outputSVGIndex({
output: `${svgDir}/index.json`
}),
outputAsReact({
output: `${reactDir}/icons`,
getDirname: ({ dirname }) => dirname,
getComponentName: ({ componentName }) => normalizeComponentName(componentName),
getComponentFilename: ({ componentName }) => normalizeComponentName(componentName)
}),
outputReactIcons({
output: `${reactDir}/icons.tsx`
}),
outputReactIndex({
output: `${reactDir}/icons/index.json`
})
]
}]
]
}
/**
* @param {string} name
* @returns {string}
*/
function normalizeIconName (name) {
return name.toLowerCase().replace(/ /g, '-')
}
function normalizeComponentName (name) {
return `Icon${name
.replace(/(^[a-z])|( [a-z])/g, substr => substr.toUpperCase())
.replace(/ /g, '')}`
}
/**
* @param {{ output: string }} options
* @returns {import('@figma-export/types').ComponentOutputter}
*/
function outputSVGIndex ({ output }) {
return async pages => {
const components = gatherComponents(pages)
const index = {
generated: timestamp(),
components: components.map(({ page, component: { id, name, svg, ...rest } }) => ({
id: normalizeIconName(name),
name,
file: `${normalizeIconName(name)}.svg`,
size: getSize(rest),
href: `https://figma.com/file/${fileId}/${page.name}?node-id=${id}`,
svg
}))
}
await writeJSON(output, index)
}
}
/**
* @param {{ output: string }} options
* @returns {ComponentOutputter}
*/
function outputReactIndex ({ output }) {
return async pages => {
const components = gatherComponents(pages)
const index = {
generated: timestamp(),
components: components.map(({ page, component: { id, name, ...rest } }) => ({
id: normalizeIconName(name),
name,
component: normalizeComponentName(name),
file: `${normalizeComponentName(name)}.jsx`,
size: getSize(rest),
href: getFigmaHref(page, id)
}))
}
await writeJSON(output, index)
}
}
/**
* @param {{ output: string }} options
* @returns {ComponentOutputter}
*/
function outputReactIcons ({ output }) {
return async pages => {
const components = gatherComponents(pages)
const wrapped = dedent`
import * as icons from './icons/index.js'
import { createStyledIcon as wrap } from './components'
${components.map(({ component }) => {
const name = normalizeComponentName(component.name)
return `export const ${name} = wrap(icons.${name})`
}).join('\n')}
`
await writeFile(output, `${wrapped}\n`)
}
}
/**
* @param {PageNode[]} pages
* @returns {{ page: PageNode, component: ComponentNode }[]}
*/
function gatherComponents (pages) {
return pages.flatMap(page => page.components.map(component => ({ component, page })))
}
/**
*
* @param {PageNode} page
* @param {string} nodeId
* @returns {string}
*/
function getFigmaHref (page, nodeId) {
const qs = nodeId ? `?node-id=${nodeId}` : ''
return `https://figma.com/file/${fileId}/${page.name}${qs}`
}
async function writeJSON (output, data) {
return writeFile(output, JSON.stringify(data, null, 2))
}
function timestamp () {
return new Date().toISOString()
}
/**
* @param {Partial<ComponentNode>} component
*/
function getSize ({ absoluteBoundingBox: { width, height } }) {
return {
width: Number(width),
height: Number(height)
}
}