chore(cli): read --version from package.json via prebuild generator#139
Conversation
Past two release bumps had to fix the same desync: package.json
version was 0.2.0 but src/index.ts had a hard-coded `.version('0.1.0')`
literal that nobody remembered to update. The contract test caught it
in CI but only after a wasted publish run.
Fix:
- New scripts/sync-version.mjs reads package.json's version and writes
src/version.ts (a single `export const VERSION = "..."`).
- package.json's `build` script gains a `prebuild` that runs the
generator, so every build (incl. prepack-on-publish) regenerates
the constant in lockstep with the package version.
- src/index.ts now imports VERSION from ./version.js and passes it to
Commander's .version().
- src/version.ts is gitignored - the generator is the only writer; a
fresh clone will get a fresh file on first `npm run build`.
End-to-end check: bumped package.json to 0.2.1-test, ran build,
confirmed `node dist/index.js --version` printed 0.2.1-test. Reverted.
Future bumps now only need to touch package.json files - the CLI's
runtime version follows automatically.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
WalkthroughThe PR introduces a build-time version synchronization mechanism for the CLI. A new ChangesVersion Synchronization System
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/cli/package.json`:
- Around line 39-40: The typecheck fails because the generated version module is
only created in the "prebuild" hook; update package.json to run the version
generator before typecheck as well by extracting the generator command (node
scripts/sync-version.mjs) into a shared script name (e.g., "sync:version") and
invoke that script from both "prebuild" and "typecheck" entries so the
./version.js module exists when running the "typecheck" (watch for the existing
"build" and "prebuild" scripts and reuse the same "sync-version.mjs"
invocation).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 110588c2-3c74-45ad-9252-f6e76ace5e16
📒 Files selected for processing (4)
.gitignorepackages/cli/package.jsonpackages/cli/scripts/sync-version.mjspackages/cli/src/index.ts
CI failed on PR #139 with TS2307 because the workflow runs typecheck (`tsc --noEmit`) BEFORE build, but only `prebuild` generated the gitignored src/version.ts. tsc had nothing to resolve. Same finding raised by CodeRabbit (one inline comment, packages/cli/ package.json:40). Resolved per their suggested patch: extract a shared `sync:version` script and run it from `pretypecheck`, `prebuild`, and `prestart` so every consumer that traces src/index.ts gets version.ts first. Local repro: `rm src/version.ts && npm run typecheck` succeeds. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Summary
The CLI's
--versionoutput is rendered from a string literal inpackages/cli/src/index.ts(Commander's.version('0.x.y')). Past two bumps had to fix the same desync — package.json got bumped, the literal didn't, the contract test caught it in CI only after a wasted publish run. PR #138's first commit needed a one-line follow-up for exactly this reason.Solution: a tiny prebuild generator that writes the version into a single-purpose file, so the literal vanishes from the source tree entirely.
How it works
packages/cli/scripts/sync-version.mjsreadspackage.jsonand writessrc/version.ts:packages/cli/package.jsongains aprebuildscript that runs the generator. npm runs this automatically beforebuild, including underprepackon publish.src/index.tsimportsVERSIONand passes it to.version().src/version.tsis gitignored — the generator is the only writer. Fresh clones get the file on firstnpm run build.End-to-end check
Locally bumped
package.jsonto0.2.1-test, rannpm run build, confirmednode dist/index.js --versionprinted0.2.1-test. Reverted to0.2.0, rebuilt, confirmed clean roll-back.What this fixes for future releases
Bumping a new version is now a single edit to each
package.json. The CLI binary's--versionfollows automatically. The contract test that asserts--versionmatchespackage.jsonwill keep enforcing this, but it should never trip again.Test plan
--versionwithout any code edit beyond the package.json bump.🤖 Generated with Claude Code
Summary by CodeRabbit
--versionoutput is now automatically synchronized with the package version at build time, eliminating manual version updates and ensuring consistency between the published package and CLI metadata.