@nmakarov/cli-toolkit 0.55.0 → 0.57.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
@@ -1081,6 +1081,10 @@ var Args = class _Args {
1081
1081
  this.parseArgs(args);
1082
1082
  this.env = this.get("env")?.toLowerCase() || "local";
1083
1083
  this.loadDotEnv();
1084
+ const envAfterDotEnv = this.get("env")?.toLowerCase();
1085
+ if (envAfterDotEnv) {
1086
+ this.env = envAfterDotEnv;
1087
+ }
1084
1088
  this.loadConfigFiles();
1085
1089
  this.checkConflicts();
1086
1090
  if (context && typeof context.registerCleanup === "function") {
@@ -5269,6 +5273,8 @@ function servicePaths(service) {
5269
5273
  current: join2(root, "current"),
5270
5274
  sharedEnv: join2(root, "shared", ".env"),
5271
5275
  ecosystem: join2(root, "shared", "ecosystem.config.cjs"),
5276
+ /** Preloaded by pm2 `node_args --require` so ENV survives flaky restarts. */
5277
+ ensureEnv: join2(root, "shared", "ensure-env.cjs"),
5272
5278
  deployLog: join2(root, "logs", "deploy.log"),
5273
5279
  lockHashFile: join2(root, "shared", ".package-lock.sha256")
5274
5280
  };
@@ -5959,12 +5965,23 @@ function resolvePm2Args(pm2) {
5959
5965
  if (/(?:^|\s)--stopAllowance=/.test(base)) return base;
5960
5966
  return `${base}${base ? " " : ""}--stopAllowance=${Math.floor(stopSec)}`.trim();
5961
5967
  }
5968
+ function buildEnsureEnvScript() {
5969
+ return `/**
5970
+ * 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).
5973
+ */
5974
+ if (!process.env.ENV) process.env.ENV = "production";
5975
+ if (!process.env.NODE_ENV) process.env.NODE_ENV = "production";
5976
+ `;
5977
+ }
5962
5978
  function buildEcosystemConfig(service, paths) {
5963
5979
  const { pm2 } = service;
5964
5980
  const outLog = join9(paths.logs, `${pm2.appName}.out.log`);
5965
5981
  const errLog = join9(paths.logs, `${pm2.appName}.err.log`);
5966
5982
  const killTimeoutMs = resolveKillTimeoutMs(pm2);
5967
5983
  const args = resolvePm2Args(pm2);
5984
+ const ensureEnv = paths.ensureEnv;
5968
5985
  return `/**
5969
5986
  * pm2 ecosystem for ${service.name} \u2014 written by cli-toolkit deploy from the
5970
5987
  * service manifest (init/deploy). Do not hand-edit; change pm2.* in the
@@ -5977,6 +5994,8 @@ module.exports = {
5977
5994
  script: "${pm2.script}",
5978
5995
  cwd: "${paths.current}",
5979
5996
  args: ${JSON.stringify(args)},
5997
+ // Absolute --require so ENV is set before Args constructs (see shared/ensure-env.cjs).
5998
+ node_args: ${JSON.stringify(`--require ${ensureEnv}`)},
5980
5999
  instances: 1,
5981
6000
  exec_mode: "fork",
5982
6001
  autorestart: true,
@@ -6018,9 +6037,17 @@ async function initServiceStructure(service, options = {}) {
6018
6037
  created.push(dir);
6019
6038
  }
6020
6039
  const ecosystemExisted = await pathExists4(paths.ecosystem);
6040
+ const ensureEnvExisted = await pathExists4(paths.ensureEnv);
6021
6041
  if (!dryRun) {
6042
+ await writeFile5(paths.ensureEnv, buildEnsureEnvScript(), { mode: 420 });
6022
6043
  await writeFile5(paths.ecosystem, buildEcosystemConfig(service, paths), { mode: 420 });
6023
6044
  }
6045
+ if (ensureEnvExisted) {
6046
+ logger.info(`synced ${paths.ensureEnv}`);
6047
+ } else {
6048
+ created.push(paths.ensureEnv);
6049
+ logger.info(`seeded ${paths.ensureEnv}`);
6050
+ }
6024
6051
  if (ecosystemExisted) {
6025
6052
  logger.info(`synced ${paths.ecosystem} from manifest`);
6026
6053
  } else {