-
Notifications
You must be signed in to change notification settings - Fork 72
Fixes "PORT" environment variable usage and proposes the "--generate-lib" CLI option #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fixes "PORT" environment variable usage and proposes the "--generate-lib" CLI option #50
Conversation
…ary instead of running the "main" method.
WalkthroughIntroduces a generateLib option throughout CLI, types, and server code generation. Adds conditional export of main, centralized startup/cleanup with signal handling, dynamic port resolution for transports, and switches dotenv usage to named import with immediate config invocation. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant U as User (CLI)
participant C as CLI Parser
participant G as Code Generator
participant S as Generated Server
participant OS as OS Signals
U->>C: Run generator with options (incl. --generate-lib)
C->>G: Parsed CliOptions { generateLib, port, ... }
G->>S: Emit server code (named dotenv import + config(), port resolution, mainDeclaration)
alt generateLib === false
note over S: main() defined and invoked
S->>S: Resolve port (options.port or default) %% color: #DDEBF7
S->>S: setupWebServer / setupStreamableHttpServer using resolved port
S->>OS: Register SIGINT/SIGTERM handlers
OS-->>S: Signal received
S->>S: cleanup() then exit
else generateLib === true
note over S: main() exported, not invoked
S-->>U: Library exposes main() for caller control
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
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. Comment |
There was a problem hiding this 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
🧹 Nitpick comments (2)
src/generator/server-code.ts (2)
48-48: Consider simplifying the generated port expression.The current implementation correctly addresses the PR objective to honor the
PORTenvironment variable when--portis not specified. However, the generated expression could be simplified.Current generated code when
options.portis undefined:process.env['PORT'] === undefined ? undefined : parseInt(process.env['PORT'], 10)This can be simplified to:
process.env['PORT'] ? parseInt(process.env['PORT'], 10) : undefinedApply this diff:
- const webServicerPortNumberStatement = `${options.port ?? "process.env['PORT'] === undefined ? undefined : parseInt(process.env['PORT'], 10)"}`; + const webServicerPortNumberStatement = `${options.port ?? "process.env['PORT'] ? parseInt(process.env['PORT'], 10) : undefined"}`;
112-113: Acknowledge the dotenv import style change.The switch from default import to named import is a valid modernization, though this change is not mentioned in the PR objectives.
Both forms work correctly with the dotenv package.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/generator/server-code.ts(5 hunks)src/index.ts(1 hunks)src/types/index.ts(1 hunks)
🔇 Additional comments (6)
src/types/index.ts (1)
32-33: LGTM!The new
generateLibfield is well-documented and correctly typed. The documentation clearly explains its purpose in controlling whether the generated code invokesmain()or exports it as a library.src/generator/server-code.ts (5)
55-55: LGTM!The web transport correctly uses the new dynamic port resolution, which honors the
PORTenvironment variable when--portis not specified.
65-65: LGTM!The StreamableHTTP transport correctly uses the new dynamic port resolution, consistent with the web transport.
85-102: LGTM!The conditional
mainDeclarationblock correctly implements the library generation mode:
- Omits cleanup and signal handlers when
generateLibis true- Includes proper shutdown handling and main invocation for standalone mode
- Aligns with the PR objective to allow
main()to be imported and called from existing code
182-182: LGTM!The conditional export correctly implements the library generation mode. When
generateLibis true,main()is exported for consumption by other code; otherwise, it remains a private function called by the startup code.
186-186: LGTM!The
mainDeclarationinsertion is correctly placed after themain()function definition, ensuring proper code structure in both library and standalone modes.
PORTenvironment variable when the--portCLI is not defined and forces the port to be 3000, while thesetupWebServerfunctionportargument already has this default value: proposes to resort to thePORTenvironment variable in that case.--generate-libCLI option, which causes the generatedmain()function to be exported and removing the clean-up code generation, so as to be able to import and run thanmain()function is an existing code, just like a library.Summary by CodeRabbit
New Features
Refactor