fix(cli): allow convert to work without a package.json#1009
Conversation
🦋 Changeset detectedLatest commit: 1b8667c The changes in this PR will be included in the next version bump. This PR includes changesets to release 16 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 |
trevor-cortex
left a comment
There was a problem hiding this comment.
Trimmed-down follow-up to #1008 — much nicer scope. The fix is two clean parts:
tryGetPackageJson()returnsundefinedinstead of throwing, andgetPackageJsonDependencies/detectPackageManagerFromPackageJsonnow degrade to "no deps" / "unknown PM" rather than blowing up.getPackageJson()is preserved as the throwing variant for callers that genuinely require it.getRootNodeFromIdltries to import@codama/nodes-from-anchordirectly before falling back to the install prompt, so a globally/npx-resolvable adapter works without apackage.jsonin cwd.
The behavior change is small and well-scoped to fixing the codama convert UX outside a Node project. Two things worth a look before merging — both around the nodes.ts try/catch — plus one note on a subtle behavior shift in getPackageJsonDependencies. Nothing blocking from my side.
Things to verify:
- Manual smoke test:
codama convertin a tmp dir with nopackage.json, with@codama/nodes-from-anchorreachable both (a) via a globally-linked install and (b) vianpx codama convert. Both should resolve the adapter without hitting the install prompt. - Same command in a tmp dir with no
package.jsonand no adapter reachable — should still fall through to the install prompt and not crash with the oldCannot read package.jsonerror. getPackageManager()in a no-package.jsondir should now fall through to lockfile / installed-CLI detection /npmfallback without throwing.
Notes for subsequent reviewers:
- No tests added; the utils don't seem to have a great harness for fs-touching code today, so probably fine — but a unit test for
tryGetPackageJsonreturningundefinedwould be cheap insurance if you have a fixture pattern in mind. - Option 1 from the #1008 thread (adapter-side CLI) is deliberately not in scope here, as called out in the description.
|
hey @trevor-cortex Scope's exactly the intent. Ran the smoke tests:
Unit test for tryGetPackageJson → undefined is fair; can add one in this PR before merge if you want, else follow-up. Adapter-side CLI stays out of scope. |
|
Found it — it's 'Missing dependency: @codama/nodes-from-anchor',
...
'Re-run with: npx -p codama -p @codama/nodes-from-anchor codama convert anchor.json codama.json',but the production code wraps those labels with Cleanest fix is to mock picocolors in that test file so the assertions are deterministic regardless of where they run: vi.mock('picocolors', () => ({
default: { bold: (s: string) => s, yellow: (s: string) => s },
}));( No other tests look affected — |
Drop the message-based assertion that the error names the requested module; any ERR_MODULE_NOT_FOUND/MODULE_NOT_FOUND now means the adapter is not resolvable and we fall back accordingly.
lorisleiva
left a comment
There was a problem hiding this comment.
Thanks! Just one last nit and LGTM. 👌
Follow-up to #1008 (closed), trimmed down to just the decoupled fix we talked about there.
Running
codama convertoutside a Node project failed withCannot read package.json. The install prompt never got a chance to run becauseinstallMissingDependenciesreads the package.json first and throws when it's missing.Changes:
tryGetPackageJsonno longer throws when there's no package.json. A missing file just means "no deps" and "no package manager set".@codama/nodes-from-anchoris already installed, use it directly instead of going through the install prompt. So convert works without a package.json when the adapter is there.Left out on purpose:
@codama/nodes-from-anchor(dropped from fix(cli): allow convert command to work without package.json #1008). It stays decoupled.The adapter-side CLI (option 1 from the thread) isn't here, can do that once we agree on the direction.
tsc --noEmit,eslint,prettier --checkandvitestall pass.