@nmakarov/cli-toolkit 0.59.0 → 0.61.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.
package/dist/index.js CHANGED
@@ -5655,10 +5655,47 @@ function isFreshOnline(proc, oldPid) {
5655
5655
  }
5656
5656
  return true;
5657
5657
  }
5658
+ async function deletePm2App(appName, options = {}) {
5659
+ const { dryRun = false, logger = console, waitTimeoutMs = 65e3, oldPid = null } = options;
5660
+ if (dryRun) {
5661
+ logger.info(`[dryRun] would pm2 delete ${appName}`);
5662
+ return;
5663
+ }
5664
+ const before = await getPm2Process(appName, { logger });
5665
+ if (!before) {
5666
+ logger.info(`pm2 ${appName}: not present (skip delete)`);
5667
+ return;
5668
+ }
5669
+ await runShell(`pm2 delete "${appName}"`, { logger });
5670
+ await waitPm2(
5671
+ appName,
5672
+ (proc) => {
5673
+ if (proc) return false;
5674
+ if (oldPid != null && isPidAlive(oldPid)) return false;
5675
+ return true;
5676
+ },
5677
+ { timeoutMs: waitTimeoutMs, logger, label: "deleted (process gone)" }
5678
+ );
5679
+ logger.info(`pm2 ${appName} deleted`);
5680
+ }
5681
+ async function startFromEcosystem(paths, options = {}) {
5682
+ const { dryRun = false, logger = console } = options;
5683
+ if (dryRun) {
5684
+ logger.info(`[dryRun] would pm2 start ${paths.ecosystem} --update-env`);
5685
+ return;
5686
+ }
5687
+ await runShell(
5688
+ `ENV=production NODE_ENV=production pm2 start "${paths.ecosystem}" --update-env`,
5689
+ { logger }
5690
+ );
5691
+ logger.info("pm2 started from ecosystem");
5692
+ }
5658
5693
  async function reloadPm2(paths, options = {}) {
5659
5694
  const { dryRun = false, logger = console, appName = null, waitTimeoutMs = 65e3 } = options;
5660
5695
  if (dryRun) {
5661
- logger.info(`[dryRun] would pm2 startOrReload ${paths.ecosystem} --update-env`);
5696
+ logger.info(
5697
+ `[dryRun] would pm2 delete${appName ? ` ${appName}` : ""} + start ${paths.ecosystem} --update-env`
5698
+ );
5662
5699
  return;
5663
5700
  }
5664
5701
  let oldPid = null;
@@ -5667,14 +5704,15 @@ async function reloadPm2(paths, options = {}) {
5667
5704
  const n = Number(before?.pid);
5668
5705
  if (Number.isFinite(n) && n > 0 && before?.pm2_env?.status === "online") {
5669
5706
  oldPid = n;
5670
- logger.info(`pm2 ${appName}: reloading (old pid=${oldPid})`);
5707
+ logger.info(`pm2 ${appName}: recreate (old pid=${oldPid})`);
5671
5708
  }
5709
+ await deletePm2App(appName, { dryRun, logger, waitTimeoutMs, oldPid });
5710
+ } else {
5711
+ logger.warn?.(
5712
+ "reloadPm2: appName missing; starting ecosystem without delete (pass appName so script/env always recreate cleanly)"
5713
+ );
5672
5714
  }
5673
- await runShell(
5674
- `ENV=production NODE_ENV=production pm2 startOrReload "${paths.ecosystem}" --update-env`,
5675
- { logger }
5676
- );
5677
- logger.info("pm2 reloaded");
5715
+ await startFromEcosystem(paths, { dryRun, logger });
5678
5716
  if (appName) {
5679
5717
  const proc = await waitPm2(
5680
5718
  appName,
@@ -5682,7 +5720,7 @@ async function reloadPm2(paths, options = {}) {
5682
5720
  {
5683
5721
  timeoutMs: waitTimeoutMs,
5684
5722
  logger,
5685
- label: oldPid ? `online on new pid (old ${oldPid} gone)` : "online after reload"
5723
+ label: oldPid ? `online on new pid (old ${oldPid} gone)` : "online after recreate"
5686
5724
  }
5687
5725
  );
5688
5726
  logger.info(`pm2 ${appName} online pid=${proc?.pid ?? "?"}`);
@@ -5719,14 +5757,15 @@ async function stopPm2(appName, options = {}) {
5719
5757
  async function startPm2(paths, options = {}) {
5720
5758
  const { dryRun = false, logger = console, appName = null, waitTimeoutMs = 65e3 } = options;
5721
5759
  if (dryRun) {
5722
- logger.info(`[dryRun] would pm2 start ${paths.ecosystem} --update-env`);
5760
+ logger.info(
5761
+ `[dryRun] would pm2 delete${appName ? ` ${appName}` : ""} + start ${paths.ecosystem} --update-env`
5762
+ );
5723
5763
  return;
5724
5764
  }
5725
- await runShell(
5726
- `ENV=production NODE_ENV=production pm2 start "${paths.ecosystem}" --update-env`,
5727
- { logger }
5728
- );
5729
- logger.info("pm2 started");
5765
+ if (appName) {
5766
+ await deletePm2App(appName, { dryRun, logger, waitTimeoutMs });
5767
+ }
5768
+ await startFromEcosystem(paths, { dryRun, logger });
5730
5769
  if (appName) {
5731
5770
  const proc = await waitPm2(
5732
5771
  appName,
@@ -5981,7 +6020,7 @@ function buildEnsureEnvScript() {
5981
6020
  return `/**
5982
6021
  * Written by cli-toolkit deploy (init-structure). Do not hand-edit.
5983
6022
  * Preloaded via pm2 node_args --require. Always set (do not only fill when unset):
5984
- * \`pm2 startOrReload --update-env\` can inject a non-production ENV from the
6023
+ * \`pm2 start \u2026 --update-env\` can inject a non-production ENV from the
5985
6024
  * deploy shell / prior dump, and a conditional assign would leave it in place.
5986
6025
  */
5987
6026
  process.env.ENV = "production";
@@ -6347,7 +6386,9 @@ async function rollbackService(service, options = {}) {
6347
6386
  logger.info(`rollback target: v${buildInfo.version} release=${buildInfo.release ?? rollbackTarget.name}`);
6348
6387
  }
6349
6388
  await activateRelease(rollbackTarget.path, paths, { dryRun, logger });
6350
- await reloadPm2(paths, { dryRun, logger });
6389
+ const appName = service.pm2?.appName ?? null;
6390
+ const waitTimeoutMs = 65e3;
6391
+ await reloadPm2(paths, { dryRun, logger, appName, waitTimeoutMs });
6351
6392
  const summary = `rollback ${activeName} \u2192 ${rollbackTarget.name} dryRun=${dryRun}`;
6352
6393
  if (!dryRun) await appendDeployLog(paths.deployLog, summary);
6353
6394
  return { from: activeName, to: rollbackTarget.name, path: rollbackTarget.path };