> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Recipe — Adding a new `@piano/*` package

Companion to [adding-a-feature.md](/docs/adding-a-feature.md). Use this when the new code is **pure TypeScript domain logic** that should live under `packages/*`. If you're adding a screen, store, or React hook, you want `adding-a-feature.md` instead.

## Decision tree

```mermaid
flowchart TB
  start([New code to add]) --> q1{Pure TypeScript?<br/>No React, no RN, no expo}
  q1 -->|no| feature["Add to src/features/<x>/<br/>(see adding-a-feature.md)"]
  q1 -->|yes| q2{Reusable across<br/>hosts / tests / scripts?}
  q2 -->|no, only this app| feature
  q2 -->|yes| q3{Already fits an<br/>existing @piano/* package?}
  q3 -->|yes| extend["Extend that package's src/index.ts"]
  q3 -->|no| newPkg[Add a new @piano/* package]
```

If you reach "Add a new `@piano/*` package", continue below.

## Step-by-step

### 1. Pick a name

Use kebab-case under the `@piano` scope. Match the existing taxonomy:

| Concern                       | Existing packages                                  |
| ----------------------------- | -------------------------------------------------- |
| Domain types / schemas        | `@piano/types`                                     |
| Music-theory primitives       | `@piano/keyboard`, `@piano/midi`                   |
| Engine primitives             | `@piano/core`                                      |
| Game modes                    | `@piano/gameplay-{learn,magic,zen}`                |
| Player progression            | `@piano/progression`, `@piano/player-data`         |
| Content                       | `@piano/catalog`                                   |
| External adapters / contracts | `@piano/network`, `@piano/analytics`, `@piano/ads` |
| Public facade                 | `@piano/sdk`                                       |

If your idea overlaps with an existing package, prefer extending it.

### 2. Scaffold the folder

Copy `packages/types/` (the simplest leaf package) as your starting template:

```bash
cp -R packages/types packages/<new-name>
rm -rf packages/<new-name>/dist packages/<new-name>/node_modules
```

Then edit:

- `packages/<new-name>/package.json` — change `"name"` to `"@piano/<new-name>"`.
- `packages/<new-name>/README.md` — write one paragraph on what this package owns and why.
- `packages/<new-name>/src/index.ts` — wipe it and add your first export.

`tsconfig.json` and `tsup.config.ts` need no edits — both extend the shared root configs.

### 3. Declare workspace dependencies

If your package needs another `@piano/*`, add it to `dependencies` (not `devDependencies`) with the workspace protocol:

```json
{
  "dependencies": {
    "@piano/types": "workspace:*",
    "@piano/keyboard": "workspace:*"
  }
}
```

Don't add `react`, `react-native`, or any RN-only dep here — `@piano/*` packages must stay platform-agnostic. If you need them, write a thin adapter in `src/features/<x>/` and inject the contract.

External npm deps go under `dependencies` like normal (e.g. `valibot`, `midi-parser-js`).

### 4. Run `bun install` from the repo root

```bash
bun install
```

This:

- Symlinks `packages/<new-name>` into `node_modules/@piano/<new-name>`.
- Resolves any new external deps and writes `bun.lock`.
- Re-runs the host app's install so Metro will see the package on next start.

### 5. Implement and build

Inside the new package:

```bash
cd packages/<new-name>
bun run build      # one-shot tsup build
bun run typecheck  # tsc --noEmit
bun run dev        # tsup --watch (only if package.json declares dev script)
```

Or from the root, in topological order:

```bash
bunx turbo run build --filter=@piano/<new-name>
bunx turbo run typecheck
```

### 6. Add tests

Drop unit tests in `packages/<new-name>/tests/`. Bun's test runner is preconfigured for every package via the `test` script:

```ts
import { describe, expect, it } from 'bun:test';
import { computeXp } from '../src/index';

describe('computeXp', () => {
  it('returns 0 for missing notes', () => {
    expect(computeXp({ hits: 0, total: 10 })).toBe(0);
  });
});
```

Run with `bun test` inside the package, or `bun run test` from the root (Turbo runs every package's tests **and** app shards in parallel; Bun ≥ 1.3.13).

### 7. Expose through `@piano/sdk` (if public)

If the new package is part of the SDK's public surface (most are), wire it up:

1. Add `"@piano/<new-name>": "workspace:*"` to `packages/sdk/package.json` `dependencies`.
2. Add `export * from '@piano/<new-name>';` (or a narrower re-export) at the top of `packages/sdk/src/index.ts`.
3. If the package contributes to `createPianoSdk(config)`, extend `PianoSdkConfig` and `PianoSdk` accordingly.
4. Re-run `bun install` and `bunx turbo run build --filter=@piano/sdk`.

If the package is internal-only (used by other `@piano/*` packages but not by the host app), skip this step.

### 8. Use it from the host app

Inside `src/features/<x>/` or wherever it makes sense:

```ts
import { computeXp } from '@piano/sdk';
```

Never `import { computeXp } from '@piano/<new-name>'` directly — the SDK is the only public surface for the host. (See [architecture.md](/docs/architecture.md) "Dependency direction rules".)

## Checklist before merging

- [ ] `bun install` succeeds from the repo root.
- [ ] `bunx turbo run build` succeeds (no broken topological order).
- [ ] `bunx turbo run typecheck` succeeds across all packages — including any package that depends on the new one.
- [ ] `bunx turbo run test` passes (or the package ships with green tests).
- [ ] `package.json` `"name"` matches `@piano/<new-name>` and uses `workspace:*` for any internal deps.
- [ ] The package does not depend on `react`, `react-native`, `expo`, or any other RN-only module.
- [ ] If public, the package is re-exported from `@piano/sdk`.
- [ ] `README.md` says in one paragraph what the package owns and why it exists.

## Visual recap

```mermaid
flowchart TB
  step1["1. Pick a kebab-case name under @piano/"] --> step2["2. Copy packages/types as a template"]
  step2 --> step3["3. Declare workspace + external deps"]
  step3 --> step4["4. bun install at root"]
  step4 --> step5["5. Implement src/index.ts + tests/"]
  step5 --> step6["6. bun run build / typecheck / test"]
  step6 --> step7["7. (optional) Re-export from @piano/sdk"]
  step7 --> step8["8. Import via @piano/sdk in src/features/*"]
```

***

<small>Source: [`docs/adding-a-package.md`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/docs/adding-a-package.md)</small>
