@dawsson/mux 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Dawson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # mux
2
+
3
+ Configurable tmux session manager for dev workflows. Reads pane config from `package.json` and manages named tmux sessions.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ bun install -g mux
9
+ ```
10
+
11
+ Or run directly with `bun run src/cli.ts`.
12
+
13
+ ## Config
14
+
15
+ Add a `"mux"` key to your project's `package.json`:
16
+
17
+ ```json
18
+ {
19
+ "mux": {
20
+ "session": "my-project",
21
+ "panes": [
22
+ { "name": "api", "cmd": "bun run dev", "cwd": "packages/api" },
23
+ { "name": "web", "cmd": "bun run start", "cwd": "packages/web" },
24
+ { "name": "expo", "cmd": "bun expo start", "cwd": "apps/mobile" }
25
+ ]
26
+ }
27
+ }
28
+ ```
29
+
30
+ - `session` — tmux session name (defaults to directory basename)
31
+ - `panes[].name` — pane identifier used in commands
32
+ - `panes[].cmd` — shell command to run in the pane
33
+ - `panes[].cwd` — working directory relative to project root (optional)
34
+
35
+ ## Commands
36
+
37
+ | Command | Description |
38
+ |----------------------|--------------------------------------------------|
39
+ | `mux` | Start session if not running, attach if it is |
40
+ | `mux start` | Explicit start, then attach |
41
+ | `mux start --detach` | Start in background (no attach), useful for CI |
42
+ | `mux stop` | Kill the session |
43
+ | `mux status` | List panes and their indices |
44
+ | `mux logs [pane]` | Capture pane output (all panes if none given) |
45
+ | `mux restart [pane]` | Restart a pane (all panes if none given) |
46
+
47
+ ## Logs
48
+
49
+ Pane output is available via `mux logs`. Session log files are written to `/tmp/mux-<session>/`.
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@dawsson/mux",
3
+ "version": "0.1.0",
4
+ "description": "Configurable tmux session manager for dev workflows",
5
+ "type": "module",
6
+ "bin": {
7
+ "mux": "src/cli.ts"
8
+ },
9
+ "files": [
10
+ "src"
11
+ ],
12
+ "scripts": {
13
+ "test": "bun test"
14
+ },
15
+ "license": "MIT",
16
+ "devDependencies": {
17
+ "bun-types": "^1.3.9"
18
+ }
19
+ }
package/src/cli.ts ADDED
@@ -0,0 +1,137 @@
1
+ #!/usr/bin/env bun
2
+ import { findConfig } from "./config";
3
+ import {
4
+ hasSession,
5
+ startSession,
6
+ killSession,
7
+ attach,
8
+ listPanes,
9
+ capturePane,
10
+ restartPane,
11
+ } from "./tmux";
12
+
13
+ const args = process.argv.slice(2);
14
+ const cmd = args[0];
15
+
16
+ function loadConfig() {
17
+ try {
18
+ return findConfig();
19
+ } catch (e: any) {
20
+ console.error(`Error: ${e.message}`);
21
+ process.exit(1);
22
+ }
23
+ }
24
+
25
+ function printUsage() {
26
+ console.log(`mux — configurable tmux session manager
27
+
28
+ Usage:
29
+ mux Start session if not running, attach if it is
30
+ mux start [--detach] Start the session (--detach: don't attach)
31
+ mux stop Kill the session
32
+ mux status Show running panes
33
+ mux logs [pane] Capture pane output (all panes if none specified)
34
+ mux restart [pane] Restart a pane or all panes
35
+ `);
36
+ }
37
+
38
+ if (!cmd || cmd === "start") {
39
+ const config = loadConfig();
40
+ const detach = args.includes("--detach");
41
+
42
+ if (hasSession(config.session)) {
43
+ if (!detach) {
44
+ attach(config.session);
45
+ } else {
46
+ console.log(`Session "${config.session}" already running.`);
47
+ }
48
+ } else {
49
+ startSession(config);
50
+ if (!detach) {
51
+ attach(config.session);
52
+ } else {
53
+ console.log(`Session "${config.session}" started (detached).`);
54
+ }
55
+ }
56
+ } else if (cmd === "stop") {
57
+ const config = loadConfig();
58
+ if (hasSession(config.session)) {
59
+ killSession(config.session);
60
+ console.log(`Session "${config.session}" stopped.`);
61
+ } else {
62
+ console.log(`Session "${config.session}" is not running.`);
63
+ }
64
+ } else if (cmd === "status") {
65
+ const config = loadConfig();
66
+ if (!hasSession(config.session)) {
67
+ console.log(`Session "${config.session}" is not running.`);
68
+ process.exit(0);
69
+ }
70
+ const panes = listPanes(config.session);
71
+ console.log(`Session: ${config.session}`);
72
+ for (let i = 0; i < panes.length; i++) {
73
+ const p = panes[i];
74
+ const name = config.panes[i]?.name ?? `pane-${i}`;
75
+ const active = p.active ? " (active)" : "";
76
+ console.log(` [${p.index}] ${name}${active}`);
77
+ }
78
+ } else if (cmd === "logs") {
79
+ const config = loadConfig();
80
+ if (!hasSession(config.session)) {
81
+ console.error(`Session "${config.session}" is not running.`);
82
+ process.exit(1);
83
+ }
84
+ const targetPane = args[1];
85
+ const panes = listPanes(config.session);
86
+
87
+ if (targetPane) {
88
+ const idx = config.panes.findIndex((p) => p.name === targetPane);
89
+ if (idx === -1) {
90
+ console.error(`Unknown pane: ${targetPane}`);
91
+ process.exit(1);
92
+ }
93
+ const pane = panes[idx];
94
+ if (!pane) {
95
+ console.error(`Pane ${targetPane} not found in tmux session.`);
96
+ process.exit(1);
97
+ }
98
+ const output = capturePane(pane.id);
99
+ console.log(`=== ${targetPane} ===`);
100
+ console.log(output);
101
+ } else {
102
+ for (let i = 0; i < panes.length; i++) {
103
+ const name = config.panes[i]?.name ?? `pane-${i}`;
104
+ const output = capturePane(panes[i].id);
105
+ console.log(`=== ${name} ===`);
106
+ console.log(output);
107
+ console.log();
108
+ }
109
+ }
110
+ } else if (cmd === "restart") {
111
+ const config = loadConfig();
112
+ if (!hasSession(config.session)) {
113
+ console.error(`Session "${config.session}" is not running.`);
114
+ process.exit(1);
115
+ }
116
+ const targetPane = args[1];
117
+ if (targetPane) {
118
+ try {
119
+ restartPane(config, targetPane);
120
+ console.log(`Restarted pane "${targetPane}".`);
121
+ } catch (e: any) {
122
+ console.error(`Error: ${e.message}`);
123
+ process.exit(1);
124
+ }
125
+ } else {
126
+ for (const pane of config.panes) {
127
+ restartPane(config, pane.name);
128
+ console.log(`Restarted pane "${pane.name}".`);
129
+ }
130
+ }
131
+ } else if (cmd === "--help" || cmd === "-h" || cmd === "help") {
132
+ printUsage();
133
+ } else {
134
+ console.error(`Unknown command: ${cmd}`);
135
+ printUsage();
136
+ process.exit(1);
137
+ }
package/src/config.ts ADDED
@@ -0,0 +1,57 @@
1
+ import { existsSync, readFileSync } from "fs";
2
+ import { join, dirname, basename } from "path";
3
+
4
+ export interface PaneConfig {
5
+ name: string;
6
+ cmd: string;
7
+ cwd?: string;
8
+ }
9
+
10
+ export interface MuxConfig {
11
+ session: string;
12
+ panes: PaneConfig[];
13
+ root: string; // absolute path to the project root (where package.json lives)
14
+ }
15
+
16
+ interface RawMuxConfig {
17
+ session?: string;
18
+ panes?: unknown[];
19
+ }
20
+
21
+ export function findConfig(from: string = process.cwd()): MuxConfig {
22
+ let dir = from;
23
+
24
+ while (true) {
25
+ const pkgPath = join(dir, "package.json");
26
+ if (existsSync(pkgPath)) {
27
+ const raw = JSON.parse(readFileSync(pkgPath, "utf8"));
28
+ if (raw.mux) {
29
+ return parseConfig(raw.mux, dir);
30
+ }
31
+ }
32
+
33
+ const parent = dirname(dir);
34
+ if (parent === dir) break;
35
+ dir = parent;
36
+ }
37
+
38
+ throw new Error(
39
+ 'No "mux" config found in any package.json from here to /'
40
+ );
41
+ }
42
+
43
+ export function parseConfig(raw: RawMuxConfig, root: string): MuxConfig {
44
+ const session = raw.session || basename(root);
45
+
46
+ if (!raw.panes || !Array.isArray(raw.panes) || raw.panes.length === 0) {
47
+ throw new Error("mux config needs at least one pane");
48
+ }
49
+
50
+ const panes: PaneConfig[] = raw.panes.map((p: any, i: number) => {
51
+ if (!p.name) throw new Error(`pane ${i} missing "name"`);
52
+ if (!p.cmd) throw new Error(`pane ${i} missing "cmd"`);
53
+ return { name: p.name, cmd: p.cmd, cwd: p.cwd };
54
+ });
55
+
56
+ return { session, panes, root };
57
+ }
@@ -0,0 +1,60 @@
1
+ import { describe, it, expect } from "bun:test";
2
+ import { parseConfig } from "./config";
3
+
4
+ describe("parseConfig", () => {
5
+ const root = "/tmp/test-project";
6
+
7
+ it("parses a valid config", () => {
8
+ const config = parseConfig(
9
+ {
10
+ session: "my-app",
11
+ panes: [
12
+ { name: "api", cmd: "bun run dev", cwd: "packages/api" },
13
+ { name: "web", cmd: "bun run start", cwd: "packages/web" },
14
+ ],
15
+ },
16
+ root
17
+ );
18
+ expect(config.session).toBe("my-app");
19
+ expect(config.root).toBe(root);
20
+ expect(config.panes).toHaveLength(2);
21
+ expect(config.panes[0]).toEqual({ name: "api", cmd: "bun run dev", cwd: "packages/api" });
22
+ expect(config.panes[1]).toEqual({ name: "web", cmd: "bun run start", cwd: "packages/web" });
23
+ });
24
+
25
+ it("defaults session to directory basename", () => {
26
+ const config = parseConfig(
27
+ { panes: [{ name: "dev", cmd: "bun run dev" }] },
28
+ "/home/user/my-project"
29
+ );
30
+ expect(config.session).toBe("my-project");
31
+ });
32
+
33
+ it("allows pane without cwd", () => {
34
+ const config = parseConfig(
35
+ { panes: [{ name: "dev", cmd: "bun run dev" }] },
36
+ root
37
+ );
38
+ expect(config.panes[0].cwd).toBeUndefined();
39
+ });
40
+
41
+ it("throws when panes is missing", () => {
42
+ expect(() => parseConfig({}, root)).toThrow("at least one pane");
43
+ });
44
+
45
+ it("throws when panes is empty", () => {
46
+ expect(() => parseConfig({ panes: [] }, root)).toThrow("at least one pane");
47
+ });
48
+
49
+ it("throws when a pane is missing name", () => {
50
+ expect(() =>
51
+ parseConfig({ panes: [{ cmd: "bun run dev" }] }, root)
52
+ ).toThrow('missing "name"');
53
+ });
54
+
55
+ it("throws when a pane is missing cmd", () => {
56
+ expect(() =>
57
+ parseConfig({ panes: [{ name: "api" }] }, root)
58
+ ).toThrow('missing "cmd"');
59
+ });
60
+ });
package/src/tmux.ts ADDED
@@ -0,0 +1,138 @@
1
+ import { mkdirSync, existsSync } from "fs";
2
+ import type { MuxConfig, PaneConfig } from "./config";
3
+ import { join } from "path";
4
+
5
+ function run(...args: string[]): string {
6
+ const result = Bun.spawnSync(["tmux", ...args], {
7
+ stdout: "pipe",
8
+ stderr: "pipe",
9
+ });
10
+ return result.stdout.toString().trim();
11
+ }
12
+
13
+ function runOk(...args: string[]): boolean {
14
+ const result = Bun.spawnSync(["tmux", ...args], {
15
+ stdout: "pipe",
16
+ stderr: "pipe",
17
+ });
18
+ return result.exitCode === 0;
19
+ }
20
+
21
+ export function hasSession(name: string): boolean {
22
+ return runOk("has-session", "-t", name);
23
+ }
24
+
25
+ export function createSession(name: string, cwd: string): void {
26
+ run("new-session", "-d", "-s", name, "-c", cwd);
27
+ }
28
+
29
+ export function splitPane(session: string, cwd: string): string {
30
+ return run(
31
+ "split-window",
32
+ "-t",
33
+ session,
34
+ "-c",
35
+ cwd,
36
+ "-P",
37
+ "-F",
38
+ "#{pane_id}"
39
+ );
40
+ }
41
+
42
+ export function sendKeys(session: string, target: string, cmd: string): void {
43
+ run("send-keys", "-t", target, cmd, "Enter");
44
+ }
45
+
46
+ export function killSession(name: string): void {
47
+ run("kill-session", "-t", name);
48
+ }
49
+
50
+ export function selectLayout(session: string, layout: string): void {
51
+ run("select-layout", "-t", session, layout);
52
+ }
53
+
54
+ export function renameWindow(session: string, name: string): void {
55
+ run("rename-window", "-t", session, name);
56
+ }
57
+
58
+ export function attach(session: string): void {
59
+ const proc = Bun.spawnSync(["tmux", "attach", "-t", session], {
60
+ stdin: "inherit",
61
+ stdout: "inherit",
62
+ stderr: "inherit",
63
+ });
64
+ process.exit(proc.exitCode ?? 0);
65
+ }
66
+
67
+ export function listPanes(
68
+ session: string
69
+ ): { id: string; index: string; title: string; active: boolean }[] {
70
+ const out = run(
71
+ "list-panes",
72
+ "-t",
73
+ session,
74
+ "-F",
75
+ "#{pane_id}\t#{pane_index}\t#{pane_title}\t#{pane_active}"
76
+ );
77
+ if (!out) return [];
78
+ return out.split("\n").map((line) => {
79
+ const [id, index, title, active] = line.split("\t");
80
+ return { id, index, title, active: active === "1" };
81
+ });
82
+ }
83
+
84
+ export function capturePane(target: string, lines = 100): string {
85
+ return run("capture-pane", "-t", target, "-p", "-S", `-${lines}`);
86
+ }
87
+
88
+ function logDir(session: string): string {
89
+ return `/tmp/mux-${session}`;
90
+ }
91
+
92
+ export function setupLogging(config: MuxConfig): void {
93
+ const dir = logDir(config.session);
94
+ if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
95
+ }
96
+
97
+ export function startSession(config: MuxConfig): void {
98
+ const { session, panes, root } = config;
99
+
100
+ createSession(session, root);
101
+ setupLogging(config);
102
+
103
+ // First pane is the session window's initial pane
104
+ const firstPane = panes[0];
105
+ const firstCwd = firstPane.cwd ? join(root, firstPane.cwd) : root;
106
+ // Set cwd for first pane
107
+ sendKeys(session, `${session}:0.0`, `cd ${firstCwd}`);
108
+ sendKeys(session, `${session}:0.0`, firstPane.cmd);
109
+
110
+ // Additional panes via split
111
+ for (let i = 1; i < panes.length; i++) {
112
+ const pane = panes[i];
113
+ const cwd = pane.cwd ? join(root, pane.cwd) : root;
114
+ const paneId = splitPane(session, cwd);
115
+ sendKeys(session, paneId, pane.cmd);
116
+ }
117
+
118
+ // Even layout
119
+ selectLayout(session, "tiled");
120
+ renameWindow(session, "mux");
121
+ }
122
+
123
+ export function restartPane(
124
+ config: MuxConfig,
125
+ paneName: string
126
+ ): void {
127
+ const paneConfig = config.panes.find((p) => p.name === paneName);
128
+ if (!paneConfig) throw new Error(`Unknown pane: ${paneName}`);
129
+
130
+ const panes = listPanes(config.session);
131
+ const idx = config.panes.findIndex((p) => p.name === paneName);
132
+ const target = panes[idx];
133
+ if (!target) throw new Error(`Pane ${paneName} not found in tmux session`);
134
+
135
+ // Send Ctrl-C then re-run command
136
+ run("send-keys", "-t", target.id, "C-c", "");
137
+ sendKeys(config.session, target.id, paneConfig.cmd);
138
+ }