figdriver 0.0.0-stage → 0.4.0
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.
- package/CHANGELOG.md +86 -0
- package/LICENSE +21 -0
- package/README.md +87 -2
- package/cli/result.ts +231 -0
- package/dist/cli.js +4125 -0
- package/index.d.ts +12 -0
- package/index.js +8 -0
- package/package.json +78 -4
- package/src/core/color.ts +435 -0
- package/src/core/css.ts +668 -0
- package/src/core/foundations.ts +389 -0
- package/src/core/json.d.ts +4 -0
- package/src/core/layout.ts +684 -0
- package/src/core/naming.ts +129 -0
- package/src/core/protocol.ts +328 -0
- package/src/core/rerun.ts +130 -0
- package/src/core/reuse.ts +149 -0
- package/src/core/snapshot.ts +88 -0
- package/src/core/spec.ts +167 -0
- package/src/core/text.ts +37 -0
- package/src/core/tokens.ts +77 -0
- package/src/core/types.ts +181 -0
- package/src/core/users.ts +17 -0
- package/src/figma/helpers.ts +238 -0
- package/src/figma/import.ts +962 -0
- package/src/figma/inspect.ts +254 -0
- package/src/figma/kit.ts +562 -0
- package/src/figma/migrate.ts +301 -0
- package/src/figma/owned.ts +56 -0
- package/src/figma/read.ts +618 -0
- package/src/figma/rerun.ts +387 -0
- package/src/figma/review.ts +481 -0
- package/src/figma/tokens.ts +97 -0
- package/src/page/capture.ts +999 -0
|
@@ -0,0 +1,618 @@
|
|
|
1
|
+
import { hexOf } from "../core/css";
|
|
2
|
+
import type {
|
|
3
|
+
Collection,
|
|
4
|
+
EffectSummary,
|
|
5
|
+
FindMatch,
|
|
6
|
+
FindQuery,
|
|
7
|
+
FindResults,
|
|
8
|
+
InspectInclude,
|
|
9
|
+
InspectOptions,
|
|
10
|
+
Inventory,
|
|
11
|
+
Layer,
|
|
12
|
+
PaintSummary,
|
|
13
|
+
Value,
|
|
14
|
+
} from "../core/protocol";
|
|
15
|
+
import { round } from "../core/snapshot";
|
|
16
|
+
import { componentFullName } from "./owned";
|
|
17
|
+
|
|
18
|
+
export type { Collection, Value };
|
|
19
|
+
|
|
20
|
+
const some = <T>(list: T[]) => (list.length ? list : undefined);
|
|
21
|
+
|
|
22
|
+
const colorOf = (c: RGB | RGBA, opacity = 1) => {
|
|
23
|
+
const a = ("a" in c ? c.a : 1) * opacity;
|
|
24
|
+
|
|
25
|
+
return a < 1
|
|
26
|
+
? hexOf(c) +
|
|
27
|
+
Math.round(a * 255)
|
|
28
|
+
.toString(16)
|
|
29
|
+
.padStart(2, "0")
|
|
30
|
+
: hexOf(c);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
function names() {
|
|
34
|
+
const cached = <T>(load: (id: string) => Promise<T>) => {
|
|
35
|
+
const cache = new Map<string, Promise<T>>();
|
|
36
|
+
|
|
37
|
+
return (id: string) => cache.get(id) ?? cache.set(id, load(id)).get(id)!;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
variable: cached((id) => figma.variables.getVariableByIdAsync(id).then((x) => x?.name)),
|
|
42
|
+
style: cached((id) => figma.getStyleByIdAsync(id).then((x) => x?.name)),
|
|
43
|
+
collection: cached((id) => figma.variables.getVariableCollectionByIdAsync(id)),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
type Names = ReturnType<typeof names>;
|
|
48
|
+
|
|
49
|
+
async function paintOf(p: Paint, name: Names): Promise<PaintSummary> {
|
|
50
|
+
const visible = p.visible === false ? false : undefined;
|
|
51
|
+
|
|
52
|
+
if (p.type === "SOLID") {
|
|
53
|
+
return {
|
|
54
|
+
color: colorOf(p.color, p.opacity),
|
|
55
|
+
variable: p.boundVariables?.color && (await name.variable(p.boundVariables.color.id)),
|
|
56
|
+
visible,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
type: p.type,
|
|
62
|
+
visible,
|
|
63
|
+
opacity: p.opacity === 1 ? undefined : p.opacity,
|
|
64
|
+
stops:
|
|
65
|
+
"gradientStops" in p ? p.gradientStops.map((s) => `${colorOf(s.color)} ${round(s.position * 100)}%`) : undefined,
|
|
66
|
+
image: p.type === "IMAGE" ? p.imageHash : undefined,
|
|
67
|
+
video: p.type === "VIDEO" ? p.videoHash : undefined,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const paintsOf = (ps: readonly Paint[] | PluginAPI["mixed"], name: Names) =>
|
|
72
|
+
ps === figma.mixed
|
|
73
|
+
? Promise.resolve("MIXED")
|
|
74
|
+
: ps.length
|
|
75
|
+
? Promise.all(ps.map((p) => paintOf(p, name)))
|
|
76
|
+
: Promise.resolve(undefined);
|
|
77
|
+
|
|
78
|
+
const effectOf = (e: Effect): EffectSummary => ({
|
|
79
|
+
type: e.type,
|
|
80
|
+
visible: e.visible ? undefined : false,
|
|
81
|
+
color: "color" in e ? colorOf(e.color) : undefined,
|
|
82
|
+
offset: "offset" in e ? [e.offset.x, e.offset.y] : undefined,
|
|
83
|
+
radius: "radius" in e ? e.radius : undefined,
|
|
84
|
+
spread: "spread" in e && e.spread ? e.spread : undefined,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const unitOf = (v: LineHeight | LetterSpacing) =>
|
|
88
|
+
"value" in v ? `${round(v.value)}${v.unit === "PIXELS" ? "px" : "%"}` : undefined;
|
|
89
|
+
|
|
90
|
+
async function boundOf(bound: SceneNodeMixin["boundVariables"] | TextStyle["boundVariables"], name: Names) {
|
|
91
|
+
const out: Record<string, string | undefined> = {};
|
|
92
|
+
|
|
93
|
+
for (const [field, alias] of Object.entries(bound ?? {})) {
|
|
94
|
+
if (Array.isArray(alias) || field === "componentProperties") continue;
|
|
95
|
+
// SAFETY: Figma binds a field to one alias or a list of them. componentProperties, the one map of bindings, was skipped above.
|
|
96
|
+
out[field] = await name.variable((alias as VariableAlias).id);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return Object.keys(out).length ? out : undefined;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const pageOf = (n: BaseNode) => {
|
|
103
|
+
while (n.parent && n.type !== "PAGE") n = n.parent;
|
|
104
|
+
|
|
105
|
+
return n;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export async function inventory(): Promise<Inventory> {
|
|
109
|
+
const name = names();
|
|
110
|
+
|
|
111
|
+
for (const page of figma.root.children) await page.loadAsync();
|
|
112
|
+
const variables = new Map((await figma.variables.getLocalVariablesAsync()).map((v) => [v.id, v]));
|
|
113
|
+
|
|
114
|
+
const value = async (v: VariableValue): Promise<Value> => {
|
|
115
|
+
if (Object(v) !== v) {
|
|
116
|
+
// SAFETY: primitive VariableValue values are string, number, or boolean.
|
|
117
|
+
return v as string | number | boolean;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// SAFETY: non-primitive VariableValue values are VariableAlias, RGB, or RGBA objects.
|
|
121
|
+
const obj = v as VariableAlias | RGB | RGBA;
|
|
122
|
+
|
|
123
|
+
return "id" in obj
|
|
124
|
+
? { alias: (await name.variable(obj.id)) ?? obj.id, id: obj.id }
|
|
125
|
+
: "r" in obj
|
|
126
|
+
? colorOf(obj)
|
|
127
|
+
: JSON.stringify(obj);
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const collections: Collection[] = [];
|
|
131
|
+
|
|
132
|
+
for (const c of await figma.variables.getLocalVariableCollectionsAsync()) {
|
|
133
|
+
const modes = [...c.modes].sort(
|
|
134
|
+
(a, b) => Number(b.modeId === c.defaultModeId) - Number(a.modeId === c.defaultModeId),
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
const vars: Collection["variables"] = [];
|
|
138
|
+
|
|
139
|
+
for (const id of c.variableIds) {
|
|
140
|
+
const v = variables.get(id);
|
|
141
|
+
|
|
142
|
+
if (!v) continue;
|
|
143
|
+
const values: Record<string, Value> = {};
|
|
144
|
+
|
|
145
|
+
for (const m of modes) values[m.name] = await value(v.valuesByMode[m.modeId]);
|
|
146
|
+
const codeSyntax = Object.entries(v.codeSyntax).filter(([, syntax]) => syntax);
|
|
147
|
+
|
|
148
|
+
vars.push({
|
|
149
|
+
name: v.name,
|
|
150
|
+
id: v.id,
|
|
151
|
+
type: v.resolvedType,
|
|
152
|
+
values,
|
|
153
|
+
scopes: v.scopes.includes("ALL_SCOPES") ? undefined : v.scopes,
|
|
154
|
+
codeSyntax: codeSyntax.length ? Object.fromEntries(codeSyntax) : undefined,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
collections.push({ name: c.name, id: c.id, modes: modes.map((m) => m.name), variables: vars });
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const text = (await figma.getLocalTextStylesAsync()).map(async (s) => ({
|
|
162
|
+
name: s.name,
|
|
163
|
+
id: s.id,
|
|
164
|
+
family: s.fontName.family,
|
|
165
|
+
style: s.fontName.style,
|
|
166
|
+
size: s.fontSize,
|
|
167
|
+
lineHeight: unitOf(s.lineHeight),
|
|
168
|
+
letterSpacing: s.letterSpacing.value ? unitOf(s.letterSpacing) : undefined,
|
|
169
|
+
paragraphSpacing: s.paragraphSpacing || undefined,
|
|
170
|
+
textCase: s.textCase === "ORIGINAL" ? undefined : s.textCase,
|
|
171
|
+
textDecoration: s.textDecoration === "NONE" ? undefined : s.textDecoration,
|
|
172
|
+
variables: await boundOf(s.boundVariables, name),
|
|
173
|
+
}));
|
|
174
|
+
|
|
175
|
+
const paint = (await figma.getLocalPaintStylesAsync()).map(async (s) => ({
|
|
176
|
+
name: s.name,
|
|
177
|
+
id: s.id,
|
|
178
|
+
paints: await paintsOf(s.paints, name),
|
|
179
|
+
}));
|
|
180
|
+
|
|
181
|
+
const effect = (await figma.getLocalEffectStylesAsync()).map((s) => ({
|
|
182
|
+
name: s.name,
|
|
183
|
+
id: s.id,
|
|
184
|
+
effects: s.effects.map(effectOf),
|
|
185
|
+
}));
|
|
186
|
+
|
|
187
|
+
const components = figma.root
|
|
188
|
+
.findAllWithCriteria({ types: ["COMPONENT_SET", "COMPONENT"] })
|
|
189
|
+
.filter((c) => c.parent?.type !== "COMPONENT_SET")
|
|
190
|
+
.map((c) => {
|
|
191
|
+
const properties = Object.entries(c.componentPropertyDefinitions).map(([key, d]) => [
|
|
192
|
+
key,
|
|
193
|
+
{
|
|
194
|
+
type: d.type,
|
|
195
|
+
default: d.defaultValue,
|
|
196
|
+
options: d.variantOptions,
|
|
197
|
+
preferredValues: d.preferredValues?.length ? d.preferredValues : undefined,
|
|
198
|
+
},
|
|
199
|
+
]);
|
|
200
|
+
|
|
201
|
+
return {
|
|
202
|
+
name: c.name,
|
|
203
|
+
id: c.id,
|
|
204
|
+
page: pageOf(c).name,
|
|
205
|
+
description: c.description || undefined,
|
|
206
|
+
properties: properties.length ? Object.fromEntries(properties) : undefined,
|
|
207
|
+
variants: c.type === "COMPONENT_SET" ? c.children.map((v) => ({ name: v.name, id: v.id })) : undefined,
|
|
208
|
+
};
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
return {
|
|
212
|
+
file: figma.root.name,
|
|
213
|
+
pages: figma.root.children.map((p) => ({
|
|
214
|
+
name: p.name,
|
|
215
|
+
id: p.id,
|
|
216
|
+
children: some(p.children.map((n) => ({ name: n.name, id: n.id, type: n.type }))),
|
|
217
|
+
flows: some(p.flowStartingPoints.map((f) => ({ name: f.name, nodeId: f.nodeId }))),
|
|
218
|
+
})),
|
|
219
|
+
collections,
|
|
220
|
+
styles: { text: await Promise.all(text), paint: await Promise.all(paint), effect },
|
|
221
|
+
components,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
type Section = Exclude<InspectInclude, "all">;
|
|
226
|
+
|
|
227
|
+
const SECTIONS: Section[] = ["prototype", "handoff", "variables", "text-runs", "grids", "vectors"];
|
|
228
|
+
|
|
229
|
+
const reactionOf = (r: Reaction) => ({
|
|
230
|
+
trigger: r.trigger?.type ?? "NONE",
|
|
231
|
+
actions: (r.actions ?? []).map((a) => ({
|
|
232
|
+
type: a.type,
|
|
233
|
+
destination: a.type === "URL" ? a.url : "destinationId" in a ? (a.destinationId ?? undefined) : undefined,
|
|
234
|
+
navigation: "navigation" in a ? a.navigation : undefined,
|
|
235
|
+
transition: "transition" in a ? a.transition?.type : undefined,
|
|
236
|
+
})),
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
async function devResourcesOf(n: BaseNode) {
|
|
240
|
+
if (!("getDevResourcesAsync" in n)) return;
|
|
241
|
+
|
|
242
|
+
// Editors without Dev Mode resources throw instead of returning none.
|
|
243
|
+
try {
|
|
244
|
+
return some((await n.getDevResourcesAsync()).map((r) => ({ name: r.name, url: r.url })));
|
|
245
|
+
} catch {
|
|
246
|
+
return undefined;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async function modesOf(explicit: Record<string, string>, name: Names) {
|
|
251
|
+
const out: Record<string, string> = {};
|
|
252
|
+
|
|
253
|
+
for (const [collectionId, modeId] of Object.entries(explicit)) {
|
|
254
|
+
const c = await name.collection(collectionId);
|
|
255
|
+
out[c?.name ?? collectionId] = c?.modes.find((m) => m.modeId === modeId)?.name ?? modeId;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return Object.keys(out).length ? out : undefined;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const runsOf = (n: TextNode, name: Names) =>
|
|
262
|
+
Promise.all(
|
|
263
|
+
n
|
|
264
|
+
.getStyledTextSegments(["fontName", "fontSize", "textStyleId", "fills", "textDecoration", "hyperlink"])
|
|
265
|
+
.map(async (s): Promise<NonNullable<Layer["runs"]>[number]> => ({
|
|
266
|
+
start: s.start,
|
|
267
|
+
end: s.end,
|
|
268
|
+
font: [s.fontName.family, s.fontName.style, s.fontSize],
|
|
269
|
+
style: s.textStyleId ? await name.style(s.textStyleId) : undefined,
|
|
270
|
+
fills: some(await Promise.all(s.fills.map((p) => paintOf(p, name)))),
|
|
271
|
+
decoration: s.textDecoration === "NONE" ? undefined : s.textDecoration,
|
|
272
|
+
link: s.hyperlink?.value,
|
|
273
|
+
})),
|
|
274
|
+
);
|
|
275
|
+
|
|
276
|
+
function boundsOf(n: DimensionAndPositionMixin) {
|
|
277
|
+
const bounds = { minW: n.minWidth, maxW: n.maxWidth, minH: n.minHeight, maxH: n.maxHeight };
|
|
278
|
+
const set = Object.entries(bounds).filter(([, v]) => v !== null);
|
|
279
|
+
|
|
280
|
+
return set.length ? Object.fromEntries(set) : undefined;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** Where a slide sits in its file's grid of slides. */
|
|
284
|
+
function slideOf(n: BaseNode) {
|
|
285
|
+
const row = n.parent?.type === "SLIDE_ROW" ? n.parent : null;
|
|
286
|
+
const grid = row?.parent?.type === "SLIDE_GRID" ? row.parent : null;
|
|
287
|
+
|
|
288
|
+
return {
|
|
289
|
+
skipped: (n.type === "SLIDE" && n.isSkippedSlide) || undefined,
|
|
290
|
+
row: grid && row ? grid.children.indexOf(row) : undefined,
|
|
291
|
+
col: row && n.type === "SLIDE" ? row.children.indexOf(n) : undefined,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const endpointOf = (e: ConnectorEndpoint) => ("endpointNodeId" in e ? e.endpointNodeId : undefined);
|
|
296
|
+
|
|
297
|
+
/** FigJam and Slides nodes, always, and the sections `--include` asks for. */
|
|
298
|
+
async function details(n: BaseNode, on: Set<Section>, name: Names): Promise<Partial<Layer>> {
|
|
299
|
+
const prototype = on.has("prototype");
|
|
300
|
+
const handoff = on.has("handoff");
|
|
301
|
+
const grids = on.has("grids");
|
|
302
|
+
|
|
303
|
+
return {
|
|
304
|
+
reactions: prototype && "reactions" in n ? some(n.reactions.map(reactionOf)) : undefined,
|
|
305
|
+
overflow: prototype && "overflowDirection" in n && n.overflowDirection !== "NONE" ? n.overflowDirection : undefined,
|
|
306
|
+
devStatus:
|
|
307
|
+
handoff && "devStatus" in n && n.devStatus
|
|
308
|
+
? { type: n.devStatus.type, description: n.devStatus.description || undefined }
|
|
309
|
+
: undefined,
|
|
310
|
+
annotations:
|
|
311
|
+
handoff && "annotations" in n
|
|
312
|
+
? some(
|
|
313
|
+
n.annotations.map((a) => ({
|
|
314
|
+
label: a.label ?? a.labelMarkdown,
|
|
315
|
+
properties: a.properties?.map((p) => p.type),
|
|
316
|
+
})),
|
|
317
|
+
)
|
|
318
|
+
: undefined,
|
|
319
|
+
devResources: handoff ? await devResourcesOf(n) : undefined,
|
|
320
|
+
explicitModes:
|
|
321
|
+
on.has("variables") && "explicitVariableModes" in n ? await modesOf(n.explicitVariableModes, name) : undefined,
|
|
322
|
+
runs: on.has("text-runs") && n.type === "TEXT" ? await runsOf(n, name) : undefined,
|
|
323
|
+
layoutGrids: grids && "layoutGrids" in n ? some([...n.layoutGrids]) : undefined,
|
|
324
|
+
constraints: grids && "constraints" in n ? { ...n.constraints } : undefined,
|
|
325
|
+
clipsContent: grids && "clipsContent" in n ? n.clipsContent : undefined,
|
|
326
|
+
blendMode:
|
|
327
|
+
grids && "blendMode" in n && n.blendMode !== "PASS_THROUGH" && n.blendMode !== "NORMAL" ? n.blendMode : undefined,
|
|
328
|
+
rotation: grids && "rotation" in n && n.rotation ? round(n.rotation) : undefined,
|
|
329
|
+
bounds: grids && "minWidth" in n ? boundsOf(n) : undefined,
|
|
330
|
+
dashPattern: grids && "dashPattern" in n ? some([...n.dashPattern]) : undefined,
|
|
331
|
+
strokeAlign: grids && "strokeAlign" in n && "strokes" in n && n.strokes.length ? n.strokeAlign : undefined,
|
|
332
|
+
vectorPaths: on.has("vectors") && "vectorPaths" in n ? some(n.vectorPaths.map((p) => p.data)) : undefined,
|
|
333
|
+
sticky:
|
|
334
|
+
n.type === "STICKY"
|
|
335
|
+
? { text: n.text.characters, authorVisible: n.authorVisible, authorName: n.authorName || undefined }
|
|
336
|
+
: undefined,
|
|
337
|
+
connector:
|
|
338
|
+
n.type === "CONNECTOR"
|
|
339
|
+
? {
|
|
340
|
+
text: n.text.characters || undefined,
|
|
341
|
+
start: endpointOf(n.connectorStart),
|
|
342
|
+
end: endpointOf(n.connectorEnd),
|
|
343
|
+
}
|
|
344
|
+
: undefined,
|
|
345
|
+
diagram: n.type === "SHAPE_WITH_TEXT" ? { kind: n.shapeType, text: n.text.characters } : undefined,
|
|
346
|
+
table:
|
|
347
|
+
n.type === "TABLE"
|
|
348
|
+
? {
|
|
349
|
+
rows: n.numRows,
|
|
350
|
+
cols: n.numColumns,
|
|
351
|
+
cells: Array.from({ length: n.numRows }, (_, r) =>
|
|
352
|
+
Array.from({ length: n.numColumns }, (_, c) => n.cellAt(r, c).text.characters),
|
|
353
|
+
),
|
|
354
|
+
}
|
|
355
|
+
: undefined,
|
|
356
|
+
slide: n.type === "SLIDE" || n.type === "SLIDE_ROW" ? slideOf(n) : undefined,
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* The page `page` names, by name or id, never switching to it: reading must not move the person's view. Throws when no page or several
|
|
362
|
+
* match, except that with `missing` an absent name gives null, for a run to make that page.
|
|
363
|
+
*/
|
|
364
|
+
export function resolvePage(page: string, missing?: false): PageNode;
|
|
365
|
+
export function resolvePage(page: string, missing: boolean): PageNode | null;
|
|
366
|
+
export function resolvePage(page: string, missing = false): PageNode | null {
|
|
367
|
+
const pages = figma.root.children.map((p) => `"${p.name}" (${p.id})`).join(", ");
|
|
368
|
+
const matches = figma.root.children.filter((p) => p.id === page || p.name === page);
|
|
369
|
+
|
|
370
|
+
if (matches.length > 1)
|
|
371
|
+
throw new Error(`Several pages are named "${page}". Pass one's id to --page instead: ${pages}`);
|
|
372
|
+
|
|
373
|
+
if (!matches.length && (!missing || /^\d+:\d+$/.test(page)))
|
|
374
|
+
throw new Error(`The file has no page "${page}". Its pages: ${pages}`);
|
|
375
|
+
|
|
376
|
+
return matches[0] ?? null;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export async function layers(
|
|
380
|
+
ids: string[],
|
|
381
|
+
selection: boolean,
|
|
382
|
+
depth: number,
|
|
383
|
+
options: InspectOptions = {},
|
|
384
|
+
): Promise<Layer[]> {
|
|
385
|
+
const name = names();
|
|
386
|
+
const include = options.include ?? [];
|
|
387
|
+
const on = new Set(include.includes("all") ? SECTIONS : include.filter((x): x is Section => x !== "all"));
|
|
388
|
+
const roots: BaseNode[] = selection ? [...figma.currentPage.selection] : [];
|
|
389
|
+
|
|
390
|
+
for (const id of selection ? [] : ids) {
|
|
391
|
+
const n = await figma.getNodeByIdAsync(id);
|
|
392
|
+
|
|
393
|
+
if (!n)
|
|
394
|
+
throw new Error(
|
|
395
|
+
`No node with id ${id} in this file. \`figdriver inspect\` lists the pages and components with their ids.`,
|
|
396
|
+
);
|
|
397
|
+
roots.push(n);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
if (!roots.length)
|
|
401
|
+
throw new Error(
|
|
402
|
+
selection ? "Nothing is selected in the open file." : "Name a node with --node <id>, or pass --selection.",
|
|
403
|
+
);
|
|
404
|
+
|
|
405
|
+
const describe = async (n: BaseNode): Promise<Layer> => {
|
|
406
|
+
if (n.type === "PAGE") await n.loadAsync();
|
|
407
|
+
const parent = n.parent;
|
|
408
|
+
|
|
409
|
+
const placed =
|
|
410
|
+
!!parent &&
|
|
411
|
+
"layoutMode" in parent &&
|
|
412
|
+
parent.layoutMode !== "NONE" &&
|
|
413
|
+
!("layoutPositioning" in n && n.layoutPositioning === "ABSOLUTE");
|
|
414
|
+
|
|
415
|
+
const padding = "layoutMode" in n ? [n.paddingTop, n.paddingRight, n.paddingBottom, n.paddingLeft].map(round) : [];
|
|
416
|
+
const main = n.type === "INSTANCE" ? await n.getMainComponentAsync() : undefined;
|
|
417
|
+
|
|
418
|
+
const values =
|
|
419
|
+
n.type === "INSTANCE" ? Object.entries(n.componentProperties).map(([key, p]) => [key, p.value] as const) : [];
|
|
420
|
+
|
|
421
|
+
return {
|
|
422
|
+
id: n.id,
|
|
423
|
+
type: n.type,
|
|
424
|
+
name: n.name,
|
|
425
|
+
visible: "visible" in n && !n.visible ? false : undefined,
|
|
426
|
+
size: "width" in n ? [round(n.width), round(n.height)] : undefined,
|
|
427
|
+
at: "x" in n && !placed ? [round(n.x), round(n.y)] : undefined,
|
|
428
|
+
sizing:
|
|
429
|
+
placed &&
|
|
430
|
+
"layoutSizingHorizontal" in n &&
|
|
431
|
+
(n.layoutSizingHorizontal !== "FIXED" || n.layoutSizingVertical !== "FIXED")
|
|
432
|
+
? [n.layoutSizingHorizontal, n.layoutSizingVertical]
|
|
433
|
+
: undefined,
|
|
434
|
+
layout:
|
|
435
|
+
"layoutMode" in n && n.layoutMode !== "NONE"
|
|
436
|
+
? {
|
|
437
|
+
mode: n.layoutMode,
|
|
438
|
+
gap:
|
|
439
|
+
n.layoutMode === "GRID"
|
|
440
|
+
? [round(n.gridRowGap), round(n.gridColumnGap)]
|
|
441
|
+
: round(n.itemSpacing) || undefined,
|
|
442
|
+
grid: n.layoutMode === "GRID" ? [n.gridRowCount, n.gridColumnCount] : undefined,
|
|
443
|
+
padding: padding.some(Boolean) ? padding : undefined,
|
|
444
|
+
align:
|
|
445
|
+
n.primaryAxisAlignItems === "MIN" && n.counterAxisAlignItems === "MIN"
|
|
446
|
+
? undefined
|
|
447
|
+
: [n.primaryAxisAlignItems, n.counterAxisAlignItems],
|
|
448
|
+
wrap: n.layoutWrap === "WRAP" || undefined,
|
|
449
|
+
}
|
|
450
|
+
: undefined,
|
|
451
|
+
fills: !("fills" in n)
|
|
452
|
+
? undefined
|
|
453
|
+
: "fillStyleId" in n && n.fillStyleId !== figma.mixed && n.fillStyleId
|
|
454
|
+
? await name.style(n.fillStyleId)
|
|
455
|
+
: await paintsOf(n.fills, name),
|
|
456
|
+
strokes:
|
|
457
|
+
"strokes" in n && n.strokes.length
|
|
458
|
+
? n.strokeStyleId
|
|
459
|
+
? await name.style(n.strokeStyleId)
|
|
460
|
+
: await paintsOf(n.strokes, name)
|
|
461
|
+
: undefined,
|
|
462
|
+
strokeWeight: !("strokes" in n && n.strokes.length)
|
|
463
|
+
? undefined
|
|
464
|
+
: n.strokeWeight !== figma.mixed
|
|
465
|
+
? n.strokeWeight
|
|
466
|
+
: "strokeTopWeight" in n
|
|
467
|
+
? [n.strokeTopWeight, n.strokeRightWeight, n.strokeBottomWeight, n.strokeLeftWeight]
|
|
468
|
+
: "MIXED",
|
|
469
|
+
radius: !("cornerRadius" in n)
|
|
470
|
+
? undefined
|
|
471
|
+
: n.cornerRadius !== figma.mixed
|
|
472
|
+
? n.cornerRadius || undefined
|
|
473
|
+
: !("topLeftRadius" in n)
|
|
474
|
+
? "MIXED"
|
|
475
|
+
: [n.topLeftRadius, n.topRightRadius, n.bottomRightRadius, n.bottomLeftRadius],
|
|
476
|
+
opacity: "opacity" in n && n.opacity < 1 ? round(n.opacity) : undefined,
|
|
477
|
+
effects:
|
|
478
|
+
"effects" in n && n.effects.length
|
|
479
|
+
? n.effectStyleId
|
|
480
|
+
? await name.style(n.effectStyleId)
|
|
481
|
+
: n.effects.map(effectOf)
|
|
482
|
+
: undefined,
|
|
483
|
+
variables: "boundVariables" in n ? await boundOf(n.boundVariables, name) : undefined,
|
|
484
|
+
references:
|
|
485
|
+
"componentPropertyReferences" in n &&
|
|
486
|
+
n.componentPropertyReferences &&
|
|
487
|
+
Object.keys(n.componentPropertyReferences).length
|
|
488
|
+
? n.componentPropertyReferences
|
|
489
|
+
: undefined,
|
|
490
|
+
text: n.type === "TEXT" ? n.characters : undefined,
|
|
491
|
+
textStyle:
|
|
492
|
+
n.type !== "TEXT"
|
|
493
|
+
? undefined
|
|
494
|
+
: n.textStyleId === figma.mixed
|
|
495
|
+
? await Promise.all(
|
|
496
|
+
n
|
|
497
|
+
.getStyledTextSegments(["textStyleId"])
|
|
498
|
+
.map(async (s) => [s.start, s.end, s.textStyleId ? await name.style(s.textStyleId) : null] as const),
|
|
499
|
+
)
|
|
500
|
+
: n.textStyleId
|
|
501
|
+
? await name.style(n.textStyleId)
|
|
502
|
+
: undefined,
|
|
503
|
+
font:
|
|
504
|
+
n.type === "TEXT" && !n.textStyleId && n.fontName !== figma.mixed
|
|
505
|
+
? [n.fontName.family, n.fontName.style, n.fontSize === figma.mixed ? "MIXED" : n.fontSize]
|
|
506
|
+
: undefined,
|
|
507
|
+
main: main
|
|
508
|
+
? {
|
|
509
|
+
name: componentFullName(main),
|
|
510
|
+
id: main.id,
|
|
511
|
+
remote: main.remote || undefined,
|
|
512
|
+
}
|
|
513
|
+
: undefined,
|
|
514
|
+
properties: values.length ? Object.fromEntries(values) : undefined,
|
|
515
|
+
...(await details(n, on, name)),
|
|
516
|
+
};
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
const walk = async (n: BaseNode, level: number): Promise<Layer> => {
|
|
520
|
+
const o = await describe(n);
|
|
521
|
+
|
|
522
|
+
if (!("children" in n && n.children.length)) return o;
|
|
523
|
+
|
|
524
|
+
return level < depth && (n.type !== "INSTANCE" || level === 0 || options.expandInstances === true)
|
|
525
|
+
? { ...o, children: await Promise.all(n.children.map((c) => walk(c, level + 1))) }
|
|
526
|
+
: { ...o, childCount: n.children.length };
|
|
527
|
+
};
|
|
528
|
+
|
|
529
|
+
return Promise.all(roots.map((r) => walk(r, 0)));
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/** The names of a node and its ancestors below its page. */
|
|
533
|
+
function pathOf(n: BaseNode) {
|
|
534
|
+
const names: string[] = [];
|
|
535
|
+
|
|
536
|
+
for (let at: BaseNode | null = n; at && at.type !== "PAGE" && at.type !== "DOCUMENT"; at = at.parent)
|
|
537
|
+
names.unshift(at.name);
|
|
538
|
+
|
|
539
|
+
return names.join(" / ");
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
const textOf = (n: BaseNode) =>
|
|
543
|
+
n.type === "TEXT"
|
|
544
|
+
? n.characters
|
|
545
|
+
: n.type === "STICKY" || n.type === "SHAPE_WITH_TEXT" || n.type === "CONNECTOR"
|
|
546
|
+
? n.text.characters
|
|
547
|
+
: undefined;
|
|
548
|
+
|
|
549
|
+
const has = (value: string | undefined, part: string | undefined) =>
|
|
550
|
+
part === undefined || (value !== undefined && value.toLowerCase().includes(part.toLowerCase()));
|
|
551
|
+
|
|
552
|
+
export const propertyValues = (n: InstanceNode) =>
|
|
553
|
+
Object.fromEntries(Object.entries(n.componentProperties).map(([key, p]) => [key, p.value]));
|
|
554
|
+
|
|
555
|
+
/**
|
|
556
|
+
* Layers matching every filter of `query`, in document order: under `node`, on `page` (or every page for "all"), or else on the current
|
|
557
|
+
* page. Only the first `limit` (50 by default) are described; `total` counts them all.
|
|
558
|
+
*/
|
|
559
|
+
export async function findNodes(query: FindQuery): Promise<FindResults> {
|
|
560
|
+
const limit = query.limit ?? 50;
|
|
561
|
+
const types = query.type?.split(",").flatMap((t) => (t.trim() ? [t.trim().toUpperCase()] : []));
|
|
562
|
+
const roots: BaseNode[] = [];
|
|
563
|
+
|
|
564
|
+
if (query.node !== undefined) {
|
|
565
|
+
const n = await figma.getNodeByIdAsync(query.node);
|
|
566
|
+
|
|
567
|
+
if (!n)
|
|
568
|
+
throw new Error(
|
|
569
|
+
`No node with id ${query.node} in this file. \`figdriver inspect\` lists the pages and components with their ids.`,
|
|
570
|
+
);
|
|
571
|
+
roots.push(n);
|
|
572
|
+
} else if (query.page === "all") roots.push(...figma.root.children);
|
|
573
|
+
else roots.push(query.page === undefined ? figma.currentPage : resolvePage(query.page));
|
|
574
|
+
|
|
575
|
+
const matches = (n: BaseNode) =>
|
|
576
|
+
(!types || types.includes(n.type)) &&
|
|
577
|
+
has(n.name, query.name) &&
|
|
578
|
+
(query.text === undefined || has(textOf(n), query.text)) &&
|
|
579
|
+
(query.component === undefined || n.type === "INSTANCE");
|
|
580
|
+
|
|
581
|
+
const candidates: BaseNode[] = [];
|
|
582
|
+
|
|
583
|
+
for (const root of roots) {
|
|
584
|
+
if (root.type === "PAGE") await root.loadAsync();
|
|
585
|
+
else if (matches(root)) candidates.push(root);
|
|
586
|
+
|
|
587
|
+
// A loop, not a spread: a page can hold more matches than a call takes arguments.
|
|
588
|
+
if ("findAll" in root) for (const n of root.findAll(matches)) candidates.push(n);
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
const found: FindMatch[] = [];
|
|
592
|
+
let total = 0;
|
|
593
|
+
|
|
594
|
+
for (const n of candidates) {
|
|
595
|
+
const component = n.type === "INSTANCE" ? await n.getMainComponentAsync() : null;
|
|
596
|
+
const main = component ? componentFullName(component) : undefined;
|
|
597
|
+
|
|
598
|
+
if (query.component !== undefined && !has(main, query.component)) continue;
|
|
599
|
+
total++;
|
|
600
|
+
|
|
601
|
+
if (found.length >= limit) continue;
|
|
602
|
+
const text = textOf(n);
|
|
603
|
+
|
|
604
|
+
found.push({
|
|
605
|
+
id: n.id,
|
|
606
|
+
type: n.type,
|
|
607
|
+
name: n.name,
|
|
608
|
+
page: pageOf(n).name,
|
|
609
|
+
path: pathOf(n),
|
|
610
|
+
size: "width" in n ? [round(n.width), round(n.height)] : undefined,
|
|
611
|
+
text: text === undefined ? undefined : text.length > 200 ? `${text.slice(0, 200)}…` : text,
|
|
612
|
+
main,
|
|
613
|
+
properties: n.type === "INSTANCE" ? propertyValues(n) : undefined,
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
return { matches: found, total, truncated: total > found.length };
|
|
618
|
+
}
|