@mak-98/tmux-ts 0.0.0-stage → 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/README.md CHANGED
@@ -1,3 +1,87 @@
1
- # Temporary Holding Version
1
+ # tmux-ts
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A TypeScript package for splitting the tmux pane containing a Node.js application and running a command in the new pane.
4
+
5
+ ## Requirements
6
+
7
+ - Node.js
8
+ - tmux installed and available on `PATH`
9
+ - The application must run inside a reachable tmux pane
10
+
11
+ ## Usage
12
+
13
+ Build the package, then install it into your application. For local development:
14
+
15
+ ```sh
16
+ # In the tmux-ts repository
17
+ npm install
18
+ npm run build
19
+
20
+ # In your application
21
+ npm install /path/to/tmux-ts
22
+ ```
23
+
24
+ Import `split` to open a new pane:
25
+
26
+ ```ts
27
+ import { split } from "tmux-ts";
28
+
29
+ const paneId = await split(
30
+ { direction: "right", focus: true },
31
+ { command: "nvim", args: ["./example.txt"] },
32
+ );
33
+
34
+ console.log(`Opened pane ${paneId}`);
35
+ ```
36
+
37
+ The original application stays running in its pane. The returned string is the new pane's tmux ID, such as `%3`.
38
+
39
+ ### Options
40
+
41
+ `split` takes two arguments:
42
+
43
+ - **Pane options**
44
+ - `direction`: `"left"`, `"right"`, `"up"`, or `"down"`.
45
+ - `focus`: Whether to focus the new pane. Defaults to `false`.
46
+ - **Terminal options**
47
+ - `command`: The executable to run.
48
+ - `args`: Optional command arguments.
49
+ - `cwd`: Working directory for the new pane. Defaults to the current process's working directory.
50
+
51
+ ### Errors
52
+
53
+ `split` throws an error named `SplitError` with one of these `code` values:
54
+
55
+ | Code | Meaning |
56
+ | --- | --- |
57
+ | `not-installed` | The tmux executable was not found. |
58
+ | `not-in-tmux` | The process does not have the expected tmux environment variables. |
59
+ | `pane-unreachable` | The process's tmux pane could not be reached. |
60
+ | `status-check-failed` | An unexpected error occurred while checking tmux status. |
61
+ | `split-failed` | The tmux split command failed. |
62
+
63
+ The underlying error is available as `cause` when applicable. A successful split returns a pane ID; it does not guarantee that the command running in the new pane will remain running.
64
+
65
+ ## Development CLI
66
+
67
+ From inside tmux, run:
68
+
69
+ ```sh
70
+ npm start
71
+ ```
72
+
73
+ This checks the current pane and prints its ID. To open a focused zsh pane on the right:
74
+
75
+ ```sh
76
+ npm start -- split
77
+ ```
78
+
79
+ Build the package and its TypeScript declarations with:
80
+
81
+ ```sh
82
+ npm run build
83
+ ```
84
+
85
+ ## License
86
+
87
+ MIT
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/dist/cli.js ADDED
@@ -0,0 +1,15 @@
1
+ import { split } from "./index.js";
2
+ import { getPaneStatus } from "./infrastructure/get-pane-status.js";
3
+ const status = await getPaneStatus();
4
+ if (!status.available) {
5
+ console.error(`tmux unavailable: ${status.reason}`);
6
+ process.exitCode = 1;
7
+ }
8
+ else if (process.argv[2] === "split") {
9
+ const paneId = await split({ direction: "right", focus: true }, { command: "zsh" });
10
+ console.log(`Opened pane ${paneId}`);
11
+ }
12
+ else {
13
+ console.log(`Running in tmux pane ${status.paneId}`);
14
+ console.log("Run with 'split' to open a zsh pane on the right.");
15
+ }
@@ -0,0 +1,6 @@
1
+ export type PaneUnavailableReason = "not-installed" | "not-in-tmux" | "pane-unreachable";
2
+ export type SplitError = Error & {
3
+ name: "SplitError";
4
+ code: PaneUnavailableReason | "status-check-failed" | "split-failed";
5
+ };
6
+ export declare function createSplitError(code: SplitError["code"], cause?: unknown): SplitError;
@@ -0,0 +1,3 @@
1
+ export function createSplitError(code, cause) {
2
+ return Object.assign(new Error(`Cannot split tmux pane: ${code}`, { cause }), { name: "SplitError", code });
3
+ }
@@ -0,0 +1,4 @@
1
+ export type PaneOptions = {
2
+ direction: "left" | "right" | "up" | "down";
3
+ focus?: boolean;
4
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import type { PaneUnavailableReason } from "./errors/split-error.js";
2
+ export type PaneStatus = {
3
+ available: true;
4
+ paneId: string;
5
+ } | {
6
+ available: false;
7
+ reason: PaneUnavailableReason;
8
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ export type TerminalOptions = {
2
+ command: string;
3
+ args?: string[];
4
+ cwd?: string;
5
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ import { TerminalOptions } from "./domain/terminal-options.js";
2
+ import { PaneOptions } from "./domain/pane-options.js";
3
+ /**
4
+ * Splits the pane containing this process and runs a command in the new pane.
5
+ *
6
+ * @param paneOptions - Direction of the split and whether to focus the new pane.
7
+ * Focus defaults to false.
8
+ * @param terminalOptions - Command, arguments, and working directory for the
9
+ * new pane. The working directory defaults to the current process directory.
10
+ * @returns The new pane's tmux ID, such as "%3".
11
+ * @throws {SplitError} If the status check or split fails.
12
+ */
13
+ export declare function split(paneOptions: PaneOptions, terminalOptions: TerminalOptions): Promise<string>;
package/dist/index.js ADDED
@@ -0,0 +1,50 @@
1
+ import { execFile } from "node:child_process";
2
+ import { promisify } from "node:util";
3
+ import { getPaneStatus } from "./infrastructure/get-pane-status.js";
4
+ import { createSplitError } from "./domain/errors/split-error.js";
5
+ /**
6
+ * Splits the pane containing this process and runs a command in the new pane.
7
+ *
8
+ * @param paneOptions - Direction of the split and whether to focus the new pane.
9
+ * Focus defaults to false.
10
+ * @param terminalOptions - Command, arguments, and working directory for the
11
+ * new pane. The working directory defaults to the current process directory.
12
+ * @returns The new pane's tmux ID, such as "%3".
13
+ * @throws {SplitError} If the status check or split fails.
14
+ */
15
+ export async function split(paneOptions, terminalOptions) {
16
+ let status;
17
+ try {
18
+ status = await getPaneStatus();
19
+ }
20
+ catch (error) {
21
+ throw createSplitError("status-check-failed", error);
22
+ }
23
+ if (!status.available) {
24
+ throw createSplitError(status.reason);
25
+ }
26
+ const horizontal = paneOptions.direction === "left" || paneOptions.direction === "right";
27
+ const before = paneOptions.direction === "left" || paneOptions.direction === "up";
28
+ const execFileAsync = promisify(execFile);
29
+ let stdout;
30
+ try {
31
+ ({ stdout } = await execFileAsync("tmux", [
32
+ "split-window",
33
+ horizontal ? "-h" : "-v",
34
+ ...(before ? ["-b"] : []),
35
+ ...(paneOptions.focus ? [] : ["-d"]),
36
+ "-P",
37
+ "-F", "#{pane_id}",
38
+ "-t", status.paneId,
39
+ "-c", terminalOptions.cwd ?? process.cwd(),
40
+ "--",
41
+ "/bin/sh", "-c", 'exec "$@"', "tmux-ts",
42
+ terminalOptions.command,
43
+ ...(terminalOptions.args ?? []),
44
+ ]));
45
+ }
46
+ catch (error) {
47
+ throw createSplitError("split-failed", error);
48
+ }
49
+ return stdout.trim();
50
+ }
@@ -0,0 +1,2 @@
1
+ import { PaneStatus } from "../domain/pane-status.js";
2
+ export declare function getPaneStatus(): Promise<PaneStatus>;
@@ -0,0 +1,33 @@
1
+ import { execFile } from "node:child_process";
2
+ import { promisify } from "node:util";
3
+ export async function getPaneStatus() {
4
+ const execFileAsync = promisify(execFile);
5
+ try {
6
+ await execFileAsync("tmux", ["-V"]);
7
+ }
8
+ catch (error) {
9
+ if (error.code === "ENOENT") {
10
+ return { available: false, reason: "not-installed" };
11
+ }
12
+ throw error;
13
+ }
14
+ const paneId = process.env.TMUX_PANE;
15
+ if (!process.env.TMUX || !paneId) {
16
+ return { available: false, reason: "not-in-tmux" };
17
+ }
18
+ try {
19
+ const { stdout } = await execFileAsync("tmux", [
20
+ "display-message",
21
+ "-p",
22
+ "-t", paneId,
23
+ "#{pane_id}",
24
+ ]);
25
+ if (stdout.trim() !== paneId) {
26
+ return { available: false, reason: "pane-unreachable" };
27
+ }
28
+ }
29
+ catch {
30
+ return { available: false, reason: "pane-unreachable" };
31
+ }
32
+ return { available: true, paneId };
33
+ }
package/package.json CHANGED
@@ -1,6 +1,25 @@
1
1
  {
2
2
  "name": "@mak-98/tmux-ts",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "private": false,
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "scripts": {
8
+ "start": "tsx src/cli.ts",
9
+ "build": "tsc",
10
+ "login": "npm login",
11
+ "deploy": "npm run build && npm publish --access public"
12
+ },
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "default": "./dist/index.js"
17
+ }
18
+ },
19
+ "files": ["dist"],
20
+ "devDependencies": {
21
+ "@types/node": "^26.6.6",
22
+ "tsx": "^4.23.15",
23
+ "typescript": "^7.0.2"
24
+ }
25
+ }