@todoforai/figma-api 1.0.1 → 1.0.3
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/package.json +1 -1
- package/src/bridge.ts +33 -2
package/package.json
CHANGED
package/src/bridge.ts
CHANGED
|
@@ -6,8 +6,19 @@
|
|
|
6
6
|
// figma-api draw-* ──POST /cmd──▶ relay ◀──GET /poll── plugin
|
|
7
7
|
// ──POST /result─▶ (draws, returns ids)
|
|
8
8
|
|
|
9
|
+
import { resolve } from "path";
|
|
10
|
+
import { existsSync } from "fs";
|
|
11
|
+
|
|
9
12
|
interface Cmd { id: string; op: string; [k: string]: unknown }
|
|
10
13
|
|
|
14
|
+
/** Absolute path to the bundled plugin/manifest.json, or a clone hint if missing. */
|
|
15
|
+
function manifestPath(): string {
|
|
16
|
+
const p = resolve(import.meta.dir, "..", "plugin", "manifest.json");
|
|
17
|
+
return existsSync(p)
|
|
18
|
+
? p
|
|
19
|
+
: `${p} (missing — clone it: git clone https://github.com/todoforai/figma-api)`;
|
|
20
|
+
}
|
|
21
|
+
|
|
11
22
|
const queue: Cmd[] = [];
|
|
12
23
|
const resultWaiters = new Map<string, (v: unknown) => void>();
|
|
13
24
|
const pollWaiters: ((c: Cmd | null) => void)[] = [];
|
|
@@ -78,17 +89,37 @@ export function startBridge(port: number): void {
|
|
|
78
89
|
console.log("Point the plugin at this URL (or a cloudflared tunnel of it), then run draw-* commands.");
|
|
79
90
|
}
|
|
80
91
|
|
|
92
|
+
/** Setup instructions shown only when a canvas-write command can't reach the plugin. */
|
|
93
|
+
const setup = () => `
|
|
94
|
+
Canvas writes (create frames/text/shapes, edit variables off-Enterprise) aren't in
|
|
95
|
+
the Figma REST API — they need the plugin bridge, which the user must install.
|
|
96
|
+
|
|
97
|
+
AGENT: don't do this silently. First tell the user this grants write access to
|
|
98
|
+
their Figma documents and ask if they want to install the bridge plugin. If yes,
|
|
99
|
+
walk them through these steps (the plugin runs in THEIR Figma, you can't do step 2
|
|
100
|
+
for them):
|
|
101
|
+
1. Start the relay (keep running): figma-api bridge
|
|
102
|
+
2. In the Figma DESKTOP app: Plugins → Development → Import plugin from manifest
|
|
103
|
+
→ ${manifestPath()}
|
|
104
|
+
Run it, paste the relay URL (default http://localhost:8917), click Connect.
|
|
105
|
+
3. Retry. Verify the round-trip with: figma-api ping
|
|
106
|
+
Cross-machine: expose the relay via 'cloudflared tunnel --url http://localhost:8917'
|
|
107
|
+
and paste that https URL into the plugin. Browser-only Figma can't load dev plugins.`;
|
|
108
|
+
|
|
81
109
|
/** CLI helper: push a command to a running bridge and return the plugin's result. */
|
|
82
110
|
export async function sendCommand(relay: string, op: string, params: Record<string, unknown>): Promise<void> {
|
|
83
111
|
const res = await fetch(`${relay.replace(/\/$/, "")}/cmd`, {
|
|
84
112
|
method: "POST",
|
|
85
113
|
headers: { "content-type": "application/json" },
|
|
86
114
|
body: JSON.stringify({ op, ...params }),
|
|
87
|
-
}).catch((e) => {
|
|
115
|
+
}).catch((e) => {
|
|
116
|
+
console.error(`Bridge relay not reachable at ${relay} (${e.message}).${setup()}`);
|
|
117
|
+
process.exit(1);
|
|
118
|
+
});
|
|
88
119
|
const data = (await (res as Response).json()) as { result?: { timeout?: boolean; error?: string } };
|
|
89
120
|
const result = data.result;
|
|
90
121
|
if (result?.timeout) {
|
|
91
|
-
console.error(
|
|
122
|
+
console.error(`Timed out waiting for the plugin — the relay is up but no plugin is Connected.${setup()}`);
|
|
92
123
|
process.exit(1);
|
|
93
124
|
}
|
|
94
125
|
console.log(JSON.stringify(result ?? data, null, 2));
|