@hangox/mg-cli 1.0.2 → 1.0.5

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/dist/cli.js CHANGED
@@ -1,15 +1,15 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  // src/cli/index.ts
4
- import { Command as Command8 } from "commander";
4
+ import { Command as Command9 } from "commander";
5
5
 
6
6
  // src/cli/commands/server.ts
7
7
  import { Command } from "commander";
8
8
 
9
9
  // src/server/daemon.ts
10
10
  import { spawn } from "child_process";
11
- import { fileURLToPath } from "url";
12
- import { dirname as dirname3, join as join2 } from "path";
11
+ import { fileURLToPath as fileURLToPath2 } from "url";
12
+ import { dirname as dirname4, join as join3 } from "path";
13
13
 
14
14
  // src/shared/utils.ts
15
15
  import { existsSync, mkdirSync, readFileSync, writeFileSync, unlinkSync } from "fs";
@@ -175,6 +175,152 @@ function extractFileId(input) {
175
175
  }
176
176
  return extractFileIdFromUrl(trimmed);
177
177
  }
178
+ var NUMERIC_FIELDS = [
179
+ "x",
180
+ "y",
181
+ "width",
182
+ "height",
183
+ "rotation",
184
+ "opacity",
185
+ "cornerRadius",
186
+ "topLeftRadius",
187
+ "topRightRadius",
188
+ "bottomLeftRadius",
189
+ "bottomRightRadius",
190
+ "strokeWeight",
191
+ "strokeTopWeight",
192
+ "strokeLeftWeight",
193
+ "strokeBottomWeight",
194
+ "strokeRightWeight",
195
+ "paddingTop",
196
+ "paddingRight",
197
+ "paddingBottom",
198
+ "paddingLeft",
199
+ "itemSpacing",
200
+ "crossAxisSpacing",
201
+ "flexGrow"
202
+ ];
203
+ function roundToOneDecimal(value) {
204
+ return Math.round(value * 10) / 10;
205
+ }
206
+ var NODE_DEFAULTS = {
207
+ // Scene Node 属性
208
+ visible: true,
209
+ isVisible: true,
210
+ isLocked: false,
211
+ // Blend 属性
212
+ opacity: 1,
213
+ blendMode: "NORMAL",
214
+ isMask: false,
215
+ isMaskOutline: false,
216
+ isMaskVisible: false,
217
+ effectStyleId: "",
218
+ // Geometry 属性
219
+ strokeStyle: "SOLID",
220
+ strokeWeight: 0,
221
+ strokeTopWeight: 0,
222
+ strokeLeftWeight: 0,
223
+ strokeBottomWeight: 0,
224
+ strokeRightWeight: 0,
225
+ strokeAlign: "CENTER",
226
+ strokeCap: "NONE",
227
+ strokeJoin: "MITER",
228
+ dashCap: "NONE",
229
+ fillStyleId: "",
230
+ strokeStyleId: "",
231
+ // Corner 属性
232
+ cornerSmooth: 0,
233
+ cornerRadius: 0,
234
+ topLeftRadius: 0,
235
+ topRightRadius: 0,
236
+ bottomLeftRadius: 0,
237
+ bottomRightRadius: 0,
238
+ // Layout 属性
239
+ rotation: 0,
240
+ flexGrow: 0,
241
+ alignSelf: "INHERIT",
242
+ layoutPositioning: "AUTO",
243
+ constrainProportions: false,
244
+ // 容器专属属性 (FrameNode)
245
+ flexMode: "NONE",
246
+ flexWrap: "NO_WRAP",
247
+ itemSpacing: 0,
248
+ crossAxisSpacing: 0,
249
+ paddingTop: 0,
250
+ paddingRight: 0,
251
+ paddingBottom: 0,
252
+ paddingLeft: 0,
253
+ clipsContent: false,
254
+ itemReverseZIndex: false,
255
+ strokesIncludedInLayout: false,
256
+ // 其他属性
257
+ componentPropertyReferences: null
258
+ };
259
+ var EMPTY_ARRAY_FIELDS = [
260
+ "fills",
261
+ "strokes",
262
+ "effects",
263
+ "strokeDashes",
264
+ "exportSettings",
265
+ "reactions",
266
+ "attachedConnectors"
267
+ ];
268
+ function isEmptyArray(value) {
269
+ return Array.isArray(value) && value.length === 0;
270
+ }
271
+ function isEqual(a, b) {
272
+ if (a === b) return true;
273
+ if (a === null || b === null) return a === b;
274
+ if (typeof a !== typeof b) return false;
275
+ return false;
276
+ }
277
+ function trimNodeDefaults(node) {
278
+ const result = {};
279
+ for (const [key, value] of Object.entries(node)) {
280
+ if (key === "children" && Array.isArray(value)) {
281
+ const trimmedChildren = value.map(
282
+ (child) => typeof child === "object" && child !== null ? trimNodeDefaults(child) : child
283
+ );
284
+ if (trimmedChildren.length > 0) {
285
+ result[key] = trimmedChildren;
286
+ }
287
+ continue;
288
+ }
289
+ if (EMPTY_ARRAY_FIELDS.includes(key) && isEmptyArray(value)) {
290
+ continue;
291
+ }
292
+ if (NUMERIC_FIELDS.includes(key) && typeof value === "number") {
293
+ const roundedValue = roundToOneDecimal(value);
294
+ if (key in NODE_DEFAULTS && isEqual(roundedValue, NODE_DEFAULTS[key])) {
295
+ continue;
296
+ }
297
+ result[key] = roundedValue;
298
+ continue;
299
+ }
300
+ if (key in NODE_DEFAULTS) {
301
+ const defaultValue = NODE_DEFAULTS[key];
302
+ if (isEqual(value, defaultValue)) {
303
+ continue;
304
+ }
305
+ }
306
+ result[key] = value;
307
+ }
308
+ return result;
309
+ }
310
+ function extractSpaceInfo(node) {
311
+ const result = {
312
+ id: node.id,
313
+ name: node.name,
314
+ x: roundToOneDecimal(typeof node.x === "number" ? node.x : 0),
315
+ y: roundToOneDecimal(typeof node.y === "number" ? node.y : 0),
316
+ width: roundToOneDecimal(typeof node.width === "number" ? node.width : 0),
317
+ height: roundToOneDecimal(typeof node.height === "number" ? node.height : 0)
318
+ };
319
+ if (node.children && Array.isArray(node.children) && node.children.length > 0) {
320
+ result.children = node.children.map((child) => extractSpaceInfo(child));
321
+ }
322
+ return result;
323
+ }
178
324
 
179
325
  // src/shared/errors.ts
180
326
  var ErrorNames = {
@@ -608,6 +754,57 @@ var RequestHandler = class {
608
754
  }
609
755
  };
610
756
 
757
+ // src/shared/version.ts
758
+ import { readFileSync as readFileSync2, existsSync as existsSync3 } from "fs";
759
+ import { fileURLToPath } from "url";
760
+ import { dirname as dirname3, join as join2 } from "path";
761
+ var cachedVersion = null;
762
+ function getVersion() {
763
+ if (cachedVersion !== null) {
764
+ return cachedVersion;
765
+ }
766
+ try {
767
+ const currentFile = fileURLToPath(import.meta.url);
768
+ const currentDir = dirname3(currentFile);
769
+ const versionFilePaths = [
770
+ join2(currentDir, "..", "VERSION"),
771
+ // dist/xxx.js -> ../VERSION
772
+ join2(currentDir, "..", "..", "VERSION")
773
+ // src/shared/version.ts -> ../../VERSION
774
+ ];
775
+ for (const versionFilePath of versionFilePaths) {
776
+ if (existsSync3(versionFilePath)) {
777
+ const version = readFileSync2(versionFilePath, "utf-8").trim();
778
+ if (version) {
779
+ cachedVersion = version;
780
+ return cachedVersion;
781
+ }
782
+ }
783
+ }
784
+ const packageJsonPaths = [
785
+ join2(currentDir, "..", "package.json"),
786
+ join2(currentDir, "..", "..", "package.json")
787
+ ];
788
+ for (const packageJsonPath of packageJsonPaths) {
789
+ if (existsSync3(packageJsonPath)) {
790
+ const packageJson = JSON.parse(readFileSync2(packageJsonPath, "utf-8"));
791
+ if (packageJson.name === "@hangox/mg-cli") {
792
+ cachedVersion = packageJson.version || "0.0.0";
793
+ return cachedVersion;
794
+ }
795
+ }
796
+ }
797
+ cachedVersion = "0.0.0";
798
+ return cachedVersion;
799
+ } catch {
800
+ cachedVersion = "0.0.0";
801
+ return cachedVersion;
802
+ }
803
+ }
804
+ function isVersionMatch(version1, version2) {
805
+ return version1 === version2;
806
+ }
807
+
611
808
  // src/server/websocket-server.ts
612
809
  var MGServer = class {
613
810
  wss = null;
@@ -616,6 +813,7 @@ var MGServer = class {
616
813
  requestHandler;
617
814
  port;
618
815
  isRunning = false;
816
+ startedAt = null;
619
817
  constructor(options = {}) {
620
818
  this.port = options.port || DEFAULT_PORT;
621
819
  this.logger = options.logger || createLogger();
@@ -630,14 +828,15 @@ var MGServer = class {
630
828
  throw new MGError("E016" /* SERVER_ALREADY_RUNNING */, "Server \u5DF2\u5728\u8FD0\u884C\u4E2D");
631
829
  }
632
830
  const port = await this.findAvailablePort();
633
- return new Promise((resolve7, reject) => {
831
+ return new Promise((resolve8, reject) => {
634
832
  this.wss = new WebSocketServer({ port });
635
833
  this.wss.on("listening", () => {
636
834
  this.port = port;
637
835
  this.isRunning = true;
836
+ this.startedAt = /* @__PURE__ */ new Date();
638
837
  this.logger.info(`Server \u542F\u52A8\u6210\u529F\uFF0C\u76D1\u542C\u7AEF\u53E3: ${port}`);
639
838
  this.connectionManager.startHeartbeatCheck(HEARTBEAT_INTERVAL);
640
- resolve7(port);
839
+ resolve8(port);
641
840
  });
642
841
  this.wss.on("error", (error) => {
643
842
  this.logger.error("Server \u9519\u8BEF:", error);
@@ -668,14 +867,14 @@ var MGServer = class {
668
867
  * 检查端口是否可用
669
868
  */
670
869
  isPortAvailable(port) {
671
- return new Promise((resolve7) => {
870
+ return new Promise((resolve8) => {
672
871
  const testServer = new WebSocketServer({ port });
673
872
  testServer.on("listening", () => {
674
873
  testServer.close();
675
- resolve7(true);
874
+ resolve8(true);
676
875
  });
677
876
  testServer.on("error", () => {
678
- resolve7(false);
877
+ resolve8(false);
679
878
  });
680
879
  });
681
880
  }
@@ -735,7 +934,8 @@ var MGServer = class {
735
934
  success: true,
736
935
  data: {
737
936
  connectionId: managedWs.connectionId,
738
- pageUrl
937
+ pageUrl,
938
+ serverVersion: getVersion()
739
939
  }
740
940
  };
741
941
  ws.send(JSON.stringify(ack));
@@ -749,6 +949,9 @@ var MGServer = class {
749
949
  case "ping" /* PING */:
750
950
  this.handlePing(ws, message);
751
951
  break;
952
+ case "get_server_status" /* GET_SERVER_STATUS */:
953
+ this.handleGetServerStatus(ws, message);
954
+ break;
752
955
  case "response" /* RESPONSE */:
753
956
  case "error" /* ERROR */:
754
957
  this.requestHandler.handleResponse(message);
@@ -770,6 +973,37 @@ var MGServer = class {
770
973
  };
771
974
  ws.send(JSON.stringify(pong));
772
975
  }
976
+ /**
977
+ * 处理 Server 状态查询
978
+ */
979
+ handleGetServerStatus(ws, message) {
980
+ const providers = this.connectionManager.getAllProviders();
981
+ const stats = this.connectionManager.getStats();
982
+ const connectedPages = providers.map((info) => ({
983
+ pageUrl: info.pageUrl || "",
984
+ connectedAt: info.connectedAt.toISOString(),
985
+ lastActiveAt: info.lastActiveAt.toISOString()
986
+ }));
987
+ const uptimeMs = this.startedAt ? Date.now() - this.startedAt.getTime() : 0;
988
+ const statusData = {
989
+ running: this.isRunning,
990
+ port: this.port,
991
+ pid: process.pid,
992
+ startedAt: this.startedAt?.toISOString() || "",
993
+ uptime: formatDuration(uptimeMs),
994
+ version: getVersion(),
995
+ stats,
996
+ connectedPages
997
+ };
998
+ const response = {
999
+ id: message.id || "",
1000
+ type: "response" /* RESPONSE */,
1001
+ success: true,
1002
+ data: statusData
1003
+ };
1004
+ ws.send(JSON.stringify(response));
1005
+ this.logger.info("\u8FD4\u56DE Server \u72B6\u6001\u4FE1\u606F");
1006
+ }
773
1007
  /**
774
1008
  * 停止服务器
775
1009
  */
@@ -780,12 +1014,12 @@ var MGServer = class {
780
1014
  this.logger.info("\u6B63\u5728\u505C\u6B62 Server...");
781
1015
  this.requestHandler.cleanupAll();
782
1016
  this.connectionManager.closeAll();
783
- return new Promise((resolve7) => {
1017
+ return new Promise((resolve8) => {
784
1018
  this.wss.close(() => {
785
1019
  this.isRunning = false;
786
1020
  this.wss = null;
787
1021
  this.logger.info("Server \u5DF2\u505C\u6B62");
788
- resolve7();
1022
+ resolve8();
789
1023
  });
790
1024
  });
791
1025
  }
@@ -860,7 +1094,8 @@ async function startServerForeground(port) {
860
1094
  writeServerInfo({
861
1095
  port: actualPort,
862
1096
  pid: process.pid,
863
- startedAt: getCurrentISOTime()
1097
+ startedAt: getCurrentISOTime(),
1098
+ version: getVersion()
864
1099
  });
865
1100
  console.log(`
866
1101
  MG Server \u542F\u52A8\u6210\u529F`);
@@ -883,9 +1118,9 @@ async function startServerDaemon(port) {
883
1118
  );
884
1119
  }
885
1120
  ensureConfigDir();
886
- const currentFile = fileURLToPath(import.meta.url);
887
- const currentDir = dirname3(currentFile);
888
- const serverScript = join2(currentDir, "daemon-runner.js");
1121
+ const currentFile = fileURLToPath2(import.meta.url);
1122
+ const currentDir = dirname4(currentFile);
1123
+ const serverScript = join3(currentDir, "daemon-runner.js");
889
1124
  const args = ["--foreground"];
890
1125
  if (port) {
891
1126
  args.push("--port", String(port));
@@ -901,7 +1136,7 @@ async function startServerDaemon(port) {
901
1136
  child.unref();
902
1137
  const startTime = Date.now();
903
1138
  while (Date.now() - startTime < SERVER_START_TIMEOUT) {
904
- await new Promise((resolve7) => setTimeout(resolve7, 200));
1139
+ await new Promise((resolve8) => setTimeout(resolve8, 200));
905
1140
  const { running: running2, info: info2 } = isServerRunning();
906
1141
  if (running2 && info2) {
907
1142
  return info2;
@@ -922,7 +1157,7 @@ function stopServer() {
922
1157
  }
923
1158
  async function restartServer(port) {
924
1159
  const { info: oldInfo } = stopServer();
925
- await new Promise((resolve7) => setTimeout(resolve7, 500));
1160
+ await new Promise((resolve8) => setTimeout(resolve8, 500));
926
1161
  return startServerDaemon(port || oldInfo?.port);
927
1162
  }
928
1163
  function getServerStatus() {
@@ -936,98 +1171,11 @@ function getServerStatus() {
936
1171
  port: info.port,
937
1172
  pid: info.pid,
938
1173
  startedAt: info.startedAt,
939
- uptime: formatDuration(uptimeMs)
1174
+ uptime: formatDuration(uptimeMs),
1175
+ version: info.version
940
1176
  };
941
1177
  }
942
1178
 
943
- // src/cli/commands/server.ts
944
- function createServerCommand() {
945
- const serverCmd = new Command("server").description("Server \u7BA1\u7406\u547D\u4EE4");
946
- serverCmd.command("start").description("\u542F\u52A8 MG Server").option("--port <number>", "\u6307\u5B9A\u542F\u52A8\u7AEF\u53E3", (value) => parseInt(value, 10)).option("--foreground", "\u524D\u53F0\u6A21\u5F0F\u8FD0\u884C\uFF08\u4E0D\u4F5C\u4E3A\u5B88\u62A4\u8FDB\u7A0B\uFF09", false).action(async (options) => {
947
- try {
948
- if (options.foreground) {
949
- await startServerForeground(options.port);
950
- } else {
951
- const info = await startServerDaemon(options.port);
952
- console.log("MG Server \u542F\u52A8\u6210\u529F");
953
- console.log(`\u76D1\u542C\u7AEF\u53E3: ${info.port}`);
954
- console.log(`\u8FDB\u7A0B PID: ${info.pid}`);
955
- console.log(`\u8FD0\u884C\u6A21\u5F0F: \u5B88\u62A4\u8FDB\u7A0B`);
956
- }
957
- } catch (error) {
958
- console.error(`\u9519\u8BEF: ${error.message}`);
959
- process.exit(1);
960
- }
961
- });
962
- serverCmd.command("stop").description("\u505C\u6B62 MG Server").action(() => {
963
- try {
964
- const { stopped, info } = stopServer();
965
- if (stopped && info) {
966
- console.log("MG Server \u5DF2\u505C\u6B62");
967
- console.log(`PID: ${info.pid}`);
968
- const uptimeMs = Date.now() - new Date(info.startedAt).getTime();
969
- const seconds = Math.floor(uptimeMs / 1e3);
970
- const minutes = Math.floor(seconds / 60);
971
- const hours = Math.floor(minutes / 60);
972
- let uptime = "";
973
- if (hours > 0) {
974
- uptime = `${hours} \u5C0F\u65F6 ${minutes % 60} \u5206\u949F`;
975
- } else if (minutes > 0) {
976
- uptime = `${minutes} \u5206\u949F ${seconds % 60} \u79D2`;
977
- } else {
978
- uptime = `${seconds} \u79D2`;
979
- }
980
- console.log(`\u8FD0\u884C\u65F6\u957F: ${uptime}`);
981
- } else {
982
- console.log("MG Server \u672A\u8FD0\u884C");
983
- }
984
- } catch (error) {
985
- console.error(`\u9519\u8BEF: ${error.message}`);
986
- process.exit(1);
987
- }
988
- });
989
- serverCmd.command("restart").description("\u91CD\u542F MG Server").option("--port <number>", "\u91CD\u542F\u540E\u4F7F\u7528\u7684\u7AEF\u53E3", (value) => parseInt(value, 10)).action(async (options) => {
990
- try {
991
- const status = getServerStatus();
992
- if (status.running) {
993
- console.log(`\u6B63\u5728\u505C\u6B62 MG Server (PID: ${status.pid})...`);
994
- }
995
- const info = await restartServer(options.port);
996
- console.log("MG Server \u5DF2\u91CD\u542F");
997
- console.log(`\u76D1\u542C\u7AEF\u53E3: ${info.port}`);
998
- console.log(`\u65B0\u8FDB\u7A0B PID: ${info.pid}`);
999
- } catch (error) {
1000
- console.error(`\u9519\u8BEF: ${error.message}`);
1001
- process.exit(1);
1002
- }
1003
- });
1004
- serverCmd.command("status").description("\u67E5\u770B Server \u8FD0\u884C\u72B6\u6001").action(() => {
1005
- try {
1006
- const status = getServerStatus();
1007
- if (status.running) {
1008
- console.log("MG Server \u72B6\u6001: \u8FD0\u884C\u4E2D \u2713");
1009
- console.log(`\u76D1\u542C\u7AEF\u53E3: ${status.port}`);
1010
- console.log(`\u8FDB\u7A0B PID: ${status.pid}`);
1011
- console.log(`\u542F\u52A8\u65F6\u95F4: ${status.startedAt}`);
1012
- console.log(`\u8FD0\u884C\u65F6\u957F: ${status.uptime}`);
1013
- } else {
1014
- console.log("MG Server \u72B6\u6001: \u672A\u8FD0\u884C \u2717");
1015
- console.log("\u63D0\u793A: \u4F7F\u7528 'mg-cli server start' \u542F\u52A8 Server");
1016
- }
1017
- } catch (error) {
1018
- console.error(`\u9519\u8BEF: ${error.message}`);
1019
- process.exit(1);
1020
- }
1021
- });
1022
- return serverCmd;
1023
- }
1024
-
1025
- // src/cli/commands/get-node-by-id.ts
1026
- import { Command as Command2 } from "commander";
1027
- import { writeFileSync as writeFileSync2 } from "fs";
1028
- import { resolve as resolve2, dirname as dirname4 } from "path";
1029
- import { mkdirSync as mkdirSync3 } from "fs";
1030
-
1031
1179
  // src/cli/client.ts
1032
1180
  import WebSocket2 from "ws";
1033
1181
  var MGClient = class {
@@ -1042,10 +1190,28 @@ var MGClient = class {
1042
1190
  async connect() {
1043
1191
  const serverInfo = readServerInfo();
1044
1192
  if (serverInfo) {
1045
- try {
1046
- await this.tryConnect(serverInfo.port);
1047
- return;
1048
- } catch {
1193
+ if (isProcessRunning(serverInfo.pid)) {
1194
+ const currentVersion = getVersion();
1195
+ if (!isVersionMatch(currentVersion, serverInfo.version)) {
1196
+ console.log(`\u7248\u672C\u4E0D\u5339\u914D: CLI ${currentVersion} vs Server ${serverInfo.version}`);
1197
+ console.log("\u6B63\u5728\u91CD\u542F Server \u4EE5\u5BF9\u9F50\u7248\u672C...");
1198
+ try {
1199
+ const newInfo = await restartServer(serverInfo.port);
1200
+ console.log(`Server \u5DF2\u91CD\u542F\uFF0C\u7248\u672C: ${newInfo.version}`);
1201
+ await this.waitForServer(newInfo.port);
1202
+ return;
1203
+ } catch (error) {
1204
+ throw new MGError(
1205
+ "E015" /* SERVER_START_FAILED */,
1206
+ `\u91CD\u542F Server \u5931\u8D25: ${error instanceof Error ? error.message : error}`
1207
+ );
1208
+ }
1209
+ }
1210
+ try {
1211
+ await this.tryConnect(serverInfo.port);
1212
+ return;
1213
+ } catch {
1214
+ }
1049
1215
  }
1050
1216
  }
1051
1217
  for (let port = PORT_RANGE_START; port <= PORT_RANGE_END; port++) {
@@ -1075,7 +1241,7 @@ var MGClient = class {
1075
1241
  * 尝试连接指定端口
1076
1242
  */
1077
1243
  tryConnect(port) {
1078
- return new Promise((resolve7, reject) => {
1244
+ return new Promise((resolve8, reject) => {
1079
1245
  const ws = new WebSocket2(`ws://localhost:${port}`);
1080
1246
  const timer = setTimeout(() => {
1081
1247
  ws.close();
@@ -1085,7 +1251,7 @@ var MGClient = class {
1085
1251
  clearTimeout(timer);
1086
1252
  this.ws = ws;
1087
1253
  this.register();
1088
- resolve7();
1254
+ resolve8();
1089
1255
  });
1090
1256
  ws.on("error", (error) => {
1091
1257
  clearTimeout(timer);
@@ -1138,7 +1304,7 @@ var MGClient = class {
1138
1304
  pageUrl,
1139
1305
  timestamp: Date.now()
1140
1306
  };
1141
- return new Promise((resolve7, reject) => {
1307
+ return new Promise((resolve8, reject) => {
1142
1308
  const timer = setTimeout(() => {
1143
1309
  reject(new MGError("E012" /* REQUEST_TIMEOUT */, ErrorMessages["E012" /* REQUEST_TIMEOUT */]));
1144
1310
  }, REQUEST_TIMEOUT);
@@ -1149,7 +1315,7 @@ var MGClient = class {
1149
1315
  clearTimeout(timer);
1150
1316
  this.ws?.off("message", messageHandler);
1151
1317
  if (response.success) {
1152
- resolve7(response.data);
1318
+ resolve8(response.data);
1153
1319
  } else {
1154
1320
  const error = response.error;
1155
1321
  reject(
@@ -1209,9 +1375,132 @@ var MGClient = class {
1209
1375
  }
1210
1376
  };
1211
1377
 
1378
+ // src/cli/commands/server.ts
1379
+ function createServerCommand() {
1380
+ const serverCmd = new Command("server").description("Server \u7BA1\u7406\u547D\u4EE4");
1381
+ serverCmd.command("start").description("\u542F\u52A8 MG Server").option("--port <number>", "\u6307\u5B9A\u542F\u52A8\u7AEF\u53E3", (value) => parseInt(value, 10)).option("--foreground", "\u524D\u53F0\u6A21\u5F0F\u8FD0\u884C\uFF08\u4E0D\u4F5C\u4E3A\u5B88\u62A4\u8FDB\u7A0B\uFF09", false).action(async (options) => {
1382
+ try {
1383
+ if (options.foreground) {
1384
+ await startServerForeground(options.port);
1385
+ } else {
1386
+ const info = await startServerDaemon(options.port);
1387
+ console.log("MG Server \u542F\u52A8\u6210\u529F");
1388
+ console.log(`\u7248\u672C: ${info.version}`);
1389
+ console.log(`\u76D1\u542C\u7AEF\u53E3: ${info.port}`);
1390
+ console.log(`\u8FDB\u7A0B PID: ${info.pid}`);
1391
+ console.log(`\u8FD0\u884C\u6A21\u5F0F: \u5B88\u62A4\u8FDB\u7A0B`);
1392
+ }
1393
+ } catch (error) {
1394
+ console.error(`\u9519\u8BEF: ${error.message}`);
1395
+ process.exit(1);
1396
+ }
1397
+ });
1398
+ serverCmd.command("stop").description("\u505C\u6B62 MG Server").action(() => {
1399
+ try {
1400
+ const { stopped, info } = stopServer();
1401
+ if (stopped && info) {
1402
+ console.log("MG Server \u5DF2\u505C\u6B62");
1403
+ console.log(`PID: ${info.pid}`);
1404
+ const uptimeMs = Date.now() - new Date(info.startedAt).getTime();
1405
+ const seconds = Math.floor(uptimeMs / 1e3);
1406
+ const minutes = Math.floor(seconds / 60);
1407
+ const hours = Math.floor(minutes / 60);
1408
+ let uptime = "";
1409
+ if (hours > 0) {
1410
+ uptime = `${hours} \u5C0F\u65F6 ${minutes % 60} \u5206\u949F`;
1411
+ } else if (minutes > 0) {
1412
+ uptime = `${minutes} \u5206\u949F ${seconds % 60} \u79D2`;
1413
+ } else {
1414
+ uptime = `${seconds} \u79D2`;
1415
+ }
1416
+ console.log(`\u8FD0\u884C\u65F6\u957F: ${uptime}`);
1417
+ } else {
1418
+ console.log("MG Server \u672A\u8FD0\u884C");
1419
+ }
1420
+ } catch (error) {
1421
+ console.error(`\u9519\u8BEF: ${error.message}`);
1422
+ process.exit(1);
1423
+ }
1424
+ });
1425
+ serverCmd.command("restart").description("\u91CD\u542F MG Server").option("--port <number>", "\u91CD\u542F\u540E\u4F7F\u7528\u7684\u7AEF\u53E3", (value) => parseInt(value, 10)).action(async (options) => {
1426
+ try {
1427
+ const status = getServerStatus();
1428
+ if (status.running) {
1429
+ console.log(`\u6B63\u5728\u505C\u6B62 MG Server (PID: ${status.pid})...`);
1430
+ }
1431
+ const info = await restartServer(options.port);
1432
+ console.log("MG Server \u5DF2\u91CD\u542F");
1433
+ console.log(`\u7248\u672C: ${info.version}`);
1434
+ console.log(`\u76D1\u542C\u7AEF\u53E3: ${info.port}`);
1435
+ console.log(`\u65B0\u8FDB\u7A0B PID: ${info.pid}`);
1436
+ } catch (error) {
1437
+ console.error(`\u9519\u8BEF: ${error.message}`);
1438
+ process.exit(1);
1439
+ }
1440
+ });
1441
+ serverCmd.command("status").description("\u67E5\u770B Server \u8FD0\u884C\u72B6\u6001").action(async () => {
1442
+ try {
1443
+ const basicStatus = getServerStatus();
1444
+ if (!basicStatus.running) {
1445
+ console.log("MG Server \u72B6\u6001: \u672A\u8FD0\u884C \u2717");
1446
+ console.log("\u63D0\u793A: \u4F7F\u7528 'mg-cli server start' \u542F\u52A8 Server");
1447
+ return;
1448
+ }
1449
+ try {
1450
+ const client = new MGClient({ noAutoStart: true });
1451
+ await client.connect();
1452
+ const status = await client.request(
1453
+ "get_server_status" /* GET_SERVER_STATUS */
1454
+ );
1455
+ client.close();
1456
+ console.log("MG Server \u72B6\u6001: \u8FD0\u884C\u4E2D \u2713");
1457
+ console.log(`\u7248\u672C: ${status.version}`);
1458
+ console.log(`\u76D1\u542C\u7AEF\u53E3: ${status.port}`);
1459
+ console.log(`\u8FDB\u7A0B PID: ${status.pid}`);
1460
+ console.log(`\u542F\u52A8\u65F6\u95F4: ${status.startedAt}`);
1461
+ console.log(`\u8FD0\u884C\u65F6\u957F: ${status.uptime}`);
1462
+ console.log(``);
1463
+ console.log(`\u8FDE\u63A5\u7EDF\u8BA1:`);
1464
+ console.log(` Provider \u6570\u91CF: ${status.stats.providers}`);
1465
+ console.log(` Consumer \u6570\u91CF: ${status.stats.consumers}`);
1466
+ if (status.connectedPages.length > 0) {
1467
+ console.log(``);
1468
+ console.log(`\u5DF2\u8FDE\u63A5\u9875\u9762 (${status.connectedPages.length}):`);
1469
+ for (const page of status.connectedPages) {
1470
+ console.log(` - ${page.pageUrl}`);
1471
+ }
1472
+ } else {
1473
+ console.log(``);
1474
+ console.log(`\u5DF2\u8FDE\u63A5\u9875\u9762: \u65E0`);
1475
+ console.log(`\u63D0\u793A: \u8BF7\u5728 Chrome \u4E2D\u6253\u5F00 MasterGo \u9875\u9762\u5E76\u786E\u4FDD\u63D2\u4EF6\u5DF2\u542F\u7528`);
1476
+ }
1477
+ } catch {
1478
+ console.log("MG Server \u72B6\u6001: \u8FD0\u884C\u4E2D \u2713");
1479
+ if (basicStatus.version) {
1480
+ console.log(`\u7248\u672C: ${basicStatus.version}`);
1481
+ }
1482
+ console.log(`\u76D1\u542C\u7AEF\u53E3: ${basicStatus.port}`);
1483
+ console.log(`\u8FDB\u7A0B PID: ${basicStatus.pid}`);
1484
+ console.log(`\u542F\u52A8\u65F6\u95F4: ${basicStatus.startedAt}`);
1485
+ console.log(`\u8FD0\u884C\u65F6\u957F: ${basicStatus.uptime}`);
1486
+ console.log(``);
1487
+ console.log(`\u6CE8\u610F: \u65E0\u6CD5\u83B7\u53D6\u8BE6\u7EC6\u8FDE\u63A5\u4FE1\u606F`);
1488
+ }
1489
+ } catch (error) {
1490
+ console.error(`\u9519\u8BEF: ${error.message}`);
1491
+ process.exit(1);
1492
+ }
1493
+ });
1494
+ return serverCmd;
1495
+ }
1496
+
1212
1497
  // src/cli/commands/get-node-by-id.ts
1498
+ import { Command as Command2 } from "commander";
1499
+ import { writeFileSync as writeFileSync2 } from "fs";
1500
+ import { resolve as resolve2, dirname as dirname5 } from "path";
1501
+ import { mkdirSync as mkdirSync3 } from "fs";
1213
1502
  function createGetNodeByIdCommand() {
1214
- return new Command2("get_node_by_id").description("\u6839\u636E\u8282\u70B9 ID \u83B7\u53D6\u8282\u70B9\u8BE6\u7EC6\u4FE1\u606F\u3002\u6570\u636E\u4FDD\u5B58\u5230\u6307\u5B9A JSON \u6587\u4EF6\uFF0C\u8FD4\u56DE\u6587\u4EF6\u8DEF\u5F84\u548C\u5927\u5C0F\u4FE1\u606F\u3002\u5982\u9700\u901A\u8FC7\u94FE\u63A5\u83B7\u53D6\uFF0C\u8BF7\u4F7F\u7528 get_node_by_link \u547D\u4EE4").requiredOption("--nodeId <id>", "\u8282\u70B9 ID\uFF0C\u683C\u5F0F\u5982 123:456\u3002\u53EF\u4ECE MasterGo \u6D6E\u7A97\u94FE\u63A5\u4E2D\u83B7\u53D6").requiredOption("--output <path>", "\u8F93\u51FA JSON \u6587\u4EF6\u8DEF\u5F84\u3002\u652F\u6301\u7EDD\u5BF9\u8DEF\u5F84\u6216\u76F8\u5BF9\u8DEF\u5F84").option("--domain <domain>", "MasterGo \u57DF\u540D\uFF0C\u9ED8\u8BA4 mastergo.netease.com\u3002\u4E0E --fileId \u914D\u5408\u4F7F\u7528", "mastergo.netease.com").option("--fileId <id>", "\u6587\u4EF6 ID\uFF08\u7EAF\u6570\u5B57\uFF09\uFF0C\u4E0E --domain \u914D\u5408\u6307\u5B9A\u76EE\u6807\u9875\u9762").option("--maxDepth <number>", "\u904D\u5386\u6DF1\u5EA6\uFF0C\u9ED8\u8BA4 1\u3002\u589E\u52A0\u6DF1\u5EA6\u4F1A\u663E\u8457\u589E\u52A0\u6570\u636E\u91CF", "1").option("--includeInvisible", "\u5305\u542B\u4E0D\u53EF\u89C1\u8282\u70B9\uFF08visible: false\uFF09\uFF0C\u9ED8\u8BA4\u4E0D\u5305\u542B", false).option("--no-auto-start", "\u7981\u7528\u81EA\u52A8\u542F\u52A8 Server").option("--no-retry", "\u7981\u7528\u81EA\u52A8\u91CD\u8BD5").action(async (options) => {
1503
+ return new Command2("get_node_by_id").description("\u6839\u636E\u8282\u70B9 ID \u83B7\u53D6\u8282\u70B9\u8BE6\u7EC6\u4FE1\u606F\u3002\u6570\u636E\u4FDD\u5B58\u5230\u6307\u5B9A JSON \u6587\u4EF6\uFF0C\u8FD4\u56DE\u6587\u4EF6\u8DEF\u5F84\u548C\u5927\u5C0F\u4FE1\u606F\u3002\u5982\u9700\u901A\u8FC7\u94FE\u63A5\u83B7\u53D6\uFF0C\u8BF7\u4F7F\u7528 get_node_by_link \u547D\u4EE4").requiredOption("--nodeId <id>", "\u8282\u70B9 ID\uFF0C\u683C\u5F0F\u5982 123:456\u3002\u53EF\u4ECE MasterGo \u6D6E\u7A97\u94FE\u63A5\u4E2D\u83B7\u53D6").requiredOption("--output <path>", "\u8F93\u51FA JSON \u6587\u4EF6\u8DEF\u5F84\u3002\u652F\u6301\u7EDD\u5BF9\u8DEF\u5F84\u6216\u76F8\u5BF9\u8DEF\u5F84").option("--domain <domain>", "MasterGo \u57DF\u540D\uFF0C\u9ED8\u8BA4 mastergo.netease.com\u3002\u4E0E --fileId \u914D\u5408\u4F7F\u7528", "mastergo.netease.com").option("--fileId <id>", "\u6587\u4EF6 ID\uFF08\u7EAF\u6570\u5B57\uFF09\uFF0C\u4E0E --domain \u914D\u5408\u6307\u5B9A\u76EE\u6807\u9875\u9762").option("--maxDepth <number>", "\u904D\u5386\u6DF1\u5EA6\uFF0C\u9ED8\u8BA4 1\u3002\u589E\u52A0\u6DF1\u5EA6\u4F1A\u663E\u8457\u589E\u52A0\u6570\u636E\u91CF", "1").option("--includeInvisible", "\u5305\u542B\u4E0D\u53EF\u89C1\u8282\u70B9\uFF08visible: false\uFF09\uFF0C\u9ED8\u8BA4\u4E0D\u5305\u542B", false).option("--raw", "\u4FDD\u7559\u539F\u59CB\u6570\u636E\uFF0C\u4E0D\u7CBE\u7B80\u9ED8\u8BA4\u503C\u5B57\u6BB5", false).option("--no-auto-start", "\u7981\u7528\u81EA\u52A8\u542F\u52A8 Server").option("--no-retry", "\u7981\u7528\u81EA\u52A8\u91CD\u8BD5").action(async (options) => {
1215
1504
  await handleGetNodeById(options);
1216
1505
  });
1217
1506
  }
@@ -1233,10 +1522,11 @@ async function handleGetNodeById(options) {
1233
1522
  includeInvisible: options.includeInvisible || false
1234
1523
  };
1235
1524
  const data = await client.requestWithRetry("get_node_by_id" /* GET_NODE_BY_ID */, params, pageUrl);
1525
+ const outputData = options.raw ? data : trimNodeDefaults(data);
1236
1526
  const outputPath = resolve2(options.output);
1237
- const outputDir = dirname4(outputPath);
1527
+ const outputDir = dirname5(outputPath);
1238
1528
  mkdirSync3(outputDir, { recursive: true });
1239
- const jsonContent = JSON.stringify(data, null, 2);
1529
+ const jsonContent = JSON.stringify(outputData, null, 2);
1240
1530
  writeFileSync2(outputPath, jsonContent, "utf-8");
1241
1531
  const size = jsonContent.length;
1242
1532
  const sizeKB = (size / 1024).toFixed(2);
@@ -1244,6 +1534,9 @@ async function handleGetNodeById(options) {
1244
1534
  console.log(`\u8282\u70B9 ID: ${options.nodeId}`);
1245
1535
  console.log(`\u6570\u636E\u5927\u5C0F: ${size.toLocaleString()} \u5B57\u7B26 (\u7EA6 ${sizeKB} KB)`);
1246
1536
  console.log(`\u8282\u70B9\u6DF1\u5EA6: ${params.maxDepth}`);
1537
+ if (!options.raw) {
1538
+ console.log(`\u6570\u636E\u6A21\u5F0F: \u7CBE\u7B80\u6A21\u5F0F (\u4F7F\u7528 --raw \u83B7\u53D6\u5B8C\u6574\u6570\u636E)`);
1539
+ }
1247
1540
  } catch (error) {
1248
1541
  console.error(`\u9519\u8BEF: ${error instanceof Error ? error.message : error}`);
1249
1542
  process.exit(1);
@@ -1255,10 +1548,10 @@ async function handleGetNodeById(options) {
1255
1548
  // src/cli/commands/get-node-by-link.ts
1256
1549
  import { Command as Command3 } from "commander";
1257
1550
  import { writeFileSync as writeFileSync3 } from "fs";
1258
- import { resolve as resolve3, dirname as dirname5 } from "path";
1551
+ import { resolve as resolve3, dirname as dirname6 } from "path";
1259
1552
  import { mkdirSync as mkdirSync4 } from "fs";
1260
1553
  function createGetNodeByLinkCommand() {
1261
- return new Command3("get_node_by_link").description("\u89E3\u6790 mgp:// \u534F\u8BAE\u94FE\u63A5\u5E76\u83B7\u53D6\u8282\u70B9\u4FE1\u606F").requiredOption("--link <url>", "mgp:// \u534F\u8BAE\u94FE\u63A5").requiredOption("--output <path>", "\u8F93\u51FA JSON \u6587\u4EF6\u8DEF\u5F84").option("--maxDepth <number>", "\u904D\u5386\u6DF1\u5EA6", "1").option("--includeInvisible", "\u5305\u542B\u4E0D\u53EF\u89C1\u8282\u70B9", false).option("--no-auto-start", "\u7981\u7528\u81EA\u52A8\u542F\u52A8 Server").option("--no-retry", "\u7981\u7528\u81EA\u52A8\u91CD\u8BD5").action(async (options) => {
1554
+ return new Command3("get_node_by_link").description("\u89E3\u6790 mgp:// \u534F\u8BAE\u94FE\u63A5\u5E76\u83B7\u53D6\u8282\u70B9\u4FE1\u606F").requiredOption("--link <url>", "mgp:// \u534F\u8BAE\u94FE\u63A5").requiredOption("--output <path>", "\u8F93\u51FA JSON \u6587\u4EF6\u8DEF\u5F84").option("--maxDepth <number>", "\u904D\u5386\u6DF1\u5EA6", "1").option("--includeInvisible", "\u5305\u542B\u4E0D\u53EF\u89C1\u8282\u70B9", false).option("--raw", "\u4FDD\u7559\u539F\u59CB\u6570\u636E\uFF0C\u4E0D\u7CBE\u7B80\u9ED8\u8BA4\u503C\u5B57\u6BB5", false).option("--no-auto-start", "\u7981\u7528\u81EA\u52A8\u542F\u52A8 Server").option("--no-retry", "\u7981\u7528\u81EA\u52A8\u91CD\u8BD5").action(async (options) => {
1262
1555
  await handleGetNodeByLink(options);
1263
1556
  });
1264
1557
  }
@@ -1287,10 +1580,11 @@ async function handleGetNodeByLink(options) {
1287
1580
  params,
1288
1581
  pageUrl
1289
1582
  );
1583
+ const outputData = options.raw ? data : trimNodeDefaults(data);
1290
1584
  const outputPath = resolve3(options.output);
1291
- const outputDir = dirname5(outputPath);
1585
+ const outputDir = dirname6(outputPath);
1292
1586
  mkdirSync4(outputDir, { recursive: true });
1293
- const jsonContent = JSON.stringify(data, null, 2);
1587
+ const jsonContent = JSON.stringify(outputData, null, 2);
1294
1588
  writeFileSync3(outputPath, jsonContent, "utf-8");
1295
1589
  const size = jsonContent.length;
1296
1590
  const sizeKB = (size / 1024).toFixed(2);
@@ -1300,6 +1594,9 @@ async function handleGetNodeByLink(options) {
1300
1594
  console.log(`\u8282\u70B9 ID: ${nodeId}`);
1301
1595
  console.log(`\u6570\u636E\u5927\u5C0F: ${size.toLocaleString()} \u5B57\u7B26 (\u7EA6 ${sizeKB} KB)`);
1302
1596
  console.log(`\u8282\u70B9\u6DF1\u5EA6: ${params.maxDepth}`);
1597
+ if (!options.raw) {
1598
+ console.log(`\u6570\u636E\u6A21\u5F0F: \u7CBE\u7B80\u6A21\u5F0F (\u4F7F\u7528 --raw \u83B7\u53D6\u5B8C\u6574\u6570\u636E)`);
1599
+ }
1303
1600
  } catch (error) {
1304
1601
  if (error instanceof MGError) {
1305
1602
  console.error(`\u9519\u8BEF [${error.code}]: ${error.message}`);
@@ -1315,10 +1612,10 @@ async function handleGetNodeByLink(options) {
1315
1612
  // src/cli/commands/get-all-nodes.ts
1316
1613
  import { Command as Command4 } from "commander";
1317
1614
  import { writeFileSync as writeFileSync4 } from "fs";
1318
- import { resolve as resolve4, dirname as dirname6 } from "path";
1615
+ import { resolve as resolve4, dirname as dirname7 } from "path";
1319
1616
  import { mkdirSync as mkdirSync5 } from "fs";
1320
1617
  function createGetAllNodesCommand() {
1321
- return new Command4("get_all_nodes").description("\u83B7\u53D6\u5F53\u524D\u9875\u9762\u7684\u6240\u6709\u8282\u70B9\u6811\u3002\u8B66\u544A\uFF1A\u6DF1\u5EA6\u6BCF\u589E\u52A0 1\uFF0C\u6570\u636E\u91CF\u53EF\u80FD\u5448\u6307\u6570\u7EA7\u589E\u957F\u3002\u5EFA\u8BAE\u4ECE maxDepth=1 \u5F00\u59CB").requiredOption("--output <path>", "\u8F93\u51FA JSON \u6587\u4EF6\u8DEF\u5F84\u3002\u652F\u6301\u7EDD\u5BF9\u8DEF\u5F84\u6216\u76F8\u5BF9\u8DEF\u5F84").option("--maxDepth <number>", "\u6700\u5927\u6DF1\u5EA6\uFF0C\u9ED8\u8BA4 1\u3002\u6DF1\u5EA6 2 \u53EF\u80FD\u4EA7\u751F 100KB-500KB\uFF0C\u6DF1\u5EA6 3 \u53EF\u80FD\u8D85\u8FC7 1MB", "1").option("--includeInvisible", "\u5305\u542B\u4E0D\u53EF\u89C1\u8282\u70B9\uFF08visible: false\uFF09\uFF0C\u9ED8\u8BA4\u4E0D\u5305\u542B", false).option("--no-auto-start", "\u7981\u7528\u81EA\u52A8\u542F\u52A8 Server").option("--no-retry", "\u7981\u7528\u81EA\u52A8\u91CD\u8BD5").action(async (options) => {
1618
+ return new Command4("get_all_nodes").description("\u83B7\u53D6\u5F53\u524D\u9875\u9762\u7684\u6240\u6709\u8282\u70B9\u6811\u3002\u8B66\u544A\uFF1A\u6DF1\u5EA6\u6BCF\u589E\u52A0 1\uFF0C\u6570\u636E\u91CF\u53EF\u80FD\u5448\u6307\u6570\u7EA7\u589E\u957F\u3002\u5EFA\u8BAE\u4ECE maxDepth=1 \u5F00\u59CB").requiredOption("--output <path>", "\u8F93\u51FA JSON \u6587\u4EF6\u8DEF\u5F84\u3002\u652F\u6301\u7EDD\u5BF9\u8DEF\u5F84\u6216\u76F8\u5BF9\u8DEF\u5F84").option("--maxDepth <number>", "\u6700\u5927\u6DF1\u5EA6\uFF0C\u9ED8\u8BA4 1\u3002\u6DF1\u5EA6 2 \u53EF\u80FD\u4EA7\u751F 100KB-500KB\uFF0C\u6DF1\u5EA6 3 \u53EF\u80FD\u8D85\u8FC7 1MB", "1").option("--includeInvisible", "\u5305\u542B\u4E0D\u53EF\u89C1\u8282\u70B9\uFF08visible: false\uFF09\uFF0C\u9ED8\u8BA4\u4E0D\u5305\u542B", false).option("--raw", "\u4FDD\u7559\u539F\u59CB\u6570\u636E\uFF0C\u4E0D\u7CBE\u7B80\u9ED8\u8BA4\u503C\u5B57\u6BB5", false).option("--no-auto-start", "\u7981\u7528\u81EA\u52A8\u542F\u52A8 Server").option("--no-retry", "\u7981\u7528\u81EA\u52A8\u91CD\u8BD5").action(async (options) => {
1322
1619
  await handleGetAllNodes(options);
1323
1620
  });
1324
1621
  }
@@ -1334,10 +1631,11 @@ async function handleGetAllNodes(options) {
1334
1631
  includeInvisible: options.includeInvisible || false
1335
1632
  };
1336
1633
  const data = await client.requestWithRetry("get_all_nodes" /* GET_ALL_NODES */, params);
1634
+ const outputData = options.raw ? data : Array.isArray(data) ? data.map((node) => trimNodeDefaults(node)) : trimNodeDefaults(data);
1337
1635
  const outputPath = resolve4(options.output);
1338
- const outputDir = dirname6(outputPath);
1636
+ const outputDir = dirname7(outputPath);
1339
1637
  mkdirSync5(outputDir, { recursive: true });
1340
- const jsonContent = JSON.stringify(data, null, 2);
1638
+ const jsonContent = JSON.stringify(outputData, null, 2);
1341
1639
  writeFileSync4(outputPath, jsonContent, "utf-8");
1342
1640
  const size = jsonContent.length;
1343
1641
  const sizeKB = (size / 1024).toFixed(2);
@@ -1346,6 +1644,9 @@ async function handleGetAllNodes(options) {
1346
1644
  console.log(`\u8282\u70B9\u6570\u91CF: ${nodeCount}`);
1347
1645
  console.log(`\u6570\u636E\u5927\u5C0F: ${size.toLocaleString()} \u5B57\u7B26 (\u7EA6 ${sizeKB} KB)`);
1348
1646
  console.log(`\u8282\u70B9\u6DF1\u5EA6: ${params.maxDepth}`);
1647
+ if (!options.raw) {
1648
+ console.log(`\u6570\u636E\u6A21\u5F0F: \u7CBE\u7B80\u6A21\u5F0F (\u4F7F\u7528 --raw \u83B7\u53D6\u5B8C\u6574\u6570\u636E)`);
1649
+ }
1349
1650
  } catch (error) {
1350
1651
  console.error(`\u9519\u8BEF: ${error instanceof Error ? error.message : error}`);
1351
1652
  process.exit(1);
@@ -1357,11 +1658,11 @@ async function handleGetAllNodes(options) {
1357
1658
  // src/cli/commands/export-image.ts
1358
1659
  import { Command as Command5 } from "commander";
1359
1660
  import { writeFileSync as writeFileSync5 } from "fs";
1360
- import { resolve as resolve5, dirname as dirname7, extname } from "path";
1661
+ import { resolve as resolve5, dirname as dirname8, extname } from "path";
1361
1662
  import { mkdirSync as mkdirSync6 } from "fs";
1362
1663
  import { tmpdir } from "os";
1363
1664
  function createExportImageCommand() {
1364
- return new Command5("export_image").description("\u5BFC\u51FA MasterGo \u8282\u70B9\u4E3A\u56FE\u7247\u6587\u4EF6\u3002\u5F3A\u70C8\u5EFA\u8BAE\u6307\u5B9A --output\uFF0C\u5426\u5219\u4FDD\u5B58\u5230\u4E34\u65F6\u76EE\u5F55\u53EF\u80FD\u88AB\u7CFB\u7EDF\u6E05\u7406").option("--output <path>", "\u8F93\u51FA\u6587\u4EF6\u8DEF\u5F84\u3002\u5F3A\u70C8\u5EFA\u8BAE\u6307\u5B9A\uFF0C\u5426\u5219\u4FDD\u5B58\u5230\u7CFB\u7EDF\u4E34\u65F6\u76EE\u5F55\u53EF\u80FD\u88AB\u6E05\u7406").option("--link <mgp-link>", "mgp:// \u534F\u8BAE\u94FE\u63A5\u3002\u4E0D\u6307\u5B9A\u5219\u5BFC\u51FA\u5F53\u524D\u9009\u4E2D\u8282\u70B9").option("--format <type>", "\u5BFC\u51FA\u683C\u5F0F\uFF1APNG\uFF08\u65E0\u635F\u900F\u660E\uFF09\u3001JPG\uFF08\u6709\u635F\uFF09\u3001SVG\uFF08\u77E2\u91CF\uFF09\u3001PDF\u3001WEBP", "PNG").option("--scale <number>", "\u7F29\u653E\u500D\u7387\uFF08\u5982 1\u30012\u30013\uFF09\u3002\u4E0E width/height \u4E92\u65A5").option("--width <number>", "\u56FA\u5B9A\u5BBD\u5EA6\uFF08\u50CF\u7D20\uFF09\u3002\u4E0E scale/height \u4E92\u65A5").option("--height <number>", "\u56FA\u5B9A\u9AD8\u5EA6\uFF08\u50CF\u7D20\uFF09\u3002\u4E0E scale/width \u4E92\u65A5").option("--useAbsoluteBounds", "\u4F7F\u7528\u5B8C\u6574\u5C3A\u5BF8\u3002true: \u5305\u542B\u88AB\u88C1\u526A\u90E8\u5206\uFF0Cfalse: \u53EA\u5BFC\u51FA\u53EF\u89C1\u533A\u57DF", false).option("--no-use-render-bounds", "\u4E0D\u5305\u542B\u7279\u6548\u548C\u5916\u63CF\u8FB9\u3002\u9ED8\u8BA4\u5305\u542B\u9634\u5F71\u3001\u5916\u63CF\u8FB9\u7B49").option("--no-auto-start", "\u7981\u7528\u81EA\u52A8\u542F\u52A8 Server").option("--no-retry", "\u7981\u7528\u81EA\u52A8\u91CD\u8BD5").action(async (options) => {
1665
+ return new Command5("export_image").description("\u5BFC\u51FA MasterGo \u8282\u70B9\u4E3A\u56FE\u7247\u6587\u4EF6\u3002\u5F3A\u70C8\u5EFA\u8BAE\u6307\u5B9A --output\uFF0C\u5426\u5219\u4FDD\u5B58\u5230\u4E34\u65F6\u76EE\u5F55\u53EF\u80FD\u88AB\u7CFB\u7EDF\u6E05\u7406").option("--output <path>", "\u8F93\u51FA\u6587\u4EF6\u8DEF\u5F84\u3002\u5F3A\u70C8\u5EFA\u8BAE\u6307\u5B9A\uFF0C\u5426\u5219\u4FDD\u5B58\u5230\u7CFB\u7EDF\u4E34\u65F6\u76EE\u5F55\u53EF\u80FD\u88AB\u6E05\u7406").option("--link <mgp-link>", "mgp:// \u534F\u8BAE\u94FE\u63A5\u3002\u4E0E --nodeId/--domain/--fileId \u4E8C\u9009\u4E00").option("--nodeId <id>", "\u8282\u70B9 ID\uFF0C\u683C\u5F0F\u5982 123:456\u3002\u4E0E --domain/--fileId \u914D\u5408\u4F7F\u7528").option("--domain <domain>", "MasterGo \u57DF\u540D\uFF0C\u9ED8\u8BA4 mastergo.netease.com", "mastergo.netease.com").option("--fileId <id>", "\u6587\u4EF6 ID\uFF08\u7EAF\u6570\u5B57\uFF09\uFF0C\u4E0E --domain \u914D\u5408\u6307\u5B9A\u76EE\u6807\u9875\u9762").option("--format <type>", "\u5BFC\u51FA\u683C\u5F0F\uFF1APNG\uFF08\u65E0\u635F\u900F\u660E\uFF09\u3001JPG\uFF08\u6709\u635F\uFF09\u3001SVG\uFF08\u77E2\u91CF\uFF09\u3001PDF\u3001WEBP", "PNG").option("--scale <number>", "\u7F29\u653E\u500D\u7387\uFF08\u5982 1\u30012\u30013\uFF09\u3002\u4E0E width/height \u4E92\u65A5").option("--width <number>", "\u56FA\u5B9A\u5BBD\u5EA6\uFF08\u50CF\u7D20\uFF09\u3002\u4E0E scale/height \u4E92\u65A5").option("--height <number>", "\u56FA\u5B9A\u9AD8\u5EA6\uFF08\u50CF\u7D20\uFF09\u3002\u4E0E scale/width \u4E92\u65A5").option("--useAbsoluteBounds", "\u4F7F\u7528\u5B8C\u6574\u5C3A\u5BF8\u3002true: \u5305\u542B\u88AB\u88C1\u526A\u90E8\u5206\uFF0Cfalse: \u53EA\u5BFC\u51FA\u53EF\u89C1\u533A\u57DF", false).option("--no-use-render-bounds", "\u4E0D\u5305\u542B\u7279\u6548\u548C\u5916\u63CF\u8FB9\u3002\u9ED8\u8BA4\u5305\u542B\u9634\u5F71\u3001\u5916\u63CF\u8FB9\u7B49").option("--no-auto-start", "\u7981\u7528\u81EA\u52A8\u542F\u52A8 Server").option("--no-retry", "\u7981\u7528\u81EA\u52A8\u91CD\u8BD5").action(async (options) => {
1365
1666
  await handleExportImage(options);
1366
1667
  });
1367
1668
  }
@@ -1378,16 +1679,30 @@ async function handleExportImage(options) {
1378
1679
  console.error("\u9519\u8BEF: scale\u3001width\u3001height \u4E09\u8005\u4E92\u65A5\uFF0C\u53EA\u80FD\u6307\u5B9A\u5176\u4E2D\u4E00\u4E2A");
1379
1680
  process.exit(1);
1380
1681
  }
1682
+ if (options.link && (options.nodeId || options.fileId)) {
1683
+ console.error(`\u9519\u8BEF [${"E011" /* INVALID_PARAMS */}]: --link \u548C --nodeId/--fileId \u4E0D\u80FD\u540C\u65F6\u4F7F\u7528`);
1684
+ process.exit(1);
1685
+ }
1381
1686
  let pageUrl;
1382
1687
  let nodeId;
1383
1688
  if (options.link) {
1384
1689
  const linkInfo = parseMgpLink(options.link);
1385
1690
  if (!linkInfo) {
1386
- console.error(`\u9519\u8BEF: \u65E0\u6548\u7684 mgp:// \u94FE\u63A5\u683C\u5F0F: ${options.link}`);
1691
+ console.error(`\u9519\u8BEF [${"E010" /* INVALID_LINK */}]: \u65E0\u6548\u7684 mgp:// \u94FE\u63A5\u683C\u5F0F`);
1692
+ console.error(`\u63D0\u4F9B\u7684\u94FE\u63A5: ${options.link}`);
1693
+ console.error(`\u671F\u671B\u683C\u5F0F: mgp://[mastergo_page_url]?nodeId=xxx`);
1387
1694
  process.exit(1);
1388
1695
  }
1389
1696
  pageUrl = linkInfo.pageUrl;
1390
1697
  nodeId = linkInfo.nodeId;
1698
+ } else {
1699
+ if (options.nodeId) {
1700
+ nodeId = options.nodeId;
1701
+ }
1702
+ if (options.fileId) {
1703
+ const domain = options.domain || "mastergo.netease.com";
1704
+ pageUrl = `${domain}/file/${options.fileId}`;
1705
+ }
1391
1706
  }
1392
1707
  const client = new MGClient({
1393
1708
  noAutoStart: options.noAutoStart,
@@ -1429,7 +1744,7 @@ async function handleExportImage(options) {
1429
1744
  outputPath = resolve5(tmpdir(), filename);
1430
1745
  console.log("\u8B66\u544A: \u672A\u6307\u5B9A --output\uFF0C\u6587\u4EF6\u5C06\u4FDD\u5B58\u5230\u4E34\u65F6\u76EE\u5F55\uFF0C\u53EF\u80FD\u4F1A\u88AB\u7CFB\u7EDF\u6E05\u7406");
1431
1746
  }
1432
- const outputDir = dirname7(outputPath);
1747
+ const outputDir = dirname8(outputPath);
1433
1748
  mkdirSync6(outputDir, { recursive: true });
1434
1749
  const buffer = Buffer.from(response.data, "base64");
1435
1750
  writeFileSync5(outputPath, buffer);
@@ -1470,18 +1785,36 @@ function getExtension(format) {
1470
1785
  // src/cli/commands/execute-code.ts
1471
1786
  import { Command as Command6 } from "commander";
1472
1787
  function createExecuteCodeCommand() {
1473
- return new Command6("execute_code").description("\u5728 MasterGo \u9875\u9762\u6267\u884C\u81EA\u5B9A\u4E49 JavaScript \u4EE3\u7801\u3002\u901A\u8FC7 mg \u53D8\u91CF\u8BBF\u95EE MasterGo API\uFF0C\u7ED3\u679C\u4F1A\u88AB JSON \u5E8F\u5217\u5316\u8FD4\u56DE").argument("<code>", "\u8981\u6267\u884C\u7684\u4EE3\u7801\u3002\u53EF\u4F7F\u7528 mg \u53D8\u91CF\uFF0C\u5982 mg.currentPage.name\u3001mg.currentPage.selection").option("--no-auto-start", "\u7981\u7528\u81EA\u52A8\u542F\u52A8 Server").option("--no-retry", "\u7981\u7528\u81EA\u52A8\u91CD\u8BD5").action(async (code, options) => {
1788
+ return new Command6("execute_code").description("\u5728 MasterGo \u9875\u9762\u6267\u884C\u81EA\u5B9A\u4E49 JavaScript \u4EE3\u7801\u3002\u901A\u8FC7 mg \u53D8\u91CF\u8BBF\u95EE MasterGo API\uFF0C\u7ED3\u679C\u4F1A\u88AB JSON \u5E8F\u5217\u5316\u8FD4\u56DE").argument("<code>", "\u8981\u6267\u884C\u7684\u4EE3\u7801\u3002\u53EF\u4F7F\u7528 mg \u53D8\u91CF\uFF0C\u5982 mg.currentPage.name\u3001mg.currentPage.selection").option("--link <url>", "mgp:// \u534F\u8BAE\u94FE\u63A5\uFF0C\u7528\u4E8E\u6307\u5B9A\u76EE\u6807\u9875\u9762\u3002\u4E0E --domain/--fileId \u4E8C\u9009\u4E00").option("--domain <domain>", "MasterGo \u57DF\u540D\uFF0C\u9ED8\u8BA4 mastergo.netease.com", "mastergo.netease.com").option("--fileId <id>", "\u6587\u4EF6 ID\uFF08\u7EAF\u6570\u5B57\uFF09\uFF0C\u4E0E --domain \u914D\u5408\u6307\u5B9A\u76EE\u6807\u9875\u9762").option("--no-auto-start", "\u7981\u7528\u81EA\u52A8\u542F\u52A8 Server").option("--no-retry", "\u7981\u7528\u81EA\u52A8\u91CD\u8BD5").action(async (code, options) => {
1474
1789
  await handleExecuteCode(code, options);
1475
1790
  });
1476
1791
  }
1477
1792
  async function handleExecuteCode(code, options) {
1793
+ if (options.link && options.fileId) {
1794
+ console.error(`\u9519\u8BEF [${"E011" /* INVALID_PARAMS */}]: --link \u548C --fileId \u4E0D\u80FD\u540C\u65F6\u4F7F\u7528`);
1795
+ process.exit(1);
1796
+ }
1797
+ let pageUrl;
1798
+ if (options.link) {
1799
+ const parsed = parseMgpLink(options.link);
1800
+ if (!parsed) {
1801
+ console.error(`\u9519\u8BEF [${"E010" /* INVALID_LINK */}]: \u65E0\u6548\u7684 mgp:// \u94FE\u63A5\u683C\u5F0F`);
1802
+ console.error(`\u63D0\u4F9B\u7684\u94FE\u63A5: ${options.link}`);
1803
+ console.error(`\u671F\u671B\u683C\u5F0F: mgp://[mastergo_page_url]?nodeId=xxx`);
1804
+ process.exit(1);
1805
+ }
1806
+ pageUrl = parsed.pageUrl;
1807
+ } else if (options.fileId) {
1808
+ const domain = options.domain || "mastergo.netease.com";
1809
+ pageUrl = `${domain}/file/${options.fileId}`;
1810
+ }
1478
1811
  const client = new MGClient({
1479
1812
  noAutoStart: options.noAutoStart,
1480
1813
  noRetry: options.noRetry
1481
1814
  });
1482
1815
  try {
1483
1816
  await client.connect();
1484
- const result = await client.requestWithRetry("execute_code" /* EXECUTE_CODE */, { code });
1817
+ const result = await client.requestWithRetry("execute_code" /* EXECUTE_CODE */, { code }, pageUrl);
1485
1818
  if (result === null || result === void 0) {
1486
1819
  console.log("\u6267\u884C\u5B8C\u6210\uFF08\u65E0\u8FD4\u56DE\u503C\uFF09");
1487
1820
  } else if (typeof result === "object") {
@@ -1500,7 +1833,7 @@ async function handleExecuteCode(code, options) {
1500
1833
  // src/cli/commands/get-all-pages.ts
1501
1834
  import { Command as Command7 } from "commander";
1502
1835
  import { writeFileSync as writeFileSync6 } from "fs";
1503
- import { resolve as resolve6, dirname as dirname8 } from "path";
1836
+ import { resolve as resolve6, dirname as dirname9 } from "path";
1504
1837
  import { mkdirSync as mkdirSync7 } from "fs";
1505
1838
  import { tmpdir as tmpdir2 } from "os";
1506
1839
  function createGetAllPagesCommand() {
@@ -1548,7 +1881,7 @@ async function handleGetAllPages(options) {
1548
1881
  const filename = `pages_${fileId || "current"}_${Date.now()}.json`;
1549
1882
  outputPath = resolve6(tmpdir2(), filename);
1550
1883
  }
1551
- const outputDir = dirname8(outputPath);
1884
+ const outputDir = dirname9(outputPath);
1552
1885
  mkdirSync7(outputDir, { recursive: true });
1553
1886
  const jsonContent = JSON.stringify(data, null, 2);
1554
1887
  writeFileSync6(outputPath, jsonContent, "utf-8");
@@ -1566,8 +1899,91 @@ async function handleGetAllPages(options) {
1566
1899
  }
1567
1900
  }
1568
1901
 
1902
+ // src/cli/commands/get-node-for-space.ts
1903
+ import { Command as Command8 } from "commander";
1904
+ import { writeFileSync as writeFileSync7 } from "fs";
1905
+ import { resolve as resolve7, dirname as dirname10 } from "path";
1906
+ import { mkdirSync as mkdirSync8 } from "fs";
1907
+ function createGetNodeForSpaceCommand() {
1908
+ return new Command8("get_node_for_space").description("\u83B7\u53D6\u8282\u70B9\u7684\u7A7A\u95F4\u4F4D\u7F6E\u4FE1\u606F\uFF08id\u3001name\u3001x\u3001y\u3001width\u3001height\uFF09\uFF0C\u7528\u4E8E AI \u7406\u89E3\u5143\u7D20\u5E03\u5C40\u3002\u9ED8\u8BA4\u83B7\u53D6\u6700\u6DF1\u5C42\u7EA7").option("--nodeId <id>", "\u8282\u70B9 ID\uFF0C\u683C\u5F0F\u5982 123:456\u3002\u4E0E --link \u4E8C\u9009\u4E00").option("--link <url>", "mgp:// \u534F\u8BAE\u94FE\u63A5\u3002\u4E0E --nodeId \u4E8C\u9009\u4E00").requiredOption("--output <path>", "\u8F93\u51FA JSON \u6587\u4EF6\u8DEF\u5F84").option("--domain <domain>", "MasterGo \u57DF\u540D\uFF0C\u9ED8\u8BA4 mastergo.netease.com\u3002\u4E0E --nodeId \u914D\u5408\u4F7F\u7528", "mastergo.netease.com").option("--fileId <id>", "\u6587\u4EF6 ID\uFF08\u7EAF\u6570\u5B57\uFF09\uFF0C\u4E0E --domain \u548C --nodeId \u914D\u5408\u4F7F\u7528").option("--maxDepth <number>", "\u904D\u5386\u6DF1\u5EA6\uFF0C\u9ED8\u8BA4 99\uFF08\u83B7\u53D6\u6700\u6DF1\u5C42\u7EA7\uFF09", "99").option("--includeInvisible", "\u5305\u542B\u4E0D\u53EF\u89C1\u8282\u70B9", false).option("--no-auto-start", "\u7981\u7528\u81EA\u52A8\u542F\u52A8 Server").option("--no-retry", "\u7981\u7528\u81EA\u52A8\u91CD\u8BD5").action(async (options) => {
1909
+ await handleGetNodeForSpace(options);
1910
+ });
1911
+ }
1912
+ async function handleGetNodeForSpace(options) {
1913
+ if (!options.nodeId && !options.link) {
1914
+ console.error(`\u9519\u8BEF [${"E011" /* INVALID_PARAMS */}]: \u5FC5\u987B\u63D0\u4F9B --nodeId \u6216 --link \u53C2\u6570`);
1915
+ process.exit(1);
1916
+ }
1917
+ if (options.nodeId && options.link) {
1918
+ console.error(`\u9519\u8BEF [${"E011" /* INVALID_PARAMS */}]: --nodeId \u548C --link \u4E0D\u80FD\u540C\u65F6\u4F7F\u7528`);
1919
+ process.exit(1);
1920
+ }
1921
+ let pageUrl;
1922
+ let nodeId;
1923
+ if (options.link) {
1924
+ const parsed = parseMgpLink(options.link);
1925
+ if (!parsed) {
1926
+ console.error(`\u9519\u8BEF [${"E010" /* INVALID_LINK */}]: \u65E0\u6548\u7684 mgp:// \u94FE\u63A5\u683C\u5F0F`);
1927
+ console.error(`\u63D0\u4F9B\u7684\u94FE\u63A5: ${options.link}`);
1928
+ console.error(`\u671F\u671B\u683C\u5F0F: mgp://[mastergo_page_url]?nodeId=xxx`);
1929
+ process.exit(1);
1930
+ }
1931
+ pageUrl = parsed.pageUrl;
1932
+ nodeId = parsed.nodeId;
1933
+ } else {
1934
+ nodeId = options.nodeId;
1935
+ if (options.fileId) {
1936
+ const domain = options.domain || "mastergo.netease.com";
1937
+ pageUrl = `${domain}/file/${options.fileId}`;
1938
+ }
1939
+ }
1940
+ const client = new MGClient({
1941
+ noAutoStart: options.noAutoStart,
1942
+ noRetry: options.noRetry
1943
+ });
1944
+ try {
1945
+ await client.connect();
1946
+ const params = {
1947
+ nodeId,
1948
+ maxDepth: parseInt(options.maxDepth || "99", 10),
1949
+ includeInvisible: options.includeInvisible || false
1950
+ };
1951
+ const data = await client.requestWithRetry(
1952
+ "get_node_by_id" /* GET_NODE_BY_ID */,
1953
+ params,
1954
+ pageUrl
1955
+ );
1956
+ const spaceData = extractSpaceInfo(data);
1957
+ const outputPath = resolve7(options.output);
1958
+ const outputDir = dirname10(outputPath);
1959
+ mkdirSync8(outputDir, { recursive: true });
1960
+ const jsonContent = JSON.stringify(spaceData, null, 2);
1961
+ writeFileSync7(outputPath, jsonContent, "utf-8");
1962
+ const size = jsonContent.length;
1963
+ const sizeKB = (size / 1024).toFixed(2);
1964
+ console.log(`\u6587\u4EF6\u8DEF\u5F84: ${outputPath}`);
1965
+ if (options.link) {
1966
+ console.log(`Link: ${options.link}`);
1967
+ console.log(`\u9875\u9762 URL: ${pageUrl}`);
1968
+ }
1969
+ console.log(`\u8282\u70B9 ID: ${nodeId}`);
1970
+ console.log(`\u6570\u636E\u5927\u5C0F: ${size.toLocaleString()} \u5B57\u7B26 (\u7EA6 ${sizeKB} KB)`);
1971
+ console.log(`\u8282\u70B9\u6DF1\u5EA6: ${params.maxDepth}`);
1972
+ console.log(`\u6570\u636E\u6A21\u5F0F: \u7A7A\u95F4\u4FE1\u606F (\u4EC5 id, name, x, y, width, height)`);
1973
+ } catch (error) {
1974
+ if (error instanceof MGError) {
1975
+ console.error(`\u9519\u8BEF [${error.code}]: ${error.message}`);
1976
+ } else {
1977
+ console.error(`\u9519\u8BEF: ${error instanceof Error ? error.message : error}`);
1978
+ }
1979
+ process.exit(1);
1980
+ } finally {
1981
+ client.close();
1982
+ }
1983
+ }
1984
+
1569
1985
  // src/cli/index.ts
1570
- var program = new Command8();
1986
+ var program = new Command9();
1571
1987
  program.name("mg-cli").description("MasterGo CLI \u5DE5\u5177 - \u7528\u4E8E Claude Code \u4E0E MasterGo \u901A\u4FE1").version("1.0.0");
1572
1988
  program.addCommand(createServerCommand());
1573
1989
  program.addCommand(createGetNodeByIdCommand());
@@ -1576,5 +1992,6 @@ program.addCommand(createGetAllNodesCommand());
1576
1992
  program.addCommand(createExportImageCommand());
1577
1993
  program.addCommand(createExecuteCodeCommand());
1578
1994
  program.addCommand(createGetAllPagesCommand());
1995
+ program.addCommand(createGetNodeForSpaceCommand());
1579
1996
  program.parse();
1580
1997
  //# sourceMappingURL=cli.js.map