@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.cjs CHANGED
@@ -3735,6 +3735,12 @@ var Db = class _Db {
3735
3735
  }
3736
3736
  if (!dbConnectionString) {
3737
3737
  if (!dbName) {
3738
+ const env = context?.args?.env;
3739
+ if (env && String(env).toLowerCase() !== "local") {
3740
+ throw new ParamError(
3741
+ `Db: dbName not set for env="${env}" (set --dbName or DB_NAME_${String(env).toUpperCase()})`
3742
+ );
3743
+ }
3738
3744
  dbName = "local";
3739
3745
  }
3740
3746
  const paramName = `dbConnectionString${capitalizeFirstLetter(dbName)}`;
@@ -5813,10 +5819,47 @@ function isFreshOnline(proc, oldPid) {
5813
5819
  }
5814
5820
  return true;
5815
5821
  }
5822
+ async function deletePm2App(appName, options = {}) {
5823
+ const { dryRun = false, logger = console, waitTimeoutMs = 65e3, oldPid = null } = options;
5824
+ if (dryRun) {
5825
+ logger.info(`[dryRun] would pm2 delete ${appName}`);
5826
+ return;
5827
+ }
5828
+ const before = await getPm2Process(appName, { logger });
5829
+ if (!before) {
5830
+ logger.info(`pm2 ${appName}: not present (skip delete)`);
5831
+ return;
5832
+ }
5833
+ await runShell(`pm2 delete "${appName}"`, { logger });
5834
+ await waitPm2(
5835
+ appName,
5836
+ (proc) => {
5837
+ if (proc) return false;
5838
+ if (oldPid != null && isPidAlive(oldPid)) return false;
5839
+ return true;
5840
+ },
5841
+ { timeoutMs: waitTimeoutMs, logger, label: "deleted (process gone)" }
5842
+ );
5843
+ logger.info(`pm2 ${appName} deleted`);
5844
+ }
5845
+ async function startFromEcosystem(paths, options = {}) {
5846
+ const { dryRun = false, logger = console } = options;
5847
+ if (dryRun) {
5848
+ logger.info(`[dryRun] would pm2 start ${paths.ecosystem} --update-env`);
5849
+ return;
5850
+ }
5851
+ await runShell(
5852
+ `ENV=production NODE_ENV=production pm2 start "${paths.ecosystem}" --update-env`,
5853
+ { logger }
5854
+ );
5855
+ logger.info("pm2 started from ecosystem");
5856
+ }
5816
5857
  async function reloadPm2(paths, options = {}) {
5817
5858
  const { dryRun = false, logger = console, appName = null, waitTimeoutMs = 65e3 } = options;
5818
5859
  if (dryRun) {
5819
- logger.info(`[dryRun] would pm2 startOrReload ${paths.ecosystem} --update-env`);
5860
+ logger.info(
5861
+ `[dryRun] would pm2 delete${appName ? ` ${appName}` : ""} + start ${paths.ecosystem} --update-env`
5862
+ );
5820
5863
  return;
5821
5864
  }
5822
5865
  let oldPid = null;
@@ -5825,11 +5868,15 @@ async function reloadPm2(paths, options = {}) {
5825
5868
  const n = Number(before?.pid);
5826
5869
  if (Number.isFinite(n) && n > 0 && before?.pm2_env?.status === "online") {
5827
5870
  oldPid = n;
5828
- logger.info(`pm2 ${appName}: reloading (old pid=${oldPid})`);
5871
+ logger.info(`pm2 ${appName}: recreate (old pid=${oldPid})`);
5829
5872
  }
5873
+ await deletePm2App(appName, { dryRun, logger, waitTimeoutMs, oldPid });
5874
+ } else {
5875
+ logger.warn?.(
5876
+ "reloadPm2: appName missing; starting ecosystem without delete (pass appName so script/env always recreate cleanly)"
5877
+ );
5830
5878
  }
5831
- await runShell(`pm2 startOrReload "${paths.ecosystem}" --update-env`, { logger });
5832
- logger.info("pm2 reloaded");
5879
+ await startFromEcosystem(paths, { dryRun, logger });
5833
5880
  if (appName) {
5834
5881
  const proc = await waitPm2(
5835
5882
  appName,
@@ -5837,7 +5884,7 @@ async function reloadPm2(paths, options = {}) {
5837
5884
  {
5838
5885
  timeoutMs: waitTimeoutMs,
5839
5886
  logger,
5840
- label: oldPid ? `online on new pid (old ${oldPid} gone)` : "online after reload"
5887
+ label: oldPid ? `online on new pid (old ${oldPid} gone)` : "online after recreate"
5841
5888
  }
5842
5889
  );
5843
5890
  logger.info(`pm2 ${appName} online pid=${proc?.pid ?? "?"}`);
@@ -5874,11 +5921,15 @@ async function stopPm2(appName, options = {}) {
5874
5921
  async function startPm2(paths, options = {}) {
5875
5922
  const { dryRun = false, logger = console, appName = null, waitTimeoutMs = 65e3 } = options;
5876
5923
  if (dryRun) {
5877
- logger.info(`[dryRun] would pm2 start ${paths.ecosystem} --update-env`);
5924
+ logger.info(
5925
+ `[dryRun] would pm2 delete${appName ? ` ${appName}` : ""} + start ${paths.ecosystem} --update-env`
5926
+ );
5878
5927
  return;
5879
5928
  }
5880
- await runShell(`pm2 start "${paths.ecosystem}" --update-env`, { logger });
5881
- logger.info("pm2 started");
5929
+ if (appName) {
5930
+ await deletePm2App(appName, { dryRun, logger, waitTimeoutMs });
5931
+ }
5932
+ await startFromEcosystem(paths, { dryRun, logger });
5882
5933
  if (appName) {
5883
5934
  const proc = await waitPm2(
5884
5935
  appName,
@@ -6132,11 +6183,12 @@ function resolvePm2Args(pm2) {
6132
6183
  function buildEnsureEnvScript() {
6133
6184
  return `/**
6134
6185
  * Written by cli-toolkit deploy (init-structure). Do not hand-edit.
6135
- * Preloaded via pm2 node_args --require so Args sees ENV=production even when
6136
- * a post-SIGKILL restart briefly omits ecosystem env (otherwise Db \u2192 local:6032).
6186
+ * Preloaded via pm2 node_args --require. Always set (do not only fill when unset):
6187
+ * \`pm2 start \u2026 --update-env\` can inject a non-production ENV from the
6188
+ * deploy shell / prior dump, and a conditional assign would leave it in place.
6137
6189
  */
6138
- if (!process.env.ENV) process.env.ENV = "production";
6139
- if (!process.env.NODE_ENV) process.env.NODE_ENV = "production";
6190
+ process.env.ENV = "production";
6191
+ process.env.NODE_ENV = "production";
6140
6192
  `;
6141
6193
  }
6142
6194
  function buildEcosystemConfig(service, paths) {
@@ -6498,7 +6550,9 @@ async function rollbackService(service, options = {}) {
6498
6550
  logger.info(`rollback target: v${buildInfo.version} release=${buildInfo.release ?? rollbackTarget.name}`);
6499
6551
  }
6500
6552
  await activateRelease(rollbackTarget.path, paths, { dryRun, logger });
6501
- await reloadPm2(paths, { dryRun, logger });
6553
+ const appName = service.pm2?.appName ?? null;
6554
+ const waitTimeoutMs = 65e3;
6555
+ await reloadPm2(paths, { dryRun, logger, appName, waitTimeoutMs });
6502
6556
  const summary = `rollback ${activeName} \u2192 ${rollbackTarget.name} dryRun=${dryRun}`;
6503
6557
  if (!dryRun) await appendDeployLog(paths.deployLog, summary);
6504
6558
  return { from: activeName, to: rollbackTarget.name, path: rollbackTarget.path };