-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexportComponents.ts
More file actions
59 lines (51 loc) · 1.53 KB
/
exportComponents.ts
File metadata and controls
59 lines (51 loc) · 1.53 KB
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
import JSZip from 'jszip'
import { Codegen } from '../codegen/Codegen'
import { downloadFile } from '../utils/download-file'
const NOTIFY_TIMEOUT = 3000
const ZIP_TEXT_FILE_OPTIONS = { compression: 'DEFLATE' as const }
const ZIP_GENERATE_OPTIONS = {
type: 'uint8array' as const,
compression: 'DEFLATE' as const,
compressionOptions: { level: 1 },
streamFiles: true,
}
export async function exportComponents() {
try {
figma.notify('Exporting components...')
const elements = figma.currentPage.selection.map(
(node) => new Codegen(node),
)
await Promise.all(elements.map((element) => element.run()))
const components = elements.map((element) => element.getComponentsCodes())
const componentCount = components.reduce(
(acc, component) => acc + component.length,
0,
)
if (componentCount === 0) {
figma.notify('No components found')
return
}
figma.notify(`Components exported ${componentCount} components`, {
timeout: NOTIFY_TIMEOUT,
})
const zip = new JSZip()
for (const codeList of components) {
for (const [name, code] of codeList) {
zip.file(name, code, ZIP_TEXT_FILE_OPTIONS)
}
}
await downloadFile(
`${figma.currentPage.name}.zip`,
await zip.generateAsync(ZIP_GENERATE_OPTIONS),
)
figma.notify('Components exported', {
timeout: NOTIFY_TIMEOUT,
})
} catch (error) {
console.error(error)
figma.notify('Error exporting components', {
timeout: NOTIFY_TIMEOUT,
error: true,
})
}
}