Skip to content

Commit bad2a63

Browse files
committed
chore: 🤖 setup formatter (prettier)
1 parent 21d7203 commit bad2a63

11 files changed

Lines changed: 757 additions & 837 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"arrowParens": "avoid",
3+
"bracketSameLine": false,
4+
"bracketSpacing": true,
5+
"endOfLine": "lf",
6+
"jsxSingleQuote": false,
7+
"printWidth": 90,
8+
"semi": true,
9+
"singleAttributePerLine": true,
10+
"singleQuote": true,
11+
"tabWidth": 2,
12+
"trailingComma": "es5",
13+
"useTabs": false
14+
}

packages/figma-design-tokens-plugin/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99
"build": "rm -rf ./dist && vite build",
1010
"lint": "tsc --noEmit",
1111
"lint:fix": "echo 'No auto-fix available for type errors'",
12-
"format": "echo 'Prettier not configured in this package'",
13-
"format:fix": "echo 'Prettier not configured in this package'",
12+
"format": "prettier --check 'src/**/*.{ts,tsx,js,jsx}'",
13+
"format:fix": "prettier --write 'src/**/*.{ts,tsx,js,jsx}'",
1414
"typecheck": "tsc --noEmit",
1515
"test": "vitest run",
1616
"test:watch": "vitest"
1717
},
1818
"devDependencies": {
1919
"@figma/plugin-typings": "^1.106.0",
2020
"@types/node": "^25.5.0",
21+
"prettier": "^3.0.0",
2122
"typescript": "^5.7.0",
2223
"vite": "^6.0.0",
2324
"vite-plugin-singlefile": "^2.0.3",

packages/figma-design-tokens-plugin/src/index.ts

Lines changed: 63 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import { rgbToHex } from "./utils/colors";
1+
import { rgbToHex } from './utils/colors';
22
import {
33
createCollection,
44
getExistingVariables,
55
processAliases,
66
traverseToken,
7-
} from "./utils/tokens";
7+
} from './utils/tokens';
88
import type {
99
AliasEntry,
1010
DTCGToken,
1111
DTCGTokenType,
1212
ExportedFile,
1313
PluginMessage,
14-
} from "./utils/types";
14+
} from './utils/types';
1515

1616
async function importJSONFile({
1717
fileName,
@@ -20,103 +20,94 @@ async function importJSONFile({
2020
fileName: string;
2121
body: string;
2222
}): Promise<{ wasUpdate: boolean; collectionName: string; tokenCount: number }> {
23-
console.log("Importing file:", fileName);
24-
23+
console.log('Importing file:', fileName);
2524

2625
let wasUpdate = false;
2726

28-
2927
const existingCollections = await figma.variables.getLocalVariableCollectionsAsync();
30-
const existingCollection = existingCollections.find((c) => c.name === fileName);
28+
const existingCollection = existingCollections.find(c => c.name === fileName);
3129
wasUpdate = !!existingCollection;
3230

31+
const isPrimitivesFile = fileName.toLowerCase().includes('primitives');
3332

34-
const isPrimitivesFile = fileName.toLowerCase().includes("primitives");
33+
const isSemanticFile = fileName.toLowerCase().includes('semantic');
3534

36-
const isSemanticFile = fileName.toLowerCase().includes("semantic");
37-
38-
console.log("DEBUG - File name:", fileName);
39-
console.log("DEBUG - isPrimitivesFile detected:", isPrimitivesFile);
40-
console.log("DEBUG - isSemanticFile detected:", isSemanticFile);
35+
console.log('DEBUG - File name:', fileName);
36+
console.log('DEBUG - isPrimitivesFile detected:', isPrimitivesFile);
37+
console.log('DEBUG - isSemanticFile detected:', isSemanticFile);
4138

4239
if (isPrimitivesFile) {
43-
console.log(
44-
"Detected primitives file - tokens will have NO scope (hidden from UI)",
45-
);
40+
console.log('Detected primitives file - tokens will have NO scope (hidden from UI)');
4641
}
4742
if (isSemanticFile) {
48-
console.log(
49-
"Detected semantic file - will create Light/Dark modes",
50-
);
43+
console.log('Detected semantic file - will create Light/Dark modes');
5144
}
5245

5346
const json = JSON.parse(body) as DTCGToken;
54-
console.log("JSON structure keys:", Object.keys(json));
55-
console.log("DEBUG - JSON top-level non-$ keys:", Object.keys(json).filter(k => !k.startsWith('$')));
56-
47+
console.log('JSON structure keys:', Object.keys(json));
48+
console.log(
49+
'DEBUG - JSON top-level non-$ keys:',
50+
Object.keys(json).filter(k => !k.startsWith('$'))
51+
);
5752

5853
const { collection, modeId, modeIds } = await createCollection(
5954
fileName,
60-
isSemanticFile,
55+
isSemanticFile
6156
);
62-
console.log("DEBUG - Collection created, modeId:", modeId, "modeIds:", modeIds);
57+
console.log('DEBUG - Collection created, modeId:', modeId, 'modeIds:', modeIds);
6358
const aliases: Record<string, AliasEntry> = {};
6459
const tokens: Record<string, Variable> = {};
6560

6661
const existingVariables = await getExistingVariables();
6762
console.log(
68-
"Existing variables from other collections:",
69-
Object.keys(existingVariables).length,
63+
'Existing variables from other collections:',
64+
Object.keys(existingVariables).length
7065
);
7166
console.log(
72-
"DEBUG - Sample existing variables:",
73-
Object.keys(existingVariables).slice(0, 10),
67+
'DEBUG - Sample existing variables:',
68+
Object.keys(existingVariables).slice(0, 10)
7469
);
7570
console.log(
7671
"DEBUG - Looking for 'color/white' in existing:",
77-
existingVariables["color/white"] ? "FOUND" : "NOT FOUND",
72+
existingVariables['color/white'] ? 'FOUND' : 'NOT FOUND'
7873
);
7974
console.log(
8075
"DEBUG - Looking for 'white' in existing:",
81-
existingVariables["white"] ? "FOUND" : "NOT FOUND",
76+
existingVariables['white'] ? 'FOUND' : 'NOT FOUND'
8277
);
8378

84-
8579
const allKeys = Object.keys(existingVariables);
8680
const conflicts: string[] = [];
8781

88-
89-
const colorConflicts = allKeys.filter((k) => k.startsWith("color/"));
82+
const colorConflicts = allKeys.filter(k => k.startsWith('color/'));
9083
if (colorConflicts.length > 0) {
9184
console.log(
92-
"DEBUG - Found existing color/* tokens:",
85+
'DEBUG - Found existing color/* tokens:',
9386
colorConflicts.slice(0, 15),
94-
"... and",
87+
'... and',
9588
colorConflicts.length - 15,
96-
"more",
89+
'more'
9790
);
9891
conflicts.push(...colorConflicts);
9992
}
10093

101-
102-
const chartConflicts = allKeys.filter((k) => k.startsWith("chart/"));
94+
const chartConflicts = allKeys.filter(k => k.startsWith('chart/'));
10395
if (chartConflicts.length > 0) {
104-
console.log("DEBUG - Found existing chart/* tokens:", chartConflicts);
96+
console.log('DEBUG - Found existing chart/* tokens:', chartConflicts);
10597
conflicts.push(...chartConflicts);
10698
}
10799

108-
109-
const checkboxConflicts = allKeys.filter((k) => k.startsWith("checkbox/"));
100+
const checkboxConflicts = allKeys.filter(k => k.startsWith('checkbox/'));
110101
if (checkboxConflicts.length > 0) {
111-
console.log("DEBUG - Found existing checkbox/* tokens:", checkboxConflicts);
102+
console.log('DEBUG - Found existing checkbox/* tokens:', checkboxConflicts);
112103
conflicts.push(...checkboxConflicts);
113104
}
114105

115106
if (conflicts.length > 0) {
116107
console.log(
117-
"DEBUG - TOTAL CONFLICTS FOUND:",
108+
'DEBUG - TOTAL CONFLICTS FOUND:',
118109
conflicts.length,
119-
"tokens will fail to create",
110+
'tokens will fail to create'
120111
);
121112
}
122113

@@ -125,16 +116,16 @@ async function importJSONFile({
125116
modeId,
126117
modeIds,
127118
type: json.$type as DTCGTokenType | undefined,
128-
key: "",
119+
key: '',
129120
object: json,
130121
tokens,
131122
aliases,
132123
existingVariables,
133124
isPrimitivesFile,
134125
});
135126

136-
console.log("Created tokens:", Object.keys(tokens).length);
137-
console.log("Pending aliases:", Object.keys(aliases).length);
127+
console.log('Created tokens:', Object.keys(tokens).length);
128+
console.log('Pending aliases:', Object.keys(aliases).length);
138129

139130
await processAliases({
140131
collection,
@@ -146,8 +137,7 @@ async function importJSONFile({
146137
isPrimitivesFile,
147138
});
148139

149-
console.log("Import complete!");
150-
140+
console.log('Import complete!');
151141

152142
return {
153143
wasUpdate,
@@ -165,7 +155,7 @@ async function exportToJSON(): Promise<void> {
165155
files.push(...collectionFiles);
166156
}
167157

168-
figma.ui.postMessage({ type: "EXPORT_RESULT", files });
158+
figma.ui.postMessage({ type: 'EXPORT_RESULT', files });
169159
}
170160

171161
async function processCollection({
@@ -189,28 +179,26 @@ async function processCollection({
189179
const { name: varName, resolvedType, valuesByMode } = variable;
190180
const value = valuesByMode[mode.modeId];
191181

192-
if (value !== undefined && ["COLOR", "FLOAT"].includes(resolvedType)) {
182+
if (value !== undefined && ['COLOR', 'FLOAT'].includes(resolvedType)) {
193183
let obj: Record<string, unknown> = file.body;
194184

195-
varName.split("/").forEach((groupName) => {
185+
varName.split('/').forEach(groupName => {
196186
obj[groupName] = obj[groupName] || {};
197187
obj = obj[groupName] as Record<string, unknown>;
198188
});
199189

200-
obj.$type = resolvedType === "COLOR" ? "color" : "number";
190+
obj.$type = resolvedType === 'COLOR' ? 'color' : 'number';
201191

202192
if (
203-
typeof value === "object" &&
204-
"type" in value &&
205-
value.type === "VARIABLE_ALIAS"
193+
typeof value === 'object' &&
194+
'type' in value &&
195+
value.type === 'VARIABLE_ALIAS'
206196
) {
207-
const aliasedVar = await figma.variables.getVariableByIdAsync(
208-
value.id,
209-
);
197+
const aliasedVar = await figma.variables.getVariableByIdAsync(value.id);
210198
if (aliasedVar) {
211-
obj.$value = `{${aliasedVar.name.replace(/\//g, ".")}}`;
199+
obj.$value = `{${aliasedVar.name.replace(/\//g, '.')}}`;
212200
}
213-
} else if (resolvedType === "COLOR" && typeof value === "object") {
201+
} else if (resolvedType === 'COLOR' && typeof value === 'object') {
214202
obj.$value = rgbToHex(value as RGBA);
215203
} else {
216204
obj.$value = value;
@@ -225,51 +213,49 @@ async function processCollection({
225213
}
226214

227215
figma.ui.onmessage = async (e: PluginMessage) => {
228-
console.log("code received message", e);
216+
console.log('code received message', e);
229217

230-
if (e.type === "IMPORT") {
218+
if (e.type === 'IMPORT') {
231219
try {
232220
const result = await importJSONFile({ fileName: e.fileName, body: e.body });
233221

234222
figma.ui.postMessage({
235-
type: "IMPORT_COMPLETE",
223+
type: 'IMPORT_COMPLETE',
236224
wasUpdate: result.wasUpdate,
237225
collectionName: result.collectionName,
238226
tokenCount: result.tokenCount,
239227
});
240228
} catch (error) {
241229
const errorMessage = error instanceof Error ? error.message : String(error);
242-
console.error("Import failed:", error);
230+
console.error('Import failed:', error);
243231
figma.ui.postMessage({
244-
type: "IMPORT_ERROR",
232+
type: 'IMPORT_ERROR',
245233
error: errorMessage,
246234
});
247235
}
248-
} else if (e.type === "EXPORT") {
236+
} else if (e.type === 'EXPORT') {
249237
await exportToJSON();
250-
} else if (e.type === "GET_COLLECTIONS") {
251-
252-
const collections =
253-
await figma.variables.getLocalVariableCollectionsAsync();
254-
const collectionsInfo = collections.map((c) => ({
238+
} else if (e.type === 'GET_COLLECTIONS') {
239+
const collections = await figma.variables.getLocalVariableCollectionsAsync();
240+
const collectionsInfo = collections.map(c => ({
255241
name: c.name,
256242
variableCount: c.variableIds.length,
257243
}));
258244
figma.ui.postMessage({
259-
type: "COLLECTIONS_LIST",
245+
type: 'COLLECTIONS_LIST',
260246
collections: collectionsInfo,
261247
});
262248
}
263249
};
264250

265-
if (figma.command === "import") {
266-
figma.showUI(__uiFiles__["import"] as string, {
251+
if (figma.command === 'import') {
252+
figma.showUI(__uiFiles__['import'] as string, {
267253
width: 500,
268254
height: 500,
269255
themeColors: true,
270256
});
271-
} else if (figma.command === "export") {
272-
figma.showUI(__uiFiles__["export"] as string, {
257+
} else if (figma.command === 'export') {
258+
figma.showUI(__uiFiles__['export'] as string, {
273259
width: 500,
274260
height: 500,
275261
themeColors: true,

packages/figma-design-tokens-plugin/src/ui/export/main.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,23 @@ interface ExportedFile {
44
}
55

66
interface ExportResultMessage {
7-
type: "EXPORT_RESULT";
7+
type: 'EXPORT_RESULT';
88
files: ExportedFile[];
99
}
1010

11-
window.onmessage = ({
12-
data,
13-
}: MessageEvent<{ pluginMessage: ExportResultMessage }>) => {
11+
window.onmessage = ({ data }: MessageEvent<{ pluginMessage: ExportResultMessage }>) => {
1412
const { pluginMessage } = data;
1513

16-
if (pluginMessage.type === "EXPORT_RESULT") {
17-
const textarea = document.querySelector("textarea") as HTMLTextAreaElement;
14+
if (pluginMessage.type === 'EXPORT_RESULT') {
15+
const textarea = document.querySelector('textarea') as HTMLTextAreaElement;
1816
textarea.value = pluginMessage.files
1917
.map(
20-
({ fileName, body }) =>
21-
`/* ${fileName} */\n\n${JSON.stringify(body, null, 2)}`
18+
({ fileName, body }) => `/* ${fileName} */\n\n${JSON.stringify(body, null, 2)}`
2219
)
23-
.join("\n\n\n");
20+
.join('\n\n\n');
2421
}
2522
};
2623

27-
document.getElementById("export")!.addEventListener("click", () => {
28-
parent.postMessage({ pluginMessage: { type: "EXPORT" } }, "*");
24+
document.getElementById('export')!.addEventListener('click', () => {
25+
parent.postMessage({ pluginMessage: { type: 'EXPORT' } }, '*');
2926
});

0 commit comments

Comments
 (0)