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,301 @@
|
|
|
1
|
+
import { componentFullName, hasAncestor, localStyles, read, type ownedItems } from "./owned";
|
|
2
|
+
import type { Use } from "../core/rerun";
|
|
3
|
+
|
|
4
|
+
type Items = Awaited<ReturnType<typeof ownedItems>>;
|
|
5
|
+
|
|
6
|
+
type Part = ComponentNode | FrameNode | Variable | PaintStyle | TextStyle | EffectStyle;
|
|
7
|
+
|
|
8
|
+
type Style = PaintStyle | TextStyle | EffectStyle | GridStyle;
|
|
9
|
+
|
|
10
|
+
type Place = { node?: SceneNode; where: string };
|
|
11
|
+
|
|
12
|
+
type Move = Use & Place & { apply(to: Part): void | Promise<void> };
|
|
13
|
+
|
|
14
|
+
const componentKey = (c: ComponentNode) => `component:${componentFullName(c)}`;
|
|
15
|
+
|
|
16
|
+
export async function partsOf(items: Items) {
|
|
17
|
+
const parts: [string, Part][] = [];
|
|
18
|
+
|
|
19
|
+
for (const x of items) {
|
|
20
|
+
if ("variableIds" in x) {
|
|
21
|
+
for (const id of x.variableIds) {
|
|
22
|
+
const v = await figma.variables.getVariableByIdAsync(id);
|
|
23
|
+
|
|
24
|
+
if (v) parts.push([`variable:${x.name}/${v.name}`, v]);
|
|
25
|
+
}
|
|
26
|
+
} else if ("getStyleConsumersAsync" in x) parts.push([`style:${x.type}/${x.name}`, x]);
|
|
27
|
+
else if ("findAll" in x) {
|
|
28
|
+
for (const c of x.findAllWithCriteria({ types: ["COMPONENT"] })) parts.push([componentKey(c), c]);
|
|
29
|
+
|
|
30
|
+
if (x.type === "SECTION") for (const f of x.children) if (f.type === "FRAME") parts.push([`screen:${f.name}`, f]);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return parts;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const MOVABLE = new Set(["fills", "strokes", "effects", "textRangeFills"]);
|
|
38
|
+
|
|
39
|
+
function rebind(n: SceneNode, field: string, from: string, to: Variable) {
|
|
40
|
+
const paint = (p: Paint) =>
|
|
41
|
+
p.type === "SOLID" && p.boundVariables?.color?.id === from
|
|
42
|
+
? figma.variables.setBoundVariableForPaint(p, "color", to)
|
|
43
|
+
: p;
|
|
44
|
+
|
|
45
|
+
if (field === "fills" && "fills" in n && n.fills !== figma.mixed) n.fills = n.fills.map(paint);
|
|
46
|
+
else if (field === "strokes" && "strokes" in n) n.strokes = n.strokes.map(paint);
|
|
47
|
+
else if (field === "effects" && "effects" in n) {
|
|
48
|
+
n.effects = n.effects.map((e) =>
|
|
49
|
+
Object.entries<VariableAlias | undefined>(("boundVariables" in e && e.boundVariables) || {}).reduce(
|
|
50
|
+
(out, [f, alias]) =>
|
|
51
|
+
alias?.id === from
|
|
52
|
+
? // SAFETY: the keys of an effect's boundVariables are the effect fields it binds.
|
|
53
|
+
figma.variables.setBoundVariableForEffect(out, f as VariableBindableEffectField, to)
|
|
54
|
+
: out,
|
|
55
|
+
e,
|
|
56
|
+
),
|
|
57
|
+
);
|
|
58
|
+
} else if (field === "textRangeFills" && n.type === "TEXT")
|
|
59
|
+
for (const s of n.getStyledTextSegments(["fills"])) n.setRangeFills(s.start, s.end, s.fills.map(paint));
|
|
60
|
+
else {
|
|
61
|
+
// SAFETY: every other key of a node's boundVariables names a field it binds one variable to.
|
|
62
|
+
n.setBoundVariable(field as VariableBindableNodeField, to);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
type Path = (string | number)[];
|
|
67
|
+
|
|
68
|
+
type Json = string | number | boolean | null | undefined | Json[] | { readonly [key: string]: Json };
|
|
69
|
+
|
|
70
|
+
export function aliasesIn(value: Json, path: Path = []): { path: Path; id: string }[] {
|
|
71
|
+
if (Array.isArray(value)) return value.flatMap((v, i) => aliasesIn(v, [...path, i]));
|
|
72
|
+
|
|
73
|
+
if (!(value instanceof Object)) return [];
|
|
74
|
+
const { type, id } = value;
|
|
75
|
+
|
|
76
|
+
if (type === "VARIABLE_ALIAS" && id === String(id)) return [{ path, id }];
|
|
77
|
+
|
|
78
|
+
return Object.entries(value).flatMap(([key, v]) => aliasesIn(v, [...path, key]));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// The aliases rebindAlias can point at another variable. Any other, such as a gradient stop's, cannot move.
|
|
82
|
+
|
|
83
|
+
const MOVABLE_ALIASES = [
|
|
84
|
+
/^valuesByMode\/[^/]+$/,
|
|
85
|
+
/^boundVariables\/\w+$/,
|
|
86
|
+
/^paints\/\d+\/boundVariables\/color$/,
|
|
87
|
+
/^(effects|layoutGrids)\/\d+\/boundVariables\/\w+$/,
|
|
88
|
+
];
|
|
89
|
+
|
|
90
|
+
export const movableAlias = (path: Path) => MOVABLE_ALIASES.some((r) => r.test(path.join("/")));
|
|
91
|
+
|
|
92
|
+
function rebindAlias(x: Variable | Style, [, at, , key]: Path, to: Variable) {
|
|
93
|
+
const replace = <T>(list: readonly T[], f: (item: T) => T) => list.map((item, i) => (i === at ? f(item) : item));
|
|
94
|
+
|
|
95
|
+
if ("valuesByMode" in x) x.setValueForMode(String(at), figma.variables.createVariableAlias(to));
|
|
96
|
+
else if (x.type === "TEXT") {
|
|
97
|
+
// SAFETY: the path is boundVariables/<field>, and a text style's boundVariables is keyed by the text fields it binds.
|
|
98
|
+
x.setBoundVariable(at as VariableBindableTextField, to);
|
|
99
|
+
} else if (x.type === "PAINT")
|
|
100
|
+
// SAFETY: the path is paints/<i>/boundVariables/color, and only a solid paint binds its color there.
|
|
101
|
+
x.paints = replace(x.paints, (p) => figma.variables.setBoundVariableForPaint(p as SolidPaint, "color", to));
|
|
102
|
+
else if (x.type === "EFFECT")
|
|
103
|
+
x.effects = replace(x.effects, (e) =>
|
|
104
|
+
// SAFETY: the path is effects/<i>/boundVariables/<key>, and an effect's boundVariables is keyed by the fields it binds.
|
|
105
|
+
figma.variables.setBoundVariableForEffect(e, key as VariableBindableEffectField, to),
|
|
106
|
+
);
|
|
107
|
+
else if (x.type === "GRID")
|
|
108
|
+
x.layoutGrids = replace(x.layoutGrids, (g) =>
|
|
109
|
+
// SAFETY: the path is layoutGrids/<i>/boundVariables/<key>, and a grid's boundVariables is keyed by the fields it binds.
|
|
110
|
+
figma.variables.setBoundVariableForLayoutGrid(g, key as VariableBindableLayoutGridField, to),
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const retarget = (reactions: readonly Reaction[], from: string, to: string): Reaction[] =>
|
|
115
|
+
reactions.map((r) => ({
|
|
116
|
+
trigger: r.trigger,
|
|
117
|
+
actions: r.actions?.map((a) => (a.type === "NODE" && a.destinationId === from ? { ...a, destinationId: to } : a)),
|
|
118
|
+
}));
|
|
119
|
+
|
|
120
|
+
export async function usesOf(old: Items, skip: PageNode[]) {
|
|
121
|
+
const parts = await partsOf(old);
|
|
122
|
+
const keyOf = new Map(parts.map(([key, part]) => [part.id, key]));
|
|
123
|
+
const excluded = new Set([...old, ...skip].map((x) => x.id));
|
|
124
|
+
|
|
125
|
+
const outside = (n: BaseNode) => !hasAncestor(n, (p) => excluded.has(p.id));
|
|
126
|
+
|
|
127
|
+
const where = (n: BaseNode) => {
|
|
128
|
+
const names: string[] = [];
|
|
129
|
+
|
|
130
|
+
for (let p: BaseNode | null = n; p && p.type !== "DOCUMENT"; p = p.parent) names.unshift(p.name);
|
|
131
|
+
|
|
132
|
+
return names.join(" › ");
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const moves: Move[] = [];
|
|
136
|
+
const at = (n: SceneNode): Place => ({ node: n, where: where(n) });
|
|
137
|
+
|
|
138
|
+
const add = (place: Place, id: string, apply: Move["apply"], movable = true) => {
|
|
139
|
+
const key = keyOf.get(id)!;
|
|
140
|
+
const kind = key.slice(0, key.indexOf(":"));
|
|
141
|
+
moves.push({ ...place, apply, what: `${kind} "${key.slice(kind.length + 1)}"`, key: movable ? key : null });
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const oldIds = new Set(old.map((x) => x.id));
|
|
145
|
+
|
|
146
|
+
const inOld = (n: BaseNode) => hasAncestor(n, (p) => oldIds.has(p.id));
|
|
147
|
+
|
|
148
|
+
const link = async (place: Place, id: string, apply: Move["apply"]) => {
|
|
149
|
+
if (keyOf.has(id)) return add(place, id, apply);
|
|
150
|
+
const dest = await figma.getNodeByIdAsync(id);
|
|
151
|
+
|
|
152
|
+
if (dest && inOld(dest)) moves.push({ ...place, apply, what: `the layer "${dest.name}"`, key: null });
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
for (const [, part] of parts) {
|
|
156
|
+
if (!("getInstancesAsync" in part)) continue;
|
|
157
|
+
|
|
158
|
+
for (const inst of await part.getInstancesAsync())
|
|
159
|
+
if (outside(inst)) {
|
|
160
|
+
// SAFETY: a component's key starts "component:", which only components have, so its counterpart is a component.
|
|
161
|
+
add(at(inst), part.id, (to) => inst.swapComponent(to as ComponentNode));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
await figma.loadAllPagesAsync();
|
|
166
|
+
|
|
167
|
+
for (const page of figma.root.children) {
|
|
168
|
+
if (excluded.has(page.id)) continue;
|
|
169
|
+
|
|
170
|
+
for (const n of page.findAll((d) => outside(d))) {
|
|
171
|
+
for (const [field, bound] of Object.entries(n.boundVariables ?? {})) {
|
|
172
|
+
const aliases: VariableAlias[] = Array.isArray(bound)
|
|
173
|
+
? bound
|
|
174
|
+
: field === "componentProperties"
|
|
175
|
+
? Object.values(bound)
|
|
176
|
+
: // SAFETY: Figma binds a field to one alias or a list of them. componentProperties, the one map of bindings, is handled above.
|
|
177
|
+
[bound as VariableAlias];
|
|
178
|
+
|
|
179
|
+
const movable = MOVABLE.has(field) || !(Array.isArray(bound) || field === "componentProperties");
|
|
180
|
+
|
|
181
|
+
for (const a of aliases)
|
|
182
|
+
if (keyOf.has(a.id)) {
|
|
183
|
+
// SAFETY: the id is a variable's, so its key starts "variable:", which only variables have.
|
|
184
|
+
add(at(n), a.id, (to) => rebind(n, field, a.id, to as Variable), movable);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (n.type === "TEXT") {
|
|
189
|
+
for (const s of n.getStyledTextSegments(["textStyleId", "fillStyleId"])) {
|
|
190
|
+
if (keyOf.has(s.textStyleId))
|
|
191
|
+
add(at(n), s.textStyleId, (to) => n.setRangeTextStyleIdAsync(s.start, s.end, to.id));
|
|
192
|
+
|
|
193
|
+
if (keyOf.has(s.fillStyleId))
|
|
194
|
+
add(at(n), s.fillStyleId, (to) => n.setRangeFillStyleIdAsync(s.start, s.end, to.id));
|
|
195
|
+
}
|
|
196
|
+
} else if ("fillStyleId" in n && n.fillStyleId !== figma.mixed && keyOf.has(n.fillStyleId))
|
|
197
|
+
add(at(n), n.fillStyleId, (to) => n.setFillStyleIdAsync(to.id));
|
|
198
|
+
|
|
199
|
+
if ("strokeStyleId" in n && keyOf.has(n.strokeStyleId))
|
|
200
|
+
add(at(n), n.strokeStyleId, (to) => n.setStrokeStyleIdAsync(to.id));
|
|
201
|
+
|
|
202
|
+
if ("effectStyleId" in n && keyOf.has(n.effectStyleId))
|
|
203
|
+
add(at(n), n.effectStyleId, (to) => n.setEffectStyleIdAsync(to.id));
|
|
204
|
+
|
|
205
|
+
if (!("reactions" in n)) continue;
|
|
206
|
+
|
|
207
|
+
for (const r of n.reactions) {
|
|
208
|
+
for (const a of r.actions ?? []) {
|
|
209
|
+
const from = a.type === "NODE" && a.destinationId;
|
|
210
|
+
|
|
211
|
+
if (from) await link(at(n), from, (to) => n.setReactionsAsync(retarget(n.reactions, from, to.id)));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
for (const f of page.flowStartingPoints) {
|
|
217
|
+
const start = await figma.getNodeByIdAsync(f.nodeId);
|
|
218
|
+
|
|
219
|
+
// A flow a run started belongs to that run's output, and goes with it.
|
|
220
|
+
if (!start || read(start, "flow") === f.name) continue;
|
|
221
|
+
await link({ where: `Flow "${f.name}" on page "${page.name}"` }, f.nodeId, (to) => {
|
|
222
|
+
page.flowStartingPoints = page.flowStartingPoints.map((x) =>
|
|
223
|
+
x.nodeId === f.nodeId && x.name === f.name ? { ...x, nodeId: to.id } : x,
|
|
224
|
+
);
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const collection = new Map((await figma.variables.getLocalVariableCollectionsAsync()).map((c) => [c.id, c.name]));
|
|
230
|
+
|
|
231
|
+
const styles: Style[] = [...(await localStyles()), ...(await figma.getLocalGridStylesAsync())];
|
|
232
|
+
|
|
233
|
+
const holders: [Variable | Style, string, object][] = [
|
|
234
|
+
...(await figma.variables.getLocalVariablesAsync()).flatMap((v): [Variable, string, object][] =>
|
|
235
|
+
excluded.has(v.variableCollectionId)
|
|
236
|
+
? []
|
|
237
|
+
: [[v, `Variable "${collection.get(v.variableCollectionId)}/${v.name}"`, { valuesByMode: v.valuesByMode }]],
|
|
238
|
+
),
|
|
239
|
+
...styles.flatMap((s): [Style, string, object][] =>
|
|
240
|
+
excluded.has(s.id)
|
|
241
|
+
? []
|
|
242
|
+
: [
|
|
243
|
+
[
|
|
244
|
+
s,
|
|
245
|
+
`Style "${s.name}"`,
|
|
246
|
+
s.type === "TEXT"
|
|
247
|
+
? { boundVariables: s.boundVariables }
|
|
248
|
+
: s.type === "PAINT"
|
|
249
|
+
? { paints: s.paints }
|
|
250
|
+
: s.type === "EFFECT"
|
|
251
|
+
? { effects: s.effects }
|
|
252
|
+
: { layoutGrids: s.layoutGrids },
|
|
253
|
+
],
|
|
254
|
+
],
|
|
255
|
+
),
|
|
256
|
+
];
|
|
257
|
+
|
|
258
|
+
for (const [x, label, data] of holders) {
|
|
259
|
+
// Walked as JSON, so the walk reads plain objects and arrays of this realm.
|
|
260
|
+
for (const { path, id } of aliasesIn(JSON.parse(JSON.stringify(data))))
|
|
261
|
+
if (keyOf.has(id)) {
|
|
262
|
+
// SAFETY: an alias holds a variable's id, whose key starts "variable:", which only variables have.
|
|
263
|
+
add({ where: label }, id, (to) => rebindAlias(x, path, to as Variable), movableAlias(path));
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return moves;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export async function migrate(
|
|
271
|
+
old: Items,
|
|
272
|
+
fresh: Items,
|
|
273
|
+
skip: PageNode[],
|
|
274
|
+
kinds: string[],
|
|
275
|
+
rename: (key: string) => string = (key) => key,
|
|
276
|
+
moved?: () => void | Promise<void>,
|
|
277
|
+
) {
|
|
278
|
+
const to = new Map(await partsOf(fresh));
|
|
279
|
+
|
|
280
|
+
const apply = async (moves: Move[], components: boolean) => {
|
|
281
|
+
for (const m of moves) {
|
|
282
|
+
const kind = m.key?.slice(0, m.key.indexOf(":"));
|
|
283
|
+
|
|
284
|
+
if (
|
|
285
|
+
m.key &&
|
|
286
|
+
kind &&
|
|
287
|
+
kinds.includes(kind) &&
|
|
288
|
+
(kind === "component") === components &&
|
|
289
|
+
!m.node?.removed &&
|
|
290
|
+
to.has(rename(m.key))
|
|
291
|
+
) {
|
|
292
|
+
await m.apply(to.get(rename(m.key))!);
|
|
293
|
+
await moved?.();
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
if (kinds.includes("component")) await apply(await usesOf(old, skip), true);
|
|
299
|
+
// Scanned again after the swaps: an instance's carried-over overrides can still use the old variables and styles.
|
|
300
|
+
await apply(await usesOf(old, skip), false);
|
|
301
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export const NS = "figdriver";
|
|
2
|
+
|
|
3
|
+
export const KEYS = {
|
|
4
|
+
owner: "owner",
|
|
5
|
+
anchor: "anchor",
|
|
6
|
+
box: "box",
|
|
7
|
+
bg: "bg",
|
|
8
|
+
fingerprint: "fingerprint",
|
|
9
|
+
layers: "layerFingerprints",
|
|
10
|
+
flow: "flow",
|
|
11
|
+
} as const;
|
|
12
|
+
|
|
13
|
+
type Holder = Pick<BaseNode, "getSharedPluginData" | "setSharedPluginData">;
|
|
14
|
+
|
|
15
|
+
export const read = (n: Holder, key: keyof typeof KEYS) => n.getSharedPluginData(NS, KEYS[key]);
|
|
16
|
+
export const write = (n: Holder, key: keyof typeof KEYS, value: string) => n.setSharedPluginData(NS, KEYS[key], value);
|
|
17
|
+
|
|
18
|
+
export const ownerOf = (n: Holder) => read(n, "owner");
|
|
19
|
+
|
|
20
|
+
/** Unset plugin data reads as "", so a blank owner owns nothing. */
|
|
21
|
+
export const ownedBy = (owner: string) => (n: Holder) => owner !== "" && ownerOf(n) === owner;
|
|
22
|
+
|
|
23
|
+
/** `owner`'s top-level output. An instance of one of its components reads as `owner`'s too, but belongs to whoever placed it. */
|
|
24
|
+
export const outputOf = (owner: string) => (n: SceneNode) => n.type !== "INSTANCE" && ownedBy(owner)(n);
|
|
25
|
+
|
|
26
|
+
export function own<N extends Holder>(n: N, owner: string): N {
|
|
27
|
+
write(n, "owner", owner);
|
|
28
|
+
|
|
29
|
+
return n;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function localStyles() {
|
|
33
|
+
return [
|
|
34
|
+
...(await figma.getLocalTextStylesAsync()),
|
|
35
|
+
...(await figma.getLocalPaintStylesAsync()),
|
|
36
|
+
...(await figma.getLocalEffectStylesAsync()),
|
|
37
|
+
];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function hasAncestor(n: BaseNode, test: (node: BaseNode) => boolean, includeSelf = true) {
|
|
41
|
+
for (let p: BaseNode | null = includeSelf ? n : n.parent; p; p = p.parent) if (test(p)) return true;
|
|
42
|
+
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const componentFullName = (c: ComponentNode) =>
|
|
47
|
+
c.parent?.type === "COMPONENT_SET" ? `${c.parent.name}/${c.name}` : c.name;
|
|
48
|
+
|
|
49
|
+
export async function ownedItems(owner: string, page?: PageNode) {
|
|
50
|
+
const styles = await localStyles();
|
|
51
|
+
|
|
52
|
+
return [
|
|
53
|
+
...(page?.children.filter(outputOf(owner)) ?? []),
|
|
54
|
+
...[...(await figma.variables.getLocalVariableCollectionsAsync()), ...styles].filter(ownedBy(owner)),
|
|
55
|
+
];
|
|
56
|
+
}
|