@dudousxd/nestjs-codegen 0.14.1 → 0.14.2

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/cli/main.js CHANGED
@@ -2151,19 +2151,41 @@ function isManifestShape(value) {
2151
2151
  if (typeof candidate.hash !== "string") return false;
2152
2152
  if (candidate.entryPoint !== void 0 && !isEntryPoint(candidate.entryPoint)) return false;
2153
2153
  if (candidate.configHash !== void 0 && typeof candidate.configHash !== "string") return false;
2154
+ if (candidate.configKeyHashes !== void 0 && !isStringRecord(candidate.configKeyHashes)) {
2155
+ return false;
2156
+ }
2154
2157
  if (!Array.isArray(candidate.files)) return false;
2155
2158
  return candidate.files.every((entry) => typeof entry === "string");
2156
2159
  }
2160
+ function isStringRecord(value) {
2161
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
2162
+ return Object.values(value).every((entry) => typeof entry === "string");
2163
+ }
2157
2164
  function serializeConfig(config) {
2165
+ return serializeConfigValue(config, `unserializable:${config.codegen.outDir}`);
2166
+ }
2167
+ function serializeConfigValue(value, unserializableMarker) {
2158
2168
  try {
2159
- return JSON.stringify(config, (_key, value) => {
2160
- if (typeof value === "function") return `[fn:${value.name}]${value.toString()}`;
2161
- return value;
2169
+ return JSON.stringify(value, (_key, entry) => {
2170
+ if (typeof entry === "function") return `[fn:${entry.name}]`;
2171
+ return entry;
2162
2172
  });
2163
2173
  } catch {
2164
- return `unserializable:${config.codegen.outDir}:${config.contracts.glob}`;
2174
+ return unserializableMarker;
2165
2175
  }
2166
2176
  }
2177
+ function computeConfigKeyHashes(config) {
2178
+ const hashes = {};
2179
+ for (const [key, value] of Object.entries(config)) {
2180
+ if (value === void 0) continue;
2181
+ hashes[key] = createHash("sha256").update(serializeConfigValue(value, `unserializable:${key}`)).digest("hex");
2182
+ }
2183
+ return hashes;
2184
+ }
2185
+ function diffConfigKeyHashes(previous, current) {
2186
+ const keys = /* @__PURE__ */ new Set([...Object.keys(previous), ...Object.keys(current)]);
2187
+ return [...keys].filter((key) => previous[key] !== current[key]).sort();
2188
+ }
2167
2189
  async function discoverInputFiles(config) {
2168
2190
  const globs = [config.contracts.glob, config.forms.watch];
2169
2191
  if (config.pages) globs.push(config.pages.glob);
@@ -2198,6 +2220,7 @@ async function readManifest(outDir) {
2198
2220
  hash: parsed.hash,
2199
2221
  ...parsed.entryPoint ? { entryPoint: parsed.entryPoint } : {},
2200
2222
  ...parsed.configHash ? { configHash: parsed.configHash } : {},
2223
+ ...parsed.configKeyHashes ? { configKeyHashes: parsed.configKeyHashes } : {},
2201
2224
  files: parsed.files
2202
2225
  };
2203
2226
  } catch {
@@ -2251,8 +2274,9 @@ function debugWarn(message) {
2251
2274
  }
2252
2275
 
2253
2276
  // src/generate.ts
2254
- function driftGuardMessage(outDir, previousEntryPoint, currentEntryPoint) {
2255
- return `[nestjs-codegen] Config drift detected in "${outDir}": the last generate ran from the "${previousEntryPoint}" entry point, this run is from the "${currentEntryPoint}" entry point, and their resolved configs differ (e.g. \`serialization: "json"\` vs \`"superjson"\`). Both entry points must read the SAME config \u2014 export a shared config object (e.g. codegen.config.ts) and import it from BOTH nestjs-codegen.config.ts (CLI) and NestjsCodegenModule.forRoot() (Nest module), or set \`driftGuard: false\` on either config to opt out of this check.`;
2277
+ function driftGuardMessage(outDir, previousEntryPoint, currentEntryPoint, differingKeys) {
2278
+ const differ = differingKeys.length > 0 ? `their resolved configs differ at: ${differingKeys.map((key) => `\`${key}\``).join(", ")}` : "their resolved configs differ (re-run after this generate records per-key hashes to see which keys)";
2279
+ return `[nestjs-codegen] Config drift detected in "${outDir}": the last generate ran from the "${previousEntryPoint}" entry point, this run is from the "${currentEntryPoint}" entry point, and ${differ}. Both entry points must read the SAME config \u2014 export a shared config object (e.g. codegen.config.ts) and import it from BOTH nestjs-codegen.config.ts (CLI) and NestjsCodegenModule.forRoot() (Nest module), or set \`driftGuard: false\` on either config to opt out of this check.`;
2256
2280
  }
2257
2281
  async function generate(config, inputRoutes = [], entryPoint = "cli") {
2258
2282
  setCodegenDebug(config.debug);
@@ -2263,9 +2287,17 @@ async function generate(config, inputRoutes = [], entryPoint = "cli") {
2263
2287
  return;
2264
2288
  }
2265
2289
  const configHash = computeConfigHash(config);
2290
+ const configKeyHashes = computeConfigKeyHashes(config);
2266
2291
  if (config.driftGuard && manifest?.entryPoint && manifest.entryPoint !== entryPoint && manifest.configHash && manifest.configHash !== configHash) {
2267
2292
  throw new DriftGuardError(
2268
- driftGuardMessage(config.codegen.outDir, manifest.entryPoint, entryPoint)
2293
+ driftGuardMessage(
2294
+ config.codegen.outDir,
2295
+ manifest.entryPoint,
2296
+ entryPoint,
2297
+ // A pre-key-hash manifest can't tell us WHICH keys differ — pass none
2298
+ // rather than diffing against {} (which would name every key).
2299
+ manifest.configKeyHashes ? diffConfigKeyHashes(manifest.configKeyHashes, configKeyHashes) : []
2300
+ )
2269
2301
  );
2270
2302
  }
2271
2303
  const extensions = config.extensions ?? [];
@@ -2335,6 +2367,7 @@ async function generate(config, inputRoutes = [], entryPoint = "cli") {
2335
2367
  hash: inputsHash,
2336
2368
  entryPoint,
2337
2369
  configHash,
2370
+ configKeyHashes,
2338
2371
  files: outputFiles
2339
2372
  });
2340
2373
  }
@@ -4782,7 +4815,7 @@ async function watch(config, onChange, options = {}) {
4782
4815
  }
4783
4816
 
4784
4817
  // src/index.ts
4785
- var VERSION = "0.14.1";
4818
+ var VERSION = "0.14.2";
4786
4819
 
4787
4820
  // src/cli/codegen.ts
4788
4821
  async function runCodegen(opts = {}) {