@hangox/mg-cli 1.5.0 → 1.5.1

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
@@ -4188,6 +4188,7 @@ import { fileURLToPath as fileURLToPath3 } from "url";
4188
4188
  var HOST_NAME = "com.hangox.mgplugin";
4189
4189
  var EXTENSION_ID = "ddhihanlpcdneicohnglnaliefnkaeja";
4190
4190
  var DESCRIPTION = "MasterGo Plugin Native Messaging Host";
4191
+ var KNOWN_BROWSERS = ["chrome", "chromium", "edge"];
4191
4192
  function getBrowserPaths(browser) {
4192
4193
  const home = os.homedir();
4193
4194
  const platform2 = os.platform();
@@ -4239,11 +4240,88 @@ function getNativeHostPath() {
4239
4240
  const distDir = path.dirname(currentFile);
4240
4241
  return path.join(distDir, "native-host.js");
4241
4242
  }
4242
- function createManifest() {
4243
+ var INSTALL_DIR_NAME = "mg-cli";
4244
+ function getInstallDir() {
4245
+ const home = os.homedir();
4246
+ if (os.platform() === "win32") {
4247
+ const appData = process.env.APPDATA || path.join(home, "AppData/Roaming");
4248
+ return path.join(appData, INSTALL_DIR_NAME);
4249
+ }
4250
+ return path.join(home, ".config", INSTALL_DIR_NAME);
4251
+ }
4252
+ var WRAPPER_FILENAME_UNIX = "native-host-wrapper.sh";
4253
+ var WRAPPER_FILENAME_WIN = "native-host-wrapper.cmd";
4254
+ function getWrapperPath(installDir) {
4255
+ const filename = os.platform() === "win32" ? WRAPPER_FILENAME_WIN : WRAPPER_FILENAME_UNIX;
4256
+ return path.join(installDir, filename);
4257
+ }
4258
+ function ensureNativeHostWrapper(sourceNativeHostPath) {
4259
+ const installDir = getInstallDir();
4260
+ fs.mkdirSync(installDir, { recursive: true });
4261
+ const installedNativeHostPath = path.join(installDir, "native-host.js");
4262
+ fs.copyFileSync(sourceNativeHostPath, installedNativeHostPath);
4263
+ const wrapperPath = getWrapperPath(installDir);
4264
+ if (os.platform() === "win32") {
4265
+ const wrapperContent2 = `@echo off\r
4266
+ "${process.execPath}" "${installedNativeHostPath}" %*\r
4267
+ `;
4268
+ fs.writeFileSync(wrapperPath, wrapperContent2);
4269
+ return wrapperPath;
4270
+ }
4271
+ const wrapperContent = `#!/bin/sh
4272
+ exec "${process.execPath}" "${installedNativeHostPath}" "$@"
4273
+ `;
4274
+ fs.writeFileSync(wrapperPath, wrapperContent);
4275
+ try {
4276
+ fs.chmodSync(wrapperPath, "755");
4277
+ } catch {
4278
+ console.warn(" \u26A0\uFE0F \u65E0\u6CD5\u8BBE\u7F6E wrapper \u6267\u884C\u6743\u9650\uFF0C\u53EF\u80FD\u9700\u8981\u624B\u52A8\u8BBE\u7F6E");
4279
+ }
4280
+ return wrapperPath;
4281
+ }
4282
+ function readWrapperNodePath(wrapperPath) {
4283
+ try {
4284
+ const content = fs.readFileSync(wrapperPath, "utf-8");
4285
+ const match = content.match(/"([^"]+)"/);
4286
+ return match?.[1];
4287
+ } catch {
4288
+ return void 0;
4289
+ }
4290
+ }
4291
+ function cleanupOrphanWrapperIfUnused() {
4292
+ const installDir = getInstallDir();
4293
+ const wrapperPath = getWrapperPath(installDir);
4294
+ if (!fs.existsSync(wrapperPath)) return;
4295
+ const stillReferenced = KNOWN_BROWSERS.some((browser) => {
4296
+ const config = getBrowserPaths(browser);
4297
+ const manifestPath = path.join(config.manifestDir, `${HOST_NAME}.json`);
4298
+ if (!fs.existsSync(manifestPath)) return false;
4299
+ try {
4300
+ const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf-8"));
4301
+ return manifest.path === wrapperPath;
4302
+ } catch {
4303
+ return true;
4304
+ }
4305
+ });
4306
+ if (stillReferenced) return;
4307
+ const installedNativeHostPath = path.join(installDir, "native-host.js");
4308
+ try {
4309
+ fs.unlinkSync(wrapperPath);
4310
+ } catch {
4311
+ }
4312
+ try {
4313
+ if (fs.existsSync(installedNativeHostPath)) {
4314
+ fs.unlinkSync(installedNativeHostPath);
4315
+ }
4316
+ } catch {
4317
+ }
4318
+ console.log(`\u5DF2\u6E05\u7406 wrapper \u5B89\u88C5\u76EE\u5F55: ${installDir}`);
4319
+ }
4320
+ function createManifest(hostPath) {
4243
4321
  return {
4244
4322
  name: HOST_NAME,
4245
4323
  description: DESCRIPTION,
4246
- path: getNativeHostPath(),
4324
+ path: hostPath,
4247
4325
  type: "stdio",
4248
4326
  allowed_origins: [`chrome-extension://${EXTENSION_ID}/`]
4249
4327
  };
@@ -4251,7 +4329,9 @@ function createManifest() {
4251
4329
  function register(browser = "chrome") {
4252
4330
  const config = getBrowserPaths(browser);
4253
4331
  const manifestPath = path.join(config.manifestDir, `${HOST_NAME}.json`);
4254
- const manifest = createManifest();
4332
+ const nativeHostPath = getNativeHostPath();
4333
+ const wrapperPath = ensureNativeHostWrapper(nativeHostPath);
4334
+ const manifest = createManifest(wrapperPath);
4255
4335
  console.log("\u6B63\u5728\u6CE8\u518C Native Messaging Host...");
4256
4336
  console.log(` Host Name: ${HOST_NAME}`);
4257
4337
  console.log(` Extension ID: ${EXTENSION_ID}`);
@@ -4293,6 +4373,7 @@ function unregister(browser = "chrome") {
4293
4373
  } catch {
4294
4374
  }
4295
4375
  }
4376
+ cleanupOrphanWrapperIfUnused();
4296
4377
  }
4297
4378
  function checkRegistration(browser = "chrome") {
4298
4379
  const config = getBrowserPaths(browser);
@@ -4300,50 +4381,156 @@ function checkRegistration(browser = "chrome") {
4300
4381
  if (!fs.existsSync(manifestPath)) {
4301
4382
  return { registered: false };
4302
4383
  }
4384
+ let manifest;
4303
4385
  try {
4304
- const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf-8"));
4305
- return {
4306
- registered: true,
4307
- manifestPath,
4308
- manifest
4309
- };
4386
+ manifest = JSON.parse(fs.readFileSync(manifestPath, "utf-8"));
4310
4387
  } catch {
4311
4388
  return { registered: false, manifestPath };
4312
4389
  }
4390
+ const wrapperPath = manifest.path;
4391
+ const wrapperExists = wrapperPath ? fs.existsSync(wrapperPath) : false;
4392
+ if (!wrapperExists) {
4393
+ return { registered: false, manifestPath, manifest, wrapperPath, wrapperExists: false };
4394
+ }
4395
+ return {
4396
+ registered: true,
4397
+ manifestPath,
4398
+ manifest,
4399
+ wrapperPath,
4400
+ wrapperExists: true,
4401
+ wrapperNodePath: wrapperPath ? readWrapperNodePath(wrapperPath) : void 0
4402
+ };
4403
+ }
4404
+ function registerAll(browsers) {
4405
+ return browsers.map((browser) => {
4406
+ try {
4407
+ register(browser);
4408
+ return { browser, success: true };
4409
+ } catch (error) {
4410
+ return { browser, success: false, error: error instanceof Error ? error.message : String(error) };
4411
+ }
4412
+ });
4413
+ }
4414
+ function unregisterAll(browsers) {
4415
+ return browsers.map((browser) => {
4416
+ try {
4417
+ unregister(browser);
4418
+ return { browser, success: true };
4419
+ } catch (error) {
4420
+ return { browser, success: false, error: error instanceof Error ? error.message : String(error) };
4421
+ }
4422
+ });
4423
+ }
4424
+ function checkAll(browsers) {
4425
+ return browsers.map((browser) => ({ browser, ...checkRegistration(browser) }));
4313
4426
  }
4314
4427
 
4315
4428
  // src/cli/commands/register.ts
4316
- function createRegisterCommand() {
4317
- const cmd = new Command22("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) => {
4318
- const browser = options.browser;
4319
- if (!["chrome", "chromium", "edge"].includes(browser)) {
4320
- console.error(`\u4E0D\u652F\u6301\u7684\u6D4F\u89C8\u5668: ${browser}`);
4321
- console.log("\u652F\u6301\u7684\u6D4F\u89C8\u5668: chrome, chromium, edge");
4322
- process.exitCode = 1;
4323
- return;
4429
+ var DEFAULT_REGISTER_BROWSERS = ["chrome", "chromium"];
4430
+ var BROWSER_LABELS = {
4431
+ chrome: "Chrome",
4432
+ chromium: "Chromium",
4433
+ edge: "Edge"
4434
+ };
4435
+ function resolveTargetBrowsers(options, defaultBrowsers) {
4436
+ const specifiedCount = [options.browser !== void 0, options.browsers !== void 0, options.all === true].filter(
4437
+ Boolean
4438
+ ).length;
4439
+ if (specifiedCount > 1) {
4440
+ return { error: "--browser / --browsers / --all \u4E09\u8005\u4E92\u65A5\uFF0C\u8BF7\u53EA\u4F7F\u7528\u5176\u4E2D\u4E00\u4E2A" };
4441
+ }
4442
+ if (options.browser !== void 0) {
4443
+ if (!KNOWN_BROWSERS.includes(options.browser)) {
4444
+ return { error: `\u4E0D\u652F\u6301\u7684\u6D4F\u89C8\u5668: ${options.browser}\uFF08\u652F\u6301: ${KNOWN_BROWSERS.join(", ")}\uFF09` };
4445
+ }
4446
+ return { browsers: [options.browser] };
4447
+ }
4448
+ if (options.browsers !== void 0) {
4449
+ const list = options.browsers.split(",").map((s) => s.trim()).filter(Boolean);
4450
+ if (list.length === 0) {
4451
+ return { error: "--browsers \u4E0D\u80FD\u4E3A\u7A7A" };
4452
+ }
4453
+ const invalid = list.filter((b) => !KNOWN_BROWSERS.includes(b));
4454
+ if (invalid.length > 0) {
4455
+ return { error: `\u4E0D\u652F\u6301\u7684\u6D4F\u89C8\u5668: ${invalid.join(", ")}\uFF08\u652F\u6301: ${KNOWN_BROWSERS.join(", ")}\uFF09` };
4456
+ }
4457
+ const deduped = Array.from(new Set(list));
4458
+ return { browsers: deduped };
4459
+ }
4460
+ if (options.all) {
4461
+ return { browsers: KNOWN_BROWSERS };
4462
+ }
4463
+ return { browsers: defaultBrowsers };
4464
+ }
4465
+ function printCheckSummary(results) {
4466
+ const indent = "".padEnd(10);
4467
+ for (const r of results) {
4468
+ const label = BROWSER_LABELS[r.browser].padEnd(10);
4469
+ if (r.registered) {
4470
+ console.log(`${label} \u2713 \u5DF2\u6CE8\u518C ${r.manifestPath}`);
4471
+ if (r.wrapperPath) {
4472
+ console.log(`${indent} wrapper: ${r.wrapperPath}`);
4473
+ }
4474
+ if (r.wrapperNodePath) {
4475
+ console.log(`${indent} node: ${r.wrapperNodePath}`);
4476
+ }
4477
+ } else if (r.manifest && r.wrapperExists === false) {
4478
+ console.log(`${label} \u2717 \u672A\u6CE8\u518C\uFF08manifest \u5B58\u5728\uFF0C\u4F46\u6307\u5411\u7684 wrapper \u7F3A\u5931: ${r.wrapperPath}\uFF09`);
4479
+ } else {
4480
+ console.log(`${label} \u2717 \u672A\u6CE8\u518C${r.manifestPath ? `\uFF08manifest \u5B58\u5728\u4F46\u89E3\u6790\u5931\u8D25: ${r.manifestPath}\uFF09` : ""}`);
4324
4481
  }
4482
+ }
4483
+ }
4484
+ function printActionSummary(actionLabel, results) {
4485
+ for (const r of results) {
4486
+ const label = BROWSER_LABELS[r.browser].padEnd(10);
4487
+ if (r.success) {
4488
+ console.log(`${label} \u2713 \u6210\u529F`);
4489
+ } else {
4490
+ console.log(`${label} \u2717 \u5931\u8D25\uFF1A${r.error}`);
4491
+ }
4492
+ }
4493
+ const successCount = results.filter((r) => r.success).length;
4494
+ const failCount = results.length - successCount;
4495
+ console.log("");
4496
+ if (failCount > 0) {
4497
+ console.log(`${failCount} \u9879\u5931\u8D25\uFF0C${successCount} \u9879\u6210\u529F\u3002\u8BF7\u68C0\u67E5\u6743\u9650\u6216\u8FD0\u884C sudo mg-cli register`);
4498
+ } else {
4499
+ console.log(`\u2713 ${actionLabel}\u6210\u529F\uFF08${successCount} \u9879\uFF09`);
4500
+ }
4501
+ return failCount > 0 && successCount === 0;
4502
+ }
4503
+ function createRegisterCommand() {
4504
+ const cmd = new Command22("register").description("\u6CE8\u518C Native Messaging Host\uFF08\u8BA9 Chrome \u63D2\u4EF6\u80FD\u81EA\u52A8\u542F\u52A8 Server\uFF09\uFF0C\u652F\u6301\u4E00\u6B21\u6CE8\u518C\u5230\u591A\u4E2A\u6D4F\u89C8\u5668\u76EE\u5F55").option("--unregister", "\u5378\u8F7D Native Host").option("--check", "\u68C0\u67E5\u6CE8\u518C\u72B6\u6001").option("--browser <type>", "\u6307\u5B9A\u5355\u4E2A\u6D4F\u89C8\u5668 (chrome/chromium/edge)\uFF0C\u4E0E --browsers/--all \u4E92\u65A5").option("--browsers <list>", "\u9017\u53F7\u5206\u9694\u7684\u591A\u4E2A\u6D4F\u89C8\u5668\uFF0C\u5982 chrome,chromium,edge\uFF0C\u4E0E --browser/--all \u4E92\u65A5").option("--all", "\u6CE8\u518C/\u64CD\u4F5C\u6240\u6709\u5DF2\u77E5\u6D4F\u89C8\u5668 (chrome,chromium,edge)\uFF0C\u4E0E --browser/--browsers \u4E92\u65A5").action((options) => {
4325
4505
  if (options.check) {
4326
- const status = checkRegistration(browser);
4327
- if (status.registered) {
4328
- console.log("\u2713 \u5DF2\u6CE8\u518C");
4329
- console.log(` Manifest: ${status.manifestPath}`);
4330
- if (status.manifest) {
4331
- console.log(` Native Host: ${status.manifest.path}`);
4332
- }
4333
- } else {
4334
- console.log("\u2717 \u672A\u6CE8\u518C");
4335
- if (status.manifestPath) {
4336
- console.log(` Manifest \u8DEF\u5F84: ${status.manifestPath}`);
4337
- }
4506
+ const resolved2 = resolveTargetBrowsers(options, KNOWN_BROWSERS);
4507
+ if ("error" in resolved2) {
4508
+ console.error(resolved2.error);
4509
+ process.exitCode = 1;
4510
+ return;
4338
4511
  }
4512
+ printCheckSummary(checkAll(resolved2.browsers));
4339
4513
  return;
4340
4514
  }
4341
4515
  if (options.unregister) {
4342
- unregister(browser);
4343
- console.log("\u2713 \u5DF2\u5378\u8F7D Native Host");
4516
+ const resolved2 = resolveTargetBrowsers(options, KNOWN_BROWSERS);
4517
+ if ("error" in resolved2) {
4518
+ console.error(resolved2.error);
4519
+ process.exitCode = 1;
4520
+ return;
4521
+ }
4522
+ const allFailed2 = printActionSummary("\u5378\u8F7D", unregisterAll(resolved2.browsers));
4523
+ if (allFailed2) process.exitCode = 1;
4524
+ return;
4525
+ }
4526
+ const resolved = resolveTargetBrowsers(options, DEFAULT_REGISTER_BROWSERS);
4527
+ if ("error" in resolved) {
4528
+ console.error(resolved.error);
4529
+ process.exitCode = 1;
4344
4530
  return;
4345
4531
  }
4346
- register(browser);
4532
+ const allFailed = printActionSummary("\u6CE8\u518C", registerAll(resolved.browsers));
4533
+ if (allFailed) process.exitCode = 1;
4347
4534
  });
4348
4535
  return cmd;
4349
4536
  }