Packages #
Each folder here is a package: code that more than one part of Pilot uses. The App, the Daemon and the gallery stay outside packages/, as they are programs, not packages.
A package is a deep module: a lot of behaviour behind a small interface. Its interface is its entry points, the files at its root. Everything in its subfolders is private.
packages/
<name>/
index.ts an entry point. Code outside the package imports this.
client.ts another entry point. A package may have several.
lib/ the code behind the entry points. Only the package's own files import it.
tests/ the package's tests and their fixtures.
ui has one entry point, index.ts, with its code in lib/. protocol has three: index.ts, scripted.ts and diff.ts.
The rules #
Entry points only. Code outside a package imports only its root files, never a file in a subfolder. This applies to the App, the Daemon, the gallery and the other packages.
Free inside. A package's own files import each other freely, in any folder.
Tests use the entry points. A file in <name>/tests/ imports any package's entry points and its own fixtures in tests/. It never imports a subfolder of a package, not even its own lib/. Only tests import tests/.
No cycles. No chain of imports comes back to where it started.
Several small entry points, not a barrel #
Do not write a barrel: an index.ts that re-exports a whole folder. Give a package a few small entry points instead, one for each use (index.ts, client.ts, server.ts). A new entry point is a new root file. It needs no change to the config. ui/index.ts is the one list of what the gallery shows (its header says why), so it stays one file.
Check the rules #
just lint checks them, in CI too. To check them alone:
pnpm run lint:boundaries
The rules are in .dependency-cruiser.cjs, at the root of the repository.