@hangox/mg-cli 1.3.3 → 1.4.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.
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ import '../dist/native-host.js'
package/dist/cli.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  // src/cli/index.ts
4
- import { Command as Command14 } from "commander";
4
+ import { Command as Command15 } from "commander";
5
5
 
6
6
  // src/cli/version-check.ts
7
7
  import { existsSync as existsSync2, mkdirSync, readFileSync as readFileSync2, writeFileSync } from "fs";
@@ -1694,7 +1694,7 @@ var MGClient = class {
1694
1694
  const currentVersion = getVersion();
1695
1695
  if (!isVersionMatch(currentVersion, serverInfo.version)) {
1696
1696
  console.warn(`\u26A0\uFE0F \u7248\u672C\u4E0D\u5339\u914D: CLI ${currentVersion} vs Server ${serverInfo.version}`);
1697
- console.warn("\u63D0\u793A: \u5982\u9700\u5BF9\u9F50\u7248\u672C\uFF0C\u8BF7\u624B\u52A8\u8FD0\u884C `npx -y @hangox/mg-cli@latest server restart`");
1697
+ console.warn("\u63D0\u793A: \u5982\u9700\u5BF9\u9F50\u7248\u672C\uFF0C\u8BF7\u624B\u52A8\u8FD0\u884C `mg-cli server restart`");
1698
1698
  }
1699
1699
  try {
1700
1700
  await this.tryConnect(serverInfo.port);
@@ -2890,7 +2890,7 @@ async function handleOpenPage(options) {
2890
2890
  console.error("\u9519\u8BEF: \u6CA1\u6709 Chrome \u6269\u5C55\u8FDE\u63A5\u5230 Server\u3002\u8BF7\u786E\u4FDD\u6D4F\u89C8\u5668\u5DF2\u6253\u5F00\u5E76\u5B89\u88C5\u4E86 MG Plugin");
2891
2891
  console.error("\n\u{1F4A1} \u89E3\u51B3\u65B9\u6CD5:");
2892
2892
  console.error(" 1. \u67E5\u770B\u53EF\u7528\u7684\u6269\u5C55\u5B9E\u4F8B:");
2893
- console.error(" npx -y @hangox/mg-cli@latest list_extensions");
2893
+ console.error(" mg-cli list_extensions");
2894
2894
  console.error("");
2895
2895
  console.error(" 2. \u5982\u679C\u6CA1\u6709\u6269\u5C55\u8FDE\u63A5\uFF0C\u8BF7\u68C0\u67E5:");
2896
2896
  console.error(" \u2022 \u6D4F\u89C8\u5668\u662F\u5426\u5DF2\u6253\u5F00");
@@ -2926,7 +2926,7 @@ async function handleOpenPage(options) {
2926
2926
  if (errorMsg.includes("NO_EXTENSION_CONNECTED") || errorMsg.includes("\u672A\u627E\u5230")) {
2927
2927
  console.error("\n\u{1F4A1} \u89E3\u51B3\u65B9\u6CD5:");
2928
2928
  console.error(" 1. \u67E5\u770B\u53EF\u7528\u7684\u6269\u5C55\u5B9E\u4F8B:");
2929
- console.error(" npx -y @hangox/mg-cli@latest list_extensions");
2929
+ console.error(" mg-cli list_extensions");
2930
2930
  console.error("");
2931
2931
  console.error(" 2. \u5982\u679C\u6CA1\u6709\u6269\u5C55\u8FDE\u63A5\uFF0C\u8BF7\u68C0\u67E5:");
2932
2932
  console.error(" \u2022 \u6D4F\u89C8\u5668\u662F\u5426\u5DF2\u6253\u5F00");
@@ -2981,7 +2981,7 @@ async function handleNavigateToNode(options) {
2981
2981
  console.error("\u9519\u8BEF: \u6CA1\u6709 Chrome \u6269\u5C55\u8FDE\u63A5\u5230 Server\u3002\u8BF7\u786E\u4FDD\u6D4F\u89C8\u5668\u5DF2\u6253\u5F00\u5E76\u5B89\u88C5\u4E86 MG Plugin");
2982
2982
  console.error("\n\u{1F4A1} \u89E3\u51B3\u65B9\u6CD5:");
2983
2983
  console.error(" 1. \u67E5\u770B\u53EF\u7528\u7684\u6269\u5C55\u5B9E\u4F8B:");
2984
- console.error(" npx -y @hangox/mg-cli@latest list_extensions");
2984
+ console.error(" mg-cli list_extensions");
2985
2985
  console.error("");
2986
2986
  console.error(" 2. \u5982\u679C\u6CA1\u6709\u6269\u5C55\u8FDE\u63A5\uFF0C\u8BF7\u68C0\u67E5:");
2987
2987
  console.error(" \u2022 \u6D4F\u89C8\u5668\u662F\u5426\u5DF2\u6253\u5F00");
@@ -3023,7 +3023,7 @@ async function handleNavigateToNode(options) {
3023
3023
  if (errorMsg.includes("NO_EXTENSION_CONNECTED") || errorMsg.includes("\u672A\u627E\u5230")) {
3024
3024
  console.error("\n\u{1F4A1} \u89E3\u51B3\u65B9\u6CD5:");
3025
3025
  console.error(" 1. \u67E5\u770B\u53EF\u7528\u7684\u6269\u5C55\u5B9E\u4F8B:");
3026
- console.error(" npx -y @hangox/mg-cli@latest list_extensions");
3026
+ console.error(" mg-cli list_extensions");
3027
3027
  console.error("");
3028
3028
  console.error(" 2. \u5982\u679C\u6CA1\u6709\u6269\u5C55\u8FDE\u63A5\uFF0C\u8BF7\u68C0\u67E5:");
3029
3029
  console.error(" \u2022 \u6D4F\u89C8\u5668\u662F\u5426\u5DF2\u6253\u5F00");
@@ -3408,8 +3408,180 @@ async function handleVisualize(options) {
3408
3408
  }
3409
3409
  }
3410
3410
 
3411
+ // src/cli/commands/register.ts
3412
+ import { Command as Command14 } from "commander";
3413
+
3414
+ // src/scripts/register-utils.ts
3415
+ import fs from "fs";
3416
+ import path from "path";
3417
+ import os from "os";
3418
+ import { execSync as execSync2 } from "child_process";
3419
+ import { fileURLToPath as fileURLToPath3 } from "url";
3420
+ var HOST_NAME = "com.hangox.mgplugin";
3421
+ var EXTENSION_ID = "ddhihanlpcdneicohnglnaliefnkaeja";
3422
+ var DESCRIPTION = "MasterGo Plugin Native Messaging Host";
3423
+ function getBrowserPaths(browser) {
3424
+ const home = os.homedir();
3425
+ const platform2 = os.platform();
3426
+ const browserDirs = {
3427
+ chrome: {
3428
+ darwin: path.join(home, "Library/Application Support/Google/Chrome/NativeMessagingHosts"),
3429
+ linux: path.join(home, ".config/google-chrome/NativeMessagingHosts"),
3430
+ win32: path.join(
3431
+ process.env.APPDATA || path.join(home, "AppData/Roaming"),
3432
+ "Google/Chrome/NativeMessagingHosts"
3433
+ )
3434
+ },
3435
+ chromium: {
3436
+ darwin: path.join(home, "Library/Application Support/Chromium/NativeMessagingHosts"),
3437
+ linux: path.join(home, ".config/chromium/NativeMessagingHosts"),
3438
+ win32: path.join(
3439
+ process.env.APPDATA || path.join(home, "AppData/Roaming"),
3440
+ "Chromium/NativeMessagingHosts"
3441
+ )
3442
+ },
3443
+ edge: {
3444
+ darwin: path.join(
3445
+ home,
3446
+ "Library/Application Support/Microsoft Edge/NativeMessagingHosts"
3447
+ ),
3448
+ linux: path.join(home, ".config/microsoft-edge/NativeMessagingHosts"),
3449
+ win32: path.join(
3450
+ process.env.APPDATA || path.join(home, "AppData/Roaming"),
3451
+ "Microsoft/Edge/NativeMessagingHosts"
3452
+ )
3453
+ }
3454
+ };
3455
+ const registryKeys = {
3456
+ chrome: `HKCU\\Software\\Google\\Chrome\\NativeMessagingHosts\\${HOST_NAME}`,
3457
+ chromium: `HKCU\\Software\\Chromium\\NativeMessagingHosts\\${HOST_NAME}`,
3458
+ edge: `HKCU\\Software\\Microsoft\\Edge\\NativeMessagingHosts\\${HOST_NAME}`
3459
+ };
3460
+ const manifestDir = browserDirs[browser][platform2];
3461
+ if (!manifestDir) {
3462
+ throw new Error(`\u4E0D\u652F\u6301\u7684\u5E73\u53F0: ${platform2}`);
3463
+ }
3464
+ return {
3465
+ manifestDir,
3466
+ registryKey: platform2 === "win32" ? registryKeys[browser] : void 0
3467
+ };
3468
+ }
3469
+ function getNativeHostPath() {
3470
+ const currentFile = fileURLToPath3(import.meta.url);
3471
+ const distDir = path.dirname(currentFile);
3472
+ return path.join(distDir, "native-host.js");
3473
+ }
3474
+ function createManifest() {
3475
+ return {
3476
+ name: HOST_NAME,
3477
+ description: DESCRIPTION,
3478
+ path: getNativeHostPath(),
3479
+ type: "stdio",
3480
+ allowed_origins: [`chrome-extension://${EXTENSION_ID}/`]
3481
+ };
3482
+ }
3483
+ function register(browser = "chrome") {
3484
+ const config = getBrowserPaths(browser);
3485
+ const manifestPath = path.join(config.manifestDir, `${HOST_NAME}.json`);
3486
+ const manifest = createManifest();
3487
+ console.log("\u6B63\u5728\u6CE8\u518C Native Messaging Host...");
3488
+ console.log(` Host Name: ${HOST_NAME}`);
3489
+ console.log(` Extension ID: ${EXTENSION_ID}`);
3490
+ console.log(` Browser: ${browser}`);
3491
+ console.log(` Native Host: ${manifest.path}`);
3492
+ console.log(` Manifest: ${manifestPath}`);
3493
+ fs.mkdirSync(config.manifestDir, { recursive: true });
3494
+ fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
3495
+ if (os.platform() !== "win32") {
3496
+ try {
3497
+ fs.chmodSync(manifest.path, "755");
3498
+ } catch {
3499
+ console.warn(" \u26A0\uFE0F \u65E0\u6CD5\u8BBE\u7F6E\u6267\u884C\u6743\u9650\uFF0C\u53EF\u80FD\u9700\u8981\u624B\u52A8\u8BBE\u7F6E");
3500
+ }
3501
+ }
3502
+ if (config.registryKey) {
3503
+ try {
3504
+ execSync2(`reg add "${config.registryKey}" /ve /t REG_SZ /d "${manifestPath}" /f`, {
3505
+ stdio: "pipe"
3506
+ });
3507
+ console.log(" \u2713 Windows \u6CE8\u518C\u8868\u5DF2\u66F4\u65B0");
3508
+ } catch {
3509
+ console.warn(" \u26A0\uFE0F \u6CE8\u518C\u8868\u5199\u5165\u5931\u8D25\uFF0C\u53EF\u80FD\u9700\u8981\u7BA1\u7406\u5458\u6743\u9650");
3510
+ }
3511
+ }
3512
+ console.log("\u2713 Native Messaging Host \u6CE8\u518C\u6210\u529F");
3513
+ }
3514
+ function unregister(browser = "chrome") {
3515
+ const config = getBrowserPaths(browser);
3516
+ const manifestPath = path.join(config.manifestDir, `${HOST_NAME}.json`);
3517
+ if (fs.existsSync(manifestPath)) {
3518
+ fs.unlinkSync(manifestPath);
3519
+ console.log(` \u5DF2\u5220\u9664: ${manifestPath}`);
3520
+ }
3521
+ if (config.registryKey) {
3522
+ try {
3523
+ execSync2(`reg delete "${config.registryKey}" /f`, { stdio: "pipe" });
3524
+ console.log(" \u2713 Windows \u6CE8\u518C\u8868\u9879\u5DF2\u5220\u9664");
3525
+ } catch {
3526
+ }
3527
+ }
3528
+ }
3529
+ function checkRegistration(browser = "chrome") {
3530
+ const config = getBrowserPaths(browser);
3531
+ const manifestPath = path.join(config.manifestDir, `${HOST_NAME}.json`);
3532
+ if (!fs.existsSync(manifestPath)) {
3533
+ return { registered: false };
3534
+ }
3535
+ try {
3536
+ const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf-8"));
3537
+ return {
3538
+ registered: true,
3539
+ manifestPath,
3540
+ manifest
3541
+ };
3542
+ } catch {
3543
+ return { registered: false, manifestPath };
3544
+ }
3545
+ }
3546
+
3547
+ // src/cli/commands/register.ts
3548
+ function createRegisterCommand() {
3549
+ const cmd = new Command14("register").description("\u6CE8\u518C Native Messaging Host\uFF08\u8BA9 Chrome \u63D2\u4EF6\u80FD\u81EA\u52A8\u542F\u52A8 Server\uFF09").option("--unregister", "\u5378\u8F7D Native Host").option("--check", "\u68C0\u67E5\u6CE8\u518C\u72B6\u6001").option("--browser <type>", "\u6307\u5B9A\u6D4F\u89C8\u5668 (chrome/chromium/edge)", "chrome").action(async (options) => {
3550
+ const browser = options.browser;
3551
+ if (!["chrome", "chromium", "edge"].includes(browser)) {
3552
+ console.error(`\u4E0D\u652F\u6301\u7684\u6D4F\u89C8\u5668: ${browser}`);
3553
+ console.log("\u652F\u6301\u7684\u6D4F\u89C8\u5668: chrome, chromium, edge");
3554
+ process.exitCode = 1;
3555
+ return;
3556
+ }
3557
+ if (options.check) {
3558
+ const status = checkRegistration(browser);
3559
+ if (status.registered) {
3560
+ console.log("\u2713 \u5DF2\u6CE8\u518C");
3561
+ console.log(` Manifest: ${status.manifestPath}`);
3562
+ if (status.manifest) {
3563
+ console.log(` Native Host: ${status.manifest.path}`);
3564
+ }
3565
+ } else {
3566
+ console.log("\u2717 \u672A\u6CE8\u518C");
3567
+ if (status.manifestPath) {
3568
+ console.log(` Manifest \u8DEF\u5F84: ${status.manifestPath}`);
3569
+ }
3570
+ }
3571
+ return;
3572
+ }
3573
+ if (options.unregister) {
3574
+ unregister(browser);
3575
+ console.log("\u2713 \u5DF2\u5378\u8F7D Native Host");
3576
+ return;
3577
+ }
3578
+ register(browser);
3579
+ });
3580
+ return cmd;
3581
+ }
3582
+
3411
3583
  // src/cli/index.ts
3412
- var program = new Command14();
3584
+ var program = new Command15();
3413
3585
  program.name("mg-cli").description("MasterGo CLI \u5DE5\u5177 - \u7528\u4E8E Claude Code \u4E0E MasterGo \u901A\u4FE1").version(getVersion());
3414
3586
  program.addCommand(createServerCommand());
3415
3587
  program.addCommand(createGetNodeByIdCommand());
@@ -3424,6 +3596,7 @@ program.addCommand(createListExtensionsCommand());
3424
3596
  program.addCommand(createOpenPageCommand());
3425
3597
  program.addCommand(createNavigateToNodeCommand());
3426
3598
  program.addCommand(createVisualizeCommand());
3599
+ program.addCommand(createRegisterCommand());
3427
3600
  checkForUpdates();
3428
3601
  program.parse();
3429
3602
  //# sourceMappingURL=cli.js.map