cloudflare/kumo
Publicmirrored fromhttps://github.com/cloudflare/kumoAvailable
packages/kumo-figma/scripts/maybe-sync.ts
61lines · modecode
| 1 | import { existsSync, readFileSync } from "node:fs"; |
| 2 | import { dirname, resolve } from "node:path"; |
| 3 | import { fileURLToPath } from "node:url"; |
| 4 | import { spawnSync } from "node:child_process"; |
| 5 | |
| 6 | const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 7 | const ENV_PATH = resolve(__dirname, ".env"); |
| 8 | |
| 9 | function loadDotEnvIfPresent() { |
| 10 | if (!existsSync(ENV_PATH)) return; |
| 11 | |
| 12 | const envContent = readFileSync(ENV_PATH, "utf-8"); |
| 13 | for (const line of envContent.split("\n")) { |
| 14 | const trimmed = line.trim(); |
| 15 | if (trimmed.length === 0) continue; |
| 16 | if (trimmed.startsWith("#")) continue; |
| 17 | |
| 18 | const [key, ...valueParts] = trimmed.split("="); |
| 19 | const value = valueParts.join("=").replace(/^["']|["']$/g, ""); |
| 20 | if (!key || !value) continue; |
| 21 | |
| 22 | if (!process.env[key]) { |
| 23 | process.env[key] = value; |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | function shouldSkipSync() { |
| 29 | const value = (process.env.KUMO_FIGMA_SKIP_SYNC || "").toLowerCase(); |
| 30 | return value === "1" || value === "true" || value === "yes"; |
| 31 | } |
| 32 | |
| 33 | function main() { |
| 34 | if (shouldSkipSync()) { |
| 35 | console.log("Skipping figma:sync (KUMO_FIGMA_SKIP_SYNC=1)"); |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | // Support the common setup of putting FIGMA_TOKEN in scripts/.env |
| 40 | loadDotEnvIfPresent(); |
| 41 | |
| 42 | if (!process.env.FIGMA_TOKEN) { |
| 43 | console.log( |
| 44 | "Skipping figma:sync (no FIGMA_TOKEN). " + |
| 45 | "Create packages/kumo-figma/scripts/.env or export FIGMA_TOKEN to enable token sync.", |
| 46 | ); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | console.log("Running figma:sync..."); |
| 51 | const result = spawnSync("pnpm", ["run", "figma:sync"], { |
| 52 | stdio: "inherit", |
| 53 | env: process.env, |
| 54 | }); |
| 55 | |
| 56 | if (typeof result.status === "number" && result.status !== 0) { |
| 57 | process.exit(result.status); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | main(); |
| 62 | |