|
| 1 | +import * as Figma from "figma-api"; |
| 2 | +import * as fs from "fs"; |
| 3 | +import { compareAllSvgs } from "./utils/compare.mjs"; |
| 4 | +import { downloadFrameImages } from "./utils/download.mjs"; |
| 5 | +import { optimizeAllSVGsInFolder } from "./utils/optimize-svg.mjs"; |
| 6 | + |
| 7 | +const INTERIM_DIRECTORY = "auto-generated-icons"; |
| 8 | +const TARGET_DIRECTORY = "auto-generated-optimized-icons"; |
| 9 | + |
| 10 | +function createFolder(directory) { |
| 11 | + if (!fs.existsSync(directory)) { |
| 12 | + fs.mkdirSync(directory); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +function findCardFrames(node) { |
| 17 | + const cardFrames = []; |
| 18 | + |
| 19 | + if ( |
| 20 | + node.type === "FRAME" && |
| 21 | + node.name === "Cards [DO NOT CHANGE NAME - REQUIRED FOR EXPORT]" && |
| 22 | + "children" in node && |
| 23 | + node.children |
| 24 | + ) { |
| 25 | + node.children.forEach((child) => { |
| 26 | + cardFrames.push(child); |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + if ("children" in node && node.children) { |
| 31 | + node.children.forEach((child) => { |
| 32 | + cardFrames.push(...findCardFrames(child)); |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + return cardFrames; |
| 37 | +} |
| 38 | + |
| 39 | +async function main() { |
| 40 | + createFolder(INTERIM_DIRECTORY); |
| 41 | + |
| 42 | + // Connect to Figma and get the file |
| 43 | + const token = process.env.FIGMA_ACCESS_TOKEN || ""; |
| 44 | + if (!token) { |
| 45 | + throw new Error("FIGMA_ACCESS_TOKEN environment variable is required"); |
| 46 | + } |
| 47 | + const api = new Figma.Api({ |
| 48 | + personalAccessToken: token, |
| 49 | + }); |
| 50 | + |
| 51 | + const fileKey = process.env.FIGMA_FILE_ID || "GQZX9DHncxo9fRYV4cOaWV"; //Sprout Icons file |
| 52 | + const file = await api.getFile(fileKey); |
| 53 | + const page = file.document.children.find((child) => child.name === "Icons [EXPORT]"); |
| 54 | + |
| 55 | + // eslint-disable-next-line no-console |
| 56 | + console.log(`Pages in file: ${file.document.children.map((c) => c.name).join(", ")}`); |
| 57 | + // eslint-disable-next-line no-console |
| 58 | + console.log(`Found page "Icons [EXPORT]": ${!!page}`); |
| 59 | + |
| 60 | + // Data to share across process |
| 61 | + const ctx = { |
| 62 | + api, |
| 63 | + fileKey, |
| 64 | + dir: INTERIM_DIRECTORY, |
| 65 | + }; |
| 66 | + |
| 67 | + // eslint-disable-next-line no-console |
| 68 | + console.log(`Downloading images for file: ${file.name}`); |
| 69 | + |
| 70 | + const componentNodeInfos = []; |
| 71 | + //const privateComponents: Figma.Node[] = []; |
| 72 | + const publicComponents = []; |
| 73 | + |
| 74 | + const iconFrame = page.children.find((child) => child.name === "Icons"); |
| 75 | + |
| 76 | + // eslint-disable-next-line no-console |
| 77 | + console.log(`Top-level children of page: ${page.children.map((c) => c.name).join(", ")}`); |
| 78 | + // eslint-disable-next-line no-console |
| 79 | + console.log(`Found "Icons" frame: ${!!iconFrame}`); |
| 80 | + |
| 81 | + const cardFrames = findCardFrames(iconFrame); |
| 82 | + |
| 83 | + // eslint-disable-next-line no-console |
| 84 | + console.log(`Found ${cardFrames.length} Card frames`); |
| 85 | + |
| 86 | + cardFrames.forEach((node) => { |
| 87 | + //Sections marks optional category |
| 88 | + |
| 89 | + const iconComponentNodes = node.children[1].children.filter((n) => n.type === "COMPONENT"); |
| 90 | + const categoryName = iconComponentNodes.length > 1 ? node.children[0].children[0].characters.trim() : null; //Category is only applied if multiple icons |
| 91 | + |
| 92 | + iconComponentNodes.forEach((child) => { |
| 93 | + const componentNode = child; |
| 94 | + componentNodeInfos.push({ |
| 95 | + node: componentNode, |
| 96 | + category: categoryName, |
| 97 | + meta: file.components[componentNode.id], |
| 98 | + }); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + componentNodeInfos.forEach((nodeInfo) => { |
| 103 | + //if (getVisibility(node) === "public") { |
| 104 | + publicComponents.push(nodeInfo.node); |
| 105 | + // } else { |
| 106 | + // privateComponents.push(node); |
| 107 | + // } |
| 108 | + }); |
| 109 | + |
| 110 | + if (publicComponents.length > 0) { |
| 111 | + // eslint-disable-next-line no-console |
| 112 | + console.log(`Downloading ${publicComponents.length} icons`); |
| 113 | + await downloadFrameImages(publicComponents, ctx); |
| 114 | + } else { |
| 115 | + // eslint-disable-next-line no-console |
| 116 | + console.warn("No icons found — check page/frame names match the Figma file structure."); |
| 117 | + } |
| 118 | + // if (privateComponents.length > 0) { |
| 119 | + // await downloadFrameImages(privateComponents, ctx, 'private'); |
| 120 | + // } |
| 121 | + |
| 122 | + //Optimize SVGs |
| 123 | + optimizeAllSVGsInFolder(INTERIM_DIRECTORY, TARGET_DIRECTORY); |
| 124 | + |
| 125 | + //Test SVGs for discrepancies |
| 126 | + compareAllSvgs(INTERIM_DIRECTORY, TARGET_DIRECTORY) |
| 127 | + .then(() => { |
| 128 | + // eslint-disable-next-line no-console |
| 129 | + console.log("\x1b[32m\x1b[1mAll SVGs are identical.\x1b[0m"); |
| 130 | + }) |
| 131 | + .catch((err) => { |
| 132 | + // eslint-disable-next-line no-console |
| 133 | + console.log("Some SVGs differ", err); |
| 134 | + }); |
| 135 | +} |
| 136 | + |
| 137 | +main() |
| 138 | + .then(() => { |
| 139 | + // eslint-disable-next-line no-console |
| 140 | + console.log("SVGS generated and tested!"); |
| 141 | + }) |
| 142 | + .catch((err) => { |
| 143 | + // eslint-disable-next-line no-console |
| 144 | + console.error("SVGS did not work or did not pass tests", err); |
| 145 | + process.exit(1); |
| 146 | + }); |
0 commit comments