|
| 1 | +import { promises as fs } from "fs"; |
| 2 | +import fetch from "node-fetch"; |
| 3 | +import path from "path"; |
| 4 | +import { fileURLToPath } from "url"; |
| 5 | +import StyleDictionary from "style-dictionary"; |
| 6 | +import { extractCollectionAndMode, extractCollectionModes } from "./utils.js"; |
| 7 | + |
| 8 | +const __filename = fileURLToPath(import.meta.url); |
| 9 | +const __dirname = path.dirname(__filename); |
| 10 | + |
| 11 | +const styleDictionaryURL = |
| 12 | + "https://zeroheight.zeroheight.com/api/token_management/token_set/7770/style_dictionary_links"; |
| 13 | + |
| 14 | +/** |
| 15 | + * Fetches links for each collection and mode |
| 16 | + * |
| 17 | + * @returns {string[]} list of URLs for each collection and mode |
| 18 | + */ |
| 19 | +async function fetchLinks() { |
| 20 | + try { |
| 21 | + /** styleDictionaryURL value is generated per a token set at zeroheight. |
| 22 | + * |
| 23 | + * If you generate a private link, you need to generate access token and add additional headers to the request |
| 24 | + * X-API-CLIENT |
| 25 | + * X-API-KEY |
| 26 | + * |
| 27 | + * Learn more: https://zeroheight.com/help/article/documenting-figma-color-variables/ |
| 28 | + */ |
| 29 | + const response = await fetch(styleDictionaryURL); |
| 30 | + const textResponse = await response.text(); |
| 31 | + const links = textResponse.split("\n"); |
| 32 | + |
| 33 | + return links; |
| 34 | + } catch (error) { |
| 35 | + console.error("❗️Error fetching links:", error); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Iterates links, fetches Style Dictionary JSON files and saves them |
| 41 | + * |
| 42 | + * @param {string[]} links |
| 43 | + */ |
| 44 | +async function saveFiles(links) { |
| 45 | + try { |
| 46 | + for (const link of links) { |
| 47 | + const response = await fetch(link); |
| 48 | + |
| 49 | + if (!response.ok) { |
| 50 | + throw new Error(`Failed to fetch from ${link}: ${response.statusText}`); |
| 51 | + } |
| 52 | + |
| 53 | + const jsonData = await response.json(); |
| 54 | + |
| 55 | + const [collection, mode] = extractCollectionAndMode(link); |
| 56 | + const directory = path.join(__dirname, "json", collection); |
| 57 | + |
| 58 | + await fs.mkdir(directory, { recursive: true }); |
| 59 | + |
| 60 | + const fileName = `${mode}.json`; |
| 61 | + const filePath = path.join(directory, fileName); |
| 62 | + |
| 63 | + await fs.writeFile(filePath, JSON.stringify(jsonData, null, 2)); |
| 64 | + } |
| 65 | + } catch (error) { |
| 66 | + console.error("❗️Error:", error); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Returns Style Dictionary config |
| 72 | + * |
| 73 | + * @param {string} mode1 |
| 74 | + * @param {string} mode2 |
| 75 | + * @returns {json} Style Dictionary config |
| 76 | + */ |
| 77 | +function getStyleDictionaryConfig(mode1, mode2) { |
| 78 | + const buildDir = [mode1, mode2].join("_"); |
| 79 | + |
| 80 | + return { |
| 81 | + source: [`json/tokens/${mode1}.json`, `json/primitives/${mode2}.json`], |
| 82 | + platforms: { |
| 83 | + web: { |
| 84 | + transformGroup: "web", |
| 85 | + buildPath: `build/web/${buildDir}/`, |
| 86 | + files: [ |
| 87 | + { |
| 88 | + destination: "tokens.css", |
| 89 | + format: "css/variables", |
| 90 | + }, |
| 91 | + ], |
| 92 | + }, |
| 93 | + ios: { |
| 94 | + transformGroup: "ios", |
| 95 | + buildPath: `build/ios/${buildDir}/`, |
| 96 | + files: [ |
| 97 | + { |
| 98 | + destination: "tokens.h", |
| 99 | + format: "ios/macros", |
| 100 | + }, |
| 101 | + ], |
| 102 | + }, |
| 103 | + }, |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Main function that builds tokens |
| 109 | + */ |
| 110 | +(async () => { |
| 111 | + const links = await fetchLinks(); |
| 112 | + await saveFiles(links); |
| 113 | + |
| 114 | + const collectionModes = extractCollectionModes(links); |
| 115 | + const tokensCollectionModes = collectionModes.tokens; |
| 116 | + const primitivesCollectionModes = collectionModes.primitives; |
| 117 | + const platforms = ["web", "ios"]; |
| 118 | + |
| 119 | + console.log("\n🚀 Build started..."); |
| 120 | + |
| 121 | + tokensCollectionModes.forEach((m1) => { |
| 122 | + primitivesCollectionModes.forEach((m2) => { |
| 123 | + platforms.forEach((platform) => { |
| 124 | + const sd = new StyleDictionary(getStyleDictionaryConfig(m1, m2)); |
| 125 | + sd.buildPlatform(platform); |
| 126 | + }); |
| 127 | + }); |
| 128 | + }); |
| 129 | +})(); |
0 commit comments