@nmakarov/cli-toolkit 0.57.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
@@ -3545,6 +3545,12 @@ var Db = class _Db {
3545
3545
  }
3546
3546
  if (!dbConnectionString) {
3547
3547
  if (!dbName) {
3548
+ const env = context?.args?.env;
3549
+ if (env && String(env).toLowerCase() !== "local") {
3550
+ throw new ParamError(
3551
+ `Db: dbName not set for env="${env}" (set --dbName or DB_NAME_${String(env).toUpperCase()})`
3552
+ );
3553
+ }
3548
3554
  dbName = "local";
3549
3555
  }
3550
3556
  const paramName = `dbConnectionString${capitalizeFirstLetter(dbName)}`;
@@ -5649,10 +5655,47 @@ function isFreshOnline(proc, oldPid) {
5649
5655
  }
5650
5656
  return true;
5651
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
+ }
5652
5693
  async function reloadPm2(paths, options = {}) {
5653
5694
  const { dryRun = false, logger = console, appName = null, waitTimeoutMs = 65e3 } = options;
5654
5695
  if (dryRun) {
5655
- 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
+ );
5656
5699
  return;
5657
5700
  }
5658
5701
  let oldPid = null;
@@ -5661,11 +5704,15 @@ async function reloadPm2(paths, options = {}) {
5661
5704
  const n = Number(before?.pid);
5662
5705
  if (Number.isFinite(n) && n > 0 && before?.pm2_env?.status === "online") {
5663
5706
  oldPid = n;
5664
- logger.info(`pm2 ${appName}: reloading (old pid=${oldPid})`);
5707
+ logger.info(`pm2 ${appName}: recreate (old pid=${oldPid})`);
5665
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
+ );
5666
5714
  }
5667
- await runShell(`pm2 startOrReload "${paths.ecosystem}" --update-env`, { logger });
5668
- logger.info("pm2 reloaded");
5715
+ await startFromEcosystem(paths, { dryRun, logger });
5669
5716
  if (appName) {
5670
5717
  const proc = await waitPm2(
5671
5718
  appName,
@@ -5673,7 +5720,7 @@ async function reloadPm2(paths, options = {}) {
5673
5720
  {
5674
5721
  timeoutMs: waitTimeoutMs,
5675
5722
  logger,
5676
- 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"
5677
5724
  }
5678
5725
  );
5679
5726
  logger.info(`pm2 ${appName} online pid=${proc?.pid ?? "?"}`);
@@ -5710,11 +5757,15 @@ async function stopPm2(appName, options = {}) {
5710
5757
  async function startPm2(paths, options = {}) {
5711
5758
  const { dryRun = false, logger = console, appName = null, waitTimeoutMs = 65e3 } = options;
5712
5759
  if (dryRun) {
5713
- 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
+ );
5714
5763
  return;
5715
5764
  }
5716
- await runShell(`pm2 start "${paths.ecosystem}" --update-env`, { logger });
5717
- logger.info("pm2 started");
5765
+ if (appName) {
5766
+ await deletePm2App(appName, { dryRun, logger, waitTimeoutMs });
5767
+ }
5768
+ await startFromEcosystem(paths, { dryRun, logger });
5718
5769
  if (appName) {
5719
5770
  const proc = await waitPm2(
5720
5771
  appName,
@@ -5968,11 +6019,12 @@ function resolvePm2Args(pm2) {
5968
6019
  function buildEnsureEnvScript() {
5969
6020
  return `/**
5970
6021
  * Written by cli-toolkit deploy (init-structure). Do not hand-edit.
5971
- * Preloaded via pm2 node_args --require so Args sees ENV=production even when
5972
- * a post-SIGKILL restart briefly omits ecosystem env (otherwise Db \u2192 local:6032).
6022
+ * Preloaded via pm2 node_args --require. Always set (do not only fill when unset):
6023
+ * \`pm2 start \u2026 --update-env\` can inject a non-production ENV from the
6024
+ * deploy shell / prior dump, and a conditional assign would leave it in place.
5973
6025
  */
5974
- if (!process.env.ENV) process.env.ENV = "production";
5975
- if (!process.env.NODE_ENV) process.env.NODE_ENV = "production";
6026
+ process.env.ENV = "production";
6027
+ process.env.NODE_ENV = "production";
5976
6028
  `;
5977
6029
  }
5978
6030
  function buildEcosystemConfig(service, paths) {
@@ -6334,7 +6386,9 @@ async function rollbackService(service, options = {}) {
6334
6386
  logger.info(`rollback target: v${buildInfo.version} release=${buildInfo.release ?? rollbackTarget.name}`);
6335
6387
  }
6336
6388
  await activateRelease(rollbackTarget.path, paths, { dryRun, logger });
6337
- await reloadPm2(paths, { dryRun, logger });
6389
+ const appName = service.pm2?.appName ?? null;
6390
+ const waitTimeoutMs = 65e3;
6391
+ await reloadPm2(paths, { dryRun, logger, appName, waitTimeoutMs });
6338
6392
  const summary = `rollback ${activeName} \u2192 ${rollbackTarget.name} dryRun=${dryRun}`;
6339
6393
  if (!dryRun) await appendDeployLog(paths.deployLog, summary);
6340
6394
  return { from: activeName, to: rollbackTarget.name, path: rollbackTarget.path };