Commit 552b51e
feat: add @metamask/platform-api-docs package (MetaMask#8012)
## Explanation
Adds `@metamask/platform-api-docs`, a publishable package that generates
and serves a searchable site documenting the Platform API — the catalog
of actions and events available to clients through the message bus.
The package scans TypeScript source and `.d.cts` declaration files,
finds every `*Messenger` type declaration, walks its `Actions` and
`Events` type arguments to discover the capability types they reference,
extracts JSDoc/handler/payload information for each, and renders the
result as a Docusaurus site with per-namespace pages and local search.
### Features
- **Anchored on Messenger declarations.** Extraction discovers
`*Messenger` type aliases and walks `Messenger<Namespace, Actions,
Events>` to find the capability types — eliminating false positives from
unrelated types that happen to share an action/event-like shape.
- **Two capability-type patterns supported.** Inline (`type FooAction =
{ type: '...'; handler: ... }`) and capability-type constructors (`type
FooGetStateAction = ControllerGetStateAction<typeof name, State>`),
including their `interface` variants.
- **Union expansion.** When a Messenger references an umbrella union
like `FooActions = FooGetAction | FooSetAction`, the walker descends
through it without documenting the intermediate alias.
- **Scans three layouts.** Configured `--scan-dir`s, `packages/*/src/`
(`.ts`), and `node_modules/@metamask/*/dist/` (`.d.cts`).
- **Source links resolve automatically.** Reads `git remote get-url
origin` and `git symbolic-ref refs/remotes/origin/HEAD` to build
`https://github.com/<owner>/<repo>/blob/<branch>/<path>#L<line>` URLs;
falls back to `main` for shallow clones.
- **Project label and commit SHA stamped on the site.** `--project-label
Core` / `--project-label Extension` produces titles like `Platform API
(Core)`, and the short Git commit is shown in the intro and navbar so
engineers can tell how current the docs are.
- **Dedup across packages.** When a capability is declared in two places
(e.g. once as the home definition and once as a re-export), the version
with JSDoc and from the matching namespace's package wins.
- **Docusaurus site** with dark/light mode, offline search (no Algolia),
and MDX-safe rendering of JSDoc.
### Usage
From the core monorepo:
```bash
yarn docs:platform-api:build # Generate docs and build static site
yarn docs:platform-api:dev # Dev server with hot reload
yarn docs:platform-api:serve # Build and serve
```
From client projects (Extension, Mobile), install
`@metamask/platform-api-docs` as a devDependency and add a script:
```json
{
"scripts": {
"docs:platform-api:build": "platform-api-docs --build --project-label MyProject"
}
}
```
### Implementation
- **`packages/platform-api-docs/`** — separate workspace from
`@metamask/messenger-cli` (different deps and release cadence).
- **CLI** built with yargs; runs Docusaurus through `execa`. Flags:
`--build`, `--serve`, `--dev`, `--scan-dir` (additive, repeatable),
`--output`, `--project-label`. Configuration is CLI-only — no
`package.json` config block.
- **AST parsing via ts-morph** instead of the raw TypeScript compiler
API. Standard JSDoc extraction (`jsDoc.getDescription()` + `getTags()`)
replaces the previous hand-rolled comment parser.
- **Single-pass extraction.** The Messenger walk returns
`TypeAliasDeclaration`/`InterfaceDeclaration` nodes directly tagged with
`'action'`/`'event'` kind, so the main loop doesn't re-walk the source
file looking up names.
- **File discovery via the `glob` package**, with results sorted for
deterministic output across filesystems.
- **Test coverage**: 100% lines / ~93% branches. Defensive guards
against AST shapes that don't appear in real messenger types are
explicitly marked with `// istanbul ignore` comments.
- **GitHub Actions workflow** (`deploy-platform-api-docs.yml`) builds
docs on PRs (uploads the build as an artifact) and deploys to GitHub
Pages on `main`.
## References
- [WPC-82](https://consensyssoftware.atlassian.net/browse/WPC-82)
## Checklist
- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've communicated my changes to consumers by updating changelogs
for packages I've changed
- [ ] I've introduced breaking changes in this PR and have prepared
draft pull requests for clients and consumer packages to resolve them
[WPC-82]:
https://consensyssoftware.atlassian.net/browse/WPC-82?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Low Risk**
> Changes are additive (new package, docs tooling, and CI deploy) with
no runtime impact on existing controllers or clients.
>
> **Overview**
> Introduces **`@metamask/platform-api-docs`**, a new workspace package
that scans TypeScript and `.d.cts` for `*Messenger` types, walks their
`Actions`/`Events` unions, extracts JSDoc and handler/payload shapes
(via **ts-morph**), and emits a **Docusaurus** site with per-namespace
pages, deduplication, and optional GitHub source links.
>
> The **monorepo** gains root scripts
(`docs:platform-api:build|dev|serve`), README/CODEOWNERS entries,
`.gitignore` for `.platform-api-docs/`, and **eslint/knip** rules for
the new package. **CI** adds `deploy-platform-api-docs.yml` (build on
every run; publish to GitHub Pages under `/platform-api/` on `main` with
`keep_files` and a token-exchange PAT) and wires that job into
**`main.yml`**’s required `all-jobs-complete` gate.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
d5fdb00. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>1 parent b54b8e2 commit 552b51e
41 files changed
Lines changed: 7705 additions & 209 deletions
File tree
- .github
- workflows
- packages/platform-api-docs
- site
- src/css
- static
- fonts/MM-Sans
- img
- favicons
- src
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
| 103 | + | |
103 | 104 | | |
104 | 105 | | |
105 | 106 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
80 | 88 | | |
81 | 89 | | |
82 | 90 | | |
| |||
135 | 143 | | |
136 | 144 | | |
137 | 145 | | |
| 146 | + | |
138 | 147 | | |
139 | 148 | | |
140 | 149 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
42 | 45 | | |
43 | 46 | | |
44 | 47 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
| 109 | + | |
109 | 110 | | |
110 | 111 | | |
111 | 112 | | |
| |||
206 | 207 | | |
207 | 208 | | |
208 | 209 | | |
| 210 | + | |
209 | 211 | | |
210 | 212 | | |
211 | 213 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| 84 | + | |
84 | 85 | | |
85 | 86 | | |
86 | 87 | | |
| |||
117 | 118 | | |
118 | 119 | | |
119 | 120 | | |
| 121 | + | |
120 | 122 | | |
121 | 123 | | |
122 | 124 | | |
| |||
210 | 212 | | |
211 | 213 | | |
212 | 214 | | |
213 | | - | |
| 215 | + | |
214 | 216 | | |
215 | 217 | | |
216 | 218 | | |
| |||
317 | 319 | | |
318 | 320 | | |
319 | 321 | | |
320 | | - | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
321 | 326 | | |
322 | 327 | | |
323 | 328 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
136 | 136 | | |
137 | 137 | | |
138 | 138 | | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
139 | 163 | | |
140 | 164 | | |
141 | 165 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
25 | 28 | | |
26 | 29 | | |
27 | 30 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
0 commit comments