Add overwrite.updateExistingFiles and overwrite.removeStaleFiles - #10921
Conversation
…ave more granularity
💻 Website PreviewThe latest changes are available as preview in: https://pr-10921.graphql-code-generator.pages.dev |
🦋 Changeset detectedLatest commit: 88cc642 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
🚀 Snapshot Release (
|
| Package | Version | Info |
|---|---|---|
@graphql-codegen/cli |
7.3.0-alpha-20260819142751-88cc6428fb4182dc8f12a1da685a0fc5280477d9 |
npm ↗︎ unpkg ↗︎ |
@graphql-codegen/plugin-helpers |
7.2.0-alpha-20260819142751-88cc6428fb4182dc8f12a1da685a0fc5280477d9 |
npm ↗︎ unpkg ↗︎ |
| import * as watcherModule from '../src/utils/watcher.js'; | ||
|
|
||
| const createWatcherSpy = vi.spyOn(watcherModule, 'createWatcher'); |
There was a problem hiding this comment.
New testing approach uses generate which is the end-to-end test behaviour.
However, because generate calls createWatcher, we cannot get access to the stopWatching to stop the test.
Adding a spy is a trick that allows us to get the stopWatching function from the return type.
| const runningWatcher = generate(context); | ||
| await waitForNextEvent(); | ||
|
|
||
| const { stopWatching } = createWatcherSpy.mock.results.at(-1)!.value as ReturnType< |
There was a problem hiding this comment.
TIL, createWatcherSpy.mock.results.at(-1)!.value means getting the last result returned by createWatch
Description
Whilst working on Server Preset next version, I noticed that resolver files may get removed in very specific conditions:
buildGeneratesSection). At this point, Codegen tracks this file.buildGeneratesSection. This is a deliberate move because sending files triggers filesystem write, which would be very slow for big codebases.overwrite: trueconfig is used to remove stale files, so the resolver file is removed.Problem:
overwrite: trueis also used to update exiting files. Server Preset needs to be able to update existing files to add resolvers/commentsFor this reason, I'm extending the existing
overwriteconfig to be more granular:overwrite.updateExistingFiles: Whether updating existing files is allowedoverwrite.removeStaleFiles: Whether Codegen should remove stale files (files that are tracked in one run, but not the next) during watch modeType of change
TODO