mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-16 17:09:09 +00:00
A default global installation creates a package `.env` from `.env.example`. The CLI treated any contributed package value as a user customization and warned on every command, even when every value would be regenerated during the next installation. The warning now compares only values actually supplied by the package file with the shipped defaults, using the same value parsing and first-writer behavior as CLI loading. Unchanged defaults stay silent; customized/unknown keys still warn. Missing or unreadable template evidence keeps the warning conservative. Existing loading precedence, shadowed-key warnings and secret provisioning are unchanged, and warning messages never include values. Closes #16763. Validation at pinned release base `d50cd8076e5c8ab7d7b4eb38388394d9c9ac8d98`, Node 24.21.0, lock-exact dependencies: - Native regression: RED with 4 expected failures and 3 passing controls; final native suite: 14/14 PASS including original guard cases, real syncEnv-generated defaults, storage-key comments, quotes, empty/unknown/custom values, shadowed contributions, duplicate keys and missing/unreadable templates. - Actual CLI entrypoint in a temporary installed-package layout: unchanged source emitted the unwanted warning (RED); patch runs both default and customized `--help` scenarios successfully. Combined final suite: 15/15 PASS. The custom value is absent from diagnostics and the `.env` file is unchanged by the read-only command. - All 36 sibling/reference files rerun serially: **269 passed, 0 failed, 1 explicit live-smoke skip**. The initial run hit the existing 30s `claude-config-set-12407` wrapper timeout after writing its fixture config. Unmodified isolated tests passed on both base and patch, and the complete second run passed without changing assertions/timeouts; the original failure log is retained, not labeled a proven base failure. - Explicit CLI ESLint (including normally ignored `.mjs` files) and helper cyclomatic/cognitive complexity checks at a ceiling of 15 passed. - Formatting, ESLint, dashboard typecheck, both complexity ratchets, changelog integrity and test discovery passed. The final commit only adds the required Markdown bullet to the fragment; production and test files are byte-identical to the gate-tested commit, and formatting/changelog were checked again. - ⚠️ base-red inherited: #15306 — file-size and core typecheck. The unchanged pinned base and patch have exactly the same five file-size violations and the same 57 TypeScript diagnostics. These are reproduced inherited failures, not passing gates; no baseline or assertion was weakened. The installed-layout smoke ran locally on Linux. No live Windows ARM64 installation, global npm reinstall, provider traffic or deployment is claimed. Thanks @diegosouzapw.
71 lines
2.9 KiB
JavaScript
71 lines
2.9 KiB
JavaScript
import { readFileSync } from "node:fs";
|
|
import { join, sep } from "node:path";
|
|
import { parseEnvValue } from "./parseEnvValue.mjs";
|
|
|
|
/**
|
|
* A `.env` inside the installed package directory does not survive an update:
|
|
* `npm i -g` replaces that directory wholesale, and postinstall recreates the
|
|
* file from `.env.example`. The CLI announces every env file it loads without
|
|
* distinguishing the ones that last from the one that doesn't.
|
|
*
|
|
* Returns the warning to print, or null when there is nothing worth saying.
|
|
*
|
|
* Two conditions, both required, so a development checkout never sees this:
|
|
* - the file sits inside the package root, and that root is inside a
|
|
* `node_modules` directory — i.e. an installed package, not a checkout,
|
|
* where the same path is stable and documented in SETUP_GUIDE.md;
|
|
* - the file actually supplied at least one value. First writer wins, so a
|
|
* file entirely shadowed by a durable one supplied nothing, and losing it
|
|
* costs nothing. Generated template defaults are restored on update too;
|
|
* only effective values that differ from those defaults are at risk.
|
|
*
|
|
* @param {{ envPath: string, packageRoot: string, durableEnvPath: string, suppliedKeys: boolean, suppliedValues?: Map<string, string | undefined> }} args
|
|
* @returns {string | null}
|
|
*/
|
|
export function describeVolatileEnvWarning({
|
|
envPath,
|
|
packageRoot,
|
|
durableEnvPath,
|
|
suppliedKeys,
|
|
suppliedValues,
|
|
}) {
|
|
if (!suppliedKeys) return null;
|
|
if (envPath === durableEnvPath) return null;
|
|
if (!isInsideInstalledPackage(packageRoot)) return null;
|
|
if (!envPath.startsWith(packageRoot + sep)) return null;
|
|
if (suppliedValues && suppliesOnlyTemplateDefaults(packageRoot, suppliedValues)) return null;
|
|
|
|
return (
|
|
`${envPath} lives inside the installed package: updating OmniRoute replaces it. ` +
|
|
`Move the values you set to ${durableEnvPath}, which updates leave alone.`
|
|
);
|
|
}
|
|
|
|
/** True when the path sits under a `node_modules` directory. */
|
|
function isInsideInstalledPackage(dir) {
|
|
return typeof dir === "string" && dir.split(sep).includes("node_modules");
|
|
}
|
|
|
|
/** Missing template evidence must never hide an at-risk configuration. */
|
|
function suppliesOnlyTemplateDefaults(packageRoot, suppliedValues) {
|
|
let content;
|
|
try {
|
|
content = readFileSync(join(packageRoot, ".env.example"), "utf8");
|
|
} catch {
|
|
return false;
|
|
}
|
|
const defaults = new Map();
|
|
for (const line of content.split("\n")) {
|
|
const trimmed = line.trim();
|
|
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
const index = trimmed.indexOf("=");
|
|
if (index < 1) continue;
|
|
const key = trimmed.slice(0, index).trim();
|
|
// Match CLI loading: the first declaration of a key wins.
|
|
if (!defaults.has(key)) defaults.set(key, parseEnvValue(trimmed.slice(index + 1)));
|
|
}
|
|
return [...suppliedValues].every(
|
|
([key, value]) => defaults.has(key) && defaults.get(key) === value
|
|
);
|
|
}
|