Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions docs/docs/commands/new.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,6 @@ my-game/

The [schematics pages](../schematics/overview) describe every generated file.

<Warning>
In a workspace, the root `package.json` scripts run `nf dev -r`, `nf build -r` and `nf start -r`.
The CLI has no `-r` option, so `npm run dev` (and the workspace `Dockerfile`, which runs the
`build` script) fails with `error: unknown option '-r'`. Run `nf dev`, `nf build` and `nf start`
directly, or remove `-r` from those scripts.
</Warning>

## Examples

Answer the questions one by one:
Expand Down
47 changes: 25 additions & 22 deletions docs/docs/schematics/project.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ a [workspace](./workspace) (`apps/client`, `apps/server`).
server side by side, run it twice with `--directory=apps/client` and `--directory=apps/server`.

Every app depends on `@nanoforge-dev/cli`, `core`, `common`, `config`, `asset` and `ecs`. A client
also gets `env`, `graphics-2d`, `input` and `network`. A server gets `network` as a runtime
dependency. The versions are pinned to the newest release published at least 48 hours before
also gets `env`, `graphics-2d` and `input`, plus `network` with `hasServer`. A server gets `network`
as a runtime dependency. The versions are pinned to the newest release published at least 48 hours before
generation.

## Usage
Expand All @@ -26,23 +26,23 @@ schematics @nanoforge-dev/schematics:project --part=client --directory=apps/clie

## Options

| Option | Type | Default | Description |
| ---------------- | ------------------------------------ | --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `part` | `"client" \| "server"` | required | The app to generate. |
| `directory` | `string` | `apps` | Full destination path. |
| `name` | `string` | see below | Package name, in kebab-case. With `workspaceName`, the package is `<workspaceName>-<name>`, and `name` defaults to `part`. Without it, `name` defaults to `nanoforge-app`. |
| `workspaceName` | `string` | none | Name of the parent workspace, used as a package name prefix. |
| `language` | `"ts" \| "js"` | `ts` | Language of the generated files. |
| `strict` | `boolean` | `true` | Strict type checking. |
| `packageManager` | `"npm" \| "yarn" \| "pnpm" \| "bun"` | `npm` | Used in the `Dockerfile` and the install-script settings. |
| `workspace` | `boolean` | `true` | The app is inside a workspace. A standalone app (`false`) allows the install script of `bun` itself, and can get a `Dockerfile`. |
| `hasServer` | `boolean` | `false` | Client only: add the `position-sync` system, which takes the entity's position from the server instead of the client. |
| `docker` | `boolean` | `false` | Add a `Dockerfile` and a `.dockerignore`. Ignored when `workspace` is `true`: the workspace has its own. |
| `editor` | `boolean` | `false` | Add the `nf editor .` instructions to the README. |
| `initFunctions` | `boolean` | `false` | Accepted, but not used by the templates yet. |
| `libs` | `string[]` | `[]` | Accepted, but not written to `nanoforge.config.ts` yet. |
| `allowBuilds` | `string[]` | `[]` | Accepted, but not used yet: a standalone app always allows `bun` only. |
| `agents` | `boolean` | `true` | Add an `AGENTS.md` for AI coding agents (files, commands, ECS rules, network for a networked app) and a `CLAUDE.md` that imports it. |
| Option | Type | Default | Description |
| ---------------- | ------------------------------------ | --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `part` | `"client" \| "server"` | required | The app to generate. |
| `directory` | `string` | `apps` | Full destination path. |
| `name` | `string` | see below | Package name, in kebab-case. With `workspaceName`, the package is `<workspaceName>-<name>`, and `name` defaults to `part`. Without it, `name` defaults to `nanoforge-app`. |
| `workspaceName` | `string` | none | Name of the parent workspace, used as a package name prefix. |
| `language` | `"ts" \| "js"` | `ts` | Language of the generated files. |
| `strict` | `boolean` | `true` | Strict type checking. |
| `packageManager` | `"npm" \| "yarn" \| "pnpm" \| "bun"` | `npm` | Used in the `Dockerfile` and the install-script settings. |
| `workspace` | `boolean` | `true` | The app is inside a workspace. A standalone app (`false`) allows the install script of `bun` itself, and can get a `Dockerfile`. |
| `hasServer` | `boolean` | `false` | Client only: add the `network` library, the `position` packet and the `position-sync` system, which takes the entity's position from the server. Without it the client has no network. |
| `docker` | `boolean` | `false` | Add a `Dockerfile` and a `.dockerignore`. Ignored when `workspace` is `true`: the workspace has its own. |
| `editor` | `boolean` | `false` | Add the `nf editor .` instructions to the README. |
| `initFunctions` | `boolean` | `false` | Accepted, but not used by the templates yet. |
| `libs` | `string[]` | `[]` | Accepted, but not written to `nanoforge.config.ts` yet. |
| `allowBuilds` | `string[]` | `[]` | Accepted, but not used yet: a standalone app always allows `bun` only. |
| `agents` | `boolean` | `true` | Add an `AGENTS.md` for AI coding agents (files, commands, ECS rules, network for a networked app) and a `CLAUDE.md` that imports it. |

## Generated files

Expand All @@ -65,17 +65,20 @@ schematics @nanoforge-dev/schematics:project --part=client --directory=apps/clie
├── components/
│ ├── position-2d.component.ts # both parts
│ └── drawable-circle-2d.component.ts # client only
├── packets/
│ └── position.packet.ts # server, or client with hasServer
└── systems/
├── draw-2d.system.ts # client only
├── position-sync.system.ts # client only, hasServer: true
└── move-2d.system.ts # server only
```

`src/main.ts` creates the app, adds the ECS, network (and, on the client, 2D graphics and input)
libraries, spawns one entity with a position, and adds the systems:
`src/main.ts` creates the app, adds the ECS, network (server, or client with `hasServer`) and, on
the client, 2D graphics and input libraries, spawns one entity with a position, and adds the systems:

- the client draws a red circle at the entity's position;
- the server moves the entity one pixel per tick and sends its position to every client;
- the server moves the entity one pixel per tick and sends its position to every client in the
`position` packet (`src/packets/position.packet.ts`);
- with `hasServer`, the client takes that position from the server.

## Examples
Expand Down
6 changes: 0 additions & 6 deletions docs/docs/schematics/workspace.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ my-game/

The `.env` values are described in [environment variables](../configuration/environment-variables).

<Warning>
The `dev`, `build` and `start` scripts of the generated `package.json` run `nf dev -r`, `nf build
-r` and `nf start -r`. The CLI has no `-r` option, so they fail, and so does the `Dockerfile`,
which runs the `build` script. Remove `-r` from the three scripts.
</Warning>

## Examples

A TypeScript workspace for npm:
Expand Down
6 changes: 0 additions & 6 deletions docs/guides/build-and-deploy.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,5 @@ docker build -t my-game .
docker run -p 3000:3000 my-game
```

<Warning>
In a workspace (`nf new --server --docker`), the `Dockerfile` runs the root `build` script, which
calls `nf build -r` and fails: the CLI has no `-r` option. Remove `-r` from the `dev`, `build` and
`start` scripts of the root `package.json` before you build the image.
</Warning>

The container serves plain HTTP. Put it behind a reverse proxy that serves HTTPS, or mount a
certificate and set the client's `tls` field.
46 changes: 38 additions & 8 deletions libs/schematics/e2e/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ describe("project schematic", () => {
expect(packageJson.devDependencies["@nanoforge-dev/input"]).toBeDefined();
});

it("should declare network as a dev dependency", () => {
it("should leave network out of a client without a server", () => {
const packageJson = JSON.parse(tree.readContent("/apps/client/package.json"));
expect(packageJson.devDependencies["@nanoforge-dev/network"]).toBeDefined();
expect(packageJson.devDependencies["@nanoforge-dev/network"]).toBeUndefined();
expect(packageJson).not.toHaveProperty("dependencies");
});

Expand All @@ -66,7 +66,8 @@ describe("project schematic", () => {
expect(content).toContain('from "@nanoforge-dev/ecs/client"');
expect(content).toContain("Graphics2DLibrary");
expect(content).toContain("InputLibrary");
expect(content).toContain("NetworkClientLibrary");
// Without a server the network library would fail its env check and stop the game.
expect(content).not.toContain("NetworkClientLibrary");
});

it("should access the graphics library directly off ctx, not through ctx.libs", () => {
Expand Down Expand Up @@ -192,6 +193,36 @@ describe("project schematic", () => {
const content = tree.readContent("/has-server-apps/client/src/main.ts");
expect(content).toContain('import { positionSync } from "./systems/position-sync.system"');
expect(content).toContain("registry.addSystem(positionSync)");
expect(content).toContain("app.use(new NetworkClientLibrary())");
const packageJson = JSON.parse(tree.readContent("/has-server-apps/client/package.json"));
expect(packageJson.devDependencies["@nanoforge-dev/network"]).toMatch(RESOLVED_VERSION);
});

it("sends and reads the position through the shared packet, not raw tcp", async () => {
for (const part of ["client", "server"] as const) {
const tree = await runner.runSchematic("project", {
part,
workspaceName: "packet-workspace",
directory: `packet-apps/${part}`,
hasServer: true,
});
const packet = tree.readContent(`/packet-apps/${part}/src/packets/position.packet.ts`);
expect(packet).toContain('definePacket<{ x: number; y: number }>("position"');
const system = tree.readContent(
`/packet-apps/${part}/src/systems/${part === "client" ? "position-sync" : "move-2d"}.system.ts`,
);
expect(system).toContain("ctx.network.packets");
expect(system).not.toContain(".tcp");
}
});

it("has no packet file without a server", async () => {
const tree = await runner.runSchematic("project", {
part: "client",
workspaceName: "no-packet-workspace",
directory: "no-packet-apps/client",
});
expect(tree.files).not.toContain("/no-packet-apps/client/src/packets/position.packet.ts");
});

it("is ignored for a server project - position handling is always active there", async () => {
Expand Down Expand Up @@ -325,16 +356,15 @@ describe("project schematic", () => {

describe("@nanoforge-dev/ecs and @nanoforge-dev/network version resolution", () => {
it("still writes the ecs/network keys even though they're resolved via their real per-part packages", async () => {
// @nanoforge-dev/ecs and @nanoforge-dev/network aren't published under
// those names (see ecs-lib/ecs-client/ecs-server and
// network-client/network-server) — the factory aliases to the real
// package to resolve a trustworthy version, but keeps writing the
// existing (unchanged) `ecs`/`network` keys.
// The factory resolves a trustworthy version for each package and
// writes it under the `ecs`/`network` keys.
const client = await runner.runSchematic("project", {
part: "client",
workspaceName: "ecs-alias-client-workspace",
directory: "ecs-alias-client-apps/client",
workspace: false,
// A client gets network only when it talks to a server.
hasServer: true,
});
const clientPackageJson = JSON.parse(
client.readContent("/ecs-alias-client-apps/client/package.json"),
Expand Down
5 changes: 5 additions & 0 deletions libs/schematics/e2e/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ describe("workspace schematic", () => {
expect(packageJson.name).toBe("my-workspace");
});

it("runs the nf commands without the -r option the CLI does not have", () => {
expect(packageJson.scripts).toEqual({ dev: "nf dev", build: "nf build", start: "nf start" });
expect(tree.readContent("/my-workspace/README.md")).not.toMatch(/-r\b/);
});

it("should declare npm workspaces in package.json (apps only, no libs)", () => {
expect(packageJson.workspaces).toEqual(["apps/*"]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ branch and can describe APIs that the installed release lacks: the installed typ
`NANOFORGE_CLIENT_X` to the client and `NANOFORGE_SERVER_X` to the server, as `X` in
`options.env`. Everything the client gets reaches the browser: never put a secret there.
https://docs.nanoforge.eu/docs/cli/configuration/environment-variables
- The generated systems use `ctx.network.tcp` and encode JSON with `TextEncoder`. Check the
installed `@nanoforge-dev/network` types before using another network API.
- The client and the server exchange typed packets declared with `definePacket` in
`src/packets/` (the same name on both sides): the server calls `ctx.network.packets.sendToAll`,
the client `ctx.network.packets.read`. Check the installed `@nanoforge-dev/network` types
before using another network API.
<% } %>
## Example

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"@nanoforge-dev/ecs": "<%= ecsVersion %>"<% if (part === "client") { %>,
"@nanoforge-dev/env": "<%= engineVersion %>",
"@nanoforge-dev/graphics-2d": "<%= graphics2dVersion %>",
"@nanoforge-dev/input": "<%= inputVersion %>",
"@nanoforge-dev/network": "<%= networkVersion %>"<% } %>
"@nanoforge-dev/input": "<%= inputVersion %>"<% if (hasServer) { %>,
"@nanoforge-dev/network": "<%= networkVersion %>"<% } %><% } %>
},<% if (packageManager === "npm" && allowBuilds.length > 0) { %>
"allowScripts": {<% allowBuilds.forEach(function (pkg, i) { %>
"<%= pkg %>": true<% if (i < allowBuilds.length - 1) { %>,<% } %><% }); %>
Expand Down
10 changes: 5 additions & 5 deletions libs/schematics/src/libs/project/files/js/src/main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { NanoforgeFactory } from "@nanoforge-dev/core";
<% var networked = part === "server" || hasServer; %>import { NanoforgeFactory } from "@nanoforge-dev/core";
import { EcsLibrary } from "@nanoforge-dev/ecs/<%= part %>";<% if (part === "client") { %>
import { Graphics2DLibrary } from "@nanoforge-dev/graphics-2d";
import { InputLibrary } from "@nanoforge-dev/input";<% } %>
import { <%= part === "client" ? "NetworkClientLibrary" : "NetworkServerLibrary" %> } from "@nanoforge-dev/network/<%= part %>";
<% if (part === "client") { %>
<% if (networked) { %>import { <%= part === "client" ? "NetworkClientLibrary" : "NetworkServerLibrary" %> } from "@nanoforge-dev/network/<%= part %>";
<% } %><% if (part === "client") { %>
import { DrawableCircle2D } from "./components/drawable-circle-2d.component";<% } %>
import { Position2D } from "./components/position-2d.component";<% if (part === "client") { %>
import { draw2D } from "./systems/draw-2d.system";<% if (hasServer) { %>
Expand All @@ -21,8 +21,8 @@ export const main = async (options) => {
app.use(ecs);<% if (part === "client") { %>
app.use(new Graphics2DLibrary());
app.use(new InputLibrary());<% } %>
app.use(new <%= part === "client" ? "NetworkClientLibrary" : "NetworkServerLibrary" %>());

<% if (networked) { %> app.use(new <%= part === "client" ? "NetworkClientLibrary" : "NetworkServerLibrary" %>());
<% } %>
await app.init(options);

const registry = ecs.registry;
Expand Down
Loading
Loading