-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
74 lines (62 loc) · 1.9 KB
/
index.ts
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
import * as fs from 'fs'
import * as path from 'path'
import { Extractor } from '@figma-extractor/core'
import { svgoOptimize, defaultPlugins, currentColorPlugin } from '@figma-extractor/svgo-optimize'
import { parseComponentProperties } from '@figma-extractor/parse-properties'
import {
transformToJsx,
svgAttributes,
Plugin as JsxPlugin,
} from '@figma-extractor/transform-to-jsx'
import { pascalCase, snakeCase } from 'change-case'
// Clone of Micorsoft's Fluent emojis
// https://www.figma.com/file/wRtfYH2sNvqSyKYsyHYYHh/Fluent-emoji-%E2%80%94-3-(Community)
const FILE_ID = 'wRtfYH2sNvqSyKYsyHYYHh'
const FOOD_PAGE_ID = '12:33'
const ICONS_FOLDER = path.join(__dirname, '../icons')
const extractor = new Extractor({
depth: 2,
nodeFilter(node) {
return (
node.type === 'COMPONENT' &&
(node.name === 'Theme=High Contrast' || node.name === 'Theme=Flat')
)
},
})
const svgoPlugins = [...defaultPlugins, currentColorPlugin]
const jsxPlugins: JsxPlugin[] = [
svgAttributes((_node) => ({
focusable: false,
'aria-hidden': true,
})),
]
const syncIcons = async () => {
const nodes = await extractor.extract({
fileId: FILE_ID,
nodeId: FOOD_PAGE_ID,
})
if (!fs.existsSync(ICONS_FOLDER)) {
fs.mkdirSync(ICONS_FOLDER)
}
nodes
.map(parseComponentProperties)
.map((node) => svgoOptimize(node, svgoPlugins))
.map((node) => transformToJsx(node, jsxPlugins))
.forEach((node) => {
const reactName = `${pascalCase(node.parent!.name)}${node.properties['Theme']}Icon`
const reactContent = `
import React from 'react'
export const ${reactName} = () => {
return (
${node.jsxContent}
)
}
`
fs.writeFileSync(path.join(ICONS_FOLDER, `${reactName}.jsx`), reactContent)
fs.writeFileSync(
path.join(ICONS_FOLDER, `${snakeCase(node.parent!.name + node.properties['Theme'])}.svg`),
node.content,
)
})
}
syncIcons()