@termwire/cli 0.5.0 → 0.7.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 +96 -0
- package/README.md +125 -4
- package/dist/click.d.ts +52 -0
- package/dist/click.js +138 -0
- package/dist/config-schema.d.ts +7 -0
- package/dist/config-schema.js +19 -0
- package/dist/config-validation.d.ts +2 -0
- package/dist/config-validation.js +14 -0
- package/dist/config-writer.d.ts +4 -2
- package/dist/config-writer.js +15 -3
- package/dist/down.d.ts +40 -0
- package/dist/down.js +99 -0
- package/dist/identity.js +4 -1
- package/dist/install-prompt.js +7 -0
- package/dist/install.d.ts +6 -0
- package/dist/install.js +101 -25
- package/dist/mouse-binding.d.ts +60 -0
- package/dist/mouse-binding.js +91 -0
- package/dist/mouse-config.d.ts +17 -0
- package/dist/mouse-config.js +63 -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 +14 -1
- package/dist/program.js +174 -38
- package/dist/reap-hook.js +1 -19
- package/dist/reap.d.ts +0 -5
- package/dist/reap.js +3 -10
- package/dist/runtime.d.ts +20 -5
- package/dist/runtime.js +44 -5
- package/dist/shell-quote.d.ts +6 -0
- package/dist/shell-quote.js +19 -0
- package/dist/state-log.d.ts +8 -0
- package/dist/state-log.js +12 -0
- package/dist/up.d.ts +5 -2
- package/dist/up.js +5 -5
- package/dist/workspaces.d.ts +45 -0
- package/dist/workspaces.js +116 -0
- package/dist/worktree.d.ts +16 -1
- package/dist/worktree.js +66 -9
- package/package.json +21 -7
package/dist/program.js
CHANGED
|
@@ -1,28 +1,25 @@
|
|
|
1
|
-
import { readFileSync } from "node:fs";
|
|
2
1
|
import { dirname, resolve as resolvePath } from "node:path";
|
|
3
2
|
import { Command, CommanderError } from "commander";
|
|
4
3
|
import { agentIds, parseAgentIds } from "./agents.js";
|
|
5
4
|
import { prepareBranch } from "./branch.js";
|
|
5
|
+
import { click } from "./click.js";
|
|
6
6
|
import { configHome, createConfigLoader, globalConfigPath } from "./config-loader.js";
|
|
7
|
-
import { resolveLayout } from "./config-validation.js";
|
|
7
|
+
import { resolveLayout, resolveMouseKey } from "./config-validation.js";
|
|
8
8
|
import { writeConfigFile } from "./config-writer.js";
|
|
9
|
+
import { down } from "./down.js";
|
|
9
10
|
import { install, noLayout } from "./install.js";
|
|
10
11
|
import { createLayout } from "./layout.js";
|
|
12
|
+
import { clickCommandName, ensureMouseBinding, workspaceOption } from "./mouse-binding.js";
|
|
11
13
|
import { open } from "./open.js";
|
|
12
|
-
import {
|
|
13
|
-
import { formatReapFailure, formatReapReport, parseTmuxServerPid,
|
|
14
|
+
import { toSingleSession } from "./process-scan.js";
|
|
15
|
+
import { formatReapFailure, formatReapReport, parseTmuxServerPid, reapSession } from "./reap.js";
|
|
14
16
|
import { ensureReapHook, reapCommandName } from "./reap-hook.js";
|
|
15
17
|
import { skillSourcePath } from "./skill.js";
|
|
18
|
+
import { stateLogPath } from "./state-log.js";
|
|
16
19
|
import { layoutTemplateIds } from "./templates.js";
|
|
17
20
|
import { up } from "./up.js";
|
|
18
|
-
import {
|
|
19
|
-
|
|
20
|
-
* Read at runtime rather than baked in at build time, the way `@termwire/mcp`
|
|
21
|
-
* reports its own version. `dist/program.js` and `src/program.ts` both sit one
|
|
22
|
-
* level below the package root, so the same specifier resolves in a published
|
|
23
|
-
* install and from a checkout.
|
|
24
|
-
*/
|
|
25
|
-
const manifest = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
21
|
+
import { collectWorkspaces, formatWorkspacesJson, formatWorkspaceTable, } from "./workspaces.js";
|
|
22
|
+
import { findGitRoot, inspectWorktree, prepareWorktree, removeWorktree } from "./worktree.js";
|
|
26
23
|
export async function removeStaleSocket(path, unlink) {
|
|
27
24
|
try {
|
|
28
25
|
await unlink(path);
|
|
@@ -40,20 +37,31 @@ export function createUp({ fs, host, git, tmux, }) {
|
|
|
40
37
|
exists: fs.exists,
|
|
41
38
|
readFile: (path) => fs.readFile(path),
|
|
42
39
|
});
|
|
43
|
-
return (request) =>
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
40
|
+
return (request) => {
|
|
41
|
+
// One read per `up`: the layout and the mouse binding both want the global config.
|
|
42
|
+
let globalConfig;
|
|
43
|
+
const loadGlobalConfig = () => {
|
|
44
|
+
globalConfig ??= loader.loadGlobal();
|
|
45
|
+
return globalConfig;
|
|
46
|
+
};
|
|
47
|
+
return up(request, {
|
|
48
|
+
cwd: host.cwd,
|
|
49
|
+
findGitRoot: (directory) => findGitRoot(git, directory),
|
|
50
|
+
prepareBranch: ({ cwd: directory, name }) => prepareBranch(git, directory, name),
|
|
51
|
+
prepareWorktree: (options) => prepareWorktree({ ...options, exec: git, pathExists: fs.exists, realpath: fs.realpath }),
|
|
52
|
+
mkdir: fs.mkdir,
|
|
53
|
+
removeFile: (path) => removeStaleSocket(path, fs.unlink),
|
|
54
|
+
tmux,
|
|
55
|
+
installServerHooks: async (session) => {
|
|
56
|
+
await installReapHook({ host, tmux });
|
|
57
|
+
await installMouseBinding({ host, tmux }, session, loadGlobalConfig);
|
|
58
|
+
},
|
|
59
|
+
loadGlobalConfig,
|
|
60
|
+
loadProjectConfig: loader.loadProject,
|
|
61
|
+
resolveLayout,
|
|
62
|
+
createLayout,
|
|
63
|
+
});
|
|
64
|
+
};
|
|
57
65
|
}
|
|
58
66
|
/**
|
|
59
67
|
* Installing the cleanup hook is best effort: a workspace is still usable when the
|
|
@@ -72,28 +80,73 @@ async function installReapHook({ host, tmux }) {
|
|
|
72
80
|
host.writeError(`termwire: session cleanup hook not installed: ${message}\n`);
|
|
73
81
|
}
|
|
74
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* Binds, moves or removes the Option+click binding to match the global config, and
|
|
85
|
+
* marks `session` as a workspace the binding may act in. Best
|
|
86
|
+
* effort like the reap hook; a key someone else bound is reported and left alone.
|
|
87
|
+
*/
|
|
88
|
+
async function installMouseBinding({ host, tmux }, session, loadGlobalConfig) {
|
|
89
|
+
try {
|
|
90
|
+
const key = resolveMouseKey(await loadGlobalConfig());
|
|
91
|
+
const outcome = await ensureMouseBinding({
|
|
92
|
+
tmux,
|
|
93
|
+
key,
|
|
94
|
+
execPath: host.execPath,
|
|
95
|
+
scriptPath: resolveScriptPath(host.scriptPath),
|
|
96
|
+
});
|
|
97
|
+
// The binding only acts in a session carrying this option, so every `up` sets it:
|
|
98
|
+
// a workspace older than the option gets it the next time it is attached to.
|
|
99
|
+
if (key !== undefined)
|
|
100
|
+
await tmux.setSessionOption(session, workspaceOption, "1");
|
|
101
|
+
if (outcome === "blocked") {
|
|
102
|
+
host.writeError(`termwire: tmux already binds ${key} to something else; ` +
|
|
103
|
+
"unbind it or choose another mouse.openPath\n");
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
108
|
+
host.writeError(`termwire: mouse binding not updated: ${message}\n`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
75
111
|
function resolveScriptPath(scriptPath) {
|
|
76
112
|
if (scriptPath === undefined) {
|
|
77
113
|
throw new Error("the termwire script path is unknown");
|
|
78
114
|
}
|
|
79
115
|
return resolvePath(scriptPath);
|
|
80
116
|
}
|
|
117
|
+
export function createLs({ fs, git, tmux, createScanner }) {
|
|
118
|
+
return () => collectWorkspaces({
|
|
119
|
+
listSessions: () => tmux.listSessions(),
|
|
120
|
+
showEnvironment: (session) => tmux.showEnvironment(session),
|
|
121
|
+
git,
|
|
122
|
+
pathExists: fs.exists,
|
|
123
|
+
// Built per run, not while the program is composed: an unsupported platform
|
|
124
|
+
// has to fail `ls` alone, not every command.
|
|
125
|
+
scan: createScanner(),
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
export function createDown({ host, git, tmux, }) {
|
|
129
|
+
return (request) => down(request, {
|
|
130
|
+
cwd: host.cwd,
|
|
131
|
+
chdir: host.chdir,
|
|
132
|
+
env: host.env,
|
|
133
|
+
findGitRoot: (directory) => findGitRoot(git, directory),
|
|
134
|
+
listSessions: () => tmux.listSessions(),
|
|
135
|
+
killSession: (session) => tmux.killSession(session),
|
|
136
|
+
inspectWorktree: (path) => inspectWorktree(git, path),
|
|
137
|
+
removeWorktree: (path, options) => removeWorktree(git, path, options),
|
|
138
|
+
});
|
|
139
|
+
}
|
|
81
140
|
export function createOpen({ host, nvim, tmux, }) {
|
|
82
141
|
return (request) => open(request, { cwd: host.cwd, env: host.env, nvim, tmux });
|
|
83
142
|
}
|
|
84
|
-
export function createReap({ fs, host,
|
|
85
|
-
const record = (line) => appendQuietly(
|
|
143
|
+
export function createReap({ fs, host, createScanner, }) {
|
|
144
|
+
const record = (line) => appendQuietly(stateLogPath({ env: host.env, homedir: host.homedir }, "reap.log"), line, fs);
|
|
86
145
|
return async (session) => {
|
|
87
146
|
try {
|
|
88
147
|
const serverPid = parseTmuxServerPid(host.env.TMUX);
|
|
89
148
|
const report = await reapSession(session, {
|
|
90
|
-
scan:
|
|
91
|
-
platform: host.platform,
|
|
92
|
-
exec,
|
|
93
|
-
uid: host.uid,
|
|
94
|
-
readdir: fs.readdir,
|
|
95
|
-
readFile: fs.readFile,
|
|
96
|
-
}),
|
|
149
|
+
scan: toSingleSession(createScanner()),
|
|
97
150
|
kill: host.kill,
|
|
98
151
|
wait: host.wait,
|
|
99
152
|
selfPid: host.pid,
|
|
@@ -109,6 +162,29 @@ export function createReap({ fs, host, exec }) {
|
|
|
109
162
|
}
|
|
110
163
|
};
|
|
111
164
|
}
|
|
165
|
+
export function createClick({ fs, host, nvim, tmux, }) {
|
|
166
|
+
return async (request) => {
|
|
167
|
+
let outcome;
|
|
168
|
+
try {
|
|
169
|
+
outcome = await click(request, {
|
|
170
|
+
showEnvironment: (session) => tmux.showEnvironment(session),
|
|
171
|
+
isFile: fs.isFile,
|
|
172
|
+
homedir: host.homedir,
|
|
173
|
+
open: async (target, context) => {
|
|
174
|
+
await open({
|
|
175
|
+
target: target.path,
|
|
176
|
+
...(target.line === undefined ? {} : { line: String(target.line) }),
|
|
177
|
+
}, { cwd: () => context.cwd, env: context.env, nvim, tmux });
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
outcome = error instanceof Error ? error.message : String(error);
|
|
183
|
+
}
|
|
184
|
+
const line = `${host.now().toISOString()} ${request.session}: ${outcome}\n`;
|
|
185
|
+
await appendQuietly(stateLogPath({ env: host.env, homedir: host.homedir }, "click.log"), line, fs);
|
|
186
|
+
};
|
|
187
|
+
}
|
|
112
188
|
/** A log that cannot be written must not hide what the reap did. */
|
|
113
189
|
async function appendQuietly(path, line, fs) {
|
|
114
190
|
try {
|
|
@@ -144,9 +220,14 @@ export function createInstall({ fs, host, git, createPrompter, }) {
|
|
|
144
220
|
export function createCommands(runtime) {
|
|
145
221
|
return {
|
|
146
222
|
up: createUp(runtime),
|
|
223
|
+
ls: createLs(runtime),
|
|
224
|
+
down: createDown(runtime),
|
|
147
225
|
open: createOpen(runtime),
|
|
148
226
|
reap: createReap(runtime),
|
|
227
|
+
click: createClick(runtime),
|
|
149
228
|
install: createInstall(runtime),
|
|
229
|
+
homedir: runtime.host.homedir,
|
|
230
|
+
version: runtime.host.version,
|
|
150
231
|
writeError: runtime.host.writeError,
|
|
151
232
|
writeOutput: runtime.host.writeOutput,
|
|
152
233
|
};
|
|
@@ -158,7 +239,7 @@ export function createProgram(dependencies) {
|
|
|
158
239
|
});
|
|
159
240
|
program.exitOverride();
|
|
160
241
|
program.showHelpAfterError();
|
|
161
|
-
program.version(
|
|
242
|
+
program.version(dependencies.version, "-V, --version", "print the installed termwire version");
|
|
162
243
|
program
|
|
163
244
|
.command("up <name>")
|
|
164
245
|
.description("create or attach to the workspace tmux session <project>-<name>")
|
|
@@ -185,6 +266,50 @@ Branch and worktree selection:
|
|
|
185
266
|
...(options.branch === undefined ? {} : { branch: options.branch }),
|
|
186
267
|
});
|
|
187
268
|
});
|
|
269
|
+
program
|
|
270
|
+
.command("ls")
|
|
271
|
+
.description("list the termwire workspaces on this machine")
|
|
272
|
+
.option("--json", "print the same rows as JSON, with absolute directories")
|
|
273
|
+
.addHelpText("after", `
|
|
274
|
+
Columns:
|
|
275
|
+
A marks an attached session, and DIRECTORY is where it was created, $HOME as ~.
|
|
276
|
+
BRANCH is the Git branch there, a short sha on a detached HEAD, - outside a repository.
|
|
277
|
+
PROCS and RSS count every process still labeled with the session, pane or not.
|
|
278
|
+
A workspace whose directory is gone is listed and marked, never fatal.
|
|
279
|
+
A session is a workspace only when its tmux environment carries TERMWIRE_SESSION.
|
|
280
|
+
`)
|
|
281
|
+
.action(async (options) => {
|
|
282
|
+
const workspaces = await dependencies.ls();
|
|
283
|
+
dependencies.writeOutput(options.json === true
|
|
284
|
+
? formatWorkspacesJson(workspaces)
|
|
285
|
+
: formatWorkspaceTable(workspaces, dependencies.homedir()));
|
|
286
|
+
});
|
|
287
|
+
program
|
|
288
|
+
.command("down [name]")
|
|
289
|
+
.description("kill a workspace tmux session, this one when [name] is omitted")
|
|
290
|
+
.option("-w, --worktree", "also remove that workspace's Git worktree")
|
|
291
|
+
.option("-f, --force", "with -w, remove a worktree that has uncommitted changes")
|
|
292
|
+
.addHelpText("after", `
|
|
293
|
+
Teardown:
|
|
294
|
+
[name] resolves exactly as it does for up, so down names the session up created.
|
|
295
|
+
Without it the target is the workspace this command runs in, from TERMWIRE_SESSION.
|
|
296
|
+
That bare form is the only one that works inside a worktree, where the Git root moves.
|
|
297
|
+
Closing the session kills what ran in it; the branch always outlives the workspace.
|
|
298
|
+
-w also removes the worktree the session sits in, refusing the main checkout.
|
|
299
|
+
A worktree with uncommitted changes, or the directory you stand in, is refused too.
|
|
300
|
+
Tearing down your own workspace removes its worktree first: the kill ends this process.
|
|
301
|
+
`)
|
|
302
|
+
.action(async (name, options) => {
|
|
303
|
+
const result = await dependencies.down({
|
|
304
|
+
...(name === undefined ? {} : { name }),
|
|
305
|
+
...(options.worktree === undefined ? {} : { worktree: true }),
|
|
306
|
+
...(options.force === undefined ? {} : { force: true }),
|
|
307
|
+
});
|
|
308
|
+
dependencies.writeOutput(`Killed session ${result.session}\n`);
|
|
309
|
+
if (result.worktree !== undefined) {
|
|
310
|
+
dependencies.writeOutput(`Removed worktree ${result.worktree}\n`);
|
|
311
|
+
}
|
|
312
|
+
});
|
|
188
313
|
program
|
|
189
314
|
.command("open <target>")
|
|
190
315
|
.description("open a file in this workspace's editor and focus it")
|
|
@@ -210,14 +335,17 @@ Target syntax:
|
|
|
210
335
|
.option("-l, --layout <name>", `layout template: ${[...layoutTemplateIds, noLayout].join(", ")}`)
|
|
211
336
|
.option("-p, --project", "write .termwire.jsonc in this repository instead of the global config")
|
|
212
337
|
.option("-a, --agents <list>", `comma-separated: ${agentIds.join(", ")}, or none`)
|
|
338
|
+
.option("--mouse", "open the path under Option+click in the editor (global config)")
|
|
339
|
+
.option("--no-mouse", "turn Option+click off again")
|
|
213
340
|
.option("-f, --force", "replace an existing config, or a skill file you own")
|
|
214
|
-
.option("-y, --yes", "never prompt: default layout,
|
|
341
|
+
.option("-y, --yes", "never prompt: default layout, no agents or mouse unless asked for")
|
|
215
342
|
.addHelpText("after", `
|
|
216
343
|
Run it before installing anything:
|
|
217
344
|
npx @termwire/cli install
|
|
218
345
|
|
|
219
346
|
Interactive by default, and silent once the flags leave nothing to ask: --yes, or
|
|
220
|
-
--layout together with --agents.
|
|
347
|
+
--layout together with --agents. Option+click is asked about along the way, and never
|
|
348
|
+
turned on by --yes alone. The skill teaches an agent to run ${"`termwire open`"}, which costs
|
|
221
349
|
no process, unlike the ${"`@termwire/mcp`"} server it replaces. It only ever adds its own file
|
|
222
350
|
to an agent's skills directory and never edits another tool's configuration.
|
|
223
351
|
`)
|
|
@@ -226,6 +354,7 @@ to an agent's skills directory and never edits another tool's configuration.
|
|
|
226
354
|
...(options.layout === undefined ? {} : { layout: options.layout }),
|
|
227
355
|
...(options.project === undefined ? {} : { project: true }),
|
|
228
356
|
...(options.agents === undefined ? {} : { agents: parseAgentIds(options.agents) }),
|
|
357
|
+
...(options.mouse === undefined ? {} : { mouse: options.mouse }),
|
|
229
358
|
...(options.force === undefined ? {} : { force: true }),
|
|
230
359
|
...(options.yes === undefined ? {} : { yes: true }),
|
|
231
360
|
});
|
|
@@ -241,6 +370,13 @@ to an agent's skills directory and never edits another tool's configuration.
|
|
|
241
370
|
.action(async (session) => {
|
|
242
371
|
await dependencies.reap(session);
|
|
243
372
|
});
|
|
373
|
+
// Run by the tmux mouse binding `up` installs, with values straight from formats.
|
|
374
|
+
program
|
|
375
|
+
.command(`${clickCommandName} <session> <pane-cwd> <root> <x> <text>`, { hidden: true })
|
|
376
|
+
.description("open the path under the mouse in the workspace editor")
|
|
377
|
+
.action(async (session, paneCwd, root, x, text) => {
|
|
378
|
+
await dependencies.click({ session, paneCwd, root, x, text });
|
|
379
|
+
});
|
|
244
380
|
return program;
|
|
245
381
|
}
|
|
246
382
|
/** The composition root's entry: one runtime in, an exit code out. */
|
package/dist/reap-hook.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { shellQuote } from "./shell-quote.js";
|
|
2
2
|
export const reapHookName = "session-closed";
|
|
3
3
|
/**
|
|
4
4
|
* Identifies our entry among the user's own `session-closed` hooks, and doubles as
|
|
@@ -56,21 +56,3 @@ export async function ensureReapHook(options) {
|
|
|
56
56
|
function hookTarget(entry) {
|
|
57
57
|
return entry.index === undefined ? entry.name : `${entry.name}[${entry.index}]`;
|
|
58
58
|
}
|
|
59
|
-
/**
|
|
60
|
-
* The path is read by two parsers: tmux, which expands `#{...}` and backslash
|
|
61
|
-
* escapes inside double quotes, and then `sh`. Quoting characters would have to
|
|
62
|
-
* survive both, so they are rejected instead of escaped twice.
|
|
63
|
-
*/
|
|
64
|
-
function shellQuote(value, label) {
|
|
65
|
-
if (value.trim().length === 0) {
|
|
66
|
-
throw new Error(`${label} must not be empty`);
|
|
67
|
-
}
|
|
68
|
-
if (!isAbsolute(value)) {
|
|
69
|
-
throw new Error(`${label} must be absolute: ${value}`);
|
|
70
|
-
}
|
|
71
|
-
// biome-ignore lint/suspicious/noControlCharactersInRegex: control characters break both parsers
|
|
72
|
-
if (/['"\\#\n\r\t\u0000-\u001f]/.test(value)) {
|
|
73
|
-
throw new Error(`${label} contains characters a tmux hook cannot carry: ${value}`);
|
|
74
|
-
}
|
|
75
|
-
return `'${value}'`;
|
|
76
|
-
}
|
package/dist/reap.d.ts
CHANGED
|
@@ -40,8 +40,3 @@ export declare function parseTmuxServerPid(tmux: string | undefined): number | u
|
|
|
40
40
|
export declare function reapSession(session: string, dependencies: ReapDependencies): Promise<ReapReport>;
|
|
41
41
|
export declare function formatReapReport(report: ReapReport, timestamp: string): string;
|
|
42
42
|
export declare function formatReapFailure(session: string, error: unknown, timestamp: string): string;
|
|
43
|
-
/** `run-shell` output goes nowhere, so every reap records what it did here. */
|
|
44
|
-
export declare function reapLogPath(dependencies: {
|
|
45
|
-
env: Record<string, string | undefined>;
|
|
46
|
-
homedir: () => string;
|
|
47
|
-
}): string;
|
package/dist/reap.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { isAbsolute, join } from "node:path";
|
|
2
1
|
export const defaultGraceMs = 2000;
|
|
3
2
|
export const defaultSettleMs = 250;
|
|
4
3
|
const killRounds = 2;
|
|
@@ -100,17 +99,11 @@ export function formatReapReport(report, timestamp) {
|
|
|
100
99
|
];
|
|
101
100
|
return `${timestamp} ${fields.join(" ")}\n`;
|
|
102
101
|
}
|
|
102
|
+
/** A log record is one line, so a multi-line error message is flattened into it. */
|
|
103
|
+
const whitespaceRun = /\s+/g;
|
|
103
104
|
export function formatReapFailure(session, error, timestamp) {
|
|
104
105
|
const message = error instanceof Error ? error.message : String(error);
|
|
105
|
-
return `${timestamp} session=${session} error=${message.replace(
|
|
106
|
-
}
|
|
107
|
-
/** `run-shell` output goes nowhere, so every reap records what it did here. */
|
|
108
|
-
export function reapLogPath(dependencies) {
|
|
109
|
-
const xdgStateHome = dependencies.env.XDG_STATE_HOME;
|
|
110
|
-
const stateHome = xdgStateHome && isAbsolute(xdgStateHome)
|
|
111
|
-
? xdgStateHome
|
|
112
|
-
: join(dependencies.homedir(), ".local", "state");
|
|
113
|
-
return join(stateHome, "termwire", "reap.log");
|
|
106
|
+
return `${timestamp} session=${session} error=${message.replace(whitespaceRun, " ")}\n`;
|
|
114
107
|
}
|
|
115
108
|
function formatPids(pids) {
|
|
116
109
|
return pids.length === 0 ? "-" : pids.join(",");
|
package/dist/runtime.d.ts
CHANGED
|
@@ -2,12 +2,14 @@ import { createNvim } from "@termwire/nvim";
|
|
|
2
2
|
import { createTmux } from "@termwire/tmux";
|
|
3
3
|
import { type Exec } from "./exec.js";
|
|
4
4
|
import type { InstallPrompter } from "./install.js";
|
|
5
|
+
import type { SessionScanner } from "./process-scan.js";
|
|
5
6
|
import type { KillOutcome, ReapSignal } from "./reap.js";
|
|
6
7
|
import type { GitExec } from "./worktree.js";
|
|
7
8
|
export interface RuntimeFileSystem {
|
|
8
9
|
readFile: (path: string) => Promise<string>;
|
|
9
|
-
readdir: (path: string) => Promise<string[]>;
|
|
10
10
|
exists: (path: string) => Promise<boolean>;
|
|
11
|
+
/** False for anything that cannot be stat'ed, not only for a missing path. */
|
|
12
|
+
isFile: (path: string) => Promise<boolean>;
|
|
11
13
|
mkdir: (path: string) => Promise<void>;
|
|
12
14
|
/** `flag: "wx"` fails on an existing file instead of replacing it. */
|
|
13
15
|
writeFile: (path: string, contents: string, options?: {
|
|
@@ -16,17 +18,20 @@ export interface RuntimeFileSystem {
|
|
|
16
18
|
rename: (from: string, to: string) => Promise<void>;
|
|
17
19
|
appendFile: (path: string, contents: string) => Promise<void>;
|
|
18
20
|
unlink: (path: string) => Promise<void>;
|
|
21
|
+
realpath: (path: string) => Promise<string>;
|
|
19
22
|
}
|
|
20
23
|
export interface RuntimeHost {
|
|
21
24
|
env: Record<string, string | undefined>;
|
|
22
25
|
cwd: () => string;
|
|
26
|
+
/** Leaves a directory before it is removed: a spawn from a deleted cwd fails. */
|
|
27
|
+
chdir: (path: string) => void;
|
|
23
28
|
homedir: () => string;
|
|
24
|
-
platform: string;
|
|
25
|
-
uid: number;
|
|
26
29
|
pid: number;
|
|
27
30
|
execPath: string;
|
|
28
31
|
/** The launched script, which the tmux hook has to name by absolute path. */
|
|
29
32
|
scriptPath: string | undefined;
|
|
33
|
+
/** The installed `@termwire/cli` version, for `--version`. */
|
|
34
|
+
version: string;
|
|
30
35
|
now: () => Date;
|
|
31
36
|
/** A unique name for a temporary file, so two writers cannot collide. */
|
|
32
37
|
randomId: () => string;
|
|
@@ -50,9 +55,12 @@ export interface RuntimeHost {
|
|
|
50
55
|
export interface CliRuntime {
|
|
51
56
|
fs: RuntimeFileSystem;
|
|
52
57
|
host: RuntimeHost;
|
|
53
|
-
/** Used for anything without an adapter, currently the process scanner. */
|
|
54
|
-
exec: Exec;
|
|
55
58
|
git: GitExec;
|
|
59
|
+
/**
|
|
60
|
+
* Built on demand, so a platform that cannot be scanned fails the command that
|
|
61
|
+
* scans rather than every command, `--help` included.
|
|
62
|
+
*/
|
|
63
|
+
createScanner: () => SessionScanner;
|
|
56
64
|
tmux: ReturnType<typeof createTmux>;
|
|
57
65
|
nvim: ReturnType<typeof createNvim>;
|
|
58
66
|
/** Built on demand, so a command that never asks anything loads no terminal UI. */
|
|
@@ -69,6 +77,13 @@ export declare function createAdapters({ exec, env, }: {
|
|
|
69
77
|
}): Pick<CliRuntime, "tmux" | "nvim">;
|
|
70
78
|
/** The one place where the real world is bound. Nothing else imports `node:*`. */
|
|
71
79
|
export declare function createNodeRuntime(): CliRuntime;
|
|
80
|
+
/**
|
|
81
|
+
* The one place that knows which platforms can be scanned. A platform module takes
|
|
82
|
+
* only its own dependencies, which a registry keyed by platform could not express:
|
|
83
|
+
* every entry would have to accept the union of what all of them need. So a new
|
|
84
|
+
* platform is a new module plus a branch here.
|
|
85
|
+
*/
|
|
86
|
+
export declare function createPlatformScanner(platform: string): SessionScanner;
|
|
72
87
|
export declare function fileExists(path: string): Promise<boolean>;
|
|
73
88
|
export declare function killProcess(pid: number, signal: ReapSignal): KillOutcome;
|
|
74
89
|
export declare const executeGit: GitExec;
|
package/dist/runtime.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
2
|
import { randomUUID } from "node:crypto";
|
|
3
|
-
import {
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
4
|
+
import { appendFile as appendFileToDisk, mkdir as mkdirDirectory, readdir as readDirectory, readFile as readFileFromDisk, rename as renameFile, realpath as resolveRealpath, stat, unlink as unlinkFile, writeFile as writeFileToDisk, } from "node:fs/promises";
|
|
4
5
|
import { homedir as getHomeDirectory } from "node:os";
|
|
5
6
|
import { createNvim } from "@termwire/nvim";
|
|
6
7
|
import { createTmux } from "@termwire/tmux";
|
|
7
8
|
import { spawnCapture } from "./exec.js";
|
|
9
|
+
import { createDarwinScanner } from "./process-scan-darwin.js";
|
|
10
|
+
import { createLinuxScanner } from "./process-scan-linux.js";
|
|
8
11
|
/**
|
|
9
12
|
* Binds both adapters to one exec and one environment. The exec has to forward
|
|
10
13
|
* `ExecOptions`: `attach` asks for an inherited terminal, and tmux refuses to attach
|
|
@@ -19,8 +22,15 @@ export function createNodeRuntime() {
|
|
|
19
22
|
return {
|
|
20
23
|
fs: {
|
|
21
24
|
readFile: (path) => readFileFromDisk(path, "utf8"),
|
|
22
|
-
readdir: readDirectory,
|
|
23
25
|
exists: fileExists,
|
|
26
|
+
isFile: async (path) => {
|
|
27
|
+
try {
|
|
28
|
+
return (await stat(path)).isFile();
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
},
|
|
24
34
|
mkdir: async (path) => {
|
|
25
35
|
await mkdirDirectory(path, { recursive: true });
|
|
26
36
|
},
|
|
@@ -28,16 +38,17 @@ export function createNodeRuntime() {
|
|
|
28
38
|
rename: renameFile,
|
|
29
39
|
appendFile: appendFileToDisk,
|
|
30
40
|
unlink: unlinkFile,
|
|
41
|
+
realpath: resolveRealpath,
|
|
31
42
|
},
|
|
32
43
|
host: {
|
|
33
44
|
env,
|
|
34
45
|
cwd: () => process.cwd(),
|
|
46
|
+
chdir: (path) => process.chdir(path),
|
|
35
47
|
homedir: getHomeDirectory,
|
|
36
|
-
platform: process.platform,
|
|
37
|
-
uid: process.getuid?.() ?? 0,
|
|
38
48
|
pid: process.pid,
|
|
39
49
|
execPath: process.execPath,
|
|
40
50
|
scriptPath: process.argv[1],
|
|
51
|
+
version: readPackageVersion(),
|
|
41
52
|
now: () => new Date(),
|
|
42
53
|
randomId: randomUUID,
|
|
43
54
|
isTerminal: process.stdin.isTTY === true && process.stdout.isTTY === true,
|
|
@@ -46,8 +57,8 @@ export function createNodeRuntime() {
|
|
|
46
57
|
writeOutput: (message) => process.stdout.write(message),
|
|
47
58
|
writeError: (message) => process.stderr.write(message),
|
|
48
59
|
},
|
|
49
|
-
exec: spawnCapture,
|
|
50
60
|
git: executeGit,
|
|
61
|
+
createScanner: () => createPlatformScanner(process.platform),
|
|
51
62
|
...createAdapters({ exec: spawnCapture, env }),
|
|
52
63
|
// Imported here and not at the top, so `up` and `open` never load the prompts.
|
|
53
64
|
createPrompter: async () => (await import("./install-prompt.js")).createClackPrompter({
|
|
@@ -55,6 +66,34 @@ export function createNodeRuntime() {
|
|
|
55
66
|
}),
|
|
56
67
|
};
|
|
57
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Read at runtime rather than baked in at build time, the way `@termwire/mcp`
|
|
71
|
+
* reports its own version. `dist/runtime.js` and `src/runtime.ts` both sit one
|
|
72
|
+
* level below the package root, so the same specifier resolves in a published
|
|
73
|
+
* install and from a checkout.
|
|
74
|
+
*/
|
|
75
|
+
function readPackageVersion() {
|
|
76
|
+
const manifest = readFileSync(new URL("../package.json", import.meta.url), "utf8");
|
|
77
|
+
return JSON.parse(manifest).version;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The one place that knows which platforms can be scanned. A platform module takes
|
|
81
|
+
* only its own dependencies, which a registry keyed by platform could not express:
|
|
82
|
+
* every entry would have to accept the union of what all of them need. So a new
|
|
83
|
+
* platform is a new module plus a branch here.
|
|
84
|
+
*/
|
|
85
|
+
export function createPlatformScanner(platform) {
|
|
86
|
+
if (platform === "darwin") {
|
|
87
|
+
return createDarwinScanner({ exec: spawnCapture, uid: process.getuid?.() ?? 0 });
|
|
88
|
+
}
|
|
89
|
+
if (platform === "linux") {
|
|
90
|
+
return createLinuxScanner({
|
|
91
|
+
readdir: readDirectory,
|
|
92
|
+
readFile: (path) => readFileFromDisk(path, "utf8"),
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
throw new Error(`process scanning is not supported on ${platform}`);
|
|
96
|
+
}
|
|
58
97
|
export async function fileExists(path) {
|
|
59
98
|
try {
|
|
60
99
|
await stat(path);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The path is read by two parsers: tmux, which expands `#{...}` and backslash
|
|
3
|
+
* escapes inside double quotes, and then `sh`. Quoting characters would have to
|
|
4
|
+
* survive both, so they are rejected instead of escaped twice.
|
|
5
|
+
*/
|
|
6
|
+
export declare function shellQuote(value: string, label: string): string;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { isAbsolute } from "node:path";
|
|
2
|
+
/**
|
|
3
|
+
* The path is read by two parsers: tmux, which expands `#{...}` and backslash
|
|
4
|
+
* escapes inside double quotes, and then `sh`. Quoting characters would have to
|
|
5
|
+
* survive both, so they are rejected instead of escaped twice.
|
|
6
|
+
*/
|
|
7
|
+
export function shellQuote(value, label) {
|
|
8
|
+
if (value.trim().length === 0) {
|
|
9
|
+
throw new Error(`${label} must not be empty`);
|
|
10
|
+
}
|
|
11
|
+
if (!isAbsolute(value)) {
|
|
12
|
+
throw new Error(`${label} must be absolute: ${value}`);
|
|
13
|
+
}
|
|
14
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: control characters break both parsers
|
|
15
|
+
if (/['"\\#\n\r\t\u0000-\u001f]/.test(value)) {
|
|
16
|
+
throw new Error(`${label} contains characters a tmux command cannot carry: ${value}`);
|
|
17
|
+
}
|
|
18
|
+
return `'${value}'`;
|
|
19
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a command run by tmux records what it did: `run-shell` output goes nowhere.
|
|
3
|
+
* The path is resolved the way the tmux server's environment sees it.
|
|
4
|
+
*/
|
|
5
|
+
export declare function stateLogPath(dependencies: {
|
|
6
|
+
env: Record<string, string | undefined>;
|
|
7
|
+
homedir: () => string;
|
|
8
|
+
}, file: string): string;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { isAbsolute, join } from "node:path";
|
|
2
|
+
/**
|
|
3
|
+
* Where a command run by tmux records what it did: `run-shell` output goes nowhere.
|
|
4
|
+
* The path is resolved the way the tmux server's environment sees it.
|
|
5
|
+
*/
|
|
6
|
+
export function stateLogPath(dependencies, file) {
|
|
7
|
+
const xdgStateHome = dependencies.env.XDG_STATE_HOME;
|
|
8
|
+
const stateHome = xdgStateHome && isAbsolute(xdgStateHome)
|
|
9
|
+
? xdgStateHome
|
|
10
|
+
: join(dependencies.homedir(), ".local", "state");
|
|
11
|
+
return join(stateHome, "termwire", file);
|
|
12
|
+
}
|
package/dist/up.d.ts
CHANGED
|
@@ -23,8 +23,11 @@ export interface UpDependencies {
|
|
|
23
23
|
mkdir: (path: string) => Promise<void>;
|
|
24
24
|
removeFile: (path: string) => Promise<void>;
|
|
25
25
|
tmux: ReturnType<typeof createTmux>;
|
|
26
|
-
/**
|
|
27
|
-
|
|
26
|
+
/**
|
|
27
|
+
* The server-wide reap hook and mouse binding, and the mark on `session` that lets
|
|
28
|
+
* the binding act there. Best effort: a workspace is worth having without them.
|
|
29
|
+
*/
|
|
30
|
+
installServerHooks: (session: string) => Promise<void>;
|
|
28
31
|
loadGlobalConfig: () => Promise<LoadedConfig | undefined>;
|
|
29
32
|
loadProjectConfig: (gitRoot: string) => Promise<LoadedConfig | undefined>;
|
|
30
33
|
resolveLayout: (globalConfig: LoadedConfig | undefined, projectConfig: LoadedConfig | undefined) => LayoutConfig;
|
package/dist/up.js
CHANGED
|
@@ -10,7 +10,7 @@ export async function up(request, dependencies) {
|
|
|
10
10
|
const gitRoot = await dependencies.findGitRoot(cwd);
|
|
11
11
|
const identity = createIdentity({ cwd, gitRoot, name: request.name });
|
|
12
12
|
if (await dependencies.tmux.hasSession(identity.session)) {
|
|
13
|
-
await
|
|
13
|
+
await bestEffortInstallServerHooks(dependencies, identity.session);
|
|
14
14
|
await dependencies.tmux.attach(identity.session);
|
|
15
15
|
return;
|
|
16
16
|
}
|
|
@@ -24,7 +24,7 @@ export async function up(request, dependencies) {
|
|
|
24
24
|
cwd: workspace,
|
|
25
25
|
});
|
|
26
26
|
try {
|
|
27
|
-
await
|
|
27
|
+
await bestEffortInstallServerHooks(dependencies, identity.session);
|
|
28
28
|
await dependencies.tmux.setSessionTitle(identity.session);
|
|
29
29
|
await dependencies.createLayout({
|
|
30
30
|
tmux: dependencies.tmux,
|
|
@@ -68,12 +68,12 @@ async function resolveWorkspace(request, dependencies, identity, gitRoot, cwd) {
|
|
|
68
68
|
branch: request.branch ?? name,
|
|
69
69
|
});
|
|
70
70
|
}
|
|
71
|
-
async function
|
|
71
|
+
async function bestEffortInstallServerHooks(dependencies, session) {
|
|
72
72
|
try {
|
|
73
|
-
await dependencies.
|
|
73
|
+
await dependencies.installServerHooks(session);
|
|
74
74
|
}
|
|
75
75
|
catch {
|
|
76
|
-
// Never trade the workspace for
|
|
76
|
+
// Never trade the workspace for a hook: an implementation that
|
|
77
77
|
// reports failures does so itself, before throwing.
|
|
78
78
|
}
|
|
79
79
|
}
|