@dudousxd/nestjs-codegen 0.11.0 → 0.13.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/cli/main.cjs CHANGED
@@ -217,8 +217,8 @@ Run \`nestjs-codegen init\` to create a starter config.`
217
217
  }
218
218
 
219
219
  // src/generate.ts
220
- var import_promises11 = require("fs/promises");
221
- var import_node_path12 = require("path");
220
+ var import_promises12 = require("fs/promises");
221
+ var import_node_path13 = require("path");
222
222
 
223
223
  // src/discovery/pages.ts
224
224
  var import_promises2 = require("fs/promises");
@@ -820,6 +820,7 @@ function buildRequestModel(c) {
820
820
  const optsParts = [];
821
821
  if (hasQuery) optsParts.push("query: input?.query as Record<string, unknown> | undefined");
822
822
  if (hasBody) optsParts.push("body: input?.body");
823
+ if (hasBody && c.contractSource.multipart) optsParts.push("multipart: true");
823
824
  const optsExpr = optsParts.length ? `{ ${optsParts.join(", ")} }` : "{}";
824
825
  return {
825
826
  routeName: c.name,
@@ -2113,6 +2114,99 @@ function buildEmpty() {
2113
2114
  ].join("\n");
2114
2115
  }
2115
2116
 
2117
+ // src/generate-manifest.ts
2118
+ var import_node_crypto = require("crypto");
2119
+ var import_promises11 = require("fs/promises");
2120
+ var import_node_path12 = require("path");
2121
+ var import_fast_glob2 = __toESM(require("fast-glob"), 1);
2122
+ var MANIFEST_FILE = ".codegen-manifest.json";
2123
+ var LOCK_FILE = ".watcher.lock";
2124
+ function isManifestShape(value) {
2125
+ if (typeof value !== "object" || value === null) return false;
2126
+ const candidate = value;
2127
+ if (typeof candidate.version !== "string") return false;
2128
+ if (typeof candidate.hash !== "string") return false;
2129
+ if (!Array.isArray(candidate.files)) return false;
2130
+ return candidate.files.every((entry) => typeof entry === "string");
2131
+ }
2132
+ function serializeConfig(config) {
2133
+ try {
2134
+ return JSON.stringify(config, (_key, value) => {
2135
+ if (typeof value === "function") return `[fn:${value.name}]${value.toString()}`;
2136
+ return value;
2137
+ });
2138
+ } catch {
2139
+ return `unserializable:${config.codegen.outDir}:${config.contracts.glob}`;
2140
+ }
2141
+ }
2142
+ async function discoverInputFiles(config) {
2143
+ const globs = [config.contracts.glob, config.forms.watch];
2144
+ if (config.pages) globs.push(config.pages.glob);
2145
+ const cwd = config.codegen.cwd;
2146
+ const matched = await (0, import_fast_glob2.default)(globs, { cwd, absolute: true, onlyFiles: true });
2147
+ return [...new Set(matched)].sort();
2148
+ }
2149
+ async function computeInputsHash(config) {
2150
+ const hash = (0, import_node_crypto.createHash)("sha256");
2151
+ hash.update(`version:${VERSION}
2152
+ `);
2153
+ hash.update(`config:${serializeConfig(config)}
2154
+ `);
2155
+ const inputFiles = await discoverInputFiles(config);
2156
+ const cwd = config.codegen.cwd;
2157
+ for (const file of inputFiles) {
2158
+ const contents = await (0, import_promises11.readFile)(file, "utf8");
2159
+ hash.update(`file:${(0, import_node_path12.relative)(cwd, file)}
2160
+ `);
2161
+ hash.update(contents);
2162
+ hash.update("\n");
2163
+ }
2164
+ return hash.digest("hex");
2165
+ }
2166
+ async function readManifest(outDir) {
2167
+ try {
2168
+ const raw = await (0, import_promises11.readFile)((0, import_node_path12.join)(outDir, MANIFEST_FILE), "utf8");
2169
+ const parsed = JSON.parse(raw);
2170
+ if (!isManifestShape(parsed)) return null;
2171
+ return { version: parsed.version, hash: parsed.hash, files: parsed.files };
2172
+ } catch {
2173
+ return null;
2174
+ }
2175
+ }
2176
+ async function writeManifest(outDir, manifest) {
2177
+ await (0, import_promises11.writeFile)((0, import_node_path12.join)(outDir, MANIFEST_FILE), `${JSON.stringify(manifest, null, 2)}
2178
+ `, "utf8");
2179
+ }
2180
+ async function listOutputFiles(outDir) {
2181
+ const found = [];
2182
+ async function walk(dir) {
2183
+ const entries = await (0, import_promises11.readdir)(dir, { withFileTypes: true }).catch(() => []);
2184
+ for (const entry of entries) {
2185
+ const abs = (0, import_node_path12.join)(dir, entry.name);
2186
+ if (entry.isDirectory()) {
2187
+ await walk(abs);
2188
+ } else if (entry.isFile()) {
2189
+ const rel = (0, import_node_path12.relative)(outDir, abs);
2190
+ if (rel === MANIFEST_FILE || rel === LOCK_FILE) continue;
2191
+ found.push(rel);
2192
+ }
2193
+ }
2194
+ }
2195
+ await walk(outDir);
2196
+ return found.sort();
2197
+ }
2198
+ async function allOutputsExist(outDir, files) {
2199
+ const present = new Set(await listOutputFiles(outDir));
2200
+ return files.every((file) => present.has(file));
2201
+ }
2202
+ async function isManifestFresh(outDir, manifest, inputsHash) {
2203
+ if (manifest === null) return false;
2204
+ if (manifest.version !== VERSION) return false;
2205
+ if (manifest.hash !== inputsHash) return false;
2206
+ if (manifest.files.length === 0) return false;
2207
+ return allOutputsExist(outDir, manifest.files);
2208
+ }
2209
+
2116
2210
  // src/util/debug-log.ts
2117
2211
  var debugEnabled = false;
2118
2212
  function setCodegenDebug(enabled) {
@@ -2125,6 +2219,12 @@ function debugWarn(message) {
2125
2219
  // src/generate.ts
2126
2220
  async function generate(config, inputRoutes = []) {
2127
2221
  setCodegenDebug(config.debug);
2222
+ const inputsHash = await computeInputsHash(config);
2223
+ const manifest = await readManifest(config.codegen.outDir);
2224
+ if (await isManifestFresh(config.codegen.outDir, manifest, inputsHash)) {
2225
+ console.log(`[nestjs-codegen] ${config.codegen.outDir} up to date, skipped`);
2226
+ return;
2227
+ }
2128
2228
  const extensions = config.extensions ?? [];
2129
2229
  let routes = inputRoutes;
2130
2230
  const ctx = createExtensionContext(config, () => routes);
@@ -2181,21 +2281,27 @@ async function generate(config, inputRoutes = []) {
2181
2281
  if (extensions.length > 0) {
2182
2282
  const extraFiles = await collectEmittedFiles(extensions, ctx);
2183
2283
  for (const file of extraFiles) {
2184
- const dest = (0, import_node_path12.join)(config.codegen.outDir, file.path);
2185
- await (0, import_promises11.mkdir)((0, import_node_path12.dirname)(dest), { recursive: true });
2186
- await (0, import_promises11.writeFile)(dest, file.contents, "utf8");
2284
+ const dest = (0, import_node_path13.join)(config.codegen.outDir, file.path);
2285
+ await (0, import_promises12.mkdir)((0, import_node_path13.dirname)(dest), { recursive: true });
2286
+ await (0, import_promises12.writeFile)(dest, file.contents, "utf8");
2187
2287
  }
2188
2288
  }
2289
+ const outputFiles = await listOutputFiles(config.codegen.outDir);
2290
+ await writeManifest(config.codegen.outDir, {
2291
+ version: VERSION,
2292
+ hash: inputsHash,
2293
+ files: outputFiles
2294
+ });
2189
2295
  }
2190
2296
 
2191
2297
  // src/watch/watcher.ts
2192
- var import_promises14 = require("fs/promises");
2193
- var import_node_path16 = require("path");
2298
+ var import_promises15 = require("fs/promises");
2299
+ var import_node_path17 = require("path");
2194
2300
  var import_chokidar = __toESM(require("chokidar"), 1);
2195
2301
 
2196
2302
  // src/discovery/contracts-fast.ts
2197
- var import_node_path14 = require("path");
2198
- var import_fast_glob2 = __toESM(require("fast-glob"), 1);
2303
+ var import_node_path15 = require("path");
2304
+ var import_fast_glob3 = __toESM(require("fast-glob"), 1);
2199
2305
  var import_ts_morph9 = require("ts-morph");
2200
2306
 
2201
2307
  // src/discovery/dto-type-resolver.ts
@@ -2206,7 +2312,7 @@ var import_ts_morph4 = require("ts-morph");
2206
2312
 
2207
2313
  // src/discovery/type-ref-resolution.ts
2208
2314
  var import_node_fs = require("fs");
2209
- var import_node_path13 = require("path");
2315
+ var import_node_path14 = require("path");
2210
2316
  var import_ts_morph3 = require("ts-morph");
2211
2317
  var _EMPTY_CTX = { projectRoot: "", tsconfigPaths: null };
2212
2318
  var _ctxByProject = /* @__PURE__ */ new WeakMap();
@@ -2258,12 +2364,12 @@ function findTypeInFile(name, file) {
2258
2364
  }
2259
2365
  function resolveModuleSpecifier(moduleSpecifier, sourceFile, project) {
2260
2366
  if (moduleSpecifier.startsWith(".")) {
2261
- const dir = (0, import_node_path13.dirname)(sourceFile.getFilePath());
2367
+ const dir = (0, import_node_path14.dirname)(sourceFile.getFilePath());
2262
2368
  const noExt = moduleSpecifier.replace(/\.(js|ts)$/, "");
2263
2369
  return [
2264
- (0, import_node_path13.resolve)(dir, `${noExt}.ts`),
2265
- (0, import_node_path13.resolve)(dir, `${moduleSpecifier}.ts`),
2266
- (0, import_node_path13.resolve)(dir, moduleSpecifier, "index.ts")
2370
+ (0, import_node_path14.resolve)(dir, `${noExt}.ts`),
2371
+ (0, import_node_path14.resolve)(dir, `${moduleSpecifier}.ts`),
2372
+ (0, import_node_path14.resolve)(dir, moduleSpecifier, "index.ts")
2267
2373
  ];
2268
2374
  }
2269
2375
  const ctx = _ctxFor(project);
@@ -2284,8 +2390,8 @@ function resolveModuleSpecifier(moduleSpecifier, sourceFile, project) {
2284
2390
  const rest = moduleSpecifier.slice(prefix.length);
2285
2391
  const candidates = [];
2286
2392
  for (const mapping of mappings) {
2287
- const resolved = (0, import_node_path13.resolve)(baseUrl, mapping.replace("*", rest));
2288
- candidates.push(`${resolved}.ts`, (0, import_node_path13.resolve)(resolved, "index.ts"));
2393
+ const resolved = (0, import_node_path14.resolve)(baseUrl, mapping.replace("*", rest));
2394
+ candidates.push(`${resolved}.ts`, (0, import_node_path14.resolve)(resolved, "index.ts"));
2289
2395
  }
2290
2396
  dbg(" resolved candidates:", candidates);
2291
2397
  return candidates;
@@ -3595,6 +3701,55 @@ function extractParamsType(method, sourceFile, project) {
3595
3701
  }
3596
3702
  return entries.length > 0 ? `{ ${entries.join("; ")} }` : null;
3597
3703
  }
3704
+ function extractUploadedFiles(method) {
3705
+ const FILE = "File | Blob";
3706
+ const entries = [];
3707
+ let multipart = false;
3708
+ const hasUploadedFileParam = method.getParameters().some(
3709
+ (p) => p.getDecorators().some((d) => {
3710
+ const name = d.getName();
3711
+ return name === "UploadedFile" || name === "UploadedFiles";
3712
+ })
3713
+ );
3714
+ for (const decorator of method.getDecorators()) {
3715
+ if (decorator.getName() !== "UseInterceptors") continue;
3716
+ for (const arg of decorator.getArguments()) {
3717
+ if (!import_ts_morph7.Node.isCallExpression(arg)) continue;
3718
+ const interceptor = arg.getExpression().getText();
3719
+ const callArgs = arg.getArguments();
3720
+ const firstArg2 = callArgs[0];
3721
+ if (interceptor === "FileInterceptor") {
3722
+ if (firstArg2 && import_ts_morph7.Node.isStringLiteral(firstArg2)) {
3723
+ entries.push(`${firstArg2.getLiteralValue()}: ${FILE}`);
3724
+ multipart = true;
3725
+ }
3726
+ } else if (interceptor === "FilesInterceptor") {
3727
+ if (firstArg2 && import_ts_morph7.Node.isStringLiteral(firstArg2)) {
3728
+ entries.push(`${firstArg2.getLiteralValue()}: Array<${FILE}>`);
3729
+ multipart = true;
3730
+ }
3731
+ } else if (interceptor === "FileFieldsInterceptor") {
3732
+ if (firstArg2 && import_ts_morph7.Node.isArrayLiteralExpression(firstArg2)) {
3733
+ for (const el of firstArg2.getElements()) {
3734
+ if (!import_ts_morph7.Node.isObjectLiteralExpression(el)) continue;
3735
+ const nameProp = el.getProperty("name");
3736
+ if (nameProp && import_ts_morph7.Node.isPropertyAssignment(nameProp)) {
3737
+ const init = nameProp.getInitializer();
3738
+ if (init && import_ts_morph7.Node.isStringLiteral(init)) {
3739
+ entries.push(`${init.getLiteralValue()}: Array<${FILE}>`);
3740
+ }
3741
+ }
3742
+ }
3743
+ multipart = true;
3744
+ }
3745
+ } else if (interceptor === "AnyFilesInterceptor") {
3746
+ multipart = true;
3747
+ }
3748
+ }
3749
+ }
3750
+ if (hasUploadedFileParam) multipart = true;
3751
+ return { fields: entries.length > 0 ? entries.join("; ") : null, multipart };
3752
+ }
3598
3753
  function extractResponseType(method, sourceFile, project) {
3599
3754
  const apiResponseDecorator = method.getDecorators().find((d) => d.getName() === "ApiResponse" && (apiResponseStatus(d) ?? 0) < 400);
3600
3755
  if (apiResponseDecorator) {
@@ -3729,6 +3884,11 @@ function extractDtoContract(method, sourceFile, project) {
3729
3884
  let body = extractBodyType(method, sourceFile, project);
3730
3885
  const filterInfo = extractApplyFilterInfo(method, sourceFile, project);
3731
3886
  const query = extractQueryType(method, sourceFile, project);
3887
+ const uploads = extractUploadedFiles(method);
3888
+ if (uploads.fields) {
3889
+ const fileObject = `{ ${uploads.fields} }`;
3890
+ body = body ? `(${body}) & ${fileObject}` : fileObject;
3891
+ }
3732
3892
  const streamElement = detectStreamElement(method);
3733
3893
  const isStream = streamElement !== null;
3734
3894
  if (filterInfo && filterInfo.source === "body") {
@@ -3738,7 +3898,7 @@ function extractDtoContract(method, sourceFile, project) {
3738
3898
  const paramsType = extractParamsType(method, sourceFile, project);
3739
3899
  const response = isStream ? resolveTypeNodeToString(streamElement, sourceFile, project, 3) : extractResponseType(method, sourceFile, project);
3740
3900
  const errorInfo = extractErrorType(method, sourceFile, project);
3741
- if (body === null && query === null && paramsType === null && response === "unknown" && errorInfo === null && filterInfo === null && !isStream) {
3901
+ if (body === null && query === null && paramsType === null && response === "unknown" && errorInfo === null && filterInfo === null && !isStream && !uploads.multipart) {
3742
3902
  return null;
3743
3903
  }
3744
3904
  let bodyRef = null;
@@ -3811,7 +3971,8 @@ function extractDtoContract(method, sourceFile, project) {
3811
3971
  formWarnings,
3812
3972
  bodySchema,
3813
3973
  querySchema,
3814
- stream: isStream
3974
+ stream: isStream,
3975
+ multipart: uploads.multipart
3815
3976
  };
3816
3977
  }
3817
3978
  function resolveParamClass(method, decoratorName, sourceFile, project) {
@@ -3957,7 +4118,7 @@ async function discoverContractsFast(opts) {
3957
4118
  const { cwd, glob, tsconfig } = opts;
3958
4119
  const tsconfigPath = resolveTsconfigPath(cwd, tsconfig);
3959
4120
  const project = createDiscoveryProject(tsconfigPath);
3960
- const files = await (0, import_fast_glob2.default)(glob, { cwd, absolute: true, onlyFiles: true });
4121
+ const files = await (0, import_fast_glob3.default)(glob, { cwd, absolute: true, onlyFiles: true });
3961
4122
  for (const f of files) {
3962
4123
  project.addSourceFileAtPath(f);
3963
4124
  }
@@ -3965,7 +4126,7 @@ async function discoverContractsFast(opts) {
3965
4126
  return extractAllRoutes(project);
3966
4127
  }
3967
4128
  function resolveTsconfigPath(cwd, tsconfig) {
3968
- return tsconfig ? (0, import_node_path14.resolve)(tsconfig) : (0, import_node_path14.join)(cwd, "tsconfig.json");
4129
+ return tsconfig ? (0, import_node_path15.resolve)(tsconfig) : (0, import_node_path15.join)(cwd, "tsconfig.json");
3969
4130
  }
3970
4131
  function createDiscoveryProject(tsconfigPath) {
3971
4132
  try {
@@ -4030,7 +4191,7 @@ var PersistentDiscovery = class _PersistentDiscovery {
4030
4191
  const project = createDiscoveryProject(tsconfigPath);
4031
4192
  bindDiscoveryContext(project, cwd, tsconfigPath);
4032
4193
  const instance = new _PersistentDiscovery(project, cwd, glob);
4033
- const files = await (0, import_fast_glob2.default)(glob, { cwd, absolute: true, onlyFiles: true });
4194
+ const files = await (0, import_fast_glob3.default)(glob, { cwd, absolute: true, onlyFiles: true });
4034
4195
  for (const f of files) {
4035
4196
  project.addSourceFileAtPath(f);
4036
4197
  instance.controllerPaths.add(f);
@@ -4051,7 +4212,7 @@ var PersistentDiscovery = class _PersistentDiscovery {
4051
4212
  async rediscover(changedPaths) {
4052
4213
  if (changedPaths) {
4053
4214
  for (const p of changedPaths) {
4054
- const abs = (0, import_node_path14.resolve)(p);
4215
+ const abs = (0, import_node_path15.resolve)(p);
4055
4216
  const sf = this.project.getSourceFile(abs);
4056
4217
  if (sf) {
4057
4218
  await sf.refreshFromFileSystem();
@@ -4059,7 +4220,7 @@ var PersistentDiscovery = class _PersistentDiscovery {
4059
4220
  }
4060
4221
  }
4061
4222
  const globbed = new Set(
4062
- await (0, import_fast_glob2.default)(this.glob, { cwd: this.cwd, absolute: true, onlyFiles: true })
4223
+ await (0, import_fast_glob3.default)(this.glob, { cwd: this.cwd, absolute: true, onlyFiles: true })
4063
4224
  );
4064
4225
  for (const f of globbed) {
4065
4226
  if (!this.controllerPaths.has(f)) {
@@ -4296,7 +4457,8 @@ function extractDtoRoute(args) {
4296
4457
  formWarnings: dtoContract?.formWarnings ?? [],
4297
4458
  bodySchema: dtoContract?.bodySchema ?? null,
4298
4459
  querySchema: dtoContract?.querySchema ?? null,
4299
- stream: dtoContract?.stream ?? false
4460
+ stream: dtoContract?.stream ?? false,
4461
+ multipart: dtoContract?.multipart ?? false
4300
4462
  }
4301
4463
  });
4302
4464
  }
@@ -4339,10 +4501,10 @@ function extractFromSourceFile(sourceFile, project) {
4339
4501
  }
4340
4502
 
4341
4503
  // src/watch/lock-file.ts
4342
- var import_promises12 = require("fs/promises");
4343
4504
  var import_promises13 = require("fs/promises");
4344
- var import_node_path15 = require("path");
4345
- var LOCK_FILE = ".watcher.lock";
4505
+ var import_promises14 = require("fs/promises");
4506
+ var import_node_path16 = require("path");
4507
+ var LOCK_FILE2 = ".watcher.lock";
4346
4508
  function isProcessAlive(pid) {
4347
4509
  try {
4348
4510
  process.kill(pid, 0);
@@ -4352,21 +4514,21 @@ function isProcessAlive(pid) {
4352
4514
  }
4353
4515
  }
4354
4516
  async function acquireLock(outDir) {
4355
- await (0, import_promises13.mkdir)(outDir, { recursive: true });
4356
- const lockPath = (0, import_node_path15.join)(outDir, LOCK_FILE);
4517
+ await (0, import_promises14.mkdir)(outDir, { recursive: true });
4518
+ const lockPath = (0, import_node_path16.join)(outDir, LOCK_FILE2);
4357
4519
  const lockData = { pid: process.pid, startedAt: (/* @__PURE__ */ new Date()).toISOString() };
4358
4520
  try {
4359
- const fd = await (0, import_promises12.open)(lockPath, "wx");
4521
+ const fd = await (0, import_promises13.open)(lockPath, "wx");
4360
4522
  await fd.writeFile(`${JSON.stringify(lockData, null, 2)}
4361
4523
  `, "utf8");
4362
4524
  await fd.close();
4363
4525
  } catch (err) {
4364
4526
  if (err.code === "EEXIST") {
4365
4527
  try {
4366
- const raw = await (0, import_promises13.readFile)(lockPath, "utf8");
4528
+ const raw = await (0, import_promises14.readFile)(lockPath, "utf8");
4367
4529
  const existing = JSON.parse(raw);
4368
4530
  if (isProcessAlive(existing.pid)) return null;
4369
- await (0, import_promises13.unlink)(lockPath);
4531
+ await (0, import_promises14.unlink)(lockPath);
4370
4532
  return acquireLock(outDir);
4371
4533
  } catch {
4372
4534
  return null;
@@ -4377,7 +4539,7 @@ async function acquireLock(outDir) {
4377
4539
  return {
4378
4540
  release: async () => {
4379
4541
  try {
4380
- await (0, import_promises13.unlink)(lockPath);
4542
+ await (0, import_promises14.unlink)(lockPath);
4381
4543
  } catch {
4382
4544
  }
4383
4545
  }
@@ -4388,12 +4550,12 @@ async function acquireLock(outDir) {
4388
4550
  var PAGES_DEBOUNCE_MS = 150;
4389
4551
  var NO_OP_WATCHER = { close: async () => {
4390
4552
  } };
4391
- async function watch(config, onChange) {
4553
+ async function watch(config, onChange, options = {}) {
4392
4554
  const lock = await acquireLock(config.codegen.outDir);
4393
4555
  if (lock === null) {
4394
4556
  let holderPid = "unknown";
4395
4557
  try {
4396
- const raw = await (0, import_promises14.readFile)((0, import_node_path16.join)(config.codegen.outDir, ".watcher.lock"), "utf8");
4558
+ const raw = await (0, import_promises15.readFile)((0, import_node_path17.join)(config.codegen.outDir, ".watcher.lock"), "utf8");
4397
4559
  const data = JSON.parse(raw);
4398
4560
  if (data.pid !== void 0) holderPid = String(data.pid);
4399
4561
  } catch {
@@ -4416,22 +4578,33 @@ async function watch(config, onChange) {
4416
4578
  }
4417
4579
  return discovery;
4418
4580
  }
4419
- try {
4420
- const initialRoutes = (await getDiscovery()).discover();
4421
- lastRoutes = initialRoutes;
4422
- await generate(config, initialRoutes);
4423
- } catch (err) {
4424
- console.warn(
4425
- `[nestjs-codegen] Initial route discovery failed, falling back to pages-only: ${err instanceof Error ? err.message : String(err)}`
4426
- );
4581
+ async function runInitialPass() {
4427
4582
  try {
4428
- await generate(config, lastRoutes);
4429
- } catch {
4583
+ const initialRoutes = (await getDiscovery()).discover();
4584
+ lastRoutes = initialRoutes;
4585
+ await generate(config, initialRoutes);
4586
+ } catch (err) {
4587
+ console.warn(
4588
+ `[nestjs-codegen] Initial route discovery failed, falling back to pages-only: ${err instanceof Error ? err.message : String(err)}`
4589
+ );
4590
+ try {
4591
+ await generate(config, lastRoutes);
4592
+ } catch {
4593
+ }
4430
4594
  }
4431
4595
  }
4596
+ if (options.deferInitialGenerate) {
4597
+ void runInitialPass().catch((err) => {
4598
+ console.warn(
4599
+ `[nestjs-codegen] Background initial generate failed: ${err instanceof Error ? err.message : String(err)}`
4600
+ );
4601
+ });
4602
+ } else {
4603
+ await runInitialPass();
4604
+ }
4432
4605
  let pagesDebounceTimer;
4433
4606
  const pagesGlob = config.pages?.glob ?? ".nestjs-codegen-no-pages";
4434
- const pagesWatcher = import_chokidar.default.watch((0, import_node_path16.join)(config.codegen.cwd, pagesGlob), {
4607
+ const pagesWatcher = import_chokidar.default.watch((0, import_node_path17.join)(config.codegen.cwd, pagesGlob), {
4435
4608
  ignoreInitial: true,
4436
4609
  persistent: true,
4437
4610
  awaitWriteFinish: { stabilityThreshold: 80, pollInterval: 20 }
@@ -4458,7 +4631,7 @@ async function watch(config, onChange) {
4458
4631
  pagesWatcher.on("unlink", schedulePagesRegenerate);
4459
4632
  let contractsDebounceTimer;
4460
4633
  const pendingChangedPaths = /* @__PURE__ */ new Set();
4461
- const contractsWatcher = import_chokidar.default.watch((0, import_node_path16.join)(config.codegen.cwd, config.contracts.glob), {
4634
+ const contractsWatcher = import_chokidar.default.watch((0, import_node_path17.join)(config.codegen.cwd, config.contracts.glob), {
4462
4635
  ignoreInitial: true,
4463
4636
  persistent: true,
4464
4637
  awaitWriteFinish: { stabilityThreshold: 80, pollInterval: 20 }
@@ -4488,7 +4661,7 @@ async function watch(config, onChange) {
4488
4661
  contractsWatcher.on("add", (p) => scheduleContractsRegenerate(p));
4489
4662
  contractsWatcher.on("change", (p) => scheduleContractsRegenerate(p));
4490
4663
  contractsWatcher.on("unlink", (p) => scheduleContractsRegenerate(p));
4491
- const formsWatcher = import_chokidar.default.watch((0, import_node_path16.join)(config.codegen.cwd, config.forms.watch), {
4664
+ const formsWatcher = import_chokidar.default.watch((0, import_node_path17.join)(config.codegen.cwd, config.forms.watch), {
4492
4665
  ignoreInitial: true,
4493
4666
  persistent: true,
4494
4667
  awaitWriteFinish: { stabilityThreshold: 80, pollInterval: 20 }
@@ -4515,7 +4688,7 @@ async function watch(config, onChange) {
4515
4688
  }
4516
4689
 
4517
4690
  // src/index.ts
4518
- var VERSION = "0.11.0";
4691
+ var VERSION = "0.13.0";
4519
4692
 
4520
4693
  // src/cli/codegen.ts
4521
4694
  async function runCodegen(opts = {}) {
@@ -4544,13 +4717,13 @@ async function runCodegen(opts = {}) {
4544
4717
  // src/cli/doctor.ts
4545
4718
  var import_node_child_process2 = require("child_process");
4546
4719
  var import_node_fs4 = require("fs");
4547
- var import_node_path18 = require("path");
4720
+ var import_node_path19 = require("path");
4548
4721
 
4549
4722
  // src/cli/init.ts
4550
4723
  var import_node_child_process = require("child_process");
4551
4724
  var import_node_fs3 = require("fs");
4552
- var import_promises15 = require("fs/promises");
4553
- var import_node_path17 = require("path");
4725
+ var import_promises16 = require("fs/promises");
4726
+ var import_node_path18 = require("path");
4554
4727
  var import_node_readline = require("readline");
4555
4728
 
4556
4729
  // src/cli/patch-utils.ts
@@ -4612,7 +4785,7 @@ ${bold(title)}`);
4612
4785
  }
4613
4786
  async function readPackageJson(cwd) {
4614
4787
  try {
4615
- const raw = await (0, import_promises15.readFile)((0, import_node_path17.join)(cwd, "package.json"), "utf8");
4788
+ const raw = await (0, import_promises16.readFile)((0, import_node_path18.join)(cwd, "package.json"), "utf8");
4616
4789
  return JSON.parse(raw);
4617
4790
  } catch {
4618
4791
  return {};
@@ -4643,7 +4816,7 @@ async function detectTemplateEngine(cwd) {
4643
4816
  async function detectPackageManager(cwd) {
4644
4817
  async function exists(file) {
4645
4818
  try {
4646
- await (0, import_promises15.access)((0, import_node_path17.join)(cwd, file));
4819
+ await (0, import_promises16.access)((0, import_node_path18.join)(cwd, file));
4647
4820
  return true;
4648
4821
  } catch {
4649
4822
  return false;
@@ -4671,7 +4844,7 @@ async function promptFramework() {
4671
4844
  }
4672
4845
  async function fileExists2(filePath) {
4673
4846
  try {
4674
- await (0, import_promises15.access)(filePath);
4847
+ await (0, import_promises16.access)(filePath);
4675
4848
  return true;
4676
4849
  } catch {
4677
4850
  return false;
@@ -4684,15 +4857,15 @@ async function writeIfNotExists(filePath, content, label) {
4684
4857
  }
4685
4858
  const dir = filePath.substring(0, filePath.lastIndexOf("/"));
4686
4859
  if (dir) {
4687
- await (0, import_promises15.mkdir)(dir, { recursive: true });
4860
+ await (0, import_promises16.mkdir)(dir, { recursive: true });
4688
4861
  }
4689
- await (0, import_promises15.writeFile)(filePath, content, "utf8");
4862
+ await (0, import_promises16.writeFile)(filePath, content, "utf8");
4690
4863
  logCreated(label);
4691
4864
  }
4692
4865
  async function handleViteConfig(cwd, framework) {
4693
- const filePath = (0, import_node_path17.join)(cwd, "vite.config.ts");
4866
+ const filePath = (0, import_node_path18.join)(cwd, "vite.config.ts");
4694
4867
  if (await fileExists2(filePath)) {
4695
- const existing = await (0, import_promises15.readFile)(filePath, "utf8");
4868
+ const existing = await (0, import_promises16.readFile)(filePath, "utf8");
4696
4869
  const hasPlugin = existing.includes("nestInertia") || existing.includes("nestjs-inertia-vite/plugin");
4697
4870
  if (!hasPlugin) {
4698
4871
  logSkipped("vite.config.ts");
@@ -4708,15 +4881,15 @@ async function handleViteConfig(cwd, framework) {
4708
4881
  }
4709
4882
  const dir = filePath.substring(0, filePath.lastIndexOf("/"));
4710
4883
  if (dir) {
4711
- await (0, import_promises15.mkdir)(dir, { recursive: true });
4884
+ await (0, import_promises16.mkdir)(dir, { recursive: true });
4712
4885
  }
4713
- await (0, import_promises15.writeFile)(filePath, viteConfigTemplate(framework), "utf8");
4886
+ await (0, import_promises16.writeFile)(filePath, viteConfigTemplate(framework), "utf8");
4714
4887
  logCreated("vite.config.ts");
4715
4888
  }
4716
4889
  async function patchGitignore(gitignorePath) {
4717
4890
  let existing = "";
4718
4891
  if (await fileExists2(gitignorePath)) {
4719
- existing = await (0, import_promises15.readFile)(gitignorePath, "utf8");
4892
+ existing = await (0, import_promises16.readFile)(gitignorePath, "utf8");
4720
4893
  }
4721
4894
  if (existing.split("\n").some((line) => line.trim() === GITIGNORE_ENTRY)) {
4722
4895
  console.log(` ${cyan("\u2192")} .gitignore ${dim("(already contains .nestjs-inertia/, skipped)")}`);
@@ -4726,7 +4899,7 @@ async function patchGitignore(gitignorePath) {
4726
4899
  ` : `${existing}
4727
4900
  ${GITIGNORE_ENTRY}
4728
4901
  `;
4729
- await (0, import_promises15.writeFile)(gitignorePath, newContent, "utf8");
4902
+ await (0, import_promises16.writeFile)(gitignorePath, newContent, "utf8");
4730
4903
  logPatched(".gitignore", "added .nestjs-inertia/");
4731
4904
  }
4732
4905
  function installDeps(pkgManager, deps, dev) {
@@ -4748,10 +4921,10 @@ function installDeps(pkgManager, deps, dev) {
4748
4921
  }
4749
4922
  }
4750
4923
  async function patchPackageJsonScripts(cwd, scripts) {
4751
- const pkgPath = (0, import_node_path17.join)(cwd, "package.json");
4924
+ const pkgPath = (0, import_node_path18.join)(cwd, "package.json");
4752
4925
  let pkg = {};
4753
4926
  try {
4754
- pkg = JSON.parse(await (0, import_promises15.readFile)(pkgPath, "utf8"));
4927
+ pkg = JSON.parse(await (0, import_promises16.readFile)(pkgPath, "utf8"));
4755
4928
  } catch {
4756
4929
  return;
4757
4930
  }
@@ -4770,7 +4943,7 @@ async function patchPackageJsonScripts(cwd, scripts) {
4770
4943
  return;
4771
4944
  }
4772
4945
  pkg.scripts = existing;
4773
- await (0, import_promises15.writeFile)(pkgPath, `${JSON.stringify(pkg, null, 2)}
4946
+ await (0, import_promises16.writeFile)(pkgPath, `${JSON.stringify(pkg, null, 2)}
4774
4947
  `, "utf8");
4775
4948
  }
4776
4949
  function patchAppModule(filePath, rootView) {
@@ -5065,7 +5238,7 @@ export class HomeController {
5065
5238
  }
5066
5239
  `;
5067
5240
  function patchTsconfigExclude(cwd, dir, filename = "tsconfig.json") {
5068
- const filePath = (0, import_node_path17.join)(cwd, filename);
5241
+ const filePath = (0, import_node_path18.join)(cwd, filename);
5069
5242
  return patchJsonFile(
5070
5243
  filePath,
5071
5244
  (json) => {
@@ -5080,7 +5253,7 @@ function patchTsconfigExclude(cwd, dir, filename = "tsconfig.json") {
5080
5253
  );
5081
5254
  }
5082
5255
  function patchNestCliJson(cwd, shellDir) {
5083
- const filePath = (0, import_node_path17.join)(cwd, "nest-cli.json");
5256
+ const filePath = (0, import_node_path18.join)(cwd, "nest-cli.json");
5084
5257
  return patchJsonFile(filePath, (json) => {
5085
5258
  const compiler = json.compilerOptions ?? {};
5086
5259
  const assets = compiler.assets ?? [];
@@ -5103,46 +5276,46 @@ async function scaffoldFiles(ctx) {
5103
5276
  const { cwd, framework, engine, shellFileName, entryExt, pageExt } = ctx;
5104
5277
  logSection("Scaffold files");
5105
5278
  await writeIfNotExists(
5106
- (0, import_node_path17.join)(cwd, "nestjs-inertia.config.ts"),
5279
+ (0, import_node_path18.join)(cwd, "nestjs-inertia.config.ts"),
5107
5280
  configTemplate(framework),
5108
5281
  "nestjs-inertia.config.ts"
5109
5282
  );
5110
- await writeIfNotExists((0, import_node_path17.join)(cwd, "nestjs-inertia.d.ts"), DTS_TEMPLATE, "nestjs-inertia.d.ts");
5283
+ await writeIfNotExists((0, import_node_path18.join)(cwd, "nestjs-inertia.d.ts"), DTS_TEMPLATE, "nestjs-inertia.d.ts");
5111
5284
  await writeIfNotExists(
5112
- (0, import_node_path17.join)(cwd, "tsconfig.inertia.json"),
5285
+ (0, import_node_path18.join)(cwd, "tsconfig.inertia.json"),
5113
5286
  TSCONFIG_INERTIA_TEMPLATE,
5114
5287
  "tsconfig.inertia.json"
5115
5288
  );
5116
5289
  await writeIfNotExists(
5117
- (0, import_node_path17.join)(cwd, "inertia", "tsconfig.json"),
5290
+ (0, import_node_path18.join)(cwd, "inertia", "tsconfig.json"),
5118
5291
  INERTIA_TSCONFIG_TEMPLATE,
5119
5292
  "inertia/tsconfig.json"
5120
5293
  );
5121
5294
  await writeIfNotExists(
5122
- (0, import_node_path17.join)(cwd, "inertia", shellFileName),
5295
+ (0, import_node_path18.join)(cwd, "inertia", shellFileName),
5123
5296
  htmlShellTemplate(framework, engine),
5124
5297
  `inertia/${shellFileName}`
5125
5298
  );
5126
5299
  await handleViteConfig(cwd, framework);
5127
5300
  await writeIfNotExists(
5128
- (0, import_node_path17.join)(cwd, "inertia", "app", `client.${entryExt}`),
5301
+ (0, import_node_path18.join)(cwd, "inertia", "app", `client.${entryExt}`),
5129
5302
  entryPointTemplate(framework),
5130
5303
  `inertia/app/client.${entryExt}`
5131
5304
  );
5132
5305
  await writeIfNotExists(
5133
- (0, import_node_path17.join)(cwd, "inertia", "pages", `Home.${pageExt}`),
5306
+ (0, import_node_path18.join)(cwd, "inertia", "pages", `Home.${pageExt}`),
5134
5307
  samplePageTemplate(framework),
5135
5308
  `inertia/pages/Home.${pageExt}`
5136
5309
  );
5137
5310
  await writeIfNotExists(
5138
- (0, import_node_path17.join)(cwd, "src", "home.controller.ts"),
5311
+ (0, import_node_path18.join)(cwd, "src", "home.controller.ts"),
5139
5312
  SAMPLE_CONTROLLER,
5140
5313
  "src/home.controller.ts"
5141
5314
  );
5142
5315
  }
5143
5316
  function patchServerAppModule(ctx) {
5144
5317
  const { cwd, rootView } = ctx;
5145
- const appModulePath = (0, import_node_path17.join)(cwd, "src", "app.module.ts");
5318
+ const appModulePath = (0, import_node_path18.join)(cwd, "src", "app.module.ts");
5146
5319
  const appModuleResult = patchAppModule(appModulePath, rootView);
5147
5320
  if (appModuleResult === "patched") {
5148
5321
  logPatched("src/app.module.ts", "added InertiaModule.forRoot");
@@ -5156,7 +5329,7 @@ function patchServerAppModule(ctx) {
5156
5329
  }
5157
5330
  }
5158
5331
  function patchServerMainTs(ctx) {
5159
- const mainTsPath = (0, import_node_path17.join)(ctx.cwd, "src", "main.ts");
5332
+ const mainTsPath = (0, import_node_path18.join)(ctx.cwd, "src", "main.ts");
5160
5333
  const mainTsResult = patchMainTs(mainTsPath);
5161
5334
  if (mainTsResult === "patched") {
5162
5335
  logPatched("src/main.ts", "added setupInertiaVite after NestFactory.create");
@@ -5191,7 +5364,7 @@ function patchBuildConfigs(ctx) {
5191
5364
  }
5192
5365
  async function patchGitignoreAndDist(ctx) {
5193
5366
  const { cwd } = ctx;
5194
- await patchGitignore((0, import_node_path17.join)(cwd, ".gitignore"));
5367
+ await patchGitignore((0, import_node_path18.join)(cwd, ".gitignore"));
5195
5368
  const tsconfigDistResult = patchTsconfigExclude(cwd, "dist", "tsconfig.json");
5196
5369
  if (tsconfigDistResult === "patched") {
5197
5370
  logPatched("tsconfig.json", "excluded dist/ from server compilation");
@@ -5297,7 +5470,7 @@ ${green("\u2713")} Setup complete! Run: ${bold("nest start --watch")}
5297
5470
 
5298
5471
  // src/cli/doctor.ts
5299
5472
  function checkFileExists(cwd, file) {
5300
- return (0, import_node_fs4.existsSync)((0, import_node_path18.join)(cwd, file));
5473
+ return (0, import_node_fs4.existsSync)((0, import_node_path19.join)(cwd, file));
5301
5474
  }
5302
5475
  function readJson(path) {
5303
5476
  try {
@@ -5333,15 +5506,15 @@ function writeJsonField(filePath, dotPath, value) {
5333
5506
  }
5334
5507
  function getPackageVersion(cwd, pkg) {
5335
5508
  try {
5336
- const pkgJson = readJson((0, import_node_path18.join)(cwd, "node_modules", pkg, "package.json"));
5509
+ const pkgJson = readJson((0, import_node_path19.join)(cwd, "node_modules", pkg, "package.json"));
5337
5510
  return pkgJson?.version ?? null;
5338
5511
  } catch {
5339
5512
  return null;
5340
5513
  }
5341
5514
  }
5342
5515
  function detectPkgManager(cwd) {
5343
- if ((0, import_node_fs4.existsSync)((0, import_node_path18.join)(cwd, "pnpm-lock.yaml"))) return "pnpm";
5344
- if ((0, import_node_fs4.existsSync)((0, import_node_path18.join)(cwd, "yarn.lock"))) return "yarn";
5516
+ if ((0, import_node_fs4.existsSync)((0, import_node_path19.join)(cwd, "pnpm-lock.yaml"))) return "pnpm";
5517
+ if ((0, import_node_fs4.existsSync)((0, import_node_path19.join)(cwd, "yarn.lock"))) return "yarn";
5345
5518
  return "npm";
5346
5519
  }
5347
5520
  async function runDoctor(opts) {
@@ -5379,7 +5552,7 @@ async function runDoctor(opts) {
5379
5552
  autoFix: () => runInit({ cwd })
5380
5553
  });
5381
5554
  if (foundShellDir) {
5382
- const nestCliPath = (0, import_node_path18.join)(cwd, "nest-cli.json");
5555
+ const nestCliPath = (0, import_node_path19.join)(cwd, "nest-cli.json");
5383
5556
  const nestCli = readJson(nestCliPath);
5384
5557
  const compiler = nestCli?.compilerOptions ?? {};
5385
5558
  const assets = compiler.assets ?? [];
@@ -5418,7 +5591,7 @@ async function runDoctor(opts) {
5418
5591
  fix: "Run: nestjs-codegen codegen",
5419
5592
  autoFix: () => runCodegen({ cwd })
5420
5593
  });
5421
- const tsconfigPath = (0, import_node_path18.join)(cwd, "tsconfig.json");
5594
+ const tsconfigPath = (0, import_node_path19.join)(cwd, "tsconfig.json");
5422
5595
  const tsconfig = readJson(tsconfigPath);
5423
5596
  const paths = tsconfig?.compilerOptions?.paths;
5424
5597
  checks.push({
@@ -5429,7 +5602,7 @@ async function runDoctor(opts) {
5429
5602
  });
5430
5603
  const inertiaDir = foundShellDir ?? "inertia";
5431
5604
  for (const tsconfigFile of ["tsconfig.json", "tsconfig.build.json"]) {
5432
- const tsc = readJson((0, import_node_path18.join)(cwd, tsconfigFile));
5605
+ const tsc = readJson((0, import_node_path19.join)(cwd, tsconfigFile));
5433
5606
  if (!tsc) continue;
5434
5607
  const excl = tsc.exclude ?? [];
5435
5608
  const excludesIt = excl.includes(inertiaDir);
@@ -5455,7 +5628,7 @@ async function runDoctor(opts) {
5455
5628
  }
5456
5629
  });
5457
5630
  }
5458
- const inertiaTsconfigPath = (0, import_node_path18.join)(cwd, "tsconfig.inertia.json");
5631
+ const inertiaTsconfigPath = (0, import_node_path19.join)(cwd, "tsconfig.inertia.json");
5459
5632
  const inertiaTsconfig = readJson(inertiaTsconfigPath);
5460
5633
  checks.push({
5461
5634
  name: "tsconfig.inertia.json exists",
@@ -5507,7 +5680,7 @@ async function runDoctor(opts) {
5507
5680
  fix: 'Add "nestjs-inertia.d.ts" to include array (resolves InertiaRegistry augmentation)'
5508
5681
  });
5509
5682
  }
5510
- const innerTsconfigPath = (0, import_node_path18.join)(cwd, "inertia", "tsconfig.json");
5683
+ const innerTsconfigPath = (0, import_node_path19.join)(cwd, "inertia", "tsconfig.json");
5511
5684
  checks.push({
5512
5685
  name: "inertia/tsconfig.json exists (VSCode picks up ~codegen alias)",
5513
5686
  pass: (0, import_node_fs4.existsSync)(innerTsconfigPath),
@@ -5517,7 +5690,7 @@ async function runDoctor(opts) {
5517
5690
  }
5518
5691
  });
5519
5692
  if (checkFileExists(cwd, "vite.config.ts")) {
5520
- const viteContent = (0, import_node_fs4.readFileSync)((0, import_node_path18.join)(cwd, "vite.config.ts"), "utf8");
5693
+ const viteContent = (0, import_node_fs4.readFileSync)((0, import_node_path19.join)(cwd, "vite.config.ts"), "utf8");
5521
5694
  checks.push({
5522
5695
  name: "vite.config.ts has resolve.alias",
5523
5696
  pass: viteContent.includes("resolve") && viteContent.includes("alias"),
@@ -5582,7 +5755,7 @@ async function runDoctor(opts) {
5582
5755
  });
5583
5756
  }
5584
5757
  if (checkFileExists(cwd, ".gitignore")) {
5585
- const gitignorePath = (0, import_node_path18.join)(cwd, ".gitignore");
5758
+ const gitignorePath = (0, import_node_path19.join)(cwd, ".gitignore");
5586
5759
  const gitignore = (0, import_node_fs4.readFileSync)(gitignorePath, "utf8");
5587
5760
  checks.push({
5588
5761
  name: ".gitignore includes .nestjs-inertia/",
@@ -5591,7 +5764,7 @@ async function runDoctor(opts) {
5591
5764
  autoFix: () => (0, import_node_fs4.appendFileSync)(gitignorePath, "\n.nestjs-inertia/\n")
5592
5765
  });
5593
5766
  }
5594
- const pkgJsonPath = (0, import_node_path18.join)(cwd, "package.json");
5767
+ const pkgJsonPath = (0, import_node_path19.join)(cwd, "package.json");
5595
5768
  const pkgJson = readJson(pkgJsonPath);
5596
5769
  const scripts = pkgJson?.scripts ?? {};
5597
5770
  checks.push({