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,254 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ActiveUserRef,
|
|
3
|
+
AuditRules,
|
|
4
|
+
EditorCapabilities,
|
|
5
|
+
ExportOptions,
|
|
6
|
+
InspectApi,
|
|
7
|
+
InspectOptions,
|
|
8
|
+
OwnedAudit,
|
|
9
|
+
PageAudit,
|
|
10
|
+
PageFrames,
|
|
11
|
+
PageLayers,
|
|
12
|
+
ScreenShot,
|
|
13
|
+
SectionShot,
|
|
14
|
+
TabStatus,
|
|
15
|
+
} from "../core/protocol";
|
|
16
|
+
import { otherUsers } from "../core/users";
|
|
17
|
+
import { fd } from "./helpers";
|
|
18
|
+
import { ownerOf, read } from "./owned";
|
|
19
|
+
import { findNodes, inventory, layers, resolvePage } from "./read";
|
|
20
|
+
import { auditOf, treeOf } from "./review";
|
|
21
|
+
import { importTokens } from "./tokens";
|
|
22
|
+
|
|
23
|
+
async function ownedRoots(design: string | null) {
|
|
24
|
+
const roots: SceneNode[] = [];
|
|
25
|
+
|
|
26
|
+
for (const page of figma.root.children) {
|
|
27
|
+
await page.loadAsync();
|
|
28
|
+
roots.push(...page.children.filter((n) => (design ? ownerOf(n) === design : ownerOf(n))));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return roots;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// SVG and PDF exports reject a constraint, so only raster formats scale.
|
|
35
|
+
function settingsOf({ scale = 1, format = "png", bounds = "render" }: ExportOptions): ExportSettings {
|
|
36
|
+
const useAbsoluteBounds = bounds === "layout";
|
|
37
|
+
const constraint = { type: "SCALE", value: scale } as const;
|
|
38
|
+
|
|
39
|
+
switch (format) {
|
|
40
|
+
case "svg":
|
|
41
|
+
return { format: "SVG", useAbsoluteBounds };
|
|
42
|
+
case "pdf":
|
|
43
|
+
return { format: "PDF", useAbsoluteBounds };
|
|
44
|
+
case "jpg":
|
|
45
|
+
return { format: "JPG", constraint, useAbsoluteBounds };
|
|
46
|
+
case "png":
|
|
47
|
+
return { format: "PNG", constraint, useAbsoluteBounds };
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const shot = async (n: SceneNode, options: ExportOptions): Promise<ScreenShot> => ({
|
|
52
|
+
name: n.name,
|
|
53
|
+
id: n.id,
|
|
54
|
+
data: figma.base64Encode(await n.exportAsync(settingsOf(options))),
|
|
55
|
+
format: options.format ?? "png",
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
function currentUser(): ActiveUserRef | null {
|
|
59
|
+
try {
|
|
60
|
+
const u = figma.currentUser;
|
|
61
|
+
|
|
62
|
+
return u ? { id: u.id, name: u.name, sessionId: u.sessionId } : null;
|
|
63
|
+
} catch {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Other active sessions in the file, or null where the editor does not say: the typings document `activeUsers` for FigJam only. */
|
|
69
|
+
function users(me: ActiveUserRef | null) {
|
|
70
|
+
try {
|
|
71
|
+
const active = figma.activeUsers;
|
|
72
|
+
|
|
73
|
+
return otherUsers(me, active ? active.map((u) => ({ id: u.id, name: u.name, sessionId: u.sessionId })) : null);
|
|
74
|
+
} catch {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** What the open editor can do, by its type and the API it exposes. Dev Mode reads only. */
|
|
80
|
+
function capabilitiesOf(): EditorCapabilities {
|
|
81
|
+
const editor = figma.editorType;
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
write: editor !== "dev",
|
|
85
|
+
variables: "variables" in figma,
|
|
86
|
+
components: editor === "figma" && "createComponent" in figma,
|
|
87
|
+
slides: editor === "slides" && "createSlide" in figma,
|
|
88
|
+
figjam: editor === "figjam" && "createSticky" in figma,
|
|
89
|
+
devMode: editor === "dev",
|
|
90
|
+
buzz: editor === "buzz" && "buzz" in figma,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** `includeOwners` lists what figdriver built, which loads every page, so only commands that report it ask. */
|
|
95
|
+
async function status(page: string | null = null, missing = false, includeOwners = true): Promise<TabStatus> {
|
|
96
|
+
let absent: string | null = null;
|
|
97
|
+
|
|
98
|
+
if (page !== null) {
|
|
99
|
+
const target = resolvePage(page, missing);
|
|
100
|
+
|
|
101
|
+
if (!target) absent = page;
|
|
102
|
+
else if (target !== figma.currentPage) await figma.setCurrentPageAsync(target);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const owners = includeOwners ? [...new Set((await ownedRoots(null)).map(ownerOf))] : [];
|
|
106
|
+
const me = currentUser();
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
file: figma.root.name,
|
|
110
|
+
editor: figma.editorType,
|
|
111
|
+
page: { name: figma.currentPage.name, id: figma.currentPage.id },
|
|
112
|
+
missing: absent,
|
|
113
|
+
me: me?.name ?? null,
|
|
114
|
+
users: users(me),
|
|
115
|
+
owners,
|
|
116
|
+
pageCount: figma.root.children.length,
|
|
117
|
+
capabilities: capabilitiesOf(),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function audit(design: string | null, rules: AuditRules = "run"): Promise<OwnedAudit> {
|
|
122
|
+
const roots = await ownedRoots(design);
|
|
123
|
+
|
|
124
|
+
return { owners: [...new Set(roots.map(ownerOf))], ...(await auditOf(roots, rules)) };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** A page's top-level layers audited with `rules`: hand-authored pages included, not only what figdriver built. */
|
|
128
|
+
async function auditPage(page: string, rules: AuditRules): Promise<PageAudit> {
|
|
129
|
+
const target = resolvePage(page);
|
|
130
|
+
await target.loadAsync();
|
|
131
|
+
const roots = [...target.children];
|
|
132
|
+
|
|
133
|
+
return {
|
|
134
|
+
page: { name: target.name, id: target.id },
|
|
135
|
+
layers: roots.length,
|
|
136
|
+
owners: [...new Set(roots.flatMap((n) => ownerOf(n) || []))],
|
|
137
|
+
...(await auditOf(roots, rules)),
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function screens(design: string | null, options: ExportOptions): Promise<ScreenShot[]> {
|
|
142
|
+
const out: ScreenShot[] = [];
|
|
143
|
+
|
|
144
|
+
for (const root of await ownedRoots(design)) {
|
|
145
|
+
if (root.type !== "SECTION") continue;
|
|
146
|
+
|
|
147
|
+
for (const frame of root.children) {
|
|
148
|
+
if (frame.type === "FRAME")
|
|
149
|
+
out.push({ ...(await shot(frame, options)), name: design ? frame.name : `${ownerOf(root)} ${frame.name}` });
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return out;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The screens of a page: Slides in the file's slide grid order; elsewhere top-level frames and slides, the frames in each section, and in
|
|
158
|
+
* FigJam a section that holds no frame as itself.
|
|
159
|
+
*/
|
|
160
|
+
function screensOf(page: PageNode): SceneNode[] {
|
|
161
|
+
if (figma.editorType === "slides" && "getSlideGrid" in figma) return figma.getSlideGrid().flat();
|
|
162
|
+
|
|
163
|
+
return page.children.flatMap((child): SceneNode[] => {
|
|
164
|
+
if (child.type === "FRAME" || child.type === "SLIDE") return [child];
|
|
165
|
+
|
|
166
|
+
if (child.type !== "SECTION") return [];
|
|
167
|
+
const frames = child.children.filter((n) => n.type === "FRAME");
|
|
168
|
+
|
|
169
|
+
return frames.length || figma.editorType !== "figjam" ? frames : [child];
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async function pageFrames(page: string | null, options: ExportOptions): Promise<PageFrames> {
|
|
174
|
+
const target = page === null ? figma.currentPage : resolvePage(page);
|
|
175
|
+
await target.loadAsync();
|
|
176
|
+
const out: ScreenShot[] = [];
|
|
177
|
+
|
|
178
|
+
for (const n of screensOf(target)) out.push(await shot(n, options));
|
|
179
|
+
|
|
180
|
+
return { page: { name: target.name, id: target.id }, shots: out };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async function nodes(ids: string[], options: ExportOptions, selection = false): Promise<ScreenShot[]> {
|
|
184
|
+
const targets: BaseNode[] = selection ? [...figma.currentPage.selection] : [];
|
|
185
|
+
|
|
186
|
+
if (selection && !targets.length) throw new Error("Nothing is selected in the open file.");
|
|
187
|
+
|
|
188
|
+
for (const id of selection ? [] : ids) {
|
|
189
|
+
const n = await figma.getNodeByIdAsync(id);
|
|
190
|
+
|
|
191
|
+
if (!n || !("exportAsync" in n) || n.type === "PAGE") throw new Error(`No exportable node with id ${id}`);
|
|
192
|
+
targets.push(n);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const out: ScreenShot[] = [];
|
|
196
|
+
|
|
197
|
+
for (const n of targets) {
|
|
198
|
+
if (!("exportAsync" in n) || n.type === "PAGE")
|
|
199
|
+
throw new Error(`Selected node "${n.name}" (${n.id}) is not exportable.`);
|
|
200
|
+
out.push(await shot(n, options));
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return out;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async function sections(ids: string[]): Promise<SectionShot[]> {
|
|
207
|
+
const out: SectionShot[] = [];
|
|
208
|
+
|
|
209
|
+
for (const id of ids) {
|
|
210
|
+
const root = await figma.getNodeByIdAsync(id);
|
|
211
|
+
|
|
212
|
+
if (!root || root.type === "DOCUMENT" || root.type === "PAGE" || !("findAll" in root) || root.removed)
|
|
213
|
+
throw new Error(`The build has no node ${id} any more: a later run or a person removed it.`);
|
|
214
|
+
|
|
215
|
+
for (const n of root.findAll((d) => d.type === "INSTANCE" && !!read(d, "box"))) {
|
|
216
|
+
out.push({
|
|
217
|
+
name: n.name,
|
|
218
|
+
id: n.id,
|
|
219
|
+
box: JSON.parse(read(n, "box")),
|
|
220
|
+
bg: read(n, "bg") || undefined,
|
|
221
|
+
// Layout bounds, so a drop shadow painting past the section never reads as a size or pixel difference.
|
|
222
|
+
png: (await shot(n, { bounds: "layout" })).data,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return out;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
async function pageLayers(page: string, depth: number, options?: InspectOptions): Promise<PageLayers> {
|
|
231
|
+
const target = resolvePage(page);
|
|
232
|
+
|
|
233
|
+
return { page: { name: target.name, id: target.id }, layers: await layers([target.id], false, depth, options) };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const tree = async () => treeOf(await ownedRoots(null));
|
|
237
|
+
|
|
238
|
+
const api: InspectApi = {
|
|
239
|
+
status,
|
|
240
|
+
audit,
|
|
241
|
+
auditPage,
|
|
242
|
+
screens,
|
|
243
|
+
pageFrames,
|
|
244
|
+
nodes,
|
|
245
|
+
sections,
|
|
246
|
+
tree,
|
|
247
|
+
inventory,
|
|
248
|
+
layers,
|
|
249
|
+
pageLayers,
|
|
250
|
+
find: findNodes,
|
|
251
|
+
importTokens,
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
Object.assign(globalThis, { figdriver: api, fd });
|