ai-project-manage-cli 6.0.96 → 6.0.98

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.
Files changed (2) hide show
  1. package/dist/index.js +149 -25
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -3829,10 +3829,18 @@ function forceReleaseConnectLock() {
3829
3829
  // src/commands/daemon.ts
3830
3830
  import { spawnSync as spawnSync2 } from "child_process";
3831
3831
  import { setTimeout as delay } from "node:timers/promises";
3832
- import { existsSync as existsSync14, mkdirSync as mkdirSync7, writeFileSync as writeFileSync13 } from "fs";
3832
+ import { existsSync as existsSync14, mkdirSync as mkdirSync7, unlinkSync as unlinkSync2, writeFileSync as writeFileSync13 } from "fs";
3833
3833
  import { join as join15 } from "path";
3834
3834
  var PM2_APP_NAME = "apm-connect";
3835
- var PM2_ECOSYSTEM_PATH = join15(APM_CONFIG_DIR, "connect.ecosystem.cjs");
3835
+ var PM2_ECOSYSTEM_PATH = join15(
3836
+ APM_CONFIG_DIR,
3837
+ "connect.ecosystem.config.cjs"
3838
+ );
3839
+ var LEGACY_PM2_ECOSYSTEM_PATH = join15(
3840
+ APM_CONFIG_DIR,
3841
+ "connect.ecosystem.cjs"
3842
+ );
3843
+ var LEGACY_PM2_APP_NAMES = ["connect.ecosystem"];
3836
3844
  function resolveConnectArgs(server) {
3837
3845
  const args = ["connect"];
3838
3846
  const trimmed = server?.trim();
@@ -3958,10 +3966,39 @@ function logPm2Ready(pm2Bin, installed2) {
3958
3966
  console.log(`[apm] \u5DF2\u68C0\u6D4B\u5230\u5168\u5C40 pm2${versionSuffix}: ${pm2Bin}`);
3959
3967
  }
3960
3968
  }
3961
- function ensureGlobalPm2() {
3969
+ function isPm2DaemonOutOfDate(output) {
3970
+ return /In-memory PM2 is out-of-date/i.test(output);
3971
+ }
3972
+ function ensurePm2DaemonUpdated(pm2Bin) {
3973
+ const probe = spawnSync2(pm2Bin, ["jlist"], {
3974
+ encoding: "utf8",
3975
+ shell: useNpmShell2,
3976
+ stdio: ["ignore", "pipe", "pipe"],
3977
+ timeout: 15e3
3978
+ });
3979
+ const combined = `${probe.stdout ?? ""}
3980
+ ${probe.stderr ?? ""}`;
3981
+ if (!isPm2DaemonOutOfDate(combined)) {
3982
+ return;
3983
+ }
3984
+ console.log("[apm] \u68C0\u6D4B\u5230 PM2 \u5185\u5B58\u7248\u672C\u4E0E\u672C\u5730\u4E0D\u4E00\u81F4\uFF0C\u6B63\u5728\u6267\u884C pm2 update \u2026");
3985
+ const update = spawnSync2(pm2Bin, ["update"], {
3986
+ encoding: "utf8",
3987
+ shell: useNpmShell2,
3988
+ stdio: "inherit",
3989
+ timeout: 6e4
3990
+ });
3991
+ if (update.error || update.status !== 0) {
3992
+ console.error("[apm] pm2 update \u5931\u8D25\uFF0C\u8BF7\u624B\u52A8\u6267\u884C: pm2 update");
3993
+ }
3994
+ }
3995
+ function ensureGlobalPm2(options) {
3962
3996
  const existing = findGlobalPm2();
3963
3997
  if (existing) {
3964
- logPm2Ready(existing, false);
3998
+ ensurePm2DaemonUpdated(existing);
3999
+ if (!options?.quiet) {
4000
+ logPm2Ready(existing, false);
4001
+ }
3965
4002
  return existing;
3966
4003
  }
3967
4004
  installGlobalPm2();
@@ -3972,7 +4009,10 @@ function ensureGlobalPm2() {
3972
4009
  );
3973
4010
  process.exit(1);
3974
4011
  }
3975
- logPm2Ready(installed2, true);
4012
+ ensurePm2DaemonUpdated(installed2);
4013
+ if (!options?.quiet) {
4014
+ logPm2Ready(installed2, true);
4015
+ }
3976
4016
  return installed2;
3977
4017
  }
3978
4018
  function resolveApmEntryPath(entryArg = process.argv[1]) {
@@ -4001,10 +4041,60 @@ function resolveApmEntryPath(entryArg = process.argv[1]) {
4001
4041
  process.exit(1);
4002
4042
  }
4003
4043
  function isRunningUnderPm2() {
4004
- return process.env.name === PM2_APP_NAME && process.env.pm_id !== void 0;
4044
+ if (process.env.pm_id === void 0) {
4045
+ return false;
4046
+ }
4047
+ const name = process.env.name;
4048
+ return name === PM2_APP_NAME || name !== void 0 && LEGACY_PM2_APP_NAMES.includes(name);
4049
+ }
4050
+ function isConnectPm2Process(app) {
4051
+ if (app.name === PM2_APP_NAME) {
4052
+ return true;
4053
+ }
4054
+ return app.name !== void 0 && LEGACY_PM2_APP_NAMES.includes(app.name);
4055
+ }
4056
+ function listPm2Processes() {
4057
+ try {
4058
+ const raw = runPm2Json(["jlist"]);
4059
+ return JSON.parse(raw);
4060
+ } catch {
4061
+ return [];
4062
+ }
4063
+ }
4064
+ function findPm2ConnectProcess() {
4065
+ return listPm2Processes().find(isConnectPm2Process) ?? null;
4066
+ }
4067
+ function resolvePm2ConnectName(app) {
4068
+ return app?.name ?? PM2_APP_NAME;
4069
+ }
4070
+ function deletePm2AppIfExists(name) {
4071
+ const pm2Bin = findGlobalPm2();
4072
+ if (!pm2Bin) {
4073
+ return;
4074
+ }
4075
+ ensurePm2DaemonUpdated(pm2Bin);
4076
+ spawnSync2(pm2Bin, ["delete", name], {
4077
+ encoding: "utf8",
4078
+ shell: useNpmShell2,
4079
+ stdio: "ignore"
4080
+ });
4081
+ }
4082
+ function removePm2ConnectProcesses() {
4083
+ const seen = /* @__PURE__ */ new Set();
4084
+ for (const app of listPm2Processes()) {
4085
+ if (!isConnectPm2Process(app) || !app.name || seen.has(app.name)) {
4086
+ continue;
4087
+ }
4088
+ seen.add(app.name);
4089
+ deletePm2AppIfExists(app.name);
4090
+ }
4091
+ for (const legacyName of LEGACY_PM2_APP_NAMES) {
4092
+ deletePm2AppIfExists(legacyName);
4093
+ }
4094
+ deletePm2AppIfExists(PM2_APP_NAME);
4005
4095
  }
4006
4096
  function runPm2(args, options) {
4007
- const pm2Bin = ensureGlobalPm2();
4097
+ const pm2Bin = ensureGlobalPm2({ quiet: true });
4008
4098
  const spawnOptions = {
4009
4099
  encoding: "utf8",
4010
4100
  stdio: options?.inherit ? "inherit" : ["ignore", "pipe", "pipe"],
@@ -4028,6 +4118,7 @@ function runPm2Json(args) {
4028
4118
  if (!pm2Bin) {
4029
4119
  return "[]";
4030
4120
  }
4121
+ ensurePm2DaemonUpdated(pm2Bin);
4031
4122
  const result = spawnSync2(pm2Bin, args, {
4032
4123
  encoding: "utf8",
4033
4124
  stdio: ["ignore", "pipe", "pipe"],
@@ -4037,15 +4128,14 @@ function runPm2Json(args) {
4037
4128
  return result.stdout?.toString() ?? "[]";
4038
4129
  }
4039
4130
  function isPm2ConnectOnline() {
4040
- try {
4041
- const raw = runPm2Json(["jlist"]);
4042
- const list = JSON.parse(raw);
4043
- return list.some(
4044
- (app) => app.name === PM2_APP_NAME && (app.pm2_env?.status === "online" || app.pm2_env?.status === "launching")
4045
- );
4046
- } catch {
4047
- return false;
4048
- }
4131
+ const app = findPm2ConnectProcess();
4132
+ if (!app) return false;
4133
+ const status = app.pm2_env?.status;
4134
+ return status === "online" || status === "launching";
4135
+ }
4136
+ function printConnectNotRunningHint() {
4137
+ console.log(`[apm] ${PM2_APP_NAME} \u672A\u5728\u8FD0\u884C`);
4138
+ console.log("[apm] \u542F\u52A8\u5B88\u62A4: apm connect --daemon");
4049
4139
  }
4050
4140
  function assertConnectNotRunning() {
4051
4141
  pruneStaleConnectLock();
@@ -4110,6 +4200,12 @@ function writeEcosystemFile(options) {
4110
4200
  formatConnectPm2EcosystemFile(ecosystem),
4111
4201
  "utf8"
4112
4202
  );
4203
+ if (existsSync14(LEGACY_PM2_ECOSYSTEM_PATH)) {
4204
+ try {
4205
+ unlinkSync2(LEGACY_PM2_ECOSYSTEM_PATH);
4206
+ } catch {
4207
+ }
4208
+ }
4113
4209
  }
4114
4210
  async function prepareEcosystem(server) {
4115
4211
  await ensureLoggedConfig();
@@ -4130,7 +4226,14 @@ async function runDaemonStart(options) {
4130
4226
  await runUpdate();
4131
4227
  ensureGlobalPm2();
4132
4228
  const cfg = await prepareEcosystem(options.server);
4229
+ removePm2ConnectProcesses();
4133
4230
  runPm2(["start", PM2_ECOSYSTEM_PATH, "--update-env"], { inherit: true });
4231
+ await delay(1e3);
4232
+ if (!isPm2ConnectOnline()) {
4233
+ console.error(
4234
+ `[apm] ${PM2_APP_NAME} \u542F\u52A8\u540E\u672A\u5904\u4E8E online \u72B6\u6001\uFF0C\u8BF7\u6267\u884C: apm daemon logs -n 200`
4235
+ );
4236
+ }
4134
4237
  console.log(
4135
4238
  `[apm] ${PM2_APP_NAME} \u5DF2\u7531 PM2 \u542F\u52A8\uFF08server=${options.server?.trim() || cfg.baseUrl}\uFF09`
4136
4239
  );
@@ -4153,14 +4256,18 @@ async function runDaemonStop() {
4153
4256
  console.log(`[apm] ${PM2_APP_NAME} \u672A\u5728\u8FD0\u884C`);
4154
4257
  return;
4155
4258
  }
4156
- runPm2(["stop", PM2_APP_NAME], { inherit: true });
4259
+ runPm2(["stop", resolvePm2ConnectName(findPm2ConnectProcess())], {
4260
+ inherit: true
4261
+ });
4157
4262
  for (let i = 0; i < 10; i += 1) {
4158
4263
  if (!isPm2ConnectOnline()) break;
4159
4264
  await delay(500);
4160
4265
  }
4161
4266
  if (isPm2ConnectOnline()) {
4162
4267
  console.log(`[apm] \u4F18\u96C5\u505C\u6B62\u8D85\u65F6\uFF0C\u5F3A\u5236\u79FB\u9664 ${PM2_APP_NAME}\u2026`);
4163
- runPm2(["delete", PM2_APP_NAME], { inherit: true });
4268
+ runPm2(["delete", resolvePm2ConnectName(findPm2ConnectProcess())], {
4269
+ inherit: true
4270
+ });
4164
4271
  }
4165
4272
  killConnectLockProcessIfAlive();
4166
4273
  forceReleaseConnectLock();
@@ -4195,21 +4302,38 @@ async function runDaemonRestart(options) {
4195
4302
  }
4196
4303
  await runUpdate();
4197
4304
  await prepareEcosystem(options.server);
4198
- runPm2(["startOrRestart", PM2_ECOSYSTEM_PATH, "--update-env"], {
4199
- inherit: true
4200
- });
4305
+ removePm2ConnectProcesses();
4306
+ runPm2(["start", PM2_ECOSYSTEM_PATH, "--update-env"], { inherit: true });
4201
4307
  console.log(`[apm] ${PM2_APP_NAME} \u5DF2\u91CD\u542F`);
4202
4308
  }
4203
4309
  async function runDaemonDelete() {
4204
- runPm2(["delete", PM2_APP_NAME], { inherit: true });
4310
+ ensureGlobalPm2();
4311
+ const app = findPm2ConnectProcess();
4312
+ if (!app?.name) {
4313
+ console.log(`[apm] ${PM2_APP_NAME} \u672A\u5728 PM2 \u4E2D\u6CE8\u518C`);
4314
+ return;
4315
+ }
4316
+ runPm2(["delete", app.name], { inherit: true });
4205
4317
  forceReleaseConnectLock();
4206
- console.log(`[apm] ${PM2_APP_NAME} \u5DF2\u4ECE PM2 \u79FB\u9664`);
4318
+ console.log(`[apm] ${app.name} \u5DF2\u4ECE PM2 \u79FB\u9664`);
4207
4319
  }
4208
4320
  async function runDaemonStatus() {
4209
- runPm2(["describe", PM2_APP_NAME], { inherit: true });
4321
+ ensureGlobalPm2();
4322
+ const app = findPm2ConnectProcess();
4323
+ if (!app?.name) {
4324
+ printConnectNotRunningHint();
4325
+ return;
4326
+ }
4327
+ runPm2(["describe", app.name], { inherit: true });
4210
4328
  }
4211
4329
  async function runDaemonLogs(options) {
4212
- const args = ["logs", PM2_APP_NAME, "--lines", String(options.lines ?? 100)];
4330
+ ensureGlobalPm2();
4331
+ const app = findPm2ConnectProcess();
4332
+ if (!app?.name) {
4333
+ printConnectNotRunningHint();
4334
+ return;
4335
+ }
4336
+ const args = ["logs", app.name, "--lines", String(options.lines ?? 100)];
4213
4337
  if (!options.follow) {
4214
4338
  args.push("--nostream");
4215
4339
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-project-manage-cli",
3
- "version": "6.0.96",
3
+ "version": "6.0.98",
4
4
  "description": "命令行工具:后续用于调用平台后端 API 完成运维与自动化操作",
5
5
  "type": "module",
6
6
  "private": false,