@todoforai/figma-api 1.0.2 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/bridge.ts +23 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@todoforai/figma-api",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Figma CLI — full REST API wrapper (files, comments, variables, webhooks…) plus a plugin bridge for canvas writes.",
5
5
  "type": "module",
6
6
  "bin": {
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)[] = [];
@@ -79,13 +90,18 @@ export function startBridge(port: number): void {
79
90
  }
80
91
 
81
92
  /** Setup instructions shown only when a canvas-write command can't reach the plugin. */
82
- const SETUP = `
83
- To run canvas commands (create frames/text/shapes, edit variables off-Enterprise),
84
- the plugin bridge must be set up the Figma REST API can't do these:
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):
85
101
  1. Start the relay (keep running): figma-api bridge
86
102
  2. In the Figma DESKTOP app: Plugins → Development → Import plugin from manifest
87
- the plugin/manifest.json shipped with this package. Run it, paste the relay
88
- URL (default http://localhost:8917), click Connect.
103
+ ${manifestPath()}
104
+ Run it, paste the relay URL (default http://localhost:8917), click Connect.
89
105
  3. Retry. Verify the round-trip with: figma-api ping
90
106
  Cross-machine: expose the relay via 'cloudflared tunnel --url http://localhost:8917'
91
107
  and paste that https URL into the plugin. Browser-only Figma can't load dev plugins.`;
@@ -97,13 +113,13 @@ export async function sendCommand(relay: string, op: string, params: Record<stri
97
113
  headers: { "content-type": "application/json" },
98
114
  body: JSON.stringify({ op, ...params }),
99
115
  }).catch((e) => {
100
- console.error(`Bridge relay not reachable at ${relay} (${e.message}).${SETUP}`);
116
+ console.error(`Bridge relay not reachable at ${relay} (${e.message}).${setup()}`);
101
117
  process.exit(1);
102
118
  });
103
119
  const data = (await (res as Response).json()) as { result?: { timeout?: boolean; error?: string } };
104
120
  const result = data.result;
105
121
  if (result?.timeout) {
106
- console.error(`Timed out waiting for the plugin — the relay is up but no plugin is Connected.${SETUP}`);
122
+ console.error(`Timed out waiting for the plugin — the relay is up but no plugin is Connected.${setup()}`);
107
123
  process.exit(1);
108
124
  }
109
125
  console.log(JSON.stringify(result ?? data, null, 2));