figma2mobile-local 0.1.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/README.md +288 -0
- package/integrity.json +4 -0
- package/loader.cjs +383 -0
- package/main.obf.cjs +1 -0
- package/package.json +30 -0
- package/plugin/dist/code.js +1 -0
- package/plugin/manifest.json +15 -0
- package/plugin/src/ui.html +682 -0
- package/skills/figma-mobile-implementation/SKILL.md +193 -0
package/README.md
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
# figma2mobile-local
|
|
2
|
+
|
|
3
|
+
A local **MCP server + Figma plugin** that lets an AI agent read, edit, and export
|
|
4
|
+
**mobile UI directly on the Figma canvas**. Import a frame → intermediate representation (IR) →
|
|
5
|
+
edit → render back into Figma → export Android/iOS assets.
|
|
6
|
+
|
|
7
|
+
- **Default transport:** local (`127.0.0.1`) between the server and the Figma plugin.
|
|
8
|
+
- **Optional fallback:** read-only Figma REST API, only if you set `FIGMA_TOKEN`.
|
|
9
|
+
- **Clients:** Claude Code, Cursor, Codex CLI (auto-config), plus Claude Desktop (manual config).
|
|
10
|
+
|
|
11
|
+
> **One command, every OS.** Works identically on **Windows, macOS, and Linux**.
|
|
12
|
+
> Requires **Node.js ≥ 18** and **Figma Desktop** (the Figma *web* app cannot reach `localhost`).
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## Quick start
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npx -y figma2mobile-local@latest install-plugin ./figma2mobile-local
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
This single command:
|
|
23
|
+
1. Unpacks the Figma plugin + the implementation skill into `./figma2mobile-local`.
|
|
24
|
+
2. Registers the MCP server into the agent(s) you pick.
|
|
25
|
+
3. **Prints the exact `manifest.json` path** you import into Figma, plus manual config for every agent.
|
|
26
|
+
|
|
27
|
+
> **Note:** `install-plugin` modifies the selected MCP client's local configuration files
|
|
28
|
+
> (`~/.claude.json`, `~/.cursor/mcp.json`, `~/.codex/config.toml`). Any existing file is backed up
|
|
29
|
+
> (`.bak-<timestamp>`) before it is changed. Re-running is idempotent — no duplicate entries.
|
|
30
|
+
|
|
31
|
+
Roughly what it prints:
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
✓ Plugin + skill extracted → ./figma2mobile-local
|
|
35
|
+
✓ MCP registered: Claude Code → ~/.claude.json (backup: ~/.claude.json.bak-...)
|
|
36
|
+
✓ Import this manifest into Figma Desktop:
|
|
37
|
+
/abs/path/figma2mobile-local/plugin/manifest.json
|
|
38
|
+
|
|
39
|
+
Next: restart your MCP client, then import the manifest into Figma Desktop.
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Then do the two manual steps it prints: restart your agent, and import the plugin into Figma Desktop.
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
## Features
|
|
47
|
+
|
|
48
|
+
- **Read** — import frames/nodes into a compact IR (`ui_import_source`, `ui_import_batch`),
|
|
49
|
+
inspect layout and structure (`ui_read_layout`, `ui_read_ir`, `ui_read_raw`, `ui_get_node`,
|
|
50
|
+
`ui_search_ir`, `ui_read_snapshot`).
|
|
51
|
+
- **Edit & write back** — mutate the IR and render it onto the real canvas
|
|
52
|
+
(`ui_update_ir`, `ui_render_to_figma`), validate structure before writing (`ui_validate`),
|
|
53
|
+
diff two IRs (`ui_diff_ir`), trace a node's lineage (`ui_trace`).
|
|
54
|
+
- **Export assets** — PNG / Android VectorDrawable / PDF for Android & iOS
|
|
55
|
+
(`ui_export_assets`), plus reference images for visual checks (`ui_export_reference_image`).
|
|
56
|
+
- **Visual verification** — capture a running device screen and compare it against the design
|
|
57
|
+
(`ui_capture_device_screen`, `ui_compare_images`) to prove the built app matches Figma.
|
|
58
|
+
- **Design tokens** — extract and report colors/spacing/radii usage (`ui_token_report`).
|
|
59
|
+
- **Observability** — a local dashboard (opens automatically) plus `ui_metrics`,
|
|
60
|
+
`ui_cache_status`, `ui_cache_clear` for token savings, cache hit/miss, and Figma API usage.
|
|
61
|
+
- **Self-documenting** — call `ui_docs` for the full tool reference and decision matrix.
|
|
62
|
+
- **Status first** — always call `ui_status` to see connection, session, and write permission.
|
|
63
|
+
|
|
64
|
+
All tools are prefixed `ui_` (**23 available** in this release). Run `ui_docs` for the
|
|
65
|
+
authoritative, always-current list.
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## Installation (3 steps, any OS)
|
|
70
|
+
|
|
71
|
+
### Step 1 — Install Node.js ≥ 18 (if needed)
|
|
72
|
+
|
|
73
|
+
Check with `node -v`. If it is missing or below 18:
|
|
74
|
+
|
|
75
|
+
| OS | How to install |
|
|
76
|
+
|---|---|
|
|
77
|
+
| **Windows** | Download the LTS installer from https://nodejs.org and run the `.msi`, or `winget install OpenJS.NodeJS.LTS` |
|
|
78
|
+
| **macOS** | Download the LTS installer from https://nodejs.org, or `brew install node` |
|
|
79
|
+
| **Linux** | `sudo apt install nodejs npm` (Debian/Ubuntu), or via [nvm](https://github.com/nvm-sh/nvm): `nvm install --lts` |
|
|
80
|
+
|
|
81
|
+
### Step 2 — Unpack the plugin & register the MCP server
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
# Windows (PowerShell / CMD), macOS, Linux — identical
|
|
85
|
+
npx -y figma2mobile-local@latest install-plugin ./figma2mobile-local
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Run in an interactive terminal and it shows an **agent picker** (exactly one prompt):
|
|
89
|
+
|
|
90
|
+
```
|
|
91
|
+
Chon agent de dang ky MCP (tu ghi config, co backup):
|
|
92
|
+
1. Claude Code
|
|
93
|
+
2. Cursor
|
|
94
|
+
3. Codex CLI
|
|
95
|
+
a. Tat ca
|
|
96
|
+
Nhap so (vd 1,3 hoac 'a', Enter = tat ca):
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
After you choose, it **merges the MCP config into each agent's own file** (creating a
|
|
100
|
+
`.bak-<timestamp>` backup if the file already exists), copies the skill, then prints the manual
|
|
101
|
+
config and the Figma import path.
|
|
102
|
+
|
|
103
|
+
**No prompt (CI / scripting)** — pass flags:
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
npx -y figma2mobile-local@latest install-plugin --agent claude,cursor,codex ./figma2mobile-local
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Auto-registrable agents: `claude`, `cursor`, `codex`. Re-running is idempotent (already-present
|
|
110
|
+
entries are updated or left untouched, never duplicated).
|
|
111
|
+
|
|
112
|
+
### Step 3 — Import the plugin into Figma Desktop
|
|
113
|
+
|
|
114
|
+
The command from Step 2 prints the **absolute path to `manifest.json`** — use it here:
|
|
115
|
+
|
|
116
|
+
1. Open **Figma Desktop** (not Figma web — web cannot reach `localhost`).
|
|
117
|
+
2. Menu → **Plugins → Development → Import plugin from manifest…**
|
|
118
|
+
3. Select **`figma2mobile-local/plugin/manifest.json`** (the exact path was printed by Step 2).
|
|
119
|
+
4. Run **Plugins → Development → Figma2Mobile Local**.
|
|
120
|
+
5. Restart your MCP client so it loads the new server → the agent now sees the `ui_*` tools.
|
|
121
|
+
|
|
122
|
+
Done. Select a frame in Figma and ask the agent to "import this frame".
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## Manual installation (any agent, or to double-check)
|
|
127
|
+
|
|
128
|
+
The `install-plugin` command always prints this, but here it is for reference. **The MCP entry
|
|
129
|
+
differs on Windows** (see the note below).
|
|
130
|
+
|
|
131
|
+
**Auto-registered by the installer:** Claude Code, Cursor, Codex CLI.
|
|
132
|
+
**Manual only:** Claude Desktop (paste the JSON below into its config file — see the table).
|
|
133
|
+
|
|
134
|
+
### macOS / Linux — MCP config JSON
|
|
135
|
+
|
|
136
|
+
For Claude Code, Cursor, and Claude Desktop:
|
|
137
|
+
|
|
138
|
+
```json
|
|
139
|
+
{
|
|
140
|
+
"mcpServers": {
|
|
141
|
+
"figma2mobile-local": {
|
|
142
|
+
"command": "npx",
|
|
143
|
+
"args": ["-y", "figma2mobile-local"]
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Windows — MCP config JSON
|
|
150
|
+
|
|
151
|
+
On Windows, `npx` resolves to `npx.cmd`, which many MCP clients cannot spawn without a shell.
|
|
152
|
+
Wrap it with `cmd /c`:
|
|
153
|
+
|
|
154
|
+
```json
|
|
155
|
+
{
|
|
156
|
+
"mcpServers": {
|
|
157
|
+
"figma2mobile-local": {
|
|
158
|
+
"command": "cmd",
|
|
159
|
+
"args": ["/c", "npx", "-y", "figma2mobile-local"]
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
> The `install-plugin` command detects Windows and writes this variant automatically.
|
|
166
|
+
|
|
167
|
+
### Codex CLI — TOML (`~/.codex/config.toml`)
|
|
168
|
+
|
|
169
|
+
```toml
|
|
170
|
+
# macOS / Linux
|
|
171
|
+
[mcp_servers.figma2mobile-local]
|
|
172
|
+
command = "npx"
|
|
173
|
+
args = ["-y", "figma2mobile-local"]
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
```toml
|
|
177
|
+
# Windows
|
|
178
|
+
[mcp_servers.figma2mobile-local]
|
|
179
|
+
command = "cmd"
|
|
180
|
+
args = ["/c", "npx", "-y", "figma2mobile-local"]
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### Config file locations per agent
|
|
184
|
+
|
|
185
|
+
| Agent | Config file | Notes |
|
|
186
|
+
|---|---|---|
|
|
187
|
+
| **Claude Code** | `~/.claude.json` (`mcpServers`) | or `claude mcp add figma2mobile-local -- npx -y figma2mobile-local` |
|
|
188
|
+
| **Cursor** | `~/.cursor/mcp.json` (global) or `.cursor/mcp.json` (per-project) | JSON `mcpServers` |
|
|
189
|
+
| **Codex CLI** | `~/.codex/config.toml` | `[mcp_servers.figma2mobile-local]` block |
|
|
190
|
+
| **Claude Desktop** | Win `%APPDATA%\Claude\claude_desktop_config.json` · macOS `~/Library/Application Support/Claude/claude_desktop_config.json` · Linux `~/.config/Claude/claude_desktop_config.json` | JSON `mcpServers` |
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## Running with npm / npx
|
|
195
|
+
|
|
196
|
+
### As an end user
|
|
197
|
+
|
|
198
|
+
```bash
|
|
199
|
+
# Run once, no global install (recommended — always current):
|
|
200
|
+
npx -y figma2mobile-local@latest
|
|
201
|
+
|
|
202
|
+
# Or install globally:
|
|
203
|
+
npm install -g figma2mobile-local@latest
|
|
204
|
+
figma2mobile-local install-plugin ./figma2mobile-local # CLI name is unscoped
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
> **Interactive / everyday use:** `@latest` (a stale npx cache won't have the newest tools or skill).
|
|
208
|
+
> **CI / reproducible environments:** pin an exact version instead, e.g.
|
|
209
|
+
> `npx -y figma2mobile-local@0.2.1`.
|
|
210
|
+
|
|
211
|
+
### Subcommands
|
|
212
|
+
|
|
213
|
+
| Command | What it does |
|
|
214
|
+
|---|---|
|
|
215
|
+
| `figma2mobile-local` | Boot the MCP server on stdio (this is what your agent runs). |
|
|
216
|
+
| `figma2mobile-local install-plugin [dir]` | Unpack plugin + skill into `dir` (default `./figma2mobile-local`) and register agents. |
|
|
217
|
+
| `install-plugin --agent claude,cursor,codex` | Register specific agents without the interactive menu. |
|
|
218
|
+
| `install-plugin --yes` | Non-interactive: unpack only, does **not** modify any agent config. |
|
|
219
|
+
|
|
220
|
+
---
|
|
221
|
+
|
|
222
|
+
## How to use it
|
|
223
|
+
|
|
224
|
+
Inside your MCP client:
|
|
225
|
+
|
|
226
|
+
1. `ui_status` — check plugin connection, active session, and write permission.
|
|
227
|
+
2. Select a frame in Figma → `ui_import_source` (or `ui_import_batch` for several frames).
|
|
228
|
+
3. `ui_read_layout` / `ui_read_ir` — read the UI structure.
|
|
229
|
+
4. Edit via `ui_update_ir` → `ui_render_to_figma` — write changes back to the canvas.
|
|
230
|
+
5. `ui_export_assets` — export PNG / VectorDrawable / PDF for Android & iOS.
|
|
231
|
+
|
|
232
|
+
Call `ui_docs` for the full tool reference. The observability dashboard (token savings, cache,
|
|
233
|
+
Figma API calls) opens automatically when the server runs.
|
|
234
|
+
|
|
235
|
+
### Optional: Figma API token
|
|
236
|
+
|
|
237
|
+
If you cannot open the plugin, the server can read (read-only) through the Figma REST API — this
|
|
238
|
+
needs a token. Paste it into the **"Figma API token"** panel on the dashboard, or export
|
|
239
|
+
`FIGMA_TOKEN`. The live plugin is always the preferred source and the only write path.
|
|
240
|
+
|
|
241
|
+
---
|
|
242
|
+
|
|
243
|
+
## Privacy & network model
|
|
244
|
+
|
|
245
|
+
Local-first. By default the server communicates with the Figma plugin over `127.0.0.1`; no design
|
|
246
|
+
data is sent to third-party servers by the local bridge. Design data leaves the machine only if you
|
|
247
|
+
explicitly set `FIGMA_TOKEN`, in which case read-only requests may be sent to Figma's REST API.
|
|
248
|
+
|
|
249
|
+
| Path | Default |
|
|
250
|
+
|---|---|
|
|
251
|
+
| Server ↔ Figma plugin | local bridge on `127.0.0.1` |
|
|
252
|
+
| Plugin write-back (render, export) | local bridge only |
|
|
253
|
+
| Figma REST API | **disabled** unless `FIGMA_TOKEN` is set — read-only |
|
|
254
|
+
| Remote MCP endpoint | not required |
|
|
255
|
+
| Agent config files | backed up before the installer modifies them |
|
|
256
|
+
|
|
257
|
+
---
|
|
258
|
+
|
|
259
|
+
## Distribution
|
|
260
|
+
|
|
261
|
+
The published npm package is a **production distribution**: it ships a protected runtime build and
|
|
262
|
+
the compiled Figma plugin, and does **not** include the original project source. This raises the
|
|
263
|
+
cost of casual inspection and redistribution — it is not cryptographic secrecy, since client-side
|
|
264
|
+
code must ultimately run on the user's machine.
|
|
265
|
+
|
|
266
|
+
---
|
|
267
|
+
|
|
268
|
+
## Troubleshooting
|
|
269
|
+
|
|
270
|
+
| Symptom | Fix |
|
|
271
|
+
|---|---|
|
|
272
|
+
| `node: command not found` | Node is not installed — see Step 1. |
|
|
273
|
+
| Agent does not see the `ui_*` tools | Fully restart the MCP client (not just reconnect). |
|
|
274
|
+
| On Windows the server never starts / spawn error | Use the `cmd /c` MCP entry (Windows section above). `install-plugin` writes it automatically. |
|
|
275
|
+
| `ui_status` reports `plugin_not_open` | Open Figma Desktop and run the plugin (Step 3). |
|
|
276
|
+
| `BRIDGE_UNAVAILABLE` | Transient — call again once; if it persists, fully quit and reopen the MCP client. |
|
|
277
|
+
| First run is slow | First run does a one-time setup; subsequent runs start instantly. |
|
|
278
|
+
|
|
279
|
+
---
|
|
280
|
+
|
|
281
|
+
## License
|
|
282
|
+
|
|
283
|
+
**UNLICENSED — proprietary.** No permission is granted to copy, modify, redistribute, or create
|
|
284
|
+
derivative works except as explicitly authorized by the author.
|
|
285
|
+
|
|
286
|
+
---
|
|
287
|
+
|
|
288
|
+
Node ≥ 18. Build: 0c8cdfc-20260730225248
|
package/integrity.json
ADDED
package/loader.cjs
ADDED
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Loader cua goi npx figma2mobile-local. Vai KB, KHONG obfuscate.
|
|
3
|
+
//
|
|
4
|
+
// Vi sao compile-on-first-run thay vi ship .jsc san: V8 cachedData khoa theo DUNG
|
|
5
|
+
// ban V8 (patch), khong phai node major. Mot .jsc build san chi chay dung tren V8
|
|
6
|
+
// do -> khong the doan V8 cua user. Nen: ship bundle DA OBFUSCATE, lan chay dau
|
|
7
|
+
// compile ra .jsc theo dung node cua user, cache lai; lan sau chay tu cache tuc
|
|
8
|
+
// thi. Loi compile -> fallback chay thang obfuscated JS (robust > thuan bytecode).
|
|
9
|
+
//
|
|
10
|
+
// Cac lop: fingerprint (BUILD_ID) + integrity (sha256 bundle) + bytecode runtime.
|
|
11
|
+
// Tren dia goi la obfuscated JS (khong phai source goc); bytecode la lop runtime.
|
|
12
|
+
"use strict";
|
|
13
|
+
const fs = require("node:fs");
|
|
14
|
+
const os = require("node:os");
|
|
15
|
+
const path = require("node:path");
|
|
16
|
+
const crypto = require("node:crypto");
|
|
17
|
+
const readline = require("node:readline");
|
|
18
|
+
|
|
19
|
+
const BUILD_ID = "0c8cdfc-20260730225248"; // pack-npx.sh thay bang build id that
|
|
20
|
+
const ROOT = __dirname;
|
|
21
|
+
const BUNDLE = path.join(ROOT, "main.obf.cjs");
|
|
22
|
+
|
|
23
|
+
const die = (msg) => {
|
|
24
|
+
process.stderr.write(`[figma2mobile-local] ${msg}\n`);
|
|
25
|
+
process.exit(1);
|
|
26
|
+
};
|
|
27
|
+
const sha256 = (file) =>
|
|
28
|
+
crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex");
|
|
29
|
+
|
|
30
|
+
function readManifest() {
|
|
31
|
+
const p = path.join(ROOT, "integrity.json");
|
|
32
|
+
if (!fs.existsSync(p)) die("integrity.json missing — goi hong, cai lai.");
|
|
33
|
+
let m;
|
|
34
|
+
try {
|
|
35
|
+
m = JSON.parse(fs.readFileSync(p, "utf8"));
|
|
36
|
+
} catch {
|
|
37
|
+
die("integrity.json khong doc duoc — goi hong.");
|
|
38
|
+
}
|
|
39
|
+
// Fingerprint: loader va bundle phai cung build (chong tro doi).
|
|
40
|
+
if (m.buildId !== BUILD_ID)
|
|
41
|
+
die(
|
|
42
|
+
`build mismatch: loader=${BUILD_ID} bundle=${m.buildId}. Loader va bundle khac ban build — khong chay.`,
|
|
43
|
+
);
|
|
44
|
+
// Integrity: bundle obfuscated khong bi sua.
|
|
45
|
+
if (!fs.existsSync(BUNDLE)) die("main.obf.cjs missing — goi hong.");
|
|
46
|
+
const got = sha256(BUNDLE);
|
|
47
|
+
if (m.bundleSha256 && got !== m.bundleSha256)
|
|
48
|
+
die(
|
|
49
|
+
`integrity fail: main.obf.cjs bi sua (${got.slice(0, 12)} != ${String(m.bundleSha256).slice(0, 12)}).`,
|
|
50
|
+
);
|
|
51
|
+
return m;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Cache .jsc theo (V8 version + buildId): patch V8 doi -> key doi -> compile lai.
|
|
55
|
+
// Nam o ~/.figma2mobile-local/bytecode de ben qua cac lan npx (npx giai nen lai goi
|
|
56
|
+
// moi lan, nhung cache compile giu nguyen -> chi cham lan dau tien).
|
|
57
|
+
function cachedJscPath() {
|
|
58
|
+
const key = `${process.versions.v8}-${BUILD_ID}`.replace(/[^\w.-]/g, "_");
|
|
59
|
+
const dir = path.join(os.homedir(), ".figma2mobile-local", "bytecode", key);
|
|
60
|
+
return { dir, file: path.join(dir, "main.jsc") };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function loadViaBytecode() {
|
|
64
|
+
let bytenode;
|
|
65
|
+
try {
|
|
66
|
+
bytenode = require("bytenode");
|
|
67
|
+
} catch {
|
|
68
|
+
return false; // khong co bytenode -> fallback JS
|
|
69
|
+
}
|
|
70
|
+
const { dir, file } = cachedJscPath();
|
|
71
|
+
try {
|
|
72
|
+
if (!fs.existsSync(file)) {
|
|
73
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
74
|
+
// Compile theo DUNG node dang chay -> cachedData chac chan hop le.
|
|
75
|
+
bytenode.compileFile({ filename: BUNDLE, output: file });
|
|
76
|
+
}
|
|
77
|
+
// .jsc nam o ~/.figma2mobile-local (ngoai goi) -> require('zod'...) walk up
|
|
78
|
+
// KHONG thay node_modules cua goi. Bom node_modules cua goi vao NODE_PATH
|
|
79
|
+
// de deps (zod, sdk, pngjs, svg2vectordrawable) resolve duoc tu .jsc.
|
|
80
|
+
const nm = path.dirname(path.dirname(require.resolve("zod/package.json")));
|
|
81
|
+
process.env.NODE_PATH = process.env.NODE_PATH
|
|
82
|
+
? `${nm}${path.delimiter}${process.env.NODE_PATH}`
|
|
83
|
+
: nm;
|
|
84
|
+
require("node:module").Module._initPaths();
|
|
85
|
+
require(file);
|
|
86
|
+
return true;
|
|
87
|
+
} catch (err) {
|
|
88
|
+
// Compile/load .jsc hong (V8 la, quyen ghi, ...) -> fallback chay JS.
|
|
89
|
+
process.stderr.write(
|
|
90
|
+
`[figma2mobile-local] bytecode unavailable (${
|
|
91
|
+
err?.message ? err.message : err
|
|
92
|
+
}); chay obfuscated JS truc tiep.\n`,
|
|
93
|
+
);
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// ── Subcommand: install-plugin ──────────────────────────────────────────────
|
|
99
|
+
// Menu kieu android-gv: chon agent (Claude/Cursor/Codex) -> tu ghi (merge) MCP
|
|
100
|
+
// config vao dung file tung agent + LUON in huong dan thu cong de doi chieu.
|
|
101
|
+
// Antigravity: bo (chua chac path) — chi huong dan thu cong chung.
|
|
102
|
+
|
|
103
|
+
const MCP_NAME = "figma2mobile-local";
|
|
104
|
+
// Windows: MCP client spawn `npx` (that ra npx.cmd) KHONG shell -> ENOENT.
|
|
105
|
+
// Chuan Windows = `cmd /c npx ...`. Loader chay TREN may user -> detect dung.
|
|
106
|
+
const IS_WIN = process.platform === "win32";
|
|
107
|
+
const MCP_ENTRY = IS_WIN
|
|
108
|
+
? {
|
|
109
|
+
command: "cmd",
|
|
110
|
+
args: ["/c", "npx", "-y", "figma2mobile-local"],
|
|
111
|
+
}
|
|
112
|
+
: { command: "npx", args: ["-y", "figma2mobile-local"] };
|
|
113
|
+
const home = os.homedir();
|
|
114
|
+
|
|
115
|
+
// Mo ta tung agent: file config + cach ghi. kind=json -> merge mcpServers;
|
|
116
|
+
// kind=toml -> chen block neu chua co (khong parse TOML, tranh them dependency).
|
|
117
|
+
const AGENTS = [
|
|
118
|
+
{
|
|
119
|
+
id: "claude",
|
|
120
|
+
label: "Claude Code",
|
|
121
|
+
kind: "json",
|
|
122
|
+
file: path.join(home, ".claude.json"),
|
|
123
|
+
key: "mcpServers",
|
|
124
|
+
skills: path.join(home, ".claude", "skills"),
|
|
125
|
+
manual: `File: ~/.claude.json (hoac lenh: claude mcp add ${MCP_NAME} -- npx -y figma2mobile-local)`,
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
id: "cursor",
|
|
129
|
+
label: "Cursor",
|
|
130
|
+
kind: "json",
|
|
131
|
+
file: path.join(home, ".cursor", "mcp.json"),
|
|
132
|
+
key: "mcpServers",
|
|
133
|
+
skills: null,
|
|
134
|
+
manual:
|
|
135
|
+
"File global: ~/.cursor/mcp.json (hoac per-project: .cursor/mcp.json)",
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
id: "codex",
|
|
139
|
+
label: "Codex CLI",
|
|
140
|
+
kind: "toml",
|
|
141
|
+
file: path.join(home, ".codex", "config.toml"),
|
|
142
|
+
skills: null,
|
|
143
|
+
manual:
|
|
144
|
+
"File: ~/.codex/config.toml (block [mcp_servers.figma2mobile-local])",
|
|
145
|
+
},
|
|
146
|
+
];
|
|
147
|
+
|
|
148
|
+
const copyDir = (src, dst) => {
|
|
149
|
+
fs.mkdirSync(dst, { recursive: true });
|
|
150
|
+
for (const e of fs.readdirSync(src, { withFileTypes: true })) {
|
|
151
|
+
const s = path.join(src, e.name);
|
|
152
|
+
const d = path.join(dst, e.name);
|
|
153
|
+
if (e.isDirectory()) copyDir(s, d);
|
|
154
|
+
else fs.copyFileSync(s, d);
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const backup = (file) => {
|
|
159
|
+
const b = `${file}.bak-${Date.now()}`;
|
|
160
|
+
fs.copyFileSync(file, b);
|
|
161
|
+
return b;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
// Merge JSON config (Claude/Cursor). Tra ve 'added' | 'updated'.
|
|
165
|
+
function writeJsonMcp(agent) {
|
|
166
|
+
fs.mkdirSync(path.dirname(agent.file), { recursive: true });
|
|
167
|
+
let cfg = {};
|
|
168
|
+
let existed = false;
|
|
169
|
+
if (fs.existsSync(agent.file)) {
|
|
170
|
+
existed = true;
|
|
171
|
+
try {
|
|
172
|
+
cfg = JSON.parse(fs.readFileSync(agent.file, "utf8") || "{}");
|
|
173
|
+
} catch {
|
|
174
|
+
throw new Error(
|
|
175
|
+
`${agent.file} khong phai JSON hop le — khong dam ghi de.`,
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (!cfg[agent.key]) cfg[agent.key] = {};
|
|
180
|
+
const servers = cfg[agent.key];
|
|
181
|
+
const state = servers[MCP_NAME] ? "updated" : "added";
|
|
182
|
+
if (existed) backup(agent.file);
|
|
183
|
+
servers[MCP_NAME] = { ...MCP_ENTRY };
|
|
184
|
+
fs.writeFileSync(agent.file, `${JSON.stringify(cfg, null, 2)}\n`, "utf8");
|
|
185
|
+
return state;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// TOML (Codex): khong parse — neu chua co block thi append; co roi thi bao 'exists'.
|
|
189
|
+
function writeTomlMcp(agent) {
|
|
190
|
+
const block = [
|
|
191
|
+
`[mcp_servers.${MCP_NAME}]`,
|
|
192
|
+
`command = ${JSON.stringify(MCP_ENTRY.command)}`,
|
|
193
|
+
`args = [${MCP_ENTRY.args.map((a) => JSON.stringify(a)).join(", ")}]`,
|
|
194
|
+
"",
|
|
195
|
+
].join("\n");
|
|
196
|
+
fs.mkdirSync(path.dirname(agent.file), { recursive: true });
|
|
197
|
+
let text = "";
|
|
198
|
+
if (fs.existsSync(agent.file)) {
|
|
199
|
+
text = fs.readFileSync(agent.file, "utf8");
|
|
200
|
+
if (text.includes(`[mcp_servers.${MCP_NAME}]`)) return "exists";
|
|
201
|
+
backup(agent.file);
|
|
202
|
+
}
|
|
203
|
+
const sep = text && !text.endsWith("\n") ? "\n\n" : text ? "\n" : "";
|
|
204
|
+
fs.writeFileSync(agent.file, `${text}${sep}${block}`, "utf8");
|
|
205
|
+
return "added";
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const registerAgent = (agent) =>
|
|
209
|
+
agent.kind === "toml" ? writeTomlMcp(agent) : writeJsonMcp(agent);
|
|
210
|
+
|
|
211
|
+
const ask = (q) =>
|
|
212
|
+
new Promise((res) => {
|
|
213
|
+
const rl = readline.createInterface({
|
|
214
|
+
input: process.stdin,
|
|
215
|
+
output: process.stdout,
|
|
216
|
+
});
|
|
217
|
+
rl.question(q, (a) => {
|
|
218
|
+
rl.close();
|
|
219
|
+
res(a.trim());
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
// Parse `--agent claude,cursor` / `--agent=claude` + `--yes` + outdir (positional).
|
|
224
|
+
function parseArgs(args) {
|
|
225
|
+
let agentsCsv = null;
|
|
226
|
+
let yes = false;
|
|
227
|
+
let outDir = null;
|
|
228
|
+
for (let i = 0; i < args.length; i++) {
|
|
229
|
+
const a = args[i];
|
|
230
|
+
if (a === "--yes" || a === "-y") yes = true;
|
|
231
|
+
else if (a === "--agent" || a === "--agents") agentsCsv = args[++i] ?? "";
|
|
232
|
+
else if (a.startsWith("--agent=")) agentsCsv = a.slice(a.indexOf("=") + 1);
|
|
233
|
+
else if (!a.startsWith("-")) outDir = a;
|
|
234
|
+
}
|
|
235
|
+
const ids = agentsCsv
|
|
236
|
+
? agentsCsv
|
|
237
|
+
.split(",")
|
|
238
|
+
.map((s) => s.trim().toLowerCase())
|
|
239
|
+
.filter(Boolean)
|
|
240
|
+
: null;
|
|
241
|
+
return { ids, yes, outDir };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
async function pickAgents() {
|
|
245
|
+
const out = process.stdout.write.bind(process.stdout);
|
|
246
|
+
out("\nChon agent de dang ky MCP (tu ghi config, co backup):\n");
|
|
247
|
+
AGENTS.forEach((a, i) => out(` ${i + 1}. ${a.label}\n`));
|
|
248
|
+
out(" a. Tat ca\n");
|
|
249
|
+
const raw = (
|
|
250
|
+
await ask("Nhap so (vd 1,3 hoac 'a' cho tat ca, Enter = tat ca): ")
|
|
251
|
+
).toLowerCase();
|
|
252
|
+
if (!raw || raw === "a" || raw === "all") return AGENTS.slice();
|
|
253
|
+
const picked = [];
|
|
254
|
+
for (const tok of raw.split(/[,\s]+/).filter(Boolean)) {
|
|
255
|
+
const idx = Number(tok) - 1;
|
|
256
|
+
if (AGENTS[idx] && !picked.includes(AGENTS[idx])) picked.push(AGENTS[idx]);
|
|
257
|
+
else {
|
|
258
|
+
const byId = AGENTS.find((a) => a.id === tok);
|
|
259
|
+
if (byId && !picked.includes(byId)) picked.push(byId);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return picked;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
async function installPlugin(args) {
|
|
266
|
+
const out = process.stdout.write.bind(process.stdout);
|
|
267
|
+
const { ids, yes, outDir } = parseArgs(args);
|
|
268
|
+
const target = path.resolve(outDir || "figma2mobile-local");
|
|
269
|
+
|
|
270
|
+
// 1. Rai plugin + skill ra thu muc.
|
|
271
|
+
const pluginSrc = path.join(ROOT, "plugin");
|
|
272
|
+
if (!fs.existsSync(pluginSrc)) die("thu muc plugin/ missing trong goi.");
|
|
273
|
+
copyDir(pluginSrc, path.join(target, "plugin"));
|
|
274
|
+
const skillSrc = path.join(ROOT, "skills");
|
|
275
|
+
const hasSkill = fs.existsSync(skillSrc);
|
|
276
|
+
if (hasSkill) copyDir(skillSrc, path.join(target, "skills"));
|
|
277
|
+
|
|
278
|
+
// 2. Chon agent: flags -> dung luon; khong flag + TTY -> menu; khong TTY -> bo qua auto.
|
|
279
|
+
let selected = [];
|
|
280
|
+
if (ids) {
|
|
281
|
+
if (ids.includes("all")) selected = AGENTS.slice();
|
|
282
|
+
else {
|
|
283
|
+
for (const id of ids) {
|
|
284
|
+
const a = AGENTS.find((x) => x.id === id);
|
|
285
|
+
if (a) selected.push(a);
|
|
286
|
+
else out(` (bo qua agent khong ho tro: ${id})\n`);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
} else if (process.stdin.isTTY && process.stdout.isTTY && !yes) {
|
|
290
|
+
selected = await pickAgents();
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// 3. Dang ky MCP cho tung agent (tu ghi + backup).
|
|
294
|
+
const results = [];
|
|
295
|
+
for (const agent of selected) {
|
|
296
|
+
try {
|
|
297
|
+
const state = registerAgent(agent);
|
|
298
|
+
// skillSrc chua san thu muc `figma-mobile-implementation/` -> copy thang
|
|
299
|
+
// vao <agent.skills>/ de ra <skills>/figma-mobile-implementation/SKILL.md.
|
|
300
|
+
if (hasSkill && agent.skills) copyDir(skillSrc, agent.skills);
|
|
301
|
+
results.push({ agent, state });
|
|
302
|
+
} catch (err) {
|
|
303
|
+
results.push({
|
|
304
|
+
agent,
|
|
305
|
+
error: err?.message ? err.message : String(err),
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// 4. Bao cao.
|
|
311
|
+
const manifestPath = path.join(target, "plugin", "manifest.json");
|
|
312
|
+
out("\n✓ Plugin + skill da rai ra:\n");
|
|
313
|
+
out(` ${target}\n\n`);
|
|
314
|
+
|
|
315
|
+
if (results.length) {
|
|
316
|
+
out("MCP da dang ky (co backup .bak-<time> neu file da ton tai):\n");
|
|
317
|
+
for (const r of results) {
|
|
318
|
+
if (r.error) out(` ✗ ${r.agent.label}: ${r.error}\n`);
|
|
319
|
+
else {
|
|
320
|
+
const verb =
|
|
321
|
+
r.state === "added"
|
|
322
|
+
? "them"
|
|
323
|
+
: r.state === "updated"
|
|
324
|
+
? "cap nhat"
|
|
325
|
+
: "da co san";
|
|
326
|
+
out(` ✓ ${r.agent.label}: ${verb} -> ${r.agent.file}\n`);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
out("\n Khoi dong lai agent de nap MCP moi.\n\n");
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// 5. Huong dan thu cong (moi agent) — de doi chieu / cai tay.
|
|
333
|
+
// JSON + TOML derive tu MCP_ENTRY -> tren Windows tu hien bien the `cmd /c`.
|
|
334
|
+
const jsonEntry = JSON.stringify(
|
|
335
|
+
{ mcpServers: { [MCP_NAME]: MCP_ENTRY } },
|
|
336
|
+
null,
|
|
337
|
+
2,
|
|
338
|
+
)
|
|
339
|
+
.split("\n")
|
|
340
|
+
.map((l) => ` ${l}`)
|
|
341
|
+
.join("\n");
|
|
342
|
+
const tomlArgs = MCP_ENTRY.args.map((a) => JSON.stringify(a)).join(", ");
|
|
343
|
+
out(
|
|
344
|
+
`Cai thu cong (agent khac, hoac de doi chieu)${IS_WIN ? " [Windows]" : ""}:\n`,
|
|
345
|
+
);
|
|
346
|
+
out(" MCP config JSON:\n");
|
|
347
|
+
out(`${jsonEntry}\n\n`);
|
|
348
|
+
for (const a of AGENTS) out(` ${a.label.padEnd(12)} ${a.manual}\n`);
|
|
349
|
+
out(
|
|
350
|
+
" Claude Desktop File: (macOS) ~/Library/Application Support/Claude/claude_desktop_config.json\n",
|
|
351
|
+
);
|
|
352
|
+
out(
|
|
353
|
+
" (Windows) %APPDATA%\\Claude\\claude_desktop_config.json\n",
|
|
354
|
+
);
|
|
355
|
+
out(
|
|
356
|
+
" (Linux) ~/.config/Claude/claude_desktop_config.json\n",
|
|
357
|
+
);
|
|
358
|
+
out(" Codex TOML block:\n");
|
|
359
|
+
out(` [mcp_servers.${MCP_NAME}]\n`);
|
|
360
|
+
out(` command = ${JSON.stringify(MCP_ENTRY.command)}\n`);
|
|
361
|
+
out(` args = [${tomlArgs}]\n\n`);
|
|
362
|
+
|
|
363
|
+
// 6. Figma import (chung cho moi agent).
|
|
364
|
+
out("Import plugin vao Figma Desktop:\n");
|
|
365
|
+
out(" 1. Mo Figma Desktop (Figma web KHONG goi duoc localhost).\n");
|
|
366
|
+
out(" 2. Menu > Plugins > Development > Import plugin from manifest...\n");
|
|
367
|
+
out(` 3. Chon: ${manifestPath}\n`);
|
|
368
|
+
out(" 4. Chay: Plugins > Development > Figma2Mobile Local\n\n");
|
|
369
|
+
if (hasSkill)
|
|
370
|
+
out(`Skill huong dan layout native: ${path.join(target, "skills")}\n\n`);
|
|
371
|
+
process.exit(0);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// ── Main ────────────────────────────────────────────────────────────────────
|
|
375
|
+
readManifest();
|
|
376
|
+
|
|
377
|
+
if (process.argv[2] === "install-plugin") {
|
|
378
|
+
installPlugin(process.argv.slice(3)).catch((err) =>
|
|
379
|
+
die(err?.message ? err.message : String(err)),
|
|
380
|
+
);
|
|
381
|
+
} else if (!loadViaBytecode()) {
|
|
382
|
+
require(BUNDLE); // fallback: chay obfuscated JS truc tiep
|
|
383
|
+
}
|