@termwire/cli 0.4.1 → 0.6.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/CHANGELOG.md +166 -0
- package/README.md +145 -8
- package/dist/agents.d.ts +24 -0
- package/dist/agents.js +54 -0
- package/dist/bin/termwire.js +2 -2
- package/dist/config-loader.d.ts +9 -0
- package/dist/config-loader.js +6 -3
- package/dist/config-writer.d.ts +17 -0
- package/dist/config-writer.js +46 -0
- package/dist/down.d.ts +40 -0
- package/dist/down.js +99 -0
- package/dist/exec.d.ts +10 -2
- package/dist/exec.js +7 -4
- package/dist/index.d.ts +7 -17
- package/dist/index.js +2 -9
- package/dist/install-prompt.d.ts +10 -0
- package/dist/install-prompt.js +100 -0
- package/dist/install.d.ts +92 -0
- package/dist/install.js +363 -0
- package/dist/layout-preview.d.ts +20 -0
- package/dist/layout-preview.js +38 -0
- package/dist/process-scan-darwin.d.ts +24 -0
- package/dist/process-scan-darwin.js +102 -0
- package/dist/process-scan-linux.d.ts +13 -0
- package/dist/process-scan-linux.js +66 -0
- package/dist/process-scan.d.ts +37 -23
- package/dist/process-scan.js +30 -131
- package/dist/program.d.ts +21 -50
- package/dist/program.js +199 -144
- package/dist/runtime.d.ts +84 -0
- package/dist/runtime.js +131 -0
- package/dist/skill.d.ts +13 -0
- package/dist/skill.js +17 -0
- package/dist/templates.d.ts +13 -0
- package/dist/templates.js +168 -0
- package/dist/workspaces.d.ts +45 -0
- package/dist/workspaces.js +116 -0
- package/dist/worktree.d.ts +15 -0
- package/dist/worktree.js +65 -7
- package/package.json +6 -3
- package/skills/termwire-open/SKILL.md +27 -0
package/dist/skill.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
import { skillName } from "./agents.js";
|
|
3
|
+
/**
|
|
4
|
+
* Marks a skill file as ours to replace. Without it we cannot tell our own older text
|
|
5
|
+
* from something the user rewrote, and an upgrade would quietly eat their edits. Deleting
|
|
6
|
+
* the line is how a user says "leave this file alone".
|
|
7
|
+
*/
|
|
8
|
+
export const skillMarker = "<!-- managed by `termwire install`; delete this line to keep your own version -->";
|
|
9
|
+
/**
|
|
10
|
+
* The skill lives as Markdown in the package rather than as a string in the source: it is
|
|
11
|
+
* editable, reviewable in a diff, and readable by anyone who wants to know what their agent
|
|
12
|
+
* was told. `dist/skill.js` and `src/skill.ts` both sit one level below the package root, so
|
|
13
|
+
* the same specifier resolves in a published install and from a checkout.
|
|
14
|
+
*/
|
|
15
|
+
export function skillSourcePath() {
|
|
16
|
+
return fileURLToPath(new URL(`../skills/${skillName}/SKILL.md`, import.meta.url));
|
|
17
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const layoutTemplateIds: readonly ["default", "three-window", "focused", "full-stack"];
|
|
2
|
+
export type LayoutTemplateId = (typeof layoutTemplateIds)[number];
|
|
3
|
+
export interface LayoutTemplate {
|
|
4
|
+
id: LayoutTemplateId;
|
|
5
|
+
title: string;
|
|
6
|
+
hint: string;
|
|
7
|
+
/** Box drawing of the windows and panes, one line per row, all the same width. */
|
|
8
|
+
preview: readonly string[];
|
|
9
|
+
contents: string;
|
|
10
|
+
}
|
|
11
|
+
/** Copies, so a caller can never mutate the registry other callers read. */
|
|
12
|
+
export declare function layoutTemplates(): LayoutTemplate[];
|
|
13
|
+
export declare function findLayoutTemplate(id: string): LayoutTemplate | undefined;
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
export const layoutTemplateIds = ["default", "three-window", "focused", "full-stack"];
|
|
2
|
+
const header = `// Termwire layout, written by \`termwire install\`.
|
|
3
|
+
// Pane commands are examples; edit them for your project.
|
|
4
|
+
`;
|
|
5
|
+
const definitions = [
|
|
6
|
+
{
|
|
7
|
+
id: "default",
|
|
8
|
+
title: "Editor and shell",
|
|
9
|
+
hint: "the built-in two-window layout",
|
|
10
|
+
preview: [
|
|
11
|
+
"editor",
|
|
12
|
+
"┌────────────────┐",
|
|
13
|
+
"│ nvim │",
|
|
14
|
+
"└────────────────┘",
|
|
15
|
+
"shell",
|
|
16
|
+
"┌────────────────┐",
|
|
17
|
+
"│ $ │",
|
|
18
|
+
"└────────────────┘",
|
|
19
|
+
],
|
|
20
|
+
contents: `${header}{
|
|
21
|
+
"version": 1,
|
|
22
|
+
"windows": [
|
|
23
|
+
{
|
|
24
|
+
"name": "editor",
|
|
25
|
+
"panes": [{ "id": "editor", "role": "editor" }],
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"name": "shell",
|
|
29
|
+
"panes": [{ "id": "shell" }],
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
}
|
|
33
|
+
`,
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
id: "three-window",
|
|
37
|
+
title: "Three windows",
|
|
38
|
+
hint: "editor, a shell for an agent, and a plain shell",
|
|
39
|
+
preview: [
|
|
40
|
+
"editor",
|
|
41
|
+
"┌────────────────┐",
|
|
42
|
+
"│ nvim │",
|
|
43
|
+
"└────────────────┘",
|
|
44
|
+
"ai",
|
|
45
|
+
"┌────────────────┐",
|
|
46
|
+
"│ $ │",
|
|
47
|
+
"└────────────────┘",
|
|
48
|
+
"shell",
|
|
49
|
+
"┌────────────────┐",
|
|
50
|
+
"│ $ │",
|
|
51
|
+
"└────────────────┘",
|
|
52
|
+
],
|
|
53
|
+
contents: `${header}{
|
|
54
|
+
"version": 1,
|
|
55
|
+
"windows": [
|
|
56
|
+
{
|
|
57
|
+
"name": "editor",
|
|
58
|
+
"panes": [{ "id": "editor", "role": "editor" }],
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
"name": "ai",
|
|
62
|
+
"panes": [{ "id": "ai" }],
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
"name": "shell",
|
|
66
|
+
"panes": [{ "id": "shell" }],
|
|
67
|
+
},
|
|
68
|
+
],
|
|
69
|
+
}
|
|
70
|
+
`,
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
id: "focused",
|
|
74
|
+
title: "Focused coding",
|
|
75
|
+
hint: "one window: editor, OpenCode, and watched tests side by side",
|
|
76
|
+
preview: [
|
|
77
|
+
"work",
|
|
78
|
+
"┌─────────┬──────┐",
|
|
79
|
+
"│ nvim │ ai │",
|
|
80
|
+
"│ ├──────┤",
|
|
81
|
+
"│ │ test │",
|
|
82
|
+
"└─────────┴──────┘",
|
|
83
|
+
" right side: 40%",
|
|
84
|
+
],
|
|
85
|
+
contents: `${header}{
|
|
86
|
+
"version": 1,
|
|
87
|
+
"windows": [
|
|
88
|
+
{
|
|
89
|
+
"name": "work",
|
|
90
|
+
"panes": [
|
|
91
|
+
{ "id": "editor", "role": "editor" },
|
|
92
|
+
{
|
|
93
|
+
"id": "ai",
|
|
94
|
+
"splitFrom": "editor",
|
|
95
|
+
"direction": "horizontal",
|
|
96
|
+
"sizePercent": 40,
|
|
97
|
+
"command": ["opencode"],
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
"id": "tests",
|
|
101
|
+
"splitFrom": "ai",
|
|
102
|
+
"direction": "vertical",
|
|
103
|
+
"sizePercent": 40,
|
|
104
|
+
"command": ["bun", "test", "--watch"],
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
}
|
|
110
|
+
`,
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
id: "full-stack",
|
|
114
|
+
title: "Full stack",
|
|
115
|
+
hint: "code with watched tests, a dev server window, and a spare shell",
|
|
116
|
+
preview: [
|
|
117
|
+
"code",
|
|
118
|
+
"┌────────────────┐",
|
|
119
|
+
"│ nvim │",
|
|
120
|
+
"├────────────────┤",
|
|
121
|
+
"│ tests (35%) │",
|
|
122
|
+
"└────────────────┘",
|
|
123
|
+
"server",
|
|
124
|
+
"┌────────────────┐",
|
|
125
|
+
"│ dev server │",
|
|
126
|
+
"└────────────────┘",
|
|
127
|
+
"shell",
|
|
128
|
+
"┌────────────────┐",
|
|
129
|
+
"│ $ │",
|
|
130
|
+
"└────────────────┘",
|
|
131
|
+
],
|
|
132
|
+
contents: `${header}{
|
|
133
|
+
"version": 1,
|
|
134
|
+
"windows": [
|
|
135
|
+
{
|
|
136
|
+
"name": "code",
|
|
137
|
+
"panes": [
|
|
138
|
+
{ "id": "editor", "role": "editor" },
|
|
139
|
+
{
|
|
140
|
+
"id": "tests",
|
|
141
|
+
"splitFrom": "editor",
|
|
142
|
+
"direction": "vertical",
|
|
143
|
+
"sizePercent": 35,
|
|
144
|
+
"command": ["bun", "test", "--watch"],
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
"name": "server",
|
|
150
|
+
"panes": [{ "id": "server", "command": ["bun", "run", "dev"] }],
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
"name": "shell",
|
|
154
|
+
"panes": [{ "id": "shell" }],
|
|
155
|
+
},
|
|
156
|
+
],
|
|
157
|
+
}
|
|
158
|
+
`,
|
|
159
|
+
},
|
|
160
|
+
];
|
|
161
|
+
/** Copies, so a caller can never mutate the registry other callers read. */
|
|
162
|
+
export function layoutTemplates() {
|
|
163
|
+
return definitions.map((template) => ({ ...template }));
|
|
164
|
+
}
|
|
165
|
+
export function findLayoutTemplate(id) {
|
|
166
|
+
const template = definitions.find((candidate) => candidate.id === id);
|
|
167
|
+
return template === undefined ? undefined : { ...template };
|
|
168
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type SessionScanner } from "./process-scan.js";
|
|
2
|
+
import type { GitExec } from "./worktree.js";
|
|
3
|
+
/** What `ls` knows about one workspace. Collected from tmux and Git, never from disk. */
|
|
4
|
+
export interface Workspace {
|
|
5
|
+
session: string;
|
|
6
|
+
attached: boolean;
|
|
7
|
+
/** The Git branch, a short sha on a detached HEAD, or `-` outside a repository. */
|
|
8
|
+
branch: string;
|
|
9
|
+
/** The session path, absolute: shortening to `~` is for the table, not for machines. */
|
|
10
|
+
directory: string;
|
|
11
|
+
/** The session outlived its directory, which is listed and marked rather than fatal. */
|
|
12
|
+
missing: boolean;
|
|
13
|
+
procs: number;
|
|
14
|
+
rssKib: number;
|
|
15
|
+
}
|
|
16
|
+
/** The fields of a tmux session this module reads, as the adapter reports them. */
|
|
17
|
+
export interface WorkspaceSession {
|
|
18
|
+
name: string;
|
|
19
|
+
attached: boolean;
|
|
20
|
+
path: string;
|
|
21
|
+
}
|
|
22
|
+
export interface WorkspacesDependencies {
|
|
23
|
+
listSessions: () => Promise<WorkspaceSession[]>;
|
|
24
|
+
showEnvironment: (session: string) => Promise<Record<string, string>>;
|
|
25
|
+
git: GitExec;
|
|
26
|
+
pathExists: (path: string) => Promise<boolean>;
|
|
27
|
+
/** One walk over the process table, asked for every workspace at once. */
|
|
28
|
+
scan: SessionScanner;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A session is a workspace when its tmux environment carries `TERMWIRE_SESSION`,
|
|
32
|
+
* never because its name looks like `<project>-<name>`: the user picks the name and
|
|
33
|
+
* `up` sets the label, so a session of their own is never mistaken for ours.
|
|
34
|
+
*/
|
|
35
|
+
export declare function collectWorkspaces({ listSessions, showEnvironment, git, pathExists, scan, }: WorkspacesDependencies): Promise<Workspace[]>;
|
|
36
|
+
/** Git answers through `-C`, so a directory that is gone fails instead of the spawn. */
|
|
37
|
+
export declare function readBranch(git: GitExec, directory: string): Promise<string>;
|
|
38
|
+
/** `$HOME` as `~`. A path that lies elsewhere, or an unknown home, is left alone. */
|
|
39
|
+
export declare function shortenHome(path: string, home: string): string;
|
|
40
|
+
/** KiB as the column shows it: K under a megabyte, M under a gigabyte, then one decimal. */
|
|
41
|
+
export declare function formatRss(kib: number): string;
|
|
42
|
+
/** The table `ls` prints, padded to its own contents. */
|
|
43
|
+
export declare function formatWorkspaceTable(workspaces: readonly Workspace[], home: string): string;
|
|
44
|
+
/** The same rows for machines: absolute directories, and `rssKib` in KiB. */
|
|
45
|
+
export declare function formatWorkspacesJson(workspaces: readonly Workspace[]): string;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { sep } from "node:path";
|
|
2
|
+
import { sessionVariable } from "./process-scan.js";
|
|
3
|
+
const unknownBranch = "-";
|
|
4
|
+
/**
|
|
5
|
+
* A session is a workspace when its tmux environment carries `TERMWIRE_SESSION`,
|
|
6
|
+
* never because its name looks like `<project>-<name>`: the user picks the name and
|
|
7
|
+
* `up` sets the label, so a session of their own is never mistaken for ours.
|
|
8
|
+
*/
|
|
9
|
+
export async function collectWorkspaces({ listSessions, showEnvironment, git, pathExists, scan, }) {
|
|
10
|
+
const sessions = await listSessions();
|
|
11
|
+
const labeled = [];
|
|
12
|
+
for (const session of sessions) {
|
|
13
|
+
const environment = await showEnvironment(session.name);
|
|
14
|
+
if (environment[sessionVariable] !== undefined)
|
|
15
|
+
labeled.push(session);
|
|
16
|
+
}
|
|
17
|
+
if (labeled.length === 0)
|
|
18
|
+
return [];
|
|
19
|
+
const usage = await measureSessions(scan, labeled.map((session) => session.name));
|
|
20
|
+
return await Promise.all(labeled.map((session) => describeWorkspace(session, usage.get(session.name) ?? noUsage, { git, pathExists })));
|
|
21
|
+
}
|
|
22
|
+
async function describeWorkspace(session, usage, { git, pathExists }) {
|
|
23
|
+
const exists = await pathExists(session.path);
|
|
24
|
+
return {
|
|
25
|
+
session: session.name,
|
|
26
|
+
attached: session.attached,
|
|
27
|
+
branch: exists ? await readBranch(git, session.path) : unknownBranch,
|
|
28
|
+
directory: session.path,
|
|
29
|
+
missing: !exists,
|
|
30
|
+
...usage,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/** Git answers through `-C`, so a directory that is gone fails instead of the spawn. */
|
|
34
|
+
export async function readBranch(git, directory) {
|
|
35
|
+
const current = await git(["git", "-C", directory, "branch", "--show-current"]);
|
|
36
|
+
if (current.exitCode !== 0)
|
|
37
|
+
return unknownBranch;
|
|
38
|
+
const branch = current.stdout.trim();
|
|
39
|
+
if (branch.length > 0)
|
|
40
|
+
return branch;
|
|
41
|
+
// No current branch: a detached HEAD, which the short sha names.
|
|
42
|
+
const head = await git(["git", "-C", directory, "rev-parse", "--short", "HEAD"]);
|
|
43
|
+
const sha = head.stdout.trim();
|
|
44
|
+
return head.exitCode === 0 && sha.length > 0 ? sha : unknownBranch;
|
|
45
|
+
}
|
|
46
|
+
const noUsage = { procs: 0, rssKib: 0 };
|
|
47
|
+
async function measureSessions(scan, sessions) {
|
|
48
|
+
const { processes, labeled } = await scan(sessions);
|
|
49
|
+
const resident = new Map(processes.map(({ pid, rss }) => [pid, rss]));
|
|
50
|
+
const usage = new Map();
|
|
51
|
+
for (const [session, pids] of labeled) {
|
|
52
|
+
let rssKib = 0;
|
|
53
|
+
for (const pid of pids)
|
|
54
|
+
rssKib += resident.get(pid) ?? 0;
|
|
55
|
+
usage.set(session, { procs: pids.length, rssKib });
|
|
56
|
+
}
|
|
57
|
+
return usage;
|
|
58
|
+
}
|
|
59
|
+
/** `$HOME` as `~`. A path that lies elsewhere, or an unknown home, is left alone. */
|
|
60
|
+
export function shortenHome(path, home) {
|
|
61
|
+
const root = home.endsWith(sep) ? home.slice(0, -sep.length) : home;
|
|
62
|
+
if (root.length === 0)
|
|
63
|
+
return path;
|
|
64
|
+
if (path === root)
|
|
65
|
+
return "~";
|
|
66
|
+
return path.startsWith(`${root}${sep}`) ? `~${path.slice(root.length)}` : path;
|
|
67
|
+
}
|
|
68
|
+
/** KiB as the column shows it: K under a megabyte, M under a gigabyte, then one decimal. */
|
|
69
|
+
export function formatRss(kib) {
|
|
70
|
+
if (kib < 1024)
|
|
71
|
+
return `${kib}K`;
|
|
72
|
+
const mib = kib / 1024;
|
|
73
|
+
if (mib < 1024)
|
|
74
|
+
return `${Math.round(mib)}M`;
|
|
75
|
+
return `${(mib / 1024).toFixed(1)}G`;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* One column: its header, how it pads, and where its value comes from. Each column
|
|
79
|
+
* carries its own cell so the header row and the data rows cannot fall out of step.
|
|
80
|
+
*/
|
|
81
|
+
const columns = [
|
|
82
|
+
{ header: "SESSION", align: "left", cell: ({ session }) => session },
|
|
83
|
+
{ header: "A", align: "left", cell: ({ attached }) => (attached ? "*" : "") },
|
|
84
|
+
{ header: "BRANCH", align: "left", cell: ({ branch }) => branch },
|
|
85
|
+
{ header: "DIRECTORY", align: "left", cell: formatDirectory },
|
|
86
|
+
{ header: "PROCS", align: "right", cell: ({ procs }) => String(procs) },
|
|
87
|
+
{ header: "RSS", align: "right", cell: ({ rssKib }) => formatRss(rssKib) },
|
|
88
|
+
];
|
|
89
|
+
/** Empty is not an error: nothing running is a normal answer. */
|
|
90
|
+
const noWorkspaces = "no termwire workspaces";
|
|
91
|
+
/** The table `ls` prints, padded to its own contents. */
|
|
92
|
+
export function formatWorkspaceTable(workspaces, home) {
|
|
93
|
+
if (workspaces.length === 0)
|
|
94
|
+
return `${noWorkspaces}\n`;
|
|
95
|
+
const rows = workspaces.map((workspace) => columns.map(({ cell }) => cell(workspace, home)));
|
|
96
|
+
const widths = columns.map(({ header }, index) => rows.reduce((width, cells) => Math.max(width, cells[index].length), header.length));
|
|
97
|
+
const lines = [columns.map(({ header }) => header), ...rows].map((cells) => formatRow(cells, widths));
|
|
98
|
+
return `${lines.join("\n")}\n`;
|
|
99
|
+
}
|
|
100
|
+
/** The same rows for machines: absolute directories, and `rssKib` in KiB. */
|
|
101
|
+
export function formatWorkspacesJson(workspaces) {
|
|
102
|
+
return `${JSON.stringify(workspaces, null, 2)}\n`;
|
|
103
|
+
}
|
|
104
|
+
function formatDirectory(workspace, home) {
|
|
105
|
+
const directory = shortenHome(workspace.directory, home);
|
|
106
|
+
return workspace.missing ? `${directory} (missing)` : directory;
|
|
107
|
+
}
|
|
108
|
+
function formatRow(cells, widths) {
|
|
109
|
+
return cells
|
|
110
|
+
.map((cell, index) => pad(cell, widths[index], columns[index].align))
|
|
111
|
+
.join(" ")
|
|
112
|
+
.trimEnd();
|
|
113
|
+
}
|
|
114
|
+
function pad(cell, width, align) {
|
|
115
|
+
return align === "right" ? cell.padStart(width) : cell.padEnd(width);
|
|
116
|
+
}
|
package/dist/worktree.d.ts
CHANGED
|
@@ -24,3 +24,18 @@ export declare function prepareWorktree(options: {
|
|
|
24
24
|
name: string;
|
|
25
25
|
branch?: string;
|
|
26
26
|
}): Promise<string>;
|
|
27
|
+
export interface WorktreeState {
|
|
28
|
+
/** The main checkout, which owns the repository and is never a workspace to remove. */
|
|
29
|
+
main: boolean;
|
|
30
|
+
/** Tracked or untracked changes. Ignored files are not changes, so a `.env` is not. */
|
|
31
|
+
dirty: boolean;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A worktree's own Git directory lives inside the common one, so the two paths are
|
|
35
|
+
* equal only in the main checkout.
|
|
36
|
+
*/
|
|
37
|
+
export declare function inspectWorktree(exec: GitExec, path: string): Promise<WorktreeState>;
|
|
38
|
+
/** Removes the worktree and its registration. The branch it held outlives it. */
|
|
39
|
+
export declare function removeWorktree(exec: GitExec, path: string, options: {
|
|
40
|
+
force: boolean;
|
|
41
|
+
}): Promise<void>;
|
package/dist/worktree.js
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
1
1
|
import { realpath as resolveRealpath } from "node:fs/promises";
|
|
2
2
|
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
3
3
|
import { sanitizeComponent } from "./identity.js";
|
|
4
|
+
/** Git's own words when the directory is outside a repository, next to exit 128. */
|
|
5
|
+
const notARepository = /not a git repository/i;
|
|
6
|
+
/** `git worktree list --porcelain` separates its records with a blank line. */
|
|
7
|
+
const recordSeparator = /\n\s*\n/;
|
|
8
|
+
/** The `worktree <path>` line that opens every record. */
|
|
9
|
+
const worktreePathLine = /^worktree (.+)$/m;
|
|
10
|
+
/** The `branch refs/heads/<name>` line, absent on a detached HEAD. */
|
|
11
|
+
const branchLine = /^branch refs\/heads\/(.+)$/m;
|
|
12
|
+
/** Flags that stand alone on a line of their own. */
|
|
13
|
+
const bareLine = /^bare$/m;
|
|
14
|
+
const detachedLine = /^detached$/m;
|
|
15
|
+
/** The same two flags, except each may carry a reason after a space. */
|
|
16
|
+
const reasonLines = {
|
|
17
|
+
locked: /^locked(?: (.*))?$/m,
|
|
18
|
+
prunable: /^prunable(?: (.*))?$/m,
|
|
19
|
+
};
|
|
4
20
|
export async function findGitRoot(exec, cwd) {
|
|
5
21
|
const result = await exec(["git", "rev-parse", "--show-toplevel"], { cwd });
|
|
6
|
-
if (result.exitCode === 128 &&
|
|
22
|
+
if (result.exitCode === 128 && notARepository.test(result.stderr)) {
|
|
7
23
|
return undefined;
|
|
8
24
|
}
|
|
9
25
|
if (result.exitCode !== 0) {
|
|
@@ -16,21 +32,21 @@ export function parseWorktreeList(output) {
|
|
|
16
32
|
if (trimmed === "") {
|
|
17
33
|
return [];
|
|
18
34
|
}
|
|
19
|
-
return trimmed.split(
|
|
20
|
-
const path = record.match(
|
|
35
|
+
return trimmed.split(recordSeparator).map((record) => {
|
|
36
|
+
const path = record.match(worktreePathLine)?.[1];
|
|
21
37
|
if (!path) {
|
|
22
38
|
throw new Error("Git worktree record is missing a path");
|
|
23
39
|
}
|
|
24
40
|
const entry = { path };
|
|
25
|
-
const branch = record.match(
|
|
41
|
+
const branch = record.match(branchLine)?.[1];
|
|
26
42
|
if (branch)
|
|
27
43
|
entry.branch = branch;
|
|
28
|
-
if (
|
|
44
|
+
if (bareLine.test(record))
|
|
29
45
|
entry.bare = true;
|
|
30
|
-
if (
|
|
46
|
+
if (detachedLine.test(record))
|
|
31
47
|
entry.detached = true;
|
|
32
48
|
for (const field of ["locked", "prunable"]) {
|
|
33
|
-
const match = record.match(
|
|
49
|
+
const match = record.match(reasonLines[field]);
|
|
34
50
|
if (match)
|
|
35
51
|
entry[field] = match[1] || true;
|
|
36
52
|
}
|
|
@@ -164,3 +180,45 @@ async function validateRegisteredWorktree(options) {
|
|
|
164
180
|
function resolveGitPath(path, cwd) {
|
|
165
181
|
return isAbsolute(path) ? path : resolve(cwd, path);
|
|
166
182
|
}
|
|
183
|
+
/**
|
|
184
|
+
* A worktree's own Git directory lives inside the common one, so the two paths are
|
|
185
|
+
* equal only in the main checkout.
|
|
186
|
+
*/
|
|
187
|
+
export async function inspectWorktree(exec, path) {
|
|
188
|
+
const directories = await exec([
|
|
189
|
+
"git",
|
|
190
|
+
"-C",
|
|
191
|
+
path,
|
|
192
|
+
"rev-parse",
|
|
193
|
+
"--absolute-git-dir",
|
|
194
|
+
"--git-common-dir",
|
|
195
|
+
]);
|
|
196
|
+
// Git refusing and Git answering with less than the two paths mean the same thing.
|
|
197
|
+
const [gitDir, commonDir] = directories.stdout.trim().split(/\r?\n/);
|
|
198
|
+
if (directories.exitCode !== 0 || !gitDir || !commonDir) {
|
|
199
|
+
throw new Error(`not a Git worktree: ${path}`);
|
|
200
|
+
}
|
|
201
|
+
const status = await exec(["git", "-C", path, "status", "--porcelain"]);
|
|
202
|
+
if (status.exitCode !== 0) {
|
|
203
|
+
throw new Error(`git status failed: ${status.stderr}`);
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
main: resolveGitPath(gitDir, path) === resolveGitPath(commonDir, path),
|
|
207
|
+
dirty: status.stdout.trim().length > 0,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
/** Removes the worktree and its registration. The branch it held outlives it. */
|
|
211
|
+
export async function removeWorktree(exec, path, options) {
|
|
212
|
+
const removed = await exec([
|
|
213
|
+
"git",
|
|
214
|
+
"-C",
|
|
215
|
+
path,
|
|
216
|
+
"worktree",
|
|
217
|
+
"remove",
|
|
218
|
+
...(options.force ? ["--force"] : []),
|
|
219
|
+
path,
|
|
220
|
+
]);
|
|
221
|
+
if (removed.exitCode !== 0) {
|
|
222
|
+
throw new Error(`git worktree remove failed: ${removed.stderr}`);
|
|
223
|
+
}
|
|
224
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@termwire/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"files": [
|
|
18
18
|
"dist",
|
|
19
|
+
"skills",
|
|
19
20
|
"README.md",
|
|
20
21
|
"LICENSE",
|
|
21
22
|
"CHANGELOG.md"
|
|
@@ -37,8 +38,10 @@
|
|
|
37
38
|
"url": "https://github.com/max-konin/termwire/issues"
|
|
38
39
|
},
|
|
39
40
|
"dependencies": {
|
|
40
|
-
"@
|
|
41
|
-
"@
|
|
41
|
+
"@clack/core": "^1.5.1",
|
|
42
|
+
"@clack/prompts": "^1.8.1",
|
|
43
|
+
"@termwire/nvim": "^0.6.0",
|
|
44
|
+
"@termwire/tmux": "^0.6.0",
|
|
42
45
|
"commander": "^15.0.0",
|
|
43
46
|
"jsonc-parser": "^3.3.1",
|
|
44
47
|
"zod": "^4.0.0"
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: termwire-open
|
|
3
|
+
description: Show the user a file in their editor instead of pasting its path. Use whenever you mention a file worth looking at — a plan, a diff, a failing test, a file you just wrote. Needs a shell created by `termwire up`.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Show a file in the user's editor
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
termwire open <path>[:<line>]
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
- A trailing `:42` selects the line, so `src/app.ts:42` opens line 42.
|
|
13
|
+
- `--line 42` does the same and leaves the path alone, which is how to open a file whose
|
|
14
|
+
name ends in a colon and digits.
|
|
15
|
+
- A relative path resolves from the working directory. The command prints the absolute
|
|
16
|
+
path it opened.
|
|
17
|
+
|
|
18
|
+
The file opens in the Neovim of the user's workspace and the editor pane takes focus, so
|
|
19
|
+
the user is looking at the file rather than at a path they have to find.
|
|
20
|
+
|
|
21
|
+
## When it does not work
|
|
22
|
+
|
|
23
|
+
It needs `TERMWIRE_SOCKET`, which `termwire up` exports into every pane of the
|
|
24
|
+
workspace. Started anywhere else, it fails with `not inside a termwire workspace` — then
|
|
25
|
+
name the path in your answer instead and carry on.
|
|
26
|
+
|
|
27
|
+
<!-- managed by `termwire install`; delete this line to keep your own version -->
|