@mp3wizard/figma-console-mcp 1.25.1 → 1.28.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (207) hide show
  1. package/README.md +53 -35
  2. package/dist/apps/design-system-dashboard/mcp-app.html +78 -78
  3. package/dist/apps/token-browser/mcp-app.html +60 -59
  4. package/dist/cloudflare/core/config.js +0 -8
  5. package/dist/cloudflare/core/console-monitor.js +3 -3
  6. package/dist/cloudflare/core/diagnose-tool.js +96 -0
  7. package/dist/cloudflare/core/figma-tools.js +69 -229
  8. package/dist/cloudflare/core/identity.js +96 -0
  9. package/dist/cloudflare/core/tokens/alias-resolver.js +135 -0
  10. package/dist/cloudflare/core/tokens/config.js +284 -0
  11. package/dist/cloudflare/core/tokens/figma-converter.js +195 -0
  12. package/dist/cloudflare/core/tokens/formatters/css-vars.js +329 -0
  13. package/dist/cloudflare/core/tokens/formatters/dtcg.js +300 -0
  14. package/dist/cloudflare/core/tokens/formatters/index.js +45 -0
  15. package/dist/cloudflare/core/tokens/formatters/json.js +187 -0
  16. package/dist/cloudflare/core/tokens/formatters/less.js +4 -0
  17. package/dist/cloudflare/core/tokens/formatters/scss.js +252 -0
  18. package/dist/cloudflare/core/tokens/formatters/stubs.js +13 -0
  19. package/dist/cloudflare/core/tokens/formatters/style-dictionary-v3.js +207 -0
  20. package/dist/cloudflare/core/tokens/formatters/tailwind-v3.js +237 -0
  21. package/dist/cloudflare/core/tokens/formatters/tailwind-v4.js +330 -0
  22. package/dist/cloudflare/core/tokens/formatters/tokens-studio.js +250 -0
  23. package/dist/cloudflare/core/tokens/formatters/ts-module.js +198 -0
  24. package/dist/cloudflare/core/tokens/index.js +15 -0
  25. package/dist/cloudflare/core/tokens/parsers/css-vars.js +4 -0
  26. package/dist/cloudflare/core/tokens/parsers/dtcg.js +253 -0
  27. package/dist/cloudflare/core/tokens/parsers/index.js +138 -0
  28. package/dist/cloudflare/core/tokens/parsers/json.js +7 -0
  29. package/dist/cloudflare/core/tokens/parsers/scss.js +4 -0
  30. package/dist/cloudflare/core/tokens/parsers/stubs.js +20 -0
  31. package/dist/cloudflare/core/tokens/parsers/style-dictionary-v3.js +4 -0
  32. package/dist/cloudflare/core/tokens/parsers/tailwind-v3.js +4 -0
  33. package/dist/cloudflare/core/tokens/parsers/tailwind-v4.js +4 -0
  34. package/dist/cloudflare/core/tokens/parsers/tokens-studio.js +4 -0
  35. package/dist/cloudflare/core/tokens/schemas.js +148 -0
  36. package/dist/cloudflare/core/tokens/transforms/color.js +12 -0
  37. package/dist/cloudflare/core/tokens/transforms/index.js +29 -0
  38. package/dist/cloudflare/core/tokens/transforms/size.js +7 -0
  39. package/dist/cloudflare/core/tokens/types.js +18 -0
  40. package/dist/cloudflare/core/tokens-tools.js +859 -0
  41. package/dist/cloudflare/core/websocket-server.js +5 -55
  42. package/dist/cloudflare/index.js +37 -26
  43. package/dist/core/config.d.ts.map +1 -1
  44. package/dist/core/config.js +0 -8
  45. package/dist/core/config.js.map +1 -1
  46. package/dist/core/console-monitor.d.ts +2 -2
  47. package/dist/core/console-monitor.d.ts.map +1 -1
  48. package/dist/core/console-monitor.js +3 -3
  49. package/dist/core/console-monitor.js.map +1 -1
  50. package/dist/core/diagnose-tool.d.ts +33 -0
  51. package/dist/core/diagnose-tool.d.ts.map +1 -0
  52. package/dist/core/diagnose-tool.js +97 -0
  53. package/dist/core/diagnose-tool.js.map +1 -0
  54. package/dist/core/figma-connector.d.ts +1 -1
  55. package/dist/core/figma-connector.d.ts.map +1 -1
  56. package/dist/core/figma-tools.d.ts +1 -2
  57. package/dist/core/figma-tools.d.ts.map +1 -1
  58. package/dist/core/figma-tools.js +69 -229
  59. package/dist/core/figma-tools.js.map +1 -1
  60. package/dist/core/identity.d.ts +41 -0
  61. package/dist/core/identity.d.ts.map +1 -0
  62. package/dist/core/identity.js +97 -0
  63. package/dist/core/identity.js.map +1 -0
  64. package/dist/core/tokens/alias-resolver.d.ts +55 -0
  65. package/dist/core/tokens/alias-resolver.d.ts.map +1 -0
  66. package/dist/core/tokens/alias-resolver.js +136 -0
  67. package/dist/core/tokens/alias-resolver.js.map +1 -0
  68. package/dist/core/tokens/config.d.ts +352 -0
  69. package/dist/core/tokens/config.d.ts.map +1 -0
  70. package/dist/core/tokens/config.js +285 -0
  71. package/dist/core/tokens/config.js.map +1 -0
  72. package/dist/core/tokens/figma-converter.d.ts +81 -0
  73. package/dist/core/tokens/figma-converter.d.ts.map +1 -0
  74. package/dist/core/tokens/figma-converter.js +196 -0
  75. package/dist/core/tokens/figma-converter.js.map +1 -0
  76. package/dist/core/tokens/formatters/css-vars.d.ts +24 -0
  77. package/dist/core/tokens/formatters/css-vars.d.ts.map +1 -0
  78. package/dist/core/tokens/formatters/css-vars.js +330 -0
  79. package/dist/core/tokens/formatters/css-vars.js.map +1 -0
  80. package/dist/core/tokens/formatters/dtcg.d.ts +28 -0
  81. package/dist/core/tokens/formatters/dtcg.d.ts.map +1 -0
  82. package/dist/core/tokens/formatters/dtcg.js +301 -0
  83. package/dist/core/tokens/formatters/dtcg.js.map +1 -0
  84. package/dist/core/tokens/formatters/index.d.ts +30 -0
  85. package/dist/core/tokens/formatters/index.d.ts.map +1 -0
  86. package/dist/core/tokens/formatters/index.js +46 -0
  87. package/dist/core/tokens/formatters/index.js.map +1 -0
  88. package/dist/core/tokens/formatters/json.d.ts +37 -0
  89. package/dist/core/tokens/formatters/json.d.ts.map +1 -0
  90. package/dist/core/tokens/formatters/json.js +188 -0
  91. package/dist/core/tokens/formatters/json.js.map +1 -0
  92. package/dist/core/tokens/formatters/less.d.ts +4 -0
  93. package/dist/core/tokens/formatters/less.d.ts.map +1 -0
  94. package/dist/core/tokens/formatters/less.js +5 -0
  95. package/dist/core/tokens/formatters/less.js.map +1 -0
  96. package/dist/core/tokens/formatters/scss.d.ts +26 -0
  97. package/dist/core/tokens/formatters/scss.d.ts.map +1 -0
  98. package/dist/core/tokens/formatters/scss.js +253 -0
  99. package/dist/core/tokens/formatters/scss.js.map +1 -0
  100. package/dist/core/tokens/formatters/stubs.d.ts +9 -0
  101. package/dist/core/tokens/formatters/stubs.d.ts.map +1 -0
  102. package/dist/core/tokens/formatters/stubs.js +14 -0
  103. package/dist/core/tokens/formatters/stubs.js.map +1 -0
  104. package/dist/core/tokens/formatters/style-dictionary-v3.d.ts +45 -0
  105. package/dist/core/tokens/formatters/style-dictionary-v3.d.ts.map +1 -0
  106. package/dist/core/tokens/formatters/style-dictionary-v3.js +208 -0
  107. package/dist/core/tokens/formatters/style-dictionary-v3.js.map +1 -0
  108. package/dist/core/tokens/formatters/tailwind-v3.d.ts +37 -0
  109. package/dist/core/tokens/formatters/tailwind-v3.d.ts.map +1 -0
  110. package/dist/core/tokens/formatters/tailwind-v3.js +238 -0
  111. package/dist/core/tokens/formatters/tailwind-v3.js.map +1 -0
  112. package/dist/core/tokens/formatters/tailwind-v4.d.ts +41 -0
  113. package/dist/core/tokens/formatters/tailwind-v4.d.ts.map +1 -0
  114. package/dist/core/tokens/formatters/tailwind-v4.js +331 -0
  115. package/dist/core/tokens/formatters/tailwind-v4.js.map +1 -0
  116. package/dist/core/tokens/formatters/tokens-studio.d.ts +44 -0
  117. package/dist/core/tokens/formatters/tokens-studio.d.ts.map +1 -0
  118. package/dist/core/tokens/formatters/tokens-studio.js +251 -0
  119. package/dist/core/tokens/formatters/tokens-studio.js.map +1 -0
  120. package/dist/core/tokens/formatters/ts-module.d.ts +35 -0
  121. package/dist/core/tokens/formatters/ts-module.d.ts.map +1 -0
  122. package/dist/core/tokens/formatters/ts-module.js +199 -0
  123. package/dist/core/tokens/formatters/ts-module.js.map +1 -0
  124. package/dist/core/tokens/index.d.ts +17 -0
  125. package/dist/core/tokens/index.d.ts.map +1 -0
  126. package/dist/core/tokens/index.js +16 -0
  127. package/dist/core/tokens/index.js.map +1 -0
  128. package/dist/core/tokens/parsers/css-vars.d.ts +3 -0
  129. package/dist/core/tokens/parsers/css-vars.d.ts.map +1 -0
  130. package/dist/core/tokens/parsers/css-vars.js +5 -0
  131. package/dist/core/tokens/parsers/css-vars.js.map +1 -0
  132. package/dist/core/tokens/parsers/dtcg.d.ts +21 -0
  133. package/dist/core/tokens/parsers/dtcg.d.ts.map +1 -0
  134. package/dist/core/tokens/parsers/dtcg.js +254 -0
  135. package/dist/core/tokens/parsers/dtcg.js.map +1 -0
  136. package/dist/core/tokens/parsers/index.d.ts +37 -0
  137. package/dist/core/tokens/parsers/index.d.ts.map +1 -0
  138. package/dist/core/tokens/parsers/index.js +139 -0
  139. package/dist/core/tokens/parsers/index.js.map +1 -0
  140. package/dist/core/tokens/parsers/json.d.ts +4 -0
  141. package/dist/core/tokens/parsers/json.d.ts.map +1 -0
  142. package/dist/core/tokens/parsers/json.js +8 -0
  143. package/dist/core/tokens/parsers/json.js.map +1 -0
  144. package/dist/core/tokens/parsers/scss.d.ts +3 -0
  145. package/dist/core/tokens/parsers/scss.d.ts.map +1 -0
  146. package/dist/core/tokens/parsers/scss.js +5 -0
  147. package/dist/core/tokens/parsers/scss.js.map +1 -0
  148. package/dist/core/tokens/parsers/stubs.d.ts +15 -0
  149. package/dist/core/tokens/parsers/stubs.d.ts.map +1 -0
  150. package/dist/core/tokens/parsers/stubs.js +21 -0
  151. package/dist/core/tokens/parsers/stubs.js.map +1 -0
  152. package/dist/core/tokens/parsers/style-dictionary-v3.d.ts +3 -0
  153. package/dist/core/tokens/parsers/style-dictionary-v3.d.ts.map +1 -0
  154. package/dist/core/tokens/parsers/style-dictionary-v3.js +5 -0
  155. package/dist/core/tokens/parsers/style-dictionary-v3.js.map +1 -0
  156. package/dist/core/tokens/parsers/tailwind-v3.d.ts +3 -0
  157. package/dist/core/tokens/parsers/tailwind-v3.d.ts.map +1 -0
  158. package/dist/core/tokens/parsers/tailwind-v3.js +5 -0
  159. package/dist/core/tokens/parsers/tailwind-v3.js.map +1 -0
  160. package/dist/core/tokens/parsers/tailwind-v4.d.ts +3 -0
  161. package/dist/core/tokens/parsers/tailwind-v4.d.ts.map +1 -0
  162. package/dist/core/tokens/parsers/tailwind-v4.js +5 -0
  163. package/dist/core/tokens/parsers/tailwind-v4.js.map +1 -0
  164. package/dist/core/tokens/parsers/tokens-studio.d.ts +3 -0
  165. package/dist/core/tokens/parsers/tokens-studio.d.ts.map +1 -0
  166. package/dist/core/tokens/parsers/tokens-studio.js +5 -0
  167. package/dist/core/tokens/parsers/tokens-studio.js.map +1 -0
  168. package/dist/core/tokens/schemas.d.ts +152 -0
  169. package/dist/core/tokens/schemas.d.ts.map +1 -0
  170. package/dist/core/tokens/schemas.js +149 -0
  171. package/dist/core/tokens/schemas.js.map +1 -0
  172. package/dist/core/tokens/transforms/color.d.ts +9 -0
  173. package/dist/core/tokens/transforms/color.d.ts.map +1 -0
  174. package/dist/core/tokens/transforms/color.js +13 -0
  175. package/dist/core/tokens/transforms/color.js.map +1 -0
  176. package/dist/core/tokens/transforms/index.d.ts +36 -0
  177. package/dist/core/tokens/transforms/index.d.ts.map +1 -0
  178. package/dist/core/tokens/transforms/index.js +30 -0
  179. package/dist/core/tokens/transforms/index.js.map +1 -0
  180. package/dist/core/tokens/transforms/size.d.ts +7 -0
  181. package/dist/core/tokens/transforms/size.d.ts.map +1 -0
  182. package/dist/core/tokens/transforms/size.js +8 -0
  183. package/dist/core/tokens/transforms/size.js.map +1 -0
  184. package/dist/core/tokens/types.d.ts +228 -0
  185. package/dist/core/tokens/types.d.ts.map +1 -0
  186. package/dist/core/tokens/types.js +19 -0
  187. package/dist/core/tokens/types.js.map +1 -0
  188. package/dist/core/tokens-tools.d.ts +42 -0
  189. package/dist/core/tokens-tools.d.ts.map +1 -0
  190. package/dist/core/tokens-tools.js +860 -0
  191. package/dist/core/tokens-tools.js.map +1 -0
  192. package/dist/core/types/index.d.ts +0 -8
  193. package/dist/core/types/index.d.ts.map +1 -1
  194. package/dist/core/websocket-connector.d.ts +1 -1
  195. package/dist/core/websocket-connector.d.ts.map +1 -1
  196. package/dist/core/websocket-server.d.ts +4 -3
  197. package/dist/core/websocket-server.d.ts.map +1 -1
  198. package/dist/core/websocket-server.js +5 -55
  199. package/dist/core/websocket-server.js.map +1 -1
  200. package/dist/local.d.ts +0 -12
  201. package/dist/local.d.ts.map +1 -1
  202. package/dist/local.js +959 -3406
  203. package/dist/local.js.map +1 -1
  204. package/figma-desktop-bridge/code.js +11 -63
  205. package/figma-desktop-bridge/ui.html +72 -11
  206. package/package.json +27 -12
  207. package/figma-desktop-bridge/ui-full.html +0 -1353
@@ -0,0 +1,195 @@
1
+ /**
2
+ * Convert Figma's variables API response into our canonical TokenDocument.
3
+ *
4
+ * Input: the shape produced by `formatVariables()` in src/core/figma-api.ts
5
+ * (an object with `collections` and `variables` arrays, matching either the
6
+ * REST API's response or the Desktop Bridge plugin's `getLocalVariablesAsync`
7
+ * normalized payload).
8
+ *
9
+ * Output: a TokenDocument with one TokenSet per collection, paths derived
10
+ * from Figma variable names (slash-separated → path arrays), values
11
+ * normalized to our TokenValue shape, and Figma IDs preserved in
12
+ * $extensions["figma-console-mcp"] for round-trip non-destructiveness.
13
+ */
14
+ /**
15
+ * Convert a Figma variables payload to our canonical TokenDocument.
16
+ */
17
+ export function convertFigmaVariablesToDocument(payload, opts = {}) {
18
+ const warnings = [];
19
+ // Build a variable index for alias resolution: variableId → variable
20
+ const variableById = new Map();
21
+ for (const v of payload.variables)
22
+ variableById.set(v.id, v);
23
+ // Filter collections per scope.
24
+ const wantedCollections = opts.collectionIds?.length
25
+ ? payload.collections.filter((c) => opts.collectionIds.includes(c.id))
26
+ : payload.collections;
27
+ const sets = wantedCollections.map((collection) => convertCollection(collection, payload.variables, variableById, opts, warnings));
28
+ return {
29
+ document: {
30
+ $schema: "https://figma-console-mcp.southleft.com/schemas/dtcg-extended-v1.json",
31
+ sets,
32
+ meta: {
33
+ figmaFileKey: opts.figmaFileKey,
34
+ exportedAt: opts.exportedAt ?? new Date().toISOString(),
35
+ mcpVersion: opts.mcpVersion,
36
+ },
37
+ },
38
+ warnings,
39
+ };
40
+ }
41
+ function convertCollection(collection, allVariables, variableById, opts, warnings) {
42
+ // Mode filter: keep only modes the caller wants, intersected with what
43
+ // the collection actually has.
44
+ const wantedModes = !opts.modes || opts.modes === "all"
45
+ ? collection.modes
46
+ : collection.modes.filter((m) => opts.modes.includes(m.name));
47
+ // Variables in this collection.
48
+ const collectionVars = allVariables.filter((v) => v.variableCollectionId === collection.id);
49
+ const tokens = collectionVars.map((variable) => convertVariable(variable, wantedModes, variableById, opts, warnings));
50
+ return {
51
+ name: collection.name,
52
+ modes: wantedModes.map((m) => m.name),
53
+ tokens,
54
+ meta: {
55
+ figmaCollectionId: collection.id,
56
+ },
57
+ };
58
+ }
59
+ function convertVariable(variable, wantedModes, variableById, opts, warnings) {
60
+ // Derive the hierarchical path from the Figma variable name. Figma uses
61
+ // slashes to indicate grouping: "color/brand/primary" → ["color", "brand", "primary"].
62
+ let name = variable.name;
63
+ if (opts.stripPrefix && name.startsWith(opts.stripPrefix)) {
64
+ name = name.slice(opts.stripPrefix.length);
65
+ }
66
+ const path = name.split("/").filter(Boolean);
67
+ // Map resolvedType to TokenType.
68
+ const type = mapResolvedType(variable.resolvedType, variable.name, warnings);
69
+ // Convert each (mode → value) pair to our TokenValue shape, filtered by
70
+ // the wanted modes.
71
+ const values = {};
72
+ for (const mode of wantedModes) {
73
+ const rawValue = variable.valuesByMode[mode.modeId];
74
+ if (rawValue === undefined) {
75
+ warnings.push(`Variable "${variable.name}" has no value for mode "${mode.name}" (${mode.modeId}); skipping that mode.`);
76
+ continue;
77
+ }
78
+ values[mode.name] = convertValue(rawValue, variable.resolvedType, variableById, warnings);
79
+ }
80
+ return {
81
+ path,
82
+ type,
83
+ description: variable.description || undefined,
84
+ values,
85
+ extensions: {
86
+ "figma-console-mcp": {
87
+ variableId: variable.id,
88
+ collectionId: variable.variableCollectionId,
89
+ lastSyncedAt: new Date().toISOString(),
90
+ // We snapshot the synced value so future merge calls can detect
91
+ // two-sided conflicts.
92
+ lastSyncedValue: { ...values },
93
+ },
94
+ },
95
+ };
96
+ }
97
+ function mapResolvedType(resolvedType, variableName, warnings) {
98
+ switch (resolvedType) {
99
+ case "COLOR":
100
+ return "color";
101
+ case "FLOAT":
102
+ // Figma FLOAT covers both pure numbers and dimensions. We default to
103
+ // "dimension" because the typical FLOAT variable represents spacing,
104
+ // sizing, or radius — all dimensions. A future enhancement could
105
+ // sniff the variable name (e.g. "opacity/*" → "number") for better
106
+ // type fidelity.
107
+ return inferFloatType(variableName);
108
+ case "STRING":
109
+ return inferStringType(variableName);
110
+ case "BOOLEAN":
111
+ return "boolean";
112
+ default: {
113
+ const _exhaustive = resolvedType;
114
+ warnings.push(`Unknown resolvedType "${_exhaustive}" for variable "${variableName}"; treating as string.`);
115
+ return "string";
116
+ }
117
+ }
118
+ }
119
+ function inferFloatType(variableName) {
120
+ const lower = variableName.toLowerCase();
121
+ if (lower.includes("opacity") || lower.includes("alpha"))
122
+ return "number";
123
+ if (lower.includes("font-weight") || lower.includes("weight"))
124
+ return "fontWeight";
125
+ if (lower.includes("duration") || lower.includes("delay"))
126
+ return "duration";
127
+ // Default: treat numeric variables as dimensions (px values).
128
+ return "dimension";
129
+ }
130
+ function inferStringType(variableName) {
131
+ const lower = variableName.toLowerCase();
132
+ if (lower.includes("font-family") || lower.includes("font/family"))
133
+ return "fontFamily";
134
+ return "string";
135
+ }
136
+ function convertValue(rawValue, resolvedType, variableById, warnings) {
137
+ // Alias references: convert variable ID → path-based reference for DTCG.
138
+ if (isVariableAlias(rawValue)) {
139
+ const target = variableById.get(rawValue.id);
140
+ if (!target) {
141
+ // Cross-library alias — target is in a published library this file
142
+ // consumes, not in the local variable set. Preserve the original
143
+ // Figma variable ID in the reference syntax so round-trip can
144
+ // recover it AND formatters can detect this is unresolvable (vs a
145
+ // genuine local-path alias).
146
+ warnings.push(`Alias to unknown variable ID ${rawValue.id} (likely a cross-library reference). Original ID preserved in reference for round-trip.`);
147
+ return { reference: `{__library:${rawValue.id}}` };
148
+ }
149
+ // The DTCG alias path uses dots: "color.brand.primary".
150
+ const dotPath = target.name.replace(/\//g, ".");
151
+ return { reference: `{${dotPath}}` };
152
+ }
153
+ // Literal values per type.
154
+ if (resolvedType === "COLOR") {
155
+ if (typeof rawValue === "object" && rawValue !== null && "r" in rawValue) {
156
+ return { literal: rgbaToHex(rawValue) };
157
+ }
158
+ warnings.push(`COLOR value isn't an RGB object: ${JSON.stringify(rawValue)}`);
159
+ return { literal: String(rawValue) };
160
+ }
161
+ if (resolvedType === "FLOAT") {
162
+ return { literal: typeof rawValue === "number" ? rawValue : Number(rawValue) };
163
+ }
164
+ if (resolvedType === "BOOLEAN") {
165
+ return { literal: Boolean(rawValue) };
166
+ }
167
+ // STRING and fallthrough.
168
+ return { literal: typeof rawValue === "string" ? rawValue : String(rawValue) };
169
+ }
170
+ function isVariableAlias(value) {
171
+ return (typeof value === "object" &&
172
+ value !== null &&
173
+ "type" in value &&
174
+ value.type === "VARIABLE_ALIAS");
175
+ }
176
+ /**
177
+ * Convert Figma's `{r, g, b, a}` floats (0–1 range) to a hex string. Returns
178
+ * `#RRGGBB` when alpha is 1 (or absent), `#RRGGBBAA` when alpha < 1.
179
+ */
180
+ function rgbaToHex(rgba) {
181
+ const r = clampByte(rgba.r);
182
+ const g = clampByte(rgba.g);
183
+ const b = clampByte(rgba.b);
184
+ const a = rgba.a ?? 1;
185
+ const hex = `#${byteToHex(r)}${byteToHex(g)}${byteToHex(b)}`;
186
+ if (a >= 1)
187
+ return hex;
188
+ return `${hex}${byteToHex(clampByte(a))}`;
189
+ }
190
+ function clampByte(f) {
191
+ return Math.max(0, Math.min(255, Math.round(f * 255)));
192
+ }
193
+ function byteToHex(byte) {
194
+ return byte.toString(16).padStart(2, "0").toUpperCase();
195
+ }
@@ -0,0 +1,329 @@
1
+ /**
2
+ * CSS custom properties formatter.
3
+ *
4
+ * Converts a TokenDocument to one or more CSS files containing `:root { ... }`
5
+ * (and optionally per-mode selectors like `.dark { ... }`). The first
6
+ * "real" non-DTCG output, completing the canonical-to-runtime pipeline.
7
+ *
8
+ * Behavior:
9
+ * - Each token becomes a CSS custom property. Path joined with `-`,
10
+ * optionally prefixed. `color/primary` → `--color-primary`.
11
+ * - Aliases resolve to `var(--target-token)` so CSS cascading still works.
12
+ * - Composite tokens (typography, shadow) expand into multiple primitive
13
+ * custom properties since CSS doesn't natively express composites.
14
+ * - Single-mode tokens go in `:root`.
15
+ * - Multi-mode tokens emit per-mode selectors. Heuristic: a mode named
16
+ * `Light`/`Default` becomes `:root`; `Dark` becomes `.dark` (Tailwind
17
+ * convention); other modes become `[data-theme="<name>"]`.
18
+ * - splitByMode emits one file per mode with just that mode's values.
19
+ * - splitByCollection emits one file per set.
20
+ */
21
+ export function formatCssVars(doc, opts) {
22
+ const warnings = [];
23
+ const files = [];
24
+ const splitByMode = opts.target.splitByMode ?? false;
25
+ const splitByCollection = opts.target.splitByCollection ?? false;
26
+ const prefix = opts.target.prefix ?? "";
27
+ if (splitByMode && splitByCollection) {
28
+ // One file per (set, mode) pair.
29
+ for (const set of doc.sets) {
30
+ for (const mode of set.modes) {
31
+ files.push({
32
+ path: filenameFor(opts, set, mode),
33
+ content: renderSingleSelector(doc.sets.filter((s) => s.name === set.name), mode, selectorFor(mode), prefix, warnings),
34
+ });
35
+ }
36
+ }
37
+ }
38
+ else if (splitByMode) {
39
+ // One file per mode, all sets combined under that mode's selector.
40
+ const allModes = new Set();
41
+ for (const set of doc.sets)
42
+ for (const m of set.modes)
43
+ allModes.add(m);
44
+ for (const mode of allModes) {
45
+ const setsWithMode = doc.sets.filter((s) => s.modes.includes(mode));
46
+ files.push({
47
+ path: filenameFor(opts, undefined, mode),
48
+ content: renderSingleSelector(setsWithMode, mode, selectorFor(mode), prefix, warnings),
49
+ });
50
+ }
51
+ }
52
+ else if (splitByCollection) {
53
+ // One file per set, all modes combined as separate selectors within.
54
+ for (const set of doc.sets) {
55
+ files.push({
56
+ path: filenameFor(opts, set),
57
+ content: renderMultiSelector([set], prefix, warnings),
58
+ });
59
+ }
60
+ }
61
+ else {
62
+ // Single file with everything.
63
+ files.push({
64
+ path: filenameFor(opts),
65
+ content: renderMultiSelector(doc.sets, prefix, warnings),
66
+ });
67
+ }
68
+ return { files, warnings };
69
+ }
70
+ function filenameFor(opts, set, mode) {
71
+ if (opts.target.filename)
72
+ return opts.target.filename;
73
+ const parts = [];
74
+ if (set)
75
+ parts.push(slugify(set.name));
76
+ if (mode)
77
+ parts.push(slugify(mode));
78
+ if (parts.length === 0)
79
+ parts.push("tokens");
80
+ return `${parts.join(".")}.css`;
81
+ }
82
+ function slugify(s) {
83
+ return s
84
+ .trim()
85
+ .toLowerCase()
86
+ .replace(/[^a-z0-9]+/g, "-")
87
+ .replace(/^-+|-+$/g, "");
88
+ }
89
+ /**
90
+ * Map a mode name to a CSS selector. Conventional defaults that match what
91
+ * most CSS frameworks expect:
92
+ * - Default / Light / Value → `:root` (the document root, runs always)
93
+ * - Dark → `.dark` (matches Tailwind's darkMode: "class" config)
94
+ * - anything else → `[data-theme="<name>"]` (general escape hatch)
95
+ */
96
+ function selectorFor(mode) {
97
+ const lower = mode.toLowerCase();
98
+ if (lower === "default" || lower === "light" || lower === "value") {
99
+ return ":root";
100
+ }
101
+ if (lower === "dark") {
102
+ return ".dark";
103
+ }
104
+ return `[data-theme="${slugify(mode)}"]`;
105
+ }
106
+ /**
107
+ * Render all tokens across the given sets under a single selector. Used when
108
+ * one file holds one mode's worth of vars (splitByMode output).
109
+ */
110
+ function renderSingleSelector(sets, mode, selector, prefix, warnings) {
111
+ const lines = [];
112
+ lines.push(`/* Generated by figma-console-mcp — do not edit by hand */`);
113
+ lines.push(`${selector} {`);
114
+ for (const set of sets) {
115
+ for (const token of set.tokens) {
116
+ const value = token.values[mode];
117
+ if (!value)
118
+ continue;
119
+ emitTokenLines(token, value, prefix, lines, warnings);
120
+ }
121
+ }
122
+ lines.push(`}`);
123
+ lines.push("");
124
+ return lines.join("\n");
125
+ }
126
+ /**
127
+ * Render multiple sets across multiple modes in one file. Each mode gets its
128
+ * own selector block.
129
+ */
130
+ function renderMultiSelector(sets, prefix, warnings) {
131
+ const lines = [];
132
+ lines.push(`/* Generated by figma-console-mcp — do not edit by hand */`);
133
+ // Collect all (mode, selector) pairs across all sets.
134
+ const modeToSelector = new Map();
135
+ for (const set of sets) {
136
+ for (const mode of set.modes) {
137
+ if (!modeToSelector.has(mode)) {
138
+ modeToSelector.set(mode, selectorFor(mode));
139
+ }
140
+ }
141
+ }
142
+ for (const [mode, selector] of modeToSelector) {
143
+ lines.push(`${selector} {`);
144
+ for (const set of sets) {
145
+ if (!set.modes.includes(mode))
146
+ continue;
147
+ for (const token of set.tokens) {
148
+ const value = token.values[mode];
149
+ if (!value)
150
+ continue;
151
+ emitTokenLines(token, value, prefix, lines, warnings);
152
+ }
153
+ }
154
+ lines.push(`}`);
155
+ lines.push("");
156
+ }
157
+ return lines.join("\n");
158
+ }
159
+ /**
160
+ * Emit one or more CSS custom property declarations for a single token.
161
+ * Primitives emit one line; composite tokens (typography, shadow) expand
162
+ * into multiple lines.
163
+ */
164
+ function emitTokenLines(token, value, prefix, out, warnings) {
165
+ // Every path segment must be a valid CSS identifier — slugify each to
166
+ // normalize spaces, dots, and other special characters that show up in
167
+ // real Figma variable names (e.g. "tailwind colors/purple/50").
168
+ const cssName = `--${prefix}${pathToCssName(token.path)}`;
169
+ if (value.reference) {
170
+ // Detect the cross-library alias sentinel ({__library:VariableID:...}).
171
+ // Emitting var() for these produces broken CSS — the target doesn't
172
+ // live in this file's variable set. Emit a clear comment that names
173
+ // the original library variable ID instead, so the user can decide
174
+ // how to handle it (manual literal, library import, etc.).
175
+ const bareRef = value.reference.replace(/^\{|\}$/g, "");
176
+ const libMatch = bareRef.match(/^__library:(.+)$/);
177
+ if (libMatch || bareRef === "unknown") {
178
+ const originalId = libMatch ? libMatch[1] : "unknown";
179
+ warnings.push(`Skipped ${token.path.join(".")} in CSS — references cross-library variable ${originalId}.`);
180
+ out.push(` /* ${cssName}: skipped — cross-library alias to ${originalId} */`);
181
+ return;
182
+ }
183
+ // Alias → var(--other) so CSS cascading semantics are preserved. The
184
+ // target path goes through the same slugify treatment as the source.
185
+ const refPath = bareRef.split(".");
186
+ const targetCssName = pathToCssName(refPath);
187
+ out.push(` ${cssName}: var(--${prefix}${targetCssName});`);
188
+ return;
189
+ }
190
+ if (value.literal === undefined || value.literal === null) {
191
+ warnings.push(`Token ${token.path.join(".")} has no value — emitting nothing.`);
192
+ return;
193
+ }
194
+ // Composite tokens — expand into multiple primitive vars.
195
+ if (token.type === "typography" && typeof value.literal === "object") {
196
+ const t = value.literal;
197
+ for (const subField of ["fontFamily", "fontSize", "fontWeight", "lineHeight", "letterSpacing"]) {
198
+ if (t[subField] !== undefined) {
199
+ out.push(` ${cssName}-${kebab(subField)}: ${formatCssValue(t[subField], token.type)};`);
200
+ }
201
+ }
202
+ return;
203
+ }
204
+ if (token.type === "shadow" && typeof value.literal === "object") {
205
+ const shadowCss = renderShadow(value.literal);
206
+ if (shadowCss) {
207
+ out.push(` ${cssName}: ${shadowCss};`);
208
+ }
209
+ else {
210
+ warnings.push(`Token ${token.path.join(".")} has an unparseable shadow value — skipping.`);
211
+ }
212
+ return;
213
+ }
214
+ // Primitives.
215
+ out.push(` ${cssName}: ${formatCssValue(value.literal, token.type)};`);
216
+ }
217
+ function kebab(s) {
218
+ return s.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
219
+ }
220
+ /**
221
+ * Convert a path array to a CSS-safe custom property name. Each segment is
222
+ * slugified (lowercase, non-alphanumerics → hyphens) before joining.
223
+ *
224
+ * Examples:
225
+ * ["color", "primary"] → "color-primary"
226
+ * ["tailwind colors", "purple", "50"] → "tailwind-colors-purple-50"
227
+ * ["UI", "background-default"] → "ui-background-default"
228
+ */
229
+ function pathToCssName(path) {
230
+ return path.map((seg) => slugify(seg)).join("-");
231
+ }
232
+ /**
233
+ * Render a primitive value as a CSS literal.
234
+ * - Numbers gain a "px" suffix when the token type is `dimension`.
235
+ * - Color-typed strings pass through (hex, oklch, rgba, hsl).
236
+ * - fontFamily-typed strings get quoted (multi-word family names are
237
+ * invalid CSS otherwise — e.g. "Geist Mono" must be `"Geist Mono"`).
238
+ * - Plain string tokens get quoted too, since CSS treats unquoted
239
+ * identifiers as keywords. Strings that look like a CSS color or unit
240
+ * stay unquoted.
241
+ * - Booleans render as their string form.
242
+ */
243
+ function formatCssValue(value, type) {
244
+ if (typeof value === "number") {
245
+ if (type === "dimension")
246
+ return `${value}px`;
247
+ return String(value);
248
+ }
249
+ if (typeof value === "string") {
250
+ // Color-typed values are always pre-formatted CSS color literals.
251
+ if (type === "color")
252
+ return value;
253
+ // fontFamily and plain strings need quoting unless they look like a
254
+ // CSS-safe identifier with no special characters. Quote conservatively
255
+ // — over-quoting is fine, under-quoting breaks the cascade.
256
+ if (type === "fontFamily" || type === "string") {
257
+ return needsQuoting(value) ? JSON.stringify(value) : value;
258
+ }
259
+ // Dimensions and other types: pass through (already formatted upstream).
260
+ return value;
261
+ }
262
+ if (typeof value === "boolean")
263
+ return String(value);
264
+ return JSON.stringify(value);
265
+ }
266
+ /**
267
+ * Returns true if a string value needs to be wrapped in CSS quotes. Multi-word
268
+ * values, values with special characters, or values that aren't pure
269
+ * alphanumeric identifiers all need quoting.
270
+ */
271
+ function needsQuoting(s) {
272
+ // Anything that already has quotes is fine.
273
+ if (/^["']/.test(s))
274
+ return false;
275
+ // Pure-numeric or unit-bearing values don't need quotes (they're not
276
+ // identifiers).
277
+ if (/^[\d.]+([a-z%]+)?$/.test(s))
278
+ return false;
279
+ // CSS keyword identifiers (single word, alphanumerics + hyphen only).
280
+ if (/^[a-zA-Z_][a-zA-Z0-9_-]*$/.test(s)) {
281
+ // Reserved CSS keywords that shouldn't be quoted (the user may have
282
+ // legitimately written e.g. "inherit", "currentColor", "transparent").
283
+ const cssKeywords = new Set([
284
+ "inherit",
285
+ "initial",
286
+ "unset",
287
+ "revert",
288
+ "currentColor",
289
+ "transparent",
290
+ "none",
291
+ "auto",
292
+ ]);
293
+ if (cssKeywords.has(s))
294
+ return false;
295
+ // Any other single identifier — for font-family this is OK unquoted
296
+ // ("Inter" works) but it's safer to quote for clarity. Keep unquoted
297
+ // to match the more common convention.
298
+ return false;
299
+ }
300
+ // Multi-word, special chars, etc. → quote.
301
+ return true;
302
+ }
303
+ function renderShadow(shadow) {
304
+ if (Array.isArray(shadow)) {
305
+ return shadow
306
+ .map((s) => renderShadow(s))
307
+ .filter(Boolean)
308
+ .join(", ");
309
+ }
310
+ if (!shadow || typeof shadow !== "object")
311
+ return null;
312
+ const s = shadow;
313
+ const inset = s.inset ? "inset " : "";
314
+ const x = withPx(s.offsetX);
315
+ const y = withPx(s.offsetY);
316
+ const blur = withPx(s.blur);
317
+ const spread = s.spread !== undefined ? ` ${withPx(s.spread)}` : "";
318
+ const color = s.color;
319
+ if (!x || !y || !blur || typeof color !== "string")
320
+ return null;
321
+ return `${inset}${x} ${y} ${blur}${spread} ${color}`;
322
+ }
323
+ function withPx(v) {
324
+ if (typeof v === "number")
325
+ return `${v}px`;
326
+ if (typeof v === "string")
327
+ return v;
328
+ return "";
329
+ }