Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.

feat: separate enums from models#661

Closed
sanny-io wants to merge 6 commits intozenstackhq:devfrom
sanny-io:feat/separate-enums
Closed

feat: separate enums from models#661
sanny-io wants to merge 6 commits intozenstackhq:devfrom
sanny-io:feat/separate-enums

Conversation

@sanny-io
Copy link
Copy Markdown
Contributor

@sanny-io sanny-io commented Feb 6, 2026

Enums are now generated to enums.ts
models.ts exports everything from enums.ts to maintain backwards compatibility.

Closes zenstackhq/zenstack#2345

Summary by CodeRabbit

  • Refactor
    • Generated enum declarations are emitted into a dedicated generated file and re-exported from the main generated models output for compatibility.
    • Enum output is produced only when enums exist and removed otherwise to avoid stale artifacts.
    • The generation order and export handling were adjusted for cleaner, more consistent generated model outputs.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Feb 6, 2026

📝 Walkthrough

Walkthrough

The TypeScript schema generator now extracts enum declarations into a separate enums.ts file and updates models.ts to re-export from ./enums when enums exist; enums.ts is created or removed conditionally. The schema import for the models/type-def phase now avoids emitting the $schema object.

Changes

Cohort / File(s) Summary
Enum separation logic
packages/sdk/src/ts-schema-generator.ts
Refactored generateModelsAndTypeDefs to collect enum-related AST into enumStatements, emit a standalone enums.ts (with banner) when enums exist and delete it when none exist, write enums.ts before models.ts, and add a conditional export * from './enums' to models.ts. Also changed generateSchemaImport invocation to pass schemaObject = false for the models/type-def emission phase.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I nudged the enums from models' den,
Kept types tidy — hopped out then in,
One file for models, one for the rest,
A little split makes bundling best. 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat: separate enums from models' directly and clearly summarizes the main change in the changeset.
Linked Issues check ✅ Passed The PR successfully implements the core requirement from issue #655: enums are now generated to a separate enums.ts file with models.ts re-exporting from it.
Out of Scope Changes check ✅ Passed All changes in ts-schema-generator.ts are focused on the enum separation logic and remain within the scope of issue #655.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@packages/sdk/src/ts-schema-generator.ts`:
- Around line 1423-1431: The enumStatements array created for enums.ts doesn't
get the autogenerated "DO NOT MODIFY" banner because generateBannerComments is
only applied to the models statements; update the flow to call
generateBannerComments(enumStatements) (same banner function used for models)
before the file emission so enums.ts receives the banner. Locate the
enumStatements construction (including generateSchemaImport) and ensure you call
generateBannerComments(enumStatements) in the same spot you apply it to the
models statements so enums.ts and any other enum output receive the standard
header.
- Around line 1504-1506: The else branch that calls
fs.unlinkSync(enumsOutputFile) can throw ENOENT when enums.ts was never created;
update the removal logic in ts-schema-generator.ts to either check existence
(using fs.existsSync on enumsOutputFile) before calling fs.unlinkSync or wrap
unlinkSync in a try/catch that ignores errors with code === 'ENOENT' and
rethrows others so only the "file not found" case is swallowed.

Comment thread packages/sdk/src/ts-schema-generator.ts
Comment thread packages/sdk/src/ts-schema-generator.ts
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@packages/sdk/src/ts-schema-generator.ts`:
- Around line 1485-1510: The current check uses enumStatements.length which is
always >0 due to the initial schema import; change the guard to check the actual
enums collection instead (use enums.length > 0) so enums.ts is only written when
real enums exist, keep the existing logic that calls
generateBannerComments(enumStatements) and writes enumsOutputFile and the export
declaration (createExportDeclaration) when enums.length > 0, and move the
fs.existsSync/enumsOutputFile unlink cleanup into the else branch so stale
enumsOutputFile is removed when enums is empty; ensure any logic that builds
enumStatements (and references options.importWithFileExtension) remains
unchanged.

Comment thread packages/sdk/src/ts-schema-generator.ts Outdated
@sanny-io
Copy link
Copy Markdown
Contributor Author

sanny-io commented Feb 6, 2026

I guess refactor is a more accurate label rather than feat, but I can't change the PR title.

@ymc9
Copy link
Copy Markdown
Member

ymc9 commented Feb 10, 2026

Hi @sanny-io , given the findings in the original issue zenstackhq/zenstack#2345, I think we don't have an immediate need to have a separate file for enums anymore. What do you think?

@sanny-io sanny-io closed this by deleting the head repository Feb 11, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Generate enums into a separate enums.ts file in ZenStack CLI

2 participants