@becomeopc/trendpublish 0.2.0 → 4.0.0-alpha.4

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/app.js +164 -50
  2. package/package.json +1 -1
package/dist/app.js CHANGED
@@ -54149,6 +54149,9 @@ import path3 from "node:path";
54149
54149
  import { fileURLToPath } from "node:url";
54150
54150
  import pm2 from "pm2";
54151
54151
  var here = path3.dirname(fileURLToPath(import.meta.url));
54152
+ var READY_POLL_INTERVAL_MS = 250;
54153
+ var READY_TIMEOUT_MS = 8e3;
54154
+ var HEALTH_REQUEST_TIMEOUT_MS = 1500;
54152
54155
  function call(method, ...args) {
54153
54156
  return new Promise((resolve3, reject) => {
54154
54157
  pm2[method](
@@ -54173,17 +54176,51 @@ function resolveDaemonEntry(dir = here) {
54173
54176
  }
54174
54177
  return { script: path3.join(dir, "main.ts"), interpreter: resolveInterpreter(dir) };
54175
54178
  }
54176
- async function withConnection(run2) {
54177
- await call("connect");
54179
+ var defaultPm2Client = {
54180
+ connect: () => call("connect"),
54181
+ disconnect: () => pm2.disconnect(),
54182
+ describe: (name) => call("describe", name),
54183
+ list: () => call("list"),
54184
+ start: (options2) => call("start", options2),
54185
+ restart: (name) => call("restart", name),
54186
+ delete: (name) => call("delete", name),
54187
+ stop: (name) => call("stop", name),
54188
+ killDaemon: async () => {
54189
+ await call("killDaemon");
54190
+ delete process.env.PM2_STATUS;
54191
+ },
54192
+ update: () => call("update"),
54193
+ save: () => call("save"),
54194
+ startup: (platform) => call("startup", platform),
54195
+ unstartup: (platform) => call("unstartup", platform)
54196
+ };
54197
+ function defaultDelay(ms) {
54198
+ return new Promise((resolve3) => setTimeout(resolve3, ms));
54199
+ }
54200
+ async function isServerReachable(config, fetchImpl = fetch) {
54201
+ try {
54202
+ const response = await fetchImpl(`http://127.0.0.1:${config.port}/api/health`, {
54203
+ headers: { authorization: `Bearer ${config.apiKey}` },
54204
+ signal: AbortSignal.timeout(HEALTH_REQUEST_TIMEOUT_MS)
54205
+ });
54206
+ return response.ok;
54207
+ } catch {
54208
+ return false;
54209
+ }
54210
+ }
54211
+ async function withConnection(client, run2) {
54212
+ await client.connect();
54178
54213
  try {
54179
54214
  return await run2();
54180
54215
  } finally {
54181
- pm2.disconnect();
54216
+ client.disconnect();
54182
54217
  }
54183
54218
  }
54184
- async function daemonStart() {
54185
- initConfig();
54186
- const entry = resolveDaemonEntry();
54219
+ function readProcessStatus(proc) {
54220
+ if (!proc) return "stopped";
54221
+ return typeof proc.pm2_env?.status === "string" ? proc.pm2_env.status : proc.status ?? "unknown";
54222
+ }
54223
+ function buildDaemonEnv() {
54187
54224
  const env = { TRENDPUBLISH_HOME: stateDir2() };
54188
54225
  for (const key of [
54189
54226
  "TRENDPUBLISH_MIGRATIONS_DIR",
@@ -54192,50 +54229,120 @@ async function daemonStart() {
54192
54229
  ]) {
54193
54230
  if (process.env[key]?.trim()) env[key] = process.env[key].trim();
54194
54231
  }
54195
- await withConnection(async () => {
54196
- const existing = await call(
54197
- "describe",
54198
- CLI_PROCESS_NAME
54199
- );
54200
- if (Array.isArray(existing) && existing.length > 0) {
54201
- const execPath = existing[0]?.pm2_env?.pm_exec_path;
54202
- if (execPath === entry.script) {
54203
- await call("restart", CLI_PROCESS_NAME);
54204
- return;
54205
- }
54206
- await call("delete", CLI_PROCESS_NAME);
54232
+ return env;
54233
+ }
54234
+ function buildStartOptions(entry, env) {
54235
+ return {
54236
+ name: CLI_PROCESS_NAME,
54237
+ script: entry.script,
54238
+ args: ["start"],
54239
+ interpreter: entry.interpreter,
54240
+ env,
54241
+ autorestart: true,
54242
+ max_restarts: 10
54243
+ };
54244
+ }
54245
+ async function startOrReplaceProcess(client, entry, env) {
54246
+ const existing = await client.describe(CLI_PROCESS_NAME);
54247
+ if (Array.isArray(existing) && existing.length > 0) {
54248
+ const execPath = existing[0]?.pm2_env?.pm_exec_path;
54249
+ const execInterpreter = existing[0]?.pm2_env?.exec_interpreter;
54250
+ const matchesScript = execPath === entry.script;
54251
+ const matchesInterpreter = !execInterpreter || execInterpreter === entry.interpreter;
54252
+ if (matchesScript && matchesInterpreter) {
54253
+ await client.restart(CLI_PROCESS_NAME);
54254
+ return;
54207
54255
  }
54208
- await call("start", {
54209
- name: CLI_PROCESS_NAME,
54210
- script: entry.script,
54211
- args: ["start"],
54212
- interpreter: entry.interpreter,
54213
- env,
54214
- autorestart: true,
54215
- max_restarts: 10
54256
+ await client.delete(CLI_PROCESS_NAME);
54257
+ }
54258
+ await client.start(buildStartOptions(entry, env));
54259
+ }
54260
+ async function waitForDaemonReady(client, config, deps) {
54261
+ const attempts = Math.max(1, Math.ceil(deps.readyTimeoutMs / deps.pollIntervalMs));
54262
+ for (let i = 0; i < attempts; i += 1) {
54263
+ const described = await client.describe(CLI_PROCESS_NAME);
54264
+ const status = readProcessStatus(described[0]);
54265
+ if (status === "errored" || status === "stopped") {
54266
+ return "errored";
54267
+ }
54268
+ if (status === "online" && await deps.isReachable(config)) {
54269
+ return "ready";
54270
+ }
54271
+ if (i < attempts - 1) {
54272
+ await deps.delay(deps.pollIntervalMs);
54273
+ }
54274
+ }
54275
+ return "timeout";
54276
+ }
54277
+ async function refreshStalePm2Daemon(client, entry, env) {
54278
+ const running = await client.list().catch(() => []);
54279
+ const hasOtherApps = running.some((proc) => proc.name && proc.name !== CLI_PROCESS_NAME);
54280
+ if (hasOtherApps) {
54281
+ await client.delete(CLI_PROCESS_NAME).catch(() => void 0);
54282
+ await client.update();
54283
+ await client.start(buildStartOptions(entry, env));
54284
+ return;
54285
+ }
54286
+ await client.killDaemon();
54287
+ client.disconnect();
54288
+ await client.connect();
54289
+ await client.start(buildStartOptions(entry, env));
54290
+ }
54291
+ async function daemonStart(deps = {}) {
54292
+ const config = initConfig();
54293
+ const client = deps.pm2 ?? defaultPm2Client;
54294
+ const entry = deps.entry ?? resolveDaemonEntry();
54295
+ const env = buildDaemonEnv();
54296
+ const isReachable = deps.isReachable ?? isServerReachable;
54297
+ const delay = deps.delay ?? defaultDelay;
54298
+ const pollIntervalMs = deps.pollIntervalMs ?? READY_POLL_INTERVAL_MS;
54299
+ const readyTimeoutMs = deps.readyTimeoutMs ?? READY_TIMEOUT_MS;
54300
+ await withConnection(client, async () => {
54301
+ await startOrReplaceProcess(client, entry, env);
54302
+ let state = await waitForDaemonReady(client, config, {
54303
+ isReachable,
54304
+ delay,
54305
+ pollIntervalMs,
54306
+ readyTimeoutMs
54216
54307
  });
54308
+ if (state === "ready") return;
54309
+ if (state === "errored") {
54310
+ await refreshStalePm2Daemon(client, entry, env);
54311
+ state = await waitForDaemonReady(client, config, {
54312
+ isReachable,
54313
+ delay,
54314
+ pollIntervalMs,
54315
+ readyTimeoutMs
54316
+ });
54317
+ if (state === "ready") return;
54318
+ }
54319
+ throw new Error(
54320
+ `\u540E\u53F0\u670D\u52A1\u672A\u80FD\u6B63\u5E38\u5C31\u7EEA\uFF08\u72B6\u6001: ${state}\uFF09\uFF0C\u8BF7\u8FD0\u884C trendpublish daemon logs \u67E5\u770B\u8BE6\u60C5\u3002`
54321
+ );
54217
54322
  });
54218
54323
  }
54219
- async function daemonStop() {
54220
- await withConnection(() => call("stop", CLI_PROCESS_NAME));
54324
+ async function daemonStop(deps = {}) {
54325
+ const client = deps.pm2 ?? defaultPm2Client;
54326
+ await withConnection(client, () => client.stop(CLI_PROCESS_NAME));
54221
54327
  }
54222
- async function daemonRestart() {
54223
- await withConnection(() => call("restart", CLI_PROCESS_NAME));
54328
+ async function daemonRestart(deps = {}) {
54329
+ await daemonStart(deps);
54224
54330
  }
54225
- async function daemonDelete() {
54226
- await withConnection(() => call("delete", CLI_PROCESS_NAME));
54331
+ async function daemonDelete(deps = {}) {
54332
+ const client = deps.pm2 ?? defaultPm2Client;
54333
+ await withConnection(client, () => client.delete(CLI_PROCESS_NAME));
54227
54334
  }
54228
- async function daemonStatus() {
54229
- return withConnection(async () => {
54230
- const described = await call("describe", CLI_PROCESS_NAME);
54335
+ async function daemonStatus(deps = {}) {
54336
+ const client = deps.pm2 ?? defaultPm2Client;
54337
+ return withConnection(client, async () => {
54338
+ const described = await client.describe(CLI_PROCESS_NAME);
54231
54339
  if (!Array.isArray(described) || described.length === 0) return null;
54232
- const process2 = described[0];
54233
- const env = process2.pm2_env ?? {};
54234
- const status = typeof env.status === "string" ? env.status : process2.status ?? "unknown";
54340
+ const proc = described[0];
54341
+ const env = proc.pm2_env ?? {};
54235
54342
  return {
54236
54343
  name: CLI_PROCESS_NAME,
54237
- status,
54238
- pid: process2.pid,
54344
+ status: readProcessStatus(proc),
54345
+ pid: proc.pid,
54239
54346
  restarts: typeof env.restart_time === "number" ? env.restart_time : void 0,
54240
54347
  uptime: typeof env.pm_uptime === "number" ? env.pm_uptime : void 0
54241
54348
  };
@@ -54244,15 +54351,17 @@ async function daemonStatus() {
54244
54351
  function startupPlatform() {
54245
54352
  return process.platform === "darwin" ? "launchd" : "systemd";
54246
54353
  }
54247
- async function serviceInstall() {
54248
- await daemonStart();
54249
- await withConnection(async () => {
54250
- await call("save");
54251
- await call("startup", startupPlatform());
54354
+ async function serviceInstall(deps = {}) {
54355
+ const client = deps.pm2 ?? defaultPm2Client;
54356
+ await daemonStart(deps);
54357
+ await withConnection(client, async () => {
54358
+ await client.save();
54359
+ await client.startup(startupPlatform());
54252
54360
  });
54253
54361
  }
54254
- async function serviceUninstall() {
54255
- await withConnection(() => call("unstartup", startupPlatform()));
54362
+ async function serviceUninstall(deps = {}) {
54363
+ const client = deps.pm2 ?? defaultPm2Client;
54364
+ await withConnection(client, () => client.unstartup(startupPlatform()));
54256
54365
  }
54257
54366
  async function daemonLogs(lines = 50) {
54258
54367
  const { readFileSync: readFileSync3 } = await import("node:fs");
@@ -54283,7 +54392,7 @@ function usage() {
54283
54392
  "",
54284
54393
  " init \u521D\u59CB\u5316 ~/.trendpublish\uFF08\u751F\u6210 API key\u3001\u56FA\u5B9A\u7AEF\u53E3\uFF09",
54285
54394
  " start \u524D\u53F0\u542F\u52A8\u670D\u52A1",
54286
- " open \u6253\u5F00\u6D4F\u89C8\u5668\u8FDB\u5165 Dashboard",
54395
+ " open \u6253\u5F00\u6D4F\u89C8\u5668\u8FDB\u5165 Dashboard\uFF08\u672A\u8FD0\u884C\u65F6\u81EA\u52A8\u540E\u53F0\u542F\u52A8\uFF09",
54287
54396
  " login \u767B\u5F55 becomeopc \u83B7\u53D6 relay \u8BA2\u9605\u51ED\u8BC1\uFF08OAuth loopback\uFF09",
54288
54397
  " doctor \u68C0\u67E5\u914D\u7F6E\u4E0E\u6570\u636E\u5E93",
54289
54398
  " daemon start \u540E\u53F0\u542F\u52A8\uFF08pm2\uFF09",
@@ -54401,7 +54510,7 @@ async function notifyRunningServerRelaySync(port) {
54401
54510
  headers: { authorization: `Bearer ${config.apiKey}` }
54402
54511
  }).catch(() => void 0);
54403
54512
  }
54404
- async function runCommand(argv) {
54513
+ async function runCommand(argv, deps = {}) {
54405
54514
  const [command, ...rest] = argv;
54406
54515
  switch (command) {
54407
54516
  case "init": {
@@ -54420,8 +54529,13 @@ async function runCommand(argv) {
54420
54529
  }
54421
54530
  case "open": {
54422
54531
  const config = readConfig() ?? initConfig();
54532
+ const checkReachable = deps.isServerReachable ?? isServerReachable;
54533
+ if (!await checkReachable(config)) {
54534
+ out("\u540E\u53F0\u670D\u52A1\u672A\u8FD0\u884C\uFF0C\u6B63\u5728\u81EA\u52A8\u542F\u52A8\u2026");
54535
+ await (deps.daemonStart ?? daemonStart)();
54536
+ }
54423
54537
  const url = `${dashboardUrl(config)}/workspace?token=${encodeURIComponent(config.apiKey)}`;
54424
- openBrowser(url);
54538
+ (deps.openBrowser ?? openBrowser)(url);
54425
54539
  out(url);
54426
54540
  return 0;
54427
54541
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@becomeopc/trendpublish",
3
- "version": "0.2.0",
3
+ "version": "4.0.0-alpha.4",
4
4
  "description": "TrendPublish — 自托管内容生成与多渠道发布服务(CLI + 常驻后台 + Web Dashboard)",
5
5
  "homepage": "https://github.com/liyown/ai-trend-publish#readme",
6
6
  "bugs": {