@hangox/mg-cli 1.0.6 → 1.0.8
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 +172 -94
- package/dist/cli.js.map +1 -1
- package/dist/daemon-runner.js +46 -35
- package/dist/daemon-runner.js.map +1 -1
- package/dist/{index-BCd-mD-X.d.ts → index-DmySkKst.d.ts} +149 -5
- package/dist/index.d.ts +64 -5
- package/dist/index.js +333 -49
- package/dist/index.js.map +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.js +46 -35
- package/dist/server.js.map +1 -1
- package/package.json +2 -3
- package/VERSION +0 -1
package/dist/cli.js
CHANGED
|
@@ -103,27 +103,44 @@ function parseMgpLink(link) {
|
|
|
103
103
|
const queryString = urlPart.slice(questionMarkIndex + 1);
|
|
104
104
|
const params = new URLSearchParams(queryString);
|
|
105
105
|
const encodedNodeId = params.get("nodeId");
|
|
106
|
-
|
|
106
|
+
const encodedPageId = params.get("pageId");
|
|
107
|
+
if (!encodedNodeId && !encodedPageId) {
|
|
107
108
|
return null;
|
|
108
109
|
}
|
|
109
|
-
|
|
110
|
-
if (!/^(\d+:\d+)(\/\d+:\d+)*$/.test(nodeId)) {
|
|
110
|
+
if (encodedNodeId && encodedPageId) {
|
|
111
111
|
return null;
|
|
112
112
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const decodedNodePath = decodeURIComponent(encodedNodePath);
|
|
117
|
-
nodePath = decodedNodePath.split("/").filter(Boolean);
|
|
118
|
-
if (!nodePath.every((segment) => /^\d+:\d+$/.test(segment))) {
|
|
113
|
+
if (encodedNodeId) {
|
|
114
|
+
const nodeId = decodeURIComponent(encodedNodeId);
|
|
115
|
+
if (!/^(\d+:\d+)(\/\d+:\d+)*$/.test(nodeId)) {
|
|
119
116
|
return null;
|
|
120
117
|
}
|
|
118
|
+
const encodedNodePath = params.get("nodePath");
|
|
119
|
+
let nodePath;
|
|
120
|
+
if (encodedNodePath) {
|
|
121
|
+
const decodedNodePath = decodeURIComponent(encodedNodePath);
|
|
122
|
+
nodePath = decodedNodePath.split("/").filter(Boolean);
|
|
123
|
+
if (!nodePath.every((segment) => /^\d+:\d+$/.test(segment))) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
pageUrl,
|
|
129
|
+
nodeId,
|
|
130
|
+
nodePath
|
|
131
|
+
};
|
|
121
132
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
133
|
+
if (encodedPageId) {
|
|
134
|
+
const pageId = decodeURIComponent(encodedPageId);
|
|
135
|
+
if (!/^\d+:\d+$/.test(pageId)) {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
pageUrl,
|
|
140
|
+
pageId
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
return null;
|
|
127
144
|
} catch {
|
|
128
145
|
return null;
|
|
129
146
|
}
|
|
@@ -477,7 +494,7 @@ function createLogger(options) {
|
|
|
477
494
|
// src/server/connection-manager.ts
|
|
478
495
|
var ConnectionManager = class {
|
|
479
496
|
logger;
|
|
480
|
-
/** Provider 连接(按页面 URL
|
|
497
|
+
/** Provider 连接(按页面 URL 索引,支持同 URL 多个连接) */
|
|
481
498
|
providers = /* @__PURE__ */ new Map();
|
|
482
499
|
/** Consumer 连接 */
|
|
483
500
|
consumers = /* @__PURE__ */ new Map();
|
|
@@ -513,7 +530,8 @@ var ConnectionManager = class {
|
|
|
513
530
|
*/
|
|
514
531
|
checkHeartbeats() {
|
|
515
532
|
const now = Date.now();
|
|
516
|
-
|
|
533
|
+
const entries = Array.from(this.allConnections.entries());
|
|
534
|
+
for (const [id, ws] of entries) {
|
|
517
535
|
const lastActive = ws.connectionInfo.lastActiveAt.getTime();
|
|
518
536
|
const elapsed = now - lastActive;
|
|
519
537
|
if (elapsed > HEARTBEAT_TIMEOUT) {
|
|
@@ -543,14 +561,10 @@ var ConnectionManager = class {
|
|
|
543
561
|
managedWs.isAlive = true;
|
|
544
562
|
this.allConnections.set(connectionId, managedWs);
|
|
545
563
|
if (type === "provider" /* PROVIDER */ && pageUrl) {
|
|
546
|
-
const existing = this.providers.get(pageUrl);
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
existing.close();
|
|
551
|
-
}
|
|
552
|
-
this.providers.set(pageUrl, managedWs);
|
|
553
|
-
this.logger.info(`Provider \u8FDE\u63A5: ${pageUrl}`);
|
|
564
|
+
const existing = this.providers.get(pageUrl) || [];
|
|
565
|
+
existing.push(managedWs);
|
|
566
|
+
this.providers.set(pageUrl, existing);
|
|
567
|
+
this.logger.info(`Provider \u8FDE\u63A5: ${pageUrl} (\u5F53\u524D\u8BE5\u9875\u9762\u8FDE\u63A5\u6570: ${existing.length})`);
|
|
554
568
|
} else if (type === "consumer" /* CONSUMER */) {
|
|
555
569
|
this.consumers.set(connectionId, managedWs);
|
|
556
570
|
this.logger.info(`Consumer \u8FDE\u63A5: ${connectionId}`);
|
|
@@ -564,8 +578,17 @@ var ConnectionManager = class {
|
|
|
564
578
|
const { connectionId, connectionInfo } = ws;
|
|
565
579
|
this.allConnections.delete(connectionId);
|
|
566
580
|
if (connectionInfo.type === "provider" /* PROVIDER */ && connectionInfo.pageUrl) {
|
|
567
|
-
this.providers.
|
|
568
|
-
|
|
581
|
+
const connections = this.providers.get(connectionInfo.pageUrl);
|
|
582
|
+
if (connections) {
|
|
583
|
+
const index = connections.findIndex((c) => c.connectionId === connectionId);
|
|
584
|
+
if (index !== -1) {
|
|
585
|
+
connections.splice(index, 1);
|
|
586
|
+
if (connections.length === 0) {
|
|
587
|
+
this.providers.delete(connectionInfo.pageUrl);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
this.logger.info(`Provider \u65AD\u5F00: ${connectionInfo.pageUrl} (\u8FDE\u63A5ID: ${connectionId})`);
|
|
569
592
|
} else if (connectionInfo.type === "consumer" /* CONSUMER */) {
|
|
570
593
|
this.consumers.delete(connectionId);
|
|
571
594
|
this.logger.info(`Consumer \u65AD\u5F00: ${connectionId}`);
|
|
@@ -579,31 +602,48 @@ var ConnectionManager = class {
|
|
|
579
602
|
ws.isAlive = true;
|
|
580
603
|
}
|
|
581
604
|
/**
|
|
582
|
-
* 根据页面 URL 查找 Provider
|
|
605
|
+
* 根据页面 URL 查找 Provider(返回第一个可用的连接)
|
|
583
606
|
*/
|
|
584
607
|
findProviderByPageUrl(pageUrl) {
|
|
585
|
-
|
|
608
|
+
const connections = this.providers.get(pageUrl);
|
|
609
|
+
return connections?.[0];
|
|
586
610
|
}
|
|
587
611
|
/**
|
|
588
612
|
* 获取第一个可用的 Provider
|
|
589
613
|
*/
|
|
590
614
|
getFirstProvider() {
|
|
591
|
-
const
|
|
592
|
-
const
|
|
593
|
-
|
|
615
|
+
const allConnections = Array.from(this.providers.values());
|
|
616
|
+
for (const connections of allConnections) {
|
|
617
|
+
if (connections.length > 0) {
|
|
618
|
+
return connections[0];
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
return void 0;
|
|
594
622
|
}
|
|
595
623
|
/**
|
|
596
624
|
* 获取所有 Provider 信息
|
|
597
625
|
*/
|
|
598
626
|
getAllProviders() {
|
|
599
|
-
|
|
627
|
+
const result = [];
|
|
628
|
+
const allConnections = Array.from(this.providers.values());
|
|
629
|
+
for (const connections of allConnections) {
|
|
630
|
+
for (const ws of connections) {
|
|
631
|
+
result.push(ws.connectionInfo);
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
return result;
|
|
600
635
|
}
|
|
601
636
|
/**
|
|
602
637
|
* 获取连接统计
|
|
603
638
|
*/
|
|
604
639
|
getStats() {
|
|
640
|
+
let providerCount = 0;
|
|
641
|
+
const allConnections = Array.from(this.providers.values());
|
|
642
|
+
for (const connections of allConnections) {
|
|
643
|
+
providerCount += connections.length;
|
|
644
|
+
}
|
|
605
645
|
return {
|
|
606
|
-
providers:
|
|
646
|
+
providers: providerCount,
|
|
607
647
|
consumers: this.consumers.size,
|
|
608
648
|
total: this.allConnections.size
|
|
609
649
|
};
|
|
@@ -619,7 +659,8 @@ var ConnectionManager = class {
|
|
|
619
659
|
*/
|
|
620
660
|
closeAll() {
|
|
621
661
|
this.stopHeartbeatCheck();
|
|
622
|
-
|
|
662
|
+
const allWs = Array.from(this.allConnections.values());
|
|
663
|
+
for (const ws of allWs) {
|
|
623
664
|
ws.close();
|
|
624
665
|
}
|
|
625
666
|
this.providers.clear();
|
|
@@ -773,24 +814,11 @@ function getVersion() {
|
|
|
773
814
|
try {
|
|
774
815
|
const currentFile = fileURLToPath(import.meta.url);
|
|
775
816
|
const currentDir = dirname3(currentFile);
|
|
776
|
-
const versionFilePaths = [
|
|
777
|
-
join2(currentDir, "..", "VERSION"),
|
|
778
|
-
// dist/xxx.js -> ../VERSION
|
|
779
|
-
join2(currentDir, "..", "..", "VERSION")
|
|
780
|
-
// src/shared/version.ts -> ../../VERSION
|
|
781
|
-
];
|
|
782
|
-
for (const versionFilePath of versionFilePaths) {
|
|
783
|
-
if (existsSync3(versionFilePath)) {
|
|
784
|
-
const version = readFileSync2(versionFilePath, "utf-8").trim();
|
|
785
|
-
if (version) {
|
|
786
|
-
cachedVersion = version;
|
|
787
|
-
return cachedVersion;
|
|
788
|
-
}
|
|
789
|
-
}
|
|
790
|
-
}
|
|
791
817
|
const packageJsonPaths = [
|
|
792
818
|
join2(currentDir, "..", "package.json"),
|
|
819
|
+
// dist/xxx.js -> ../package.json
|
|
793
820
|
join2(currentDir, "..", "..", "package.json")
|
|
821
|
+
// src/shared/version.ts -> ../../package.json
|
|
794
822
|
];
|
|
795
823
|
for (const packageJsonPath of packageJsonPaths) {
|
|
796
824
|
if (existsSync3(packageJsonPath)) {
|
|
@@ -808,7 +836,11 @@ function getVersion() {
|
|
|
808
836
|
return cachedVersion;
|
|
809
837
|
}
|
|
810
838
|
}
|
|
839
|
+
var DEV_VERSION = "9.9.9";
|
|
811
840
|
function isVersionMatch(version1, version2) {
|
|
841
|
+
if (version1 === DEV_VERSION || version2 === DEV_VERSION) {
|
|
842
|
+
return true;
|
|
843
|
+
}
|
|
812
844
|
return version1 === version2;
|
|
813
845
|
}
|
|
814
846
|
|
|
@@ -1200,19 +1232,8 @@ var MGClient = class {
|
|
|
1200
1232
|
if (isProcessRunning(serverInfo.pid)) {
|
|
1201
1233
|
const currentVersion = getVersion();
|
|
1202
1234
|
if (!isVersionMatch(currentVersion, serverInfo.version)) {
|
|
1203
|
-
console.
|
|
1204
|
-
console.
|
|
1205
|
-
try {
|
|
1206
|
-
const newInfo = await restartServer(serverInfo.port);
|
|
1207
|
-
console.log(`Server \u5DF2\u91CD\u542F\uFF0C\u7248\u672C: ${newInfo.version}`);
|
|
1208
|
-
await this.waitForServer(newInfo.port);
|
|
1209
|
-
return;
|
|
1210
|
-
} catch (error) {
|
|
1211
|
-
throw new MGError(
|
|
1212
|
-
"E015" /* SERVER_START_FAILED */,
|
|
1213
|
-
`\u91CD\u542F Server \u5931\u8D25: ${error instanceof Error ? error.message : error}`
|
|
1214
|
-
);
|
|
1215
|
-
}
|
|
1235
|
+
console.warn(`\u26A0\uFE0F \u7248\u672C\u4E0D\u5339\u914D: CLI ${currentVersion} vs Server ${serverInfo.version}`);
|
|
1236
|
+
console.warn("\u63D0\u793A: \u5982\u9700\u5BF9\u9F50\u7248\u672C\uFF0C\u8BF7\u624B\u52A8\u8FD0\u884C `npx -y @hangox/mg-cli@latest server restart`");
|
|
1216
1237
|
}
|
|
1217
1238
|
try {
|
|
1218
1239
|
await this.tryConnect(serverInfo.port);
|
|
@@ -1559,7 +1580,7 @@ import { writeFileSync as writeFileSync3 } from "fs";
|
|
|
1559
1580
|
import { resolve as resolve3, dirname as dirname6 } from "path";
|
|
1560
1581
|
import { mkdirSync as mkdirSync4 } from "fs";
|
|
1561
1582
|
function createGetNodeByLinkCommand() {
|
|
1562
|
-
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) => {
|
|
1583
|
+
return new Command3("get_node_by_link").description("\u89E3\u6790 mgp:// \u534F\u8BAE\u94FE\u63A5\u5E76\u83B7\u53D6\u8282\u70B9/\u9875\u9762\u4FE1\u606F").requiredOption("--link <url>", "mgp:// \u534F\u8BAE\u94FE\u63A5\uFF08\u652F\u6301 nodeId \u548C pageId\uFF09").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) => {
|
|
1563
1584
|
await handleGetNodeByLink(options);
|
|
1564
1585
|
});
|
|
1565
1586
|
}
|
|
@@ -1568,26 +1589,43 @@ async function handleGetNodeByLink(options) {
|
|
|
1568
1589
|
if (!parsed) {
|
|
1569
1590
|
console.error(`\u9519\u8BEF [${"E010" /* INVALID_LINK */}]: \u65E0\u6548\u7684 mgp:// \u94FE\u63A5\u683C\u5F0F`);
|
|
1570
1591
|
console.error(`\u63D0\u4F9B\u7684\u94FE\u63A5: ${options.link}`);
|
|
1571
|
-
console.error(`\u671F\u671B\u683C\u5F0F
|
|
1592
|
+
console.error(`\u671F\u671B\u683C\u5F0F:`);
|
|
1593
|
+
console.error(` \u8282\u70B9\u94FE\u63A5: mgp://[mastergo_page_url]?nodeId=[\u8282\u70B9ID]`);
|
|
1594
|
+
console.error(` \u9875\u9762\u94FE\u63A5: mgp://[mastergo_page_url]?pageId=[\u9875\u9762ID]`);
|
|
1572
1595
|
process.exit(1);
|
|
1573
1596
|
}
|
|
1574
|
-
const { pageUrl, nodeId } = parsed;
|
|
1597
|
+
const { pageUrl, nodeId, pageId } = parsed;
|
|
1598
|
+
const isPageLink = !!pageId;
|
|
1575
1599
|
const client = new MGClient({
|
|
1576
1600
|
noAutoStart: options.noAutoStart,
|
|
1577
1601
|
noRetry: options.noRetry
|
|
1578
1602
|
});
|
|
1579
1603
|
try {
|
|
1580
1604
|
await client.connect();
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1605
|
+
let data;
|
|
1606
|
+
if (isPageLink) {
|
|
1607
|
+
const params = {
|
|
1608
|
+
pageId,
|
|
1609
|
+
maxDepth: parseInt(options.maxDepth || "1", 10),
|
|
1610
|
+
includeInvisible: options.includeInvisible || false
|
|
1611
|
+
};
|
|
1612
|
+
data = await client.requestWithRetry(
|
|
1613
|
+
"get_page_by_id" /* GET_PAGE_BY_ID */,
|
|
1614
|
+
params,
|
|
1615
|
+
pageUrl
|
|
1616
|
+
);
|
|
1617
|
+
} else {
|
|
1618
|
+
const params = {
|
|
1619
|
+
nodeId,
|
|
1620
|
+
maxDepth: parseInt(options.maxDepth || "1", 10),
|
|
1621
|
+
includeInvisible: options.includeInvisible || false
|
|
1622
|
+
};
|
|
1623
|
+
data = await client.requestWithRetry(
|
|
1624
|
+
"get_node_by_id" /* GET_NODE_BY_ID */,
|
|
1625
|
+
params,
|
|
1626
|
+
pageUrl
|
|
1627
|
+
);
|
|
1628
|
+
}
|
|
1591
1629
|
const outputData = options.raw ? data : trimNodeDefaults(data);
|
|
1592
1630
|
const outputPath = resolve3(options.output);
|
|
1593
1631
|
const outputDir = dirname6(outputPath);
|
|
@@ -1599,9 +1637,15 @@ async function handleGetNodeByLink(options) {
|
|
|
1599
1637
|
console.log(`\u6587\u4EF6\u8DEF\u5F84: ${outputPath}`);
|
|
1600
1638
|
console.log(`Link: ${options.link}`);
|
|
1601
1639
|
console.log(`\u9875\u9762 URL: ${pageUrl}`);
|
|
1602
|
-
|
|
1640
|
+
if (isPageLink) {
|
|
1641
|
+
console.log(`\u9875\u9762 ID: ${pageId}`);
|
|
1642
|
+
console.log(`\u7C7B\u578B: \u9875\u9762`);
|
|
1643
|
+
} else {
|
|
1644
|
+
console.log(`\u8282\u70B9 ID: ${nodeId}`);
|
|
1645
|
+
console.log(`\u7C7B\u578B: \u8282\u70B9`);
|
|
1646
|
+
}
|
|
1603
1647
|
console.log(`\u6570\u636E\u5927\u5C0F: ${size.toLocaleString()} \u5B57\u7B26 (\u7EA6 ${sizeKB} KB)`);
|
|
1604
|
-
console.log(`\
|
|
1648
|
+
console.log(`\u904D\u5386\u6DF1\u5EA6: ${options.maxDepth || "1"}`);
|
|
1605
1649
|
if (!options.raw) {
|
|
1606
1650
|
console.log(`\u6570\u636E\u6A21\u5F0F: \u7CBE\u7B80\u6A21\u5F0F (\u4F7F\u7528 --raw \u83B7\u53D6\u5B8C\u6574\u6570\u636E)`);
|
|
1607
1651
|
}
|
|
@@ -1696,10 +1740,11 @@ async function handleExportImage(options) {
|
|
|
1696
1740
|
let nodeId;
|
|
1697
1741
|
if (options.link) {
|
|
1698
1742
|
const linkInfo = parseMgpLink(options.link);
|
|
1699
|
-
if (!linkInfo) {
|
|
1743
|
+
if (!linkInfo || !linkInfo.nodeId) {
|
|
1700
1744
|
console.error(`\u9519\u8BEF [${"E010" /* INVALID_LINK */}]: \u65E0\u6548\u7684 mgp:// \u94FE\u63A5\u683C\u5F0F`);
|
|
1701
1745
|
console.error(`\u63D0\u4F9B\u7684\u94FE\u63A5: ${options.link}`);
|
|
1702
1746
|
console.error(`\u671F\u671B\u683C\u5F0F: mgp://[mastergo_page_url]?nodeId=xxx`);
|
|
1747
|
+
console.error(`\u6CE8\u610F: \u6B64\u547D\u4EE4\u4E0D\u652F\u6301\u9875\u9762\u94FE\u63A5 (pageId)\uFF0C\u8BF7\u4F7F\u7528\u8282\u70B9\u94FE\u63A5 (nodeId)`);
|
|
1703
1748
|
process.exit(1);
|
|
1704
1749
|
}
|
|
1705
1750
|
pageUrl = linkInfo.pageUrl;
|
|
@@ -1865,7 +1910,7 @@ async function handleExecuteCode(code, options) {
|
|
|
1865
1910
|
if (!parsed) {
|
|
1866
1911
|
console.error(`\u9519\u8BEF [${"E010" /* INVALID_LINK */}]: \u65E0\u6548\u7684 mgp:// \u94FE\u63A5\u683C\u5F0F`);
|
|
1867
1912
|
console.error(`\u63D0\u4F9B\u7684\u94FE\u63A5: ${options.link}`);
|
|
1868
|
-
console.error(`\u671F\u671B\u683C\u5F0F: mgp://[mastergo_page_url]?nodeId=xxx`);
|
|
1913
|
+
console.error(`\u671F\u671B\u683C\u5F0F: mgp://[mastergo_page_url]?nodeId=xxx \u6216 mgp://[mastergo_page_url]?pageId=xxx`);
|
|
1869
1914
|
process.exit(1);
|
|
1870
1915
|
}
|
|
1871
1916
|
pageUrl = parsed.pageUrl;
|
|
@@ -1970,7 +2015,7 @@ import { writeFileSync as writeFileSync7 } from "fs";
|
|
|
1970
2015
|
import { resolve as resolve7, dirname as dirname10 } from "path";
|
|
1971
2016
|
import { mkdirSync as mkdirSync8 } from "fs";
|
|
1972
2017
|
function createGetNodeForSpaceCommand() {
|
|
1973
|
-
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) => {
|
|
2018
|
+
return new Command8("get_node_for_space").description("\u83B7\u53D6\u8282\u70B9\u6216\u9875\u9762\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\u3002\u652F\u6301 nodeId \u548C pageId \u94FE\u63A5").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\uFF08\u652F\u6301 nodeId \u548C pageId\uFF09\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) => {
|
|
1974
2019
|
await handleGetNodeForSpace(options);
|
|
1975
2020
|
});
|
|
1976
2021
|
}
|
|
@@ -1985,16 +2030,27 @@ async function handleGetNodeForSpace(options) {
|
|
|
1985
2030
|
}
|
|
1986
2031
|
let pageUrl;
|
|
1987
2032
|
let nodeId;
|
|
2033
|
+
let pageId;
|
|
2034
|
+
let isPageMode = false;
|
|
1988
2035
|
if (options.link) {
|
|
1989
2036
|
const parsed = parseMgpLink(options.link);
|
|
1990
2037
|
if (!parsed) {
|
|
1991
2038
|
console.error(`\u9519\u8BEF [${"E010" /* INVALID_LINK */}]: \u65E0\u6548\u7684 mgp:// \u94FE\u63A5\u683C\u5F0F`);
|
|
1992
2039
|
console.error(`\u63D0\u4F9B\u7684\u94FE\u63A5: ${options.link}`);
|
|
1993
|
-
console.error(`\u671F\u671B\u683C\u5F0F: mgp://[mastergo_page_url]?nodeId=xxx`);
|
|
2040
|
+
console.error(`\u671F\u671B\u683C\u5F0F: mgp://[mastergo_page_url]?nodeId=xxx \u6216 mgp://[mastergo_page_url]?pageId=xxx`);
|
|
1994
2041
|
process.exit(1);
|
|
1995
2042
|
}
|
|
1996
2043
|
pageUrl = parsed.pageUrl;
|
|
1997
|
-
|
|
2044
|
+
if (parsed.pageId) {
|
|
2045
|
+
isPageMode = true;
|
|
2046
|
+
pageId = parsed.pageId;
|
|
2047
|
+
} else if (parsed.nodeId) {
|
|
2048
|
+
nodeId = parsed.nodeId;
|
|
2049
|
+
} else {
|
|
2050
|
+
console.error(`\u9519\u8BEF [${"E010" /* INVALID_LINK */}]: \u94FE\u63A5\u4E2D\u5FC5\u987B\u5305\u542B nodeId \u6216 pageId \u53C2\u6570`);
|
|
2051
|
+
console.error(`\u63D0\u4F9B\u7684\u94FE\u63A5: ${options.link}`);
|
|
2052
|
+
process.exit(1);
|
|
2053
|
+
}
|
|
1998
2054
|
} else {
|
|
1999
2055
|
nodeId = options.nodeId;
|
|
2000
2056
|
if (options.fileId) {
|
|
@@ -2008,16 +2064,32 @@ async function handleGetNodeForSpace(options) {
|
|
|
2008
2064
|
});
|
|
2009
2065
|
try {
|
|
2010
2066
|
await client.connect();
|
|
2011
|
-
const
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2067
|
+
const maxDepth = parseInt(options.maxDepth || "99", 10);
|
|
2068
|
+
const includeInvisible = options.includeInvisible || false;
|
|
2069
|
+
let data;
|
|
2070
|
+
if (isPageMode && pageId) {
|
|
2071
|
+
const params = {
|
|
2072
|
+
pageId,
|
|
2073
|
+
maxDepth,
|
|
2074
|
+
includeInvisible
|
|
2075
|
+
};
|
|
2076
|
+
data = await client.requestWithRetry(
|
|
2077
|
+
"get_page_by_id" /* GET_PAGE_BY_ID */,
|
|
2078
|
+
params,
|
|
2079
|
+
pageUrl
|
|
2080
|
+
);
|
|
2081
|
+
} else {
|
|
2082
|
+
const params = {
|
|
2083
|
+
nodeId,
|
|
2084
|
+
maxDepth,
|
|
2085
|
+
includeInvisible
|
|
2086
|
+
};
|
|
2087
|
+
data = await client.requestWithRetry(
|
|
2088
|
+
"get_node_by_id" /* GET_NODE_BY_ID */,
|
|
2089
|
+
params,
|
|
2090
|
+
pageUrl
|
|
2091
|
+
);
|
|
2092
|
+
}
|
|
2021
2093
|
const spaceData = extractSpaceInfo(data);
|
|
2022
2094
|
const outputPath = resolve7(options.output);
|
|
2023
2095
|
const outputDir = dirname10(outputPath);
|
|
@@ -2031,9 +2103,15 @@ async function handleGetNodeForSpace(options) {
|
|
|
2031
2103
|
console.log(`Link: ${options.link}`);
|
|
2032
2104
|
console.log(`\u9875\u9762 URL: ${pageUrl}`);
|
|
2033
2105
|
}
|
|
2034
|
-
|
|
2106
|
+
if (isPageMode) {
|
|
2107
|
+
console.log(`\u9875\u9762 ID: ${pageId}`);
|
|
2108
|
+
console.log(`\u6A21\u5F0F: \u9875\u9762\u7A7A\u95F4\u4FE1\u606F`);
|
|
2109
|
+
} else {
|
|
2110
|
+
console.log(`\u8282\u70B9 ID: ${nodeId}`);
|
|
2111
|
+
console.log(`\u6A21\u5F0F: \u8282\u70B9\u7A7A\u95F4\u4FE1\u606F`);
|
|
2112
|
+
}
|
|
2035
2113
|
console.log(`\u6570\u636E\u5927\u5C0F: ${size.toLocaleString()} \u5B57\u7B26 (\u7EA6 ${sizeKB} KB)`);
|
|
2036
|
-
console.log(`\u8282\u70B9\u6DF1\u5EA6: ${
|
|
2114
|
+
console.log(`\u8282\u70B9\u6DF1\u5EA6: ${maxDepth}`);
|
|
2037
2115
|
console.log(`\u6570\u636E\u6A21\u5F0F: \u7A7A\u95F4\u4FE1\u606F (\u4EC5 id, name, x, y, width, height)`);
|
|
2038
2116
|
} catch (error) {
|
|
2039
2117
|
if (error instanceof MGError) {
|