rhys-vdw / ts-auto-guard

Generate type guard functions from TypeScript interfaces
MIT License
497 stars 54 forks source link

Multiple generation runs fail if a concrete file path is specified #150

Closed SebastianKuehn closed 3 years ago

SebastianKuehn commented 3 years ago

Calling ts-auto-guard --guard-file-name=type-guard src/Person.ts with Version 1.0.0-alpha.25 or 1.0.0-alpha.26 fails the second time with:

UnhandledPromiseRejectionWarning: Error: Did you mean to provide the overwrite option? A source file already exists at the provided file path: /fullpath/to/src/Person.ts

Version 1.0.0-alpha.24 works fine.

I think the problem is, that the guard files are not known to the project. In function generate() (line 944) the provided path (not the generated guard) is added to the project. Therefore processProject() doesn't find the generated guards in line 965:

  // Delete previously generated guard.
  project
    .getSourceFiles(`./**/*.${guardFileName}.ts`)
    .forEach(sourceFile => deleteGuardFile(sourceFile))

and deleteGuardFile() is never called. The last assertion is a fact, as I checked it. It's not called in my setup.

I'm not providing a PR because I'm not sure how to fix the issue. Maybe extending generate() would do the trick:

  const project = new Project({
    skipAddingFilesFromTsConfig: paths.length !== 0,
    tsConfigFilePath,
  })
  project.addSourceFilesAtPaths(paths)
  // also add generated guard files to the project
  project.addSourceFilesAtPaths(paths.map(path -> outFilePath(path, guardFileName)));
rhys-vdw commented 3 years ago

@SebastianKuehn hm... perhaps we should provide the overwrite option? Or at least allow it to be opt-in with a command line argument?

Either that or be smart and expand the list of supplies scripts to include any relative guard files. But I feel like ultimately that's the same thing.

I think we should also catch that error from ts-morph and report it a bit more gracefully.

Would you be interested in opening a PR with the relevant changes? Happy to answer any questions if you get stuck.

rhys-vdw commented 3 years ago

Hey, I just noticed your error:

A source file already exists at the provided file path: /fullpath/to/src/Person.ts

This is not a guard file! Shouldn't it be Person.type-guard.ts with those arguments?

SebastianKuehn commented 3 years ago

@rhys-vdw Hi, sorry thats a typo from my side. The error contains a guard-file. I tried to be brief and messed up the file-part of the error message.

rhys-vdw commented 3 years ago

Thought that might be the case, all g. My original reply stands. 👍