-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathexec-mode.ts
More file actions
40 lines (32 loc) · 1.26 KB
/
exec-mode.ts
File metadata and controls
40 lines (32 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { FigmaService } from "./services/figma.js";
import { simplifyRawFigmaObject, allExtractors } from "./extractors/index.js";
import { parseFigmaUrl } from "./utils/url-parser.js";
import type { FigmaAuthOptions } from "./services/figma.js";
import yaml from "js-yaml";
/**
* Execute a single Figma data fetch and output to stdout, then exit.
* This is a non-server mode for one-off data retrieval.
*/
export async function executeOnce(
figmaUrl: string,
authOptions: FigmaAuthOptions,
outputFormat: "yaml" | "json",
): Promise<void> {
const { fileKey, nodeId: rawNodeId } = parseFigmaUrl(figmaUrl);
// Replace - with : in nodeId for API query Figma API expects
const nodeId = rawNodeId?.replace(/-/g, ":");
const figmaService = new FigmaService(authOptions);
const rawApiResponse = nodeId
? await figmaService.getRawNode(fileKey, nodeId, null)
: await figmaService.getRawFile(fileKey, null);
const simplifiedDesign = simplifyRawFigmaObject(rawApiResponse, allExtractors);
const { nodes, globalVars, ...metadata } = simplifiedDesign;
const result = {
metadata,
nodes,
globalVars,
};
const formattedResult =
outputFormat === "json" ? JSON.stringify(result, null, 2) : yaml.dump(result);
console.log(formattedResult);
}