node-app-build 7.7.21

Mini app developer CLI: scaffold, validate, package node-app-* Debian packages
# {{name}}

The full Stage developer guide is [`docs/development/stages/README.md`](../../../../docs/development/stages/README.md).
Start with its [creation walkthrough](../../../../docs/development/stages/02-create-a-stage.md), then use the API and testing chapters as the contract for this scaffold.

A **client-node stage app**, scaffolded by `node-app new --profile stage`.
Real, running code — not a stub — so you always have a working stage to
build on top of.

## What is a stage?

A stage is an app whose `manifest.json` declares a `ui` block with
`"kind": "stage"` and `"entry": "ui/dist/main.js"` (see `manifest.json` in
this folder). The host loads that compiled ES module and calls exactly two
exports:

- `mount(ctx: StageContext)` — called when the host navigates to this
  stage. Render into `ctx.host.root` (e.g. via
  `ctx.host.root.replaceChildren(el)`).
- `unmount()` — called before the host tears this stage down (navigation,
  hot-reload, shutdown). Undo everything `mount()` started.

`ctx` is the **only** platform surface a stage sees — no direct imports of
storage, network, or IPC. Its exact shape lives in
`ui/src/stage-context.ts` (re-exported from
`ui/src/vendor-node-client-rpc-stage-context.d.ts`); read that file to see
every member (`ctx.host.root`, `ctx.app.{name,version}`,
`ctx.invokeCapability<T>(name, payload)`, `ctx.query(name, params)`,
`ctx.liveQuery(name, params, listener)`, `ctx.events.subscribe/publish`,
`ctx.navigate(path)`) before you start editing.

## Where to start editing

The scaffold declares one app-owned collection in `manifest.json`.
`src/app.ts` serves the installed version at the key `installed` and keeps the
replica ledger in the app's data directory. `ui/src/stage.ts` subscribes to
`{{name}}.about.v1` with `ctx.liveQuery`. A subsequent open can show the
version from the device immediately and receive changes while the screen is open.

For a new list, project the device-safe fields in its producer and declare its
pull capability in both `ui.requires.capabilities` and `ui.data.collections`.
Record individual updates and deletions at the app's write sites with the SDK's
`touch` and `remove`. Keep keys, secrets and self-destruct message bodies
outside every projection. A screen with no durable data can remove the example
collection, its pull provider and subscription, and use `offline: "online-only"`.
Regenerate the host contract with `node-app contract stamp` when the declarations change.

The rest of the files are boilerplate that rarely needs to change:

| Path | Purpose |
| --- | --- |
| `manifest.json` | App + stage registration (name, capabilities, UI entry point). |
| `src/index.ts` | Starts the backend in `src/app.ts`. |
| `ui/src/main.ts` | Exports the UI lifecycle from `ui/src/stage.ts`. |
| `ui/src/stage-context.ts` | Re-exports the `StageContext` type. |
| `ui/src/vendor-node-client-rpc-stage-context.d.ts` | The full `StageContext` type definition. |
| `ui/src/css-modules.d.ts` | TypeScript ambient type for `import css from "./x.css?raw"`. |
| `ui/scripts/stamp-integrity.mjs` | Writes `manifest.json`'s `ui.integrity` from the built `ui/dist/` after every `vite build`. |
| `ui/public/icon.svg` | Stage icon, copied to `ui/dist/icon.svg` on build. |

## Build

Backend and UI are two independent packages, each with its own
`bun install` and `bun run build`:

```bash
# from the app root
bun install && bun run build
```

The root `build` script chains both halves
(`bun build src/index.ts ... && cd ui && bun install && bun run build`), so
one command is enough. After it finishes:

- `dist/index.js` exists (backend).
- `ui/dist/main.js` (and `ui/dist/icon.svg`) exist (UI).
- `manifest.json`'s `ui.integrity` has been rewritten with the SHA-256 of
  every file in `ui/dist/`, so the manifest is valid and loadable as-is —
  `ui.integrity: {}` in the freshly-scaffolded manifest is a **placeholder**
  the build step above always fills in.

## Renaming this into your own stage

1. Update `name`, `description` in `manifest.json`, `package.json`, and
   `ui/package.json` if you didn't pass them to `node-app new`.
2. Update `ui.title` and `ui.nav.order` in `manifest.json`.
3. Adjust `requires` / `capabilities.requires` in `manifest.json` to match
   the capabilities your stage actually calls.
4. Edit `src/app.ts` and `ui/src/stage.ts`.
5. Re-run `bun run build` to re-stamp `ui.integrity`.

## Next steps

```bash
bun run build        # stamps manifest.json's ui.integrity (required first)
node-app audit       # check this app against the blueprint
node-app dev         # hot-reload dev loop
node-app package     # build a .deb (regenerates ui.integrity for the staged copy too)
```

Platform contributors run all published stages automatically from the platform
repository:

```bash
cd /path/to/node
node-app dev
node-app dev --client-node
```

Both commands use the same `platform-depends` source-resolution and staging
pipeline. No manual `make bootstrap-apps` step is required.