Skip to content

Add --prune option to remove defunct route files when OpenAPI spec changes#1550

Merged
pmcelhaney merged 5 commits intomainfrom
copilot/add-prune-option-for-clearing-paths
Mar 20, 2026
Merged

Add --prune option to remove defunct route files when OpenAPI spec changes#1550
pmcelhaney merged 5 commits intomainfrom
copilot/add-prune-option-for-clearing-paths

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 20, 2026

When an OpenAPI spec renames a path parameter (e.g., {Name}{nickname}), both the old and new route files coexist, creating wildcard ambiguity in ModuleTree resolution. The --prune flag removes route files that no longer exist in the spec before generating new ones.

Changes

  • src/typescript-generator/prune.js — New pruneRoutes(destination, openApiPaths) function: walks routes/, deletes .ts files with no matching spec path, removes empty directories post-prune. _.context.ts files are always preserved.
  • src/typescript-generator/generate.js — Calls pruneRoutes() when generateOptions.prune && generateOptions.routes, before writing new files.
  • src/server/config.ts — Adds optional prune?: boolean to the generate config shape.
  • src/typescript-generator/code-generator.ts — Updates constructor/field types to accept prune?.
  • bin/counterfact.js — Adds --prune CLI flag wired into config.generate.prune.
  • test/typescript-generator/prune.test.js — 7 tests covering file deletion, context file preservation, empty-dir cleanup, root path (/index.ts), no-op when all files match, and missing routes/ directory.
  • .changeset/add-prune-option.md — Changeset entry documenting this as a minor release.

Example

# After renaming {Name} to {nickname} in the spec:
npx counterfact openapi.yaml ./out --generate --prune
# Deletes routes/pet/{id}/update/{Name}.ts before generating routes/pet/{id}/update/{nickname}.ts
Original prompt

This section details on the original issue you should resolve

<issue_title>Add Prune option for clearing out defunct paths and endpoints</issue_title>
<issue_description>Sometimes an OpenApi spec might change path names or endpoint names. In that case, if a consumer tries to run Counterfact again, since it will leave the old paths and routes alone it is possible to end up with multiple wild card routes at the same end path. Example:

Old path:
/pet/{id}/update/{Name}.ts
New path:
/pet/{id}/update/{nickname}.ts

In this case, the old file will be left in place and the new one created. Then when module-tree.ts attempts to resolve the path it will have ambiguity because both wildcards can be a math if they both have the same HTTP verb.

We should offer a --prune option that will clean out any paths that aren't represented in the OpenApi spec.</issue_description>

Comments on the Issue (you are @copilot in this section)

@pmcelhaney Or maybe it should detect this situation and rename {Name}.ts to {nickname}.ts

📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 20, 2026

🦋 Changeset detected

Latest commit: 2fc3033

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
counterfact Minor

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

Copilot AI and others added 2 commits March 20, 2026 03:20
…anges

Co-authored-by: pmcelhaney <51504+pmcelhaney@users.noreply.github.com>
…t.js

Co-authored-by: pmcelhaney <51504+pmcelhaney@users.noreply.github.com>
Copilot AI changed the title [WIP] Add prune option for clearing out defunct paths and endpoints Add --prune option to remove defunct route files when OpenAPI spec changes Mar 20, 2026
Copilot AI requested a review from pmcelhaney March 20, 2026 03:21
@pmcelhaney
Copy link
Copy Markdown
Collaborator

@copilot add a changes

Co-authored-by: pmcelhaney <51504+pmcelhaney@users.noreply.github.com>
Agent-Logs-Url: https://github.com/pmcelhaney/counterfact/sessions/e3e0d52e-2ea8-42bb-880a-56a5013c5fa3
@coveralls
Copy link
Copy Markdown
Collaborator

coveralls commented Mar 20, 2026

Pull Request Test Coverage Report for Build 23353533229

Details

  • 135 of 141 (95.74%) changed or added relevant lines in 4 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.3%) to 83.848%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/typescript-generator/prune.js 125 127 98.43%
src/typescript-generator/generate.js 3 7 42.86%
Totals Coverage Status
Change from base Build 23342753853: 0.3%
Covered Lines: 4609
Relevant Lines: 5604

💛 - Coveralls

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 20, 2026

@copilot add a changes

Added a changeset file (.changeset/add-prune-option.md) documenting this as a minor change in commit 2fc3033.

@pmcelhaney pmcelhaney marked this pull request as ready for review March 20, 2026 17:10
@pmcelhaney pmcelhaney enabled auto-merge March 20, 2026 17:10
@pmcelhaney
Copy link
Copy Markdown
Collaborator

@copilot add a changeset

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 20, 2026

@copilot add a changeset

The changeset was already added in commit 2fc3033 as .changeset/add-prune-option.md (minor bump).

@pmcelhaney pmcelhaney merged commit e8cb8ff into main Mar 20, 2026
10 checks passed
@pmcelhaney pmcelhaney deleted the copilot/add-prune-option-for-clearing-paths branch March 20, 2026 17:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Prune option for clearing out defunct paths and endpoints

3 participants