# TypeScript workspace builder examples

Source: https://libtmux.org/en/ts/latest/workspace/internals/examples/

> Internal examples for building and inspecting workspaces through the TypeScript API.

The workspace example declares a development session, applies it through
`@libtmux/workspace`, and provides cleanup that tolerates an absent session.
It also shows the lower-level core API for constructing a session manually.

## Build and remove a workspace

The `buildWorkspace` function applies `DEVELOPMENT_WORKSPACE`. Pass it the
`Server` you want to use. `removeWorkspace` checks a fresh snapshot before
killing the named session and returns whether a session was present.

```typescript file="examples/workspace/workspace.ts"
import type { Server } from "libtmux/server";
import type { Session } from "libtmux/session";
import { applyWorkspace } from "@libtmux/workspace";
import type { WorkspaceInput } from "@libtmux/workspace/config";

/**
 * Build the shape most people reach for tmux to get: one session, a window
 * per concern, each pane already running the thing it is there for.
 *
 * `server.batch` plans several windows and resolves them from one final
 * snapshot. `buildWorkspace`, below, delegates declared topology to the
 * workspace package instead of maintaining another reconciler here.
 */
export async function buildSimpleWorkspace(server: Server): Promise<Session> {
  const built = await server.newSession({
    name: "work",
    shellCommand: "sleep 30",
    windowName: "editor",
  });

  const [logs, shell] = await server.batch([
    built.plan.newWindow({ name: "logs", shellCommand: "tail -f /dev/null" }),
    built.plan.newWindow({ name: "shell" }),
  ]);

  await logs.selectLayout("even-horizontal");
  void shell.name; // "shell"

  return built;
}

export const DEVELOPMENT_WORKSPACE = {
  session_name: "workspace-example",
  windows: [
    { panes: ["sleep 30", "sleep 30"], window_name: "editor" },
    { panes: ["sleep 30"], window_name: "server" },
    { window_name: "logs" },
  ],
} satisfies WorkspaceInput;

/** Apply the package's declarative workspace to a server. */
export function buildWorkspace(server: Server): Promise<Session> {
  return applyWorkspace(server, DEVELOPMENT_WORKSPACE);
}

/**
 * Tear a workspace down without caring whether it is there.
 *
 * Killing a session that has already gone is not a failure worth propagating,
 * which is the one case worth handling separately from every other tmux error.
 */
export async function removeWorkspace(server: Server, name: string): Promise<boolean> {
  const found = (await server.snapshot()).sessions.first({ name });
  if (found === undefined) return false;
  await found.kill();
  return true;
}
```

The `sleep` commands keep panes alive for inspection. They do not represent
application readiness checks. Cleanup removes the session and its processes,
so use a dedicated server or a session name owned by the example.

## Verification

The port's workspace integration suite calls this source against real tmux.
From a prepared source checkout, run that suite with:

```console
$ bun test examples/workspace
```

The source include keeps this page's code aligned with the example. Rendering
the page alone does not execute the integration suite. For a smaller runnable
entry point, use the [application guide](../guides/).

[Example source](https://github.com/libtmux/libtmux-ts/blob/f85b8de551353f746d50eaf36bf0112f4fe5a528/examples/workspace/workspace.ts)
