figma2mobile-local 0.1.6 → 0.3.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 CHANGED
@@ -287,4 +287,4 @@ malware — either is fine.
287
287
 
288
288
  ---
289
289
 
290
- Node ≥ 18. Build: fba3a8d-20260802112357
290
+ Node ≥ 18. Build: 705f2d6-20261020204357
package/integrity.json CHANGED
@@ -1,4 +1,4 @@
1
1
  {
2
- "buildId": "fba3a8d-20260802112357",
3
- "bundleSha256": "fa43abafc818de40ec314e1758d573034ea1178b1ffff7a1fd0a6b8b54bac841"
2
+ "buildId": "705f2d6-20261020204357",
3
+ "bundleSha256": "965d49c8c4d6e8a95d52d3109c8f9871ca1455899f2dc9c3404cf9c5b2fea05a"
4
4
  }
package/loader.cjs CHANGED
@@ -16,7 +16,7 @@ const path = require("node:path");
16
16
  const crypto = require("node:crypto");
17
17
  const readline = require("node:readline");
18
18
 
19
- const BUILD_ID = "fba3a8d-20260802112357"; // pack-npx.sh thay bang build id that
19
+ const BUILD_ID = "705f2d6-20261020204357"; // pack-npx.sh thay bang build id that
20
20
  const ROOT = __dirname;
21
21
  const BUNDLE = path.join(ROOT, "main.obf.cjs");
22
22
 
@@ -57,10 +57,35 @@ function readManifest() {
57
57
  // moi lan, nhung cache compile giu nguyen -> chi cham lan dau tien).
58
58
  function cachedJscPath() {
59
59
  const key = `${process.versions.v8}-${BUILD_ID}`.replace(/[^\w.-]/g, "_");
60
- const dir = path.join(os.homedir(), ".figma2mobile-local", "bytecode", key);
60
+ // Cung quy tac goc luu tru nhu runtime (mcp-server/src/storage/paths.ts):
61
+ // FIGMA2MOBILE_HOME phai tuyet doi. Sai => bo bytecode cache (de runtime bao
62
+ // INVALID_CONFIG ro rang), khong crash voi loi kho hieu o day.
63
+ const home = process.env.FIGMA2MOBILE_HOME;
64
+ if (home !== undefined && (home.trim() === "" || !path.isAbsolute(home))) {
65
+ return null;
66
+ }
67
+ const root = home ?? path.join(os.homedir(), ".figma2mobile-local");
68
+ const dir = path.join(root, "bytecode", key);
61
69
  return { dir, file: path.join(dir, "main.jsc") };
62
70
  }
63
71
 
72
+ // Ghi .jsc nguyen tu: compile ra file tam roi rename. Hai process npx khoi dong
73
+ // cung luc, hoac mot process bi kill giua chung, khong de lai .jsc cut dau ma
74
+ // lan sau require duoc nhu file "co".
75
+ function ensureCompiled(bytenode, dir, file) {
76
+ if (fs.existsSync(file) && fs.statSync(file).size > 0) return;
77
+ fs.mkdirSync(dir, { recursive: true });
78
+ const tmp = `${file}.tmp-${process.pid}`;
79
+ try {
80
+ // Compile theo DUNG node dang chay -> cachedData chac chan hop le.
81
+ bytenode.compileFile({ filename: BUNDLE, output: tmp });
82
+ if (fs.statSync(tmp).size === 0) throw new Error("empty bytecode output");
83
+ fs.renameSync(tmp, file);
84
+ } finally {
85
+ fs.rmSync(tmp, { force: true });
86
+ }
87
+ }
88
+
64
89
  function loadViaBytecode() {
65
90
  let bytenode;
66
91
  try {
@@ -68,13 +93,11 @@ function loadViaBytecode() {
68
93
  } catch {
69
94
  return false; // khong co bytenode -> fallback JS
70
95
  }
71
- const { dir, file } = cachedJscPath();
96
+ const jsc = cachedJscPath();
97
+ if (!jsc) return false;
98
+ const { dir, file } = jsc;
72
99
  try {
73
- if (!fs.existsSync(file)) {
74
- fs.mkdirSync(dir, { recursive: true });
75
- // Compile theo DUNG node dang chay -> cachedData chac chan hop le.
76
- bytenode.compileFile({ filename: BUNDLE, output: file });
77
- }
100
+ ensureCompiled(bytenode, dir, file);
78
101
  // .jsc nam o ~/.figma2mobile-local (ngoai goi) -> require('zod'...) walk up
79
102
  // KHONG thay node_modules cua goi. Bom node_modules cua goi vao NODE_PATH
80
103
  // de deps (zod, sdk, pngjs, svg2vectordrawable) resolve duoc tu .jsc.
@@ -86,6 +109,15 @@ function loadViaBytecode() {
86
109
  require(file);
87
110
  return true;
88
111
  } catch (err) {
112
+ // Bundle da bat dau chay (main() da dat co) roi moi loi: chay lai bang JS
113
+ // thuan se mo bridge/server lan hai. Loi that, khong phai loi bytecode.
114
+ if (globalThis.__F2M_STARTED) throw err;
115
+ // .jsc hong/khong tuong thich: xoa de lan sau compile lai (tu phuc hoi).
116
+ try {
117
+ fs.rmSync(file, { force: true });
118
+ } catch {
119
+ // khong xoa duoc: lan sau se thu lai
120
+ }
89
121
  // Compile/load .jsc hong (V8 la, quyen ghi, ...) -> fallback chay JS.
90
122
  process.stderr.write(
91
123
  `[figma2mobile-local] bytecode unavailable (${
@@ -102,15 +134,28 @@ function loadViaBytecode() {
102
134
  // Antigravity: bo (chua chac path) — chi huong dan thu cong chung.
103
135
 
104
136
  const MCP_NAME = "figma2mobile-local";
137
+ // Ghim phien ban dang chay: `npx -y figma2mobile-local` khong ghim se tai ban
138
+ // MOI NHAT moi lan, tuc la code khong ai review chay trong client cua user.
139
+ // Doi lai, nang cap = chay lai install-plugin.
140
+ const PKG_SPEC = (() => {
141
+ try {
142
+ const v = JSON.parse(
143
+ fs.readFileSync(path.join(ROOT, "package.json"), "utf8"),
144
+ ).version;
145
+ return v ? `figma2mobile-local@${v}` : "figma2mobile-local";
146
+ } catch {
147
+ return "figma2mobile-local";
148
+ }
149
+ })();
105
150
  // Windows: MCP client spawn `npx` (that ra npx.cmd) KHONG shell -> ENOENT.
106
151
  // Chuan Windows = `cmd /c npx ...`. Loader chay TREN may user -> detect dung.
107
152
  const IS_WIN = process.platform === "win32";
108
153
  const MCP_ENTRY = IS_WIN
109
154
  ? {
110
155
  command: "cmd",
111
- args: ["/c", "npx", "-y", "figma2mobile-local"],
156
+ args: ["/c", "npx", "-y", PKG_SPEC],
112
157
  }
113
- : { command: "npx", args: ["-y", "figma2mobile-local"] };
158
+ : { command: "npx", args: ["-y", PKG_SPEC] };
114
159
  const home = os.homedir();
115
160
 
116
161
  // Mo ta tung agent: file config + cach ghi. kind=json -> merge mcpServers;
@@ -123,7 +168,7 @@ const AGENTS = [
123
168
  file: path.join(home, ".claude.json"),
124
169
  key: "mcpServers",
125
170
  skills: path.join(home, ".claude", "skills"),
126
- manual: `File: ~/.claude.json (or run: claude mcp add ${MCP_NAME} -- npx -y figma2mobile-local)`,
171
+ manual: `File: ~/.claude.json (or run: claude mcp add ${MCP_NAME} -- npx -y ${PKG_SPEC})`,
127
172
  },
128
173
  {
129
174
  id: "cursor",
@@ -155,6 +200,44 @@ const copyDir = (src, dst) => {
155
200
  }
156
201
  };
157
202
 
203
+ // Ghi qua file tam + rename: client dang doc config khong thay file cut dau.
204
+ // Giu mode cua file cu (config agent hay chua token -> thuong 0600); file moi thi
205
+ // owner-only. Mac dinh cua writeFileSync (0666 & umask) se ha quyen khi rename de.
206
+ const writeAtomic = (file, text) => {
207
+ let mode = 0o600;
208
+ try {
209
+ mode = fs.statSync(file).mode & 0o777;
210
+ } catch {
211
+ // file chua ton tai: giu 0600
212
+ }
213
+ const tmp = `${file}.tmp-${process.pid}`;
214
+ try {
215
+ fs.rmSync(tmp, { force: true });
216
+ fs.writeFileSync(tmp, text, { encoding: "utf8", mode, flag: "wx" });
217
+ fs.renameSync(tmp, file);
218
+ } finally {
219
+ fs.rmSync(tmp, { force: true });
220
+ }
221
+ };
222
+
223
+ // Skill cu bi de len phai co ban sao, nhung KHONG de ban sao trong thu muc skills
224
+ // (client se nap ca ban .bak nhu mot skill nua).
225
+ const copySkillsSafely = (src, dst) => {
226
+ const backupRoot = path.join(
227
+ os.homedir(),
228
+ ".figma2mobile-local",
229
+ "backups",
230
+ `skills-${Date.now()}`,
231
+ );
232
+ for (const e of fs.readdirSync(src, { withFileTypes: true })) {
233
+ const existing = path.join(dst, e.name);
234
+ if (fs.existsSync(existing)) {
235
+ copyDir(existing, path.join(backupRoot, e.name));
236
+ }
237
+ }
238
+ copyDir(src, dst);
239
+ };
240
+
158
241
  const backup = (file) => {
159
242
  const b = `${file}.bak-${Date.now()}`;
160
243
  fs.copyFileSync(file, b);
@@ -180,8 +263,10 @@ function writeJsonMcp(agent) {
180
263
  const servers = cfg[agent.key];
181
264
  const state = servers[MCP_NAME] ? "updated" : "added";
182
265
  if (existed) backup(agent.file);
183
- servers[MCP_NAME] = { ...MCP_ENTRY };
184
- fs.writeFileSync(agent.file, `${JSON.stringify(cfg, null, 2)}\n`, "utf8");
266
+ // Giu moi key user da them (env: FIGMA2MOBILE_HOME, FIGMA_TOKEN...); chi ghi de
267
+ // command/args.
268
+ servers[MCP_NAME] = { ...(servers[MCP_NAME] ?? {}), ...MCP_ENTRY };
269
+ writeAtomic(agent.file, `${JSON.stringify(cfg, null, 2)}\n`);
185
270
  return state;
186
271
  }
187
272
 
@@ -201,7 +286,7 @@ function writeTomlMcp(agent) {
201
286
  backup(agent.file);
202
287
  }
203
288
  const sep = text && !text.endsWith("\n") ? "\n\n" : text ? "\n" : "";
204
- fs.writeFileSync(agent.file, `${text}${sep}${block}`, "utf8");
289
+ writeAtomic(agent.file, `${text}${sep}${block}`);
205
290
  return "added";
206
291
  }
207
292
 
@@ -371,7 +456,7 @@ async function installPlugin(args) {
371
456
  const state = registerAgent(agent);
372
457
  // skillSrc already contains `figma-mobile-implementation/` -> copy straight
373
458
  // into <agent.skills>/ giving <skills>/figma-mobile-implementation/SKILL.md.
374
- if (hasSkill && agent.skills) copyDir(skillSrc, agent.skills);
459
+ if (hasSkill && agent.skills) copySkillsSafely(skillSrc, agent.skills);
375
460
  results.push({ agent, state });
376
461
  } catch (err) {
377
462
  results.push({