@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/program.js
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
|
-
import { spawn } from "node:child_process";
|
|
2
1
|
import { readFileSync } from "node:fs";
|
|
3
|
-
import { appendFile as appendFileToDisk, mkdir as mkdirDirectory, readdir as readDirectory, readFile as readFileFromDisk, stat, unlink as unlinkFile, } from "node:fs/promises";
|
|
4
|
-
import { homedir as getHomeDirectory } from "node:os";
|
|
5
2
|
import { dirname, resolve as resolvePath } from "node:path";
|
|
6
|
-
import { createNvim } from "@termwire/nvim";
|
|
7
|
-
import { createTmux } from "@termwire/tmux";
|
|
8
3
|
import { Command, CommanderError } from "commander";
|
|
4
|
+
import { agentIds, parseAgentIds } from "./agents.js";
|
|
9
5
|
import { prepareBranch } from "./branch.js";
|
|
10
|
-
import { createConfigLoader } from "./config-loader.js";
|
|
6
|
+
import { configHome, createConfigLoader, globalConfigPath } from "./config-loader.js";
|
|
11
7
|
import { resolveLayout } from "./config-validation.js";
|
|
12
|
-
import {
|
|
8
|
+
import { writeConfigFile } from "./config-writer.js";
|
|
9
|
+
import { down } from "./down.js";
|
|
10
|
+
import { install, noLayout } from "./install.js";
|
|
13
11
|
import { createLayout } from "./layout.js";
|
|
14
12
|
import { open } from "./open.js";
|
|
15
|
-
import {
|
|
13
|
+
import { toSingleSession } from "./process-scan.js";
|
|
16
14
|
import { formatReapFailure, formatReapReport, parseTmuxServerPid, reapLogPath, reapSession, } from "./reap.js";
|
|
17
15
|
import { ensureReapHook, reapCommandName } from "./reap-hook.js";
|
|
16
|
+
import { skillSourcePath } from "./skill.js";
|
|
17
|
+
import { layoutTemplateIds } from "./templates.js";
|
|
18
18
|
import { up } from "./up.js";
|
|
19
|
-
import {
|
|
19
|
+
import { collectWorkspaces, formatWorkspacesJson, formatWorkspaceTable, } from "./workspaces.js";
|
|
20
|
+
import { findGitRoot, inspectWorktree, prepareWorktree, removeWorktree } from "./worktree.js";
|
|
20
21
|
/**
|
|
21
22
|
* Read at runtime rather than baked in at build time, the way `@termwire/mcp`
|
|
22
23
|
* reports its own version. `dist/program.js` and `src/program.ts` both sit one
|
|
@@ -24,32 +25,6 @@ import { findGitRoot, prepareWorktree } from "./worktree.js";
|
|
|
24
25
|
* install and from a checkout.
|
|
25
26
|
*/
|
|
26
27
|
const manifest = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
27
|
-
export const executeGit = async (argv, options) => {
|
|
28
|
-
const [command, ...args] = argv;
|
|
29
|
-
if (command === undefined) {
|
|
30
|
-
throw new Error("git execution failed: empty command");
|
|
31
|
-
}
|
|
32
|
-
return await new Promise((resolve, reject) => {
|
|
33
|
-
const child = spawn(command, args, {
|
|
34
|
-
cwd: options?.cwd,
|
|
35
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
36
|
-
});
|
|
37
|
-
let stdout = "";
|
|
38
|
-
let stderr = "";
|
|
39
|
-
child.stdout.setEncoding("utf8").on("data", (chunk) => {
|
|
40
|
-
stdout += chunk;
|
|
41
|
-
});
|
|
42
|
-
child.stderr.setEncoding("utf8").on("data", (chunk) => {
|
|
43
|
-
stderr += chunk;
|
|
44
|
-
});
|
|
45
|
-
child.once("error", (cause) => {
|
|
46
|
-
reject(new Error(`git execution failed: ${argv.join(" ")}`, { cause }));
|
|
47
|
-
});
|
|
48
|
-
child.once("close", (code) => {
|
|
49
|
-
resolve({ exitCode: code ?? 1, stdout, stderr });
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
};
|
|
53
28
|
export async function removeStaleSocket(path, unlink) {
|
|
54
29
|
try {
|
|
55
30
|
await unlink(path);
|
|
@@ -60,46 +35,24 @@ export async function removeStaleSocket(path, unlink) {
|
|
|
60
35
|
}
|
|
61
36
|
}
|
|
62
37
|
}
|
|
63
|
-
export function
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const mkdir = dependencies.mkdir ?? mkdirDirectory;
|
|
71
|
-
const pathExists = dependencies.pathExists ??
|
|
72
|
-
(async (path) => {
|
|
73
|
-
try {
|
|
74
|
-
await stat(path);
|
|
75
|
-
return true;
|
|
76
|
-
}
|
|
77
|
-
catch (error) {
|
|
78
|
-
if (error.code === "ENOENT")
|
|
79
|
-
return false;
|
|
80
|
-
throw error;
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
const unlink = dependencies.unlink ?? unlinkFile;
|
|
84
|
-
const execPath = dependencies.execPath ?? process.execPath;
|
|
85
|
-
const scriptPath = "scriptPath" in dependencies ? dependencies.scriptPath : process.argv[1];
|
|
86
|
-
const warn = dependencies.warn ?? ((message) => process.stderr.write(message));
|
|
87
|
-
const loader = createConfigLoader({ env, homedir, exists: pathExists, readFile });
|
|
88
|
-
const loadGlobalConfig = loader.loadGlobal;
|
|
89
|
-
const loadProjectConfig = loader.loadProject;
|
|
38
|
+
export function createUp({ fs, host, git, tmux, }) {
|
|
39
|
+
const loader = createConfigLoader({
|
|
40
|
+
env: host.env,
|
|
41
|
+
homedir: host.homedir,
|
|
42
|
+
exists: fs.exists,
|
|
43
|
+
readFile: (path) => fs.readFile(path),
|
|
44
|
+
});
|
|
90
45
|
return (request) => up(request, {
|
|
91
|
-
cwd,
|
|
92
|
-
findGitRoot: (directory) => findGitRoot(
|
|
93
|
-
prepareBranch: ({ cwd: directory, name }) => prepareBranch(
|
|
94
|
-
prepareWorktree: (options) => prepareWorktree({ ...options, exec:
|
|
95
|
-
mkdir:
|
|
96
|
-
|
|
97
|
-
},
|
|
98
|
-
removeFile: (path) => removeStaleSocket(path, unlink),
|
|
46
|
+
cwd: host.cwd,
|
|
47
|
+
findGitRoot: (directory) => findGitRoot(git, directory),
|
|
48
|
+
prepareBranch: ({ cwd: directory, name }) => prepareBranch(git, directory, name),
|
|
49
|
+
prepareWorktree: (options) => prepareWorktree({ ...options, exec: git, pathExists: fs.exists }),
|
|
50
|
+
mkdir: fs.mkdir,
|
|
51
|
+
removeFile: (path) => removeStaleSocket(path, fs.unlink),
|
|
99
52
|
tmux,
|
|
100
|
-
installReapHook: () => installReapHook({
|
|
101
|
-
loadGlobalConfig,
|
|
102
|
-
loadProjectConfig,
|
|
53
|
+
installReapHook: () => installReapHook({ host, tmux }),
|
|
54
|
+
loadGlobalConfig: loader.loadGlobal,
|
|
55
|
+
loadProjectConfig: loader.loadProject,
|
|
103
56
|
resolveLayout,
|
|
104
57
|
createLayout,
|
|
105
58
|
});
|
|
@@ -108,90 +61,117 @@ export function createRuntimeUp(dependencies = {}) {
|
|
|
108
61
|
* Installing the cleanup hook is best effort: a workspace is still usable when the
|
|
109
62
|
* tmux server refuses the hook, so `up` only warns.
|
|
110
63
|
*/
|
|
111
|
-
async function installReapHook(
|
|
64
|
+
async function installReapHook({ host, tmux }) {
|
|
112
65
|
try {
|
|
113
|
-
if (options.scriptPath === undefined) {
|
|
114
|
-
throw new Error("the termwire script path is unknown");
|
|
115
|
-
}
|
|
116
66
|
await ensureReapHook({
|
|
117
|
-
tmux
|
|
118
|
-
execPath:
|
|
119
|
-
scriptPath:
|
|
67
|
+
tmux,
|
|
68
|
+
execPath: host.execPath,
|
|
69
|
+
scriptPath: resolveScriptPath(host.scriptPath),
|
|
120
70
|
});
|
|
121
71
|
}
|
|
122
72
|
catch (error) {
|
|
123
73
|
const message = error instanceof Error ? error.message : String(error);
|
|
124
|
-
|
|
74
|
+
host.writeError(`termwire: session cleanup hook not installed: ${message}\n`);
|
|
125
75
|
}
|
|
126
76
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
return "signalled";
|
|
131
|
-
}
|
|
132
|
-
catch (error) {
|
|
133
|
-
const code = error.code;
|
|
134
|
-
if (code === "ESRCH")
|
|
135
|
-
return "gone";
|
|
136
|
-
if (code === "EPERM")
|
|
137
|
-
return "denied";
|
|
138
|
-
throw error;
|
|
77
|
+
function resolveScriptPath(scriptPath) {
|
|
78
|
+
if (scriptPath === undefined) {
|
|
79
|
+
throw new Error("the termwire script path is unknown");
|
|
139
80
|
}
|
|
81
|
+
return resolvePath(scriptPath);
|
|
140
82
|
}
|
|
141
|
-
export function
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
};
|
|
83
|
+
export function createLs({ fs, git, tmux, createScanner }) {
|
|
84
|
+
return () => collectWorkspaces({
|
|
85
|
+
listSessions: () => tmux.listSessions(),
|
|
86
|
+
showEnvironment: (session) => tmux.showEnvironment(session),
|
|
87
|
+
git,
|
|
88
|
+
pathExists: fs.exists,
|
|
89
|
+
// Built per run, not while the program is composed: an unsupported platform
|
|
90
|
+
// has to fail `ls` alone, not every command.
|
|
91
|
+
scan: createScanner(),
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
export function createDown({ host, git, tmux, }) {
|
|
95
|
+
return (request) => down(request, {
|
|
96
|
+
cwd: host.cwd,
|
|
97
|
+
chdir: host.chdir,
|
|
98
|
+
env: host.env,
|
|
99
|
+
findGitRoot: (directory) => findGitRoot(git, directory),
|
|
100
|
+
listSessions: () => tmux.listSessions(),
|
|
101
|
+
killSession: (session) => tmux.killSession(session),
|
|
102
|
+
inspectWorktree: (path) => inspectWorktree(git, path),
|
|
103
|
+
removeWorktree: (path, options) => removeWorktree(git, path, options),
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
export function createOpen({ host, nvim, tmux, }) {
|
|
107
|
+
return (request) => open(request, { cwd: host.cwd, env: host.env, nvim, tmux });
|
|
108
|
+
}
|
|
109
|
+
export function createReap({ fs, host, createScanner, }) {
|
|
110
|
+
const record = (line) => appendQuietly(reapLogPath({ env: host.env, homedir: host.homedir }), line, fs);
|
|
169
111
|
return async (session) => {
|
|
170
112
|
try {
|
|
171
|
-
const serverPid = parseTmuxServerPid(env.TMUX);
|
|
113
|
+
const serverPid = parseTmuxServerPid(host.env.TMUX);
|
|
172
114
|
const report = await reapSession(session, {
|
|
173
|
-
scan:
|
|
174
|
-
kill,
|
|
175
|
-
wait,
|
|
176
|
-
selfPid: pid,
|
|
115
|
+
scan: toSingleSession(createScanner()),
|
|
116
|
+
kill: host.kill,
|
|
117
|
+
wait: host.wait,
|
|
118
|
+
selfPid: host.pid,
|
|
177
119
|
// The reap inherits the tmux server's environment, TMUX included, which is
|
|
178
120
|
// the only way back to the server it must not kill.
|
|
179
121
|
...(serverPid === undefined ? {} : { serverPid }),
|
|
180
122
|
});
|
|
181
|
-
await record(formatReapReport(report, now().toISOString()));
|
|
123
|
+
await record(formatReapReport(report, host.now().toISOString()));
|
|
182
124
|
}
|
|
183
125
|
catch (error) {
|
|
184
|
-
await record(formatReapFailure(session, error, now().toISOString()));
|
|
126
|
+
await record(formatReapFailure(session, error, host.now().toISOString()));
|
|
185
127
|
throw error;
|
|
186
128
|
}
|
|
187
129
|
};
|
|
188
130
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
131
|
+
/** A log that cannot be written must not hide what the reap did. */
|
|
132
|
+
async function appendQuietly(path, line, fs) {
|
|
133
|
+
try {
|
|
134
|
+
await fs.mkdir(dirname(path));
|
|
135
|
+
await fs.appendFile(path, line);
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
// Nothing left to report it to.
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
export function createInstall({ fs, host, git, createPrompter, }) {
|
|
142
|
+
return async (request) => {
|
|
143
|
+
const prompt = host.isTerminal && request.yes !== true ? await createPrompter() : undefined;
|
|
144
|
+
return await install(request, {
|
|
145
|
+
cwd: host.cwd,
|
|
146
|
+
findGitRoot: (directory) => findGitRoot(git, directory),
|
|
147
|
+
globalConfigPath: () => globalConfigPath({ env: host.env, homedir: host.homedir }),
|
|
148
|
+
agentPaths: {
|
|
149
|
+
home: host.homedir(),
|
|
150
|
+
configHome: configHome({ env: host.env, homedir: host.homedir }),
|
|
151
|
+
},
|
|
152
|
+
skillSource: skillSourcePath(),
|
|
153
|
+
pathExists: fs.exists,
|
|
154
|
+
readFile: fs.readFile,
|
|
155
|
+
writeConfig: (options) => writeConfigFile(options, { ...fs, suffix: host.randomId }),
|
|
156
|
+
write: host.writeOutput,
|
|
157
|
+
isTerminal: host.isTerminal,
|
|
158
|
+
...(prompt === undefined ? {} : { prompt }),
|
|
159
|
+
});
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/** Binds every command to one runtime. The only caller that needs the real one. */
|
|
163
|
+
export function createCommands(runtime) {
|
|
164
|
+
return {
|
|
165
|
+
up: createUp(runtime),
|
|
166
|
+
ls: createLs(runtime),
|
|
167
|
+
down: createDown(runtime),
|
|
168
|
+
open: createOpen(runtime),
|
|
169
|
+
reap: createReap(runtime),
|
|
170
|
+
install: createInstall(runtime),
|
|
171
|
+
homedir: runtime.host.homedir,
|
|
172
|
+
writeError: runtime.host.writeError,
|
|
173
|
+
writeOutput: runtime.host.writeOutput,
|
|
174
|
+
};
|
|
195
175
|
}
|
|
196
176
|
export function createProgram(dependencies) {
|
|
197
177
|
const program = new Command().name("termwire").configureOutput({
|
|
@@ -227,6 +207,50 @@ Branch and worktree selection:
|
|
|
227
207
|
...(options.branch === undefined ? {} : { branch: options.branch }),
|
|
228
208
|
});
|
|
229
209
|
});
|
|
210
|
+
program
|
|
211
|
+
.command("ls")
|
|
212
|
+
.description("list the termwire workspaces on this machine")
|
|
213
|
+
.option("--json", "print the same rows as JSON, with absolute directories")
|
|
214
|
+
.addHelpText("after", `
|
|
215
|
+
Columns:
|
|
216
|
+
A marks an attached session, and DIRECTORY is where it was created, $HOME as ~.
|
|
217
|
+
BRANCH is the Git branch there, a short sha on a detached HEAD, - outside a repository.
|
|
218
|
+
PROCS and RSS count every process still labeled with the session, pane or not.
|
|
219
|
+
A workspace whose directory is gone is listed and marked, never fatal.
|
|
220
|
+
A session is a workspace only when its tmux environment carries TERMWIRE_SESSION.
|
|
221
|
+
`)
|
|
222
|
+
.action(async (options) => {
|
|
223
|
+
const workspaces = await dependencies.ls();
|
|
224
|
+
dependencies.writeOutput(options.json === true
|
|
225
|
+
? formatWorkspacesJson(workspaces)
|
|
226
|
+
: formatWorkspaceTable(workspaces, dependencies.homedir()));
|
|
227
|
+
});
|
|
228
|
+
program
|
|
229
|
+
.command("down [name]")
|
|
230
|
+
.description("kill a workspace tmux session, this one when [name] is omitted")
|
|
231
|
+
.option("-w, --worktree", "also remove that workspace's Git worktree")
|
|
232
|
+
.option("-f, --force", "with -w, remove a worktree that has uncommitted changes")
|
|
233
|
+
.addHelpText("after", `
|
|
234
|
+
Teardown:
|
|
235
|
+
[name] resolves exactly as it does for up, so down names the session up created.
|
|
236
|
+
Without it the target is the workspace this command runs in, from TERMWIRE_SESSION.
|
|
237
|
+
That bare form is the only one that works inside a worktree, where the Git root moves.
|
|
238
|
+
Closing the session kills what ran in it; the branch always outlives the workspace.
|
|
239
|
+
-w also removes the worktree the session sits in, refusing the main checkout.
|
|
240
|
+
A worktree with uncommitted changes, or the directory you stand in, is refused too.
|
|
241
|
+
Tearing down your own workspace removes its worktree first: the kill ends this process.
|
|
242
|
+
`)
|
|
243
|
+
.action(async (name, options) => {
|
|
244
|
+
const result = await dependencies.down({
|
|
245
|
+
...(name === undefined ? {} : { name }),
|
|
246
|
+
...(options.worktree === undefined ? {} : { worktree: true }),
|
|
247
|
+
...(options.force === undefined ? {} : { force: true }),
|
|
248
|
+
});
|
|
249
|
+
dependencies.writeOutput(`Killed session ${result.session}\n`);
|
|
250
|
+
if (result.worktree !== undefined) {
|
|
251
|
+
dependencies.writeOutput(`Removed worktree ${result.worktree}\n`);
|
|
252
|
+
}
|
|
253
|
+
});
|
|
230
254
|
program
|
|
231
255
|
.command("open <target>")
|
|
232
256
|
.description("open a file in this workspace's editor and focus it")
|
|
@@ -246,6 +270,35 @@ Target syntax:
|
|
|
246
270
|
const suffix = result.line === undefined ? "" : ` at line ${result.line}`;
|
|
247
271
|
dependencies.writeOutput(`Opened ${result.path}${suffix}\n`);
|
|
248
272
|
});
|
|
273
|
+
program
|
|
274
|
+
.command("install")
|
|
275
|
+
.description("write a layout config and install the file-opening skill for your agents")
|
|
276
|
+
.option("-l, --layout <name>", `layout template: ${[...layoutTemplateIds, noLayout].join(", ")}`)
|
|
277
|
+
.option("-p, --project", "write .termwire.jsonc in this repository instead of the global config")
|
|
278
|
+
.option("-a, --agents <list>", `comma-separated: ${agentIds.join(", ")}, or none`)
|
|
279
|
+
.option("-f, --force", "replace an existing config, or a skill file you own")
|
|
280
|
+
.option("-y, --yes", "never prompt: default layout, and no agents unless --agents")
|
|
281
|
+
.addHelpText("after", `
|
|
282
|
+
Run it before installing anything:
|
|
283
|
+
npx @termwire/cli install
|
|
284
|
+
|
|
285
|
+
Interactive by default, and silent once the flags leave nothing to ask: --yes, or
|
|
286
|
+
--layout together with --agents. The skill teaches an agent to run ${"`termwire open`"}, which costs
|
|
287
|
+
no process, unlike the ${"`@termwire/mcp`"} server it replaces. It only ever adds its own file
|
|
288
|
+
to an agent's skills directory and never edits another tool's configuration.
|
|
289
|
+
`)
|
|
290
|
+
.action(async (options) => {
|
|
291
|
+
const result = await dependencies.install({
|
|
292
|
+
...(options.layout === undefined ? {} : { layout: options.layout }),
|
|
293
|
+
...(options.project === undefined ? {} : { project: true }),
|
|
294
|
+
...(options.agents === undefined ? {} : { agents: parseAgentIds(options.agents) }),
|
|
295
|
+
...(options.force === undefined ? {} : { force: true }),
|
|
296
|
+
...(options.yes === undefined ? {} : { yes: true }),
|
|
297
|
+
});
|
|
298
|
+
if (result.failures.length > 0) {
|
|
299
|
+
throw new Error(`install finished with errors: ${result.failures.join("; ")}`);
|
|
300
|
+
}
|
|
301
|
+
});
|
|
249
302
|
// Part of the `up` lifecycle, not a user-facing command: the global tmux
|
|
250
303
|
// `session-closed` hook installed by `up` invokes it with the closed session.
|
|
251
304
|
program
|
|
@@ -256,15 +309,17 @@ Target syntax:
|
|
|
256
309
|
});
|
|
257
310
|
return program;
|
|
258
311
|
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
312
|
+
/** The composition root's entry: one runtime in, an exit code out. */
|
|
313
|
+
export async function run(argv, runtime) {
|
|
314
|
+
if (runtime === undefined) {
|
|
315
|
+
// Reachable from JavaScript, where the required parameter is only a suggestion.
|
|
316
|
+
throw new Error("run needs a runtime: pass createNodeRuntime()");
|
|
317
|
+
}
|
|
318
|
+
return await runCommands(argv, createCommands(runtime));
|
|
319
|
+
}
|
|
320
|
+
/** Parsing and exit-code mapping, indifferent to how the commands were built. */
|
|
321
|
+
export async function runCommands(argv, commands) {
|
|
322
|
+
const program = createProgram(commands);
|
|
268
323
|
try {
|
|
269
324
|
await program.parseAsync(argv, { from: "user" });
|
|
270
325
|
return 0;
|
|
@@ -274,7 +329,7 @@ export async function run(argv, dependencies = {}) {
|
|
|
274
329
|
return error.exitCode;
|
|
275
330
|
}
|
|
276
331
|
const message = error instanceof Error ? error.message : String(error);
|
|
277
|
-
writeError(`termwire: ${message}\n`);
|
|
332
|
+
commands.writeError(`termwire: ${message}\n`);
|
|
278
333
|
return 1;
|
|
279
334
|
}
|
|
280
335
|
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { createNvim } from "@termwire/nvim";
|
|
2
|
+
import { createTmux } from "@termwire/tmux";
|
|
3
|
+
import { type Exec } from "./exec.js";
|
|
4
|
+
import type { InstallPrompter } from "./install.js";
|
|
5
|
+
import type { SessionScanner } from "./process-scan.js";
|
|
6
|
+
import type { KillOutcome, ReapSignal } from "./reap.js";
|
|
7
|
+
import type { GitExec } from "./worktree.js";
|
|
8
|
+
export interface RuntimeFileSystem {
|
|
9
|
+
readFile: (path: string) => Promise<string>;
|
|
10
|
+
exists: (path: string) => Promise<boolean>;
|
|
11
|
+
mkdir: (path: string) => Promise<void>;
|
|
12
|
+
/** `flag: "wx"` fails on an existing file instead of replacing it. */
|
|
13
|
+
writeFile: (path: string, contents: string, options?: {
|
|
14
|
+
flag: "wx";
|
|
15
|
+
}) => Promise<void>;
|
|
16
|
+
rename: (from: string, to: string) => Promise<void>;
|
|
17
|
+
appendFile: (path: string, contents: string) => Promise<void>;
|
|
18
|
+
unlink: (path: string) => Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
export interface RuntimeHost {
|
|
21
|
+
env: Record<string, string | undefined>;
|
|
22
|
+
cwd: () => string;
|
|
23
|
+
/** Leaves a directory before it is removed: a spawn from a deleted cwd fails. */
|
|
24
|
+
chdir: (path: string) => void;
|
|
25
|
+
homedir: () => string;
|
|
26
|
+
pid: number;
|
|
27
|
+
execPath: string;
|
|
28
|
+
/** The launched script, which the tmux hook has to name by absolute path. */
|
|
29
|
+
scriptPath: string | undefined;
|
|
30
|
+
now: () => Date;
|
|
31
|
+
/** A unique name for a temporary file, so two writers cannot collide. */
|
|
32
|
+
randomId: () => string;
|
|
33
|
+
/** Whether there is a terminal to ask questions on, both ways. */
|
|
34
|
+
isTerminal: boolean;
|
|
35
|
+
kill: (pid: number, signal: ReapSignal) => KillOutcome;
|
|
36
|
+
wait: (milliseconds: number) => Promise<void>;
|
|
37
|
+
writeOutput: (message: string) => void;
|
|
38
|
+
writeError: (message: string) => void;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Everything the CLI needs from the outside world. Commands receive it whole and
|
|
42
|
+
* never reach for `node:*` or `process` themselves, so a test builds a runtime of
|
|
43
|
+
* fakes instead of overriding a dozen separate default arguments.
|
|
44
|
+
*
|
|
45
|
+
* The adapters carry their own copy of the environment, so a composed runtime should
|
|
46
|
+
* build them from the same `host.env` it passes here — `createAdapters` does that.
|
|
47
|
+
* Otherwise `attach` reads one environment to decide between `attach-session` and
|
|
48
|
+
* `switch-client` while the reap reads another to find the tmux server.
|
|
49
|
+
*/
|
|
50
|
+
export interface CliRuntime {
|
|
51
|
+
fs: RuntimeFileSystem;
|
|
52
|
+
host: RuntimeHost;
|
|
53
|
+
git: GitExec;
|
|
54
|
+
/**
|
|
55
|
+
* Built on demand, so a platform that cannot be scanned fails the command that
|
|
56
|
+
* scans rather than every command, `--help` included.
|
|
57
|
+
*/
|
|
58
|
+
createScanner: () => SessionScanner;
|
|
59
|
+
tmux: ReturnType<typeof createTmux>;
|
|
60
|
+
nvim: ReturnType<typeof createNvim>;
|
|
61
|
+
/** Built on demand, so a command that never asks anything loads no terminal UI. */
|
|
62
|
+
createPrompter: () => Promise<InstallPrompter>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Binds both adapters to one exec and one environment. The exec has to forward
|
|
66
|
+
* `ExecOptions`: `attach` asks for an inherited terminal, and tmux refuses to attach
|
|
67
|
+
* without one.
|
|
68
|
+
*/
|
|
69
|
+
export declare function createAdapters({ exec, env, }: {
|
|
70
|
+
exec: Exec;
|
|
71
|
+
env: Record<string, string | undefined>;
|
|
72
|
+
}): Pick<CliRuntime, "tmux" | "nvim">;
|
|
73
|
+
/** The one place where the real world is bound. Nothing else imports `node:*`. */
|
|
74
|
+
export declare function createNodeRuntime(): CliRuntime;
|
|
75
|
+
/**
|
|
76
|
+
* The one place that knows which platforms can be scanned. A platform module takes
|
|
77
|
+
* only its own dependencies, which a registry keyed by platform could not express:
|
|
78
|
+
* every entry would have to accept the union of what all of them need. So a new
|
|
79
|
+
* platform is a new module plus a branch here.
|
|
80
|
+
*/
|
|
81
|
+
export declare function createPlatformScanner(platform: string): SessionScanner;
|
|
82
|
+
export declare function fileExists(path: string): Promise<boolean>;
|
|
83
|
+
export declare function killProcess(pid: number, signal: ReapSignal): KillOutcome;
|
|
84
|
+
export declare const executeGit: GitExec;
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { appendFile as appendFileToDisk, mkdir as mkdirDirectory, readdir as readDirectory, readFile as readFileFromDisk, rename as renameFile, stat, unlink as unlinkFile, writeFile as writeFileToDisk, } from "node:fs/promises";
|
|
4
|
+
import { homedir as getHomeDirectory } from "node:os";
|
|
5
|
+
import { createNvim } from "@termwire/nvim";
|
|
6
|
+
import { createTmux } from "@termwire/tmux";
|
|
7
|
+
import { spawnCapture } from "./exec.js";
|
|
8
|
+
import { createDarwinScanner } from "./process-scan-darwin.js";
|
|
9
|
+
import { createLinuxScanner } from "./process-scan-linux.js";
|
|
10
|
+
/**
|
|
11
|
+
* Binds both adapters to one exec and one environment. The exec has to forward
|
|
12
|
+
* `ExecOptions`: `attach` asks for an inherited terminal, and tmux refuses to attach
|
|
13
|
+
* without one.
|
|
14
|
+
*/
|
|
15
|
+
export function createAdapters({ exec, env, }) {
|
|
16
|
+
return { tmux: createTmux({ exec, env }), nvim: createNvim({ exec }) };
|
|
17
|
+
}
|
|
18
|
+
/** The one place where the real world is bound. Nothing else imports `node:*`. */
|
|
19
|
+
export function createNodeRuntime() {
|
|
20
|
+
const env = process.env;
|
|
21
|
+
return {
|
|
22
|
+
fs: {
|
|
23
|
+
readFile: (path) => readFileFromDisk(path, "utf8"),
|
|
24
|
+
exists: fileExists,
|
|
25
|
+
mkdir: async (path) => {
|
|
26
|
+
await mkdirDirectory(path, { recursive: true });
|
|
27
|
+
},
|
|
28
|
+
writeFile: (path, contents, options) => writeFileToDisk(path, contents, options ? { flag: options.flag } : undefined),
|
|
29
|
+
rename: renameFile,
|
|
30
|
+
appendFile: appendFileToDisk,
|
|
31
|
+
unlink: unlinkFile,
|
|
32
|
+
},
|
|
33
|
+
host: {
|
|
34
|
+
env,
|
|
35
|
+
cwd: () => process.cwd(),
|
|
36
|
+
chdir: (path) => process.chdir(path),
|
|
37
|
+
homedir: getHomeDirectory,
|
|
38
|
+
pid: process.pid,
|
|
39
|
+
execPath: process.execPath,
|
|
40
|
+
scriptPath: process.argv[1],
|
|
41
|
+
now: () => new Date(),
|
|
42
|
+
randomId: randomUUID,
|
|
43
|
+
isTerminal: process.stdin.isTTY === true && process.stdout.isTTY === true,
|
|
44
|
+
kill: killProcess,
|
|
45
|
+
wait: waitFor,
|
|
46
|
+
writeOutput: (message) => process.stdout.write(message),
|
|
47
|
+
writeError: (message) => process.stderr.write(message),
|
|
48
|
+
},
|
|
49
|
+
git: executeGit,
|
|
50
|
+
createScanner: () => createPlatformScanner(process.platform),
|
|
51
|
+
...createAdapters({ exec: spawnCapture, env }),
|
|
52
|
+
// Imported here and not at the top, so `up` and `open` never load the prompts.
|
|
53
|
+
createPrompter: async () => (await import("./install-prompt.js")).createClackPrompter({
|
|
54
|
+
columns: process.stdout.columns ?? 80,
|
|
55
|
+
}),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The one place that knows which platforms can be scanned. A platform module takes
|
|
60
|
+
* only its own dependencies, which a registry keyed by platform could not express:
|
|
61
|
+
* every entry would have to accept the union of what all of them need. So a new
|
|
62
|
+
* platform is a new module plus a branch here.
|
|
63
|
+
*/
|
|
64
|
+
export function createPlatformScanner(platform) {
|
|
65
|
+
if (platform === "darwin") {
|
|
66
|
+
return createDarwinScanner({ exec: spawnCapture, uid: process.getuid?.() ?? 0 });
|
|
67
|
+
}
|
|
68
|
+
if (platform === "linux") {
|
|
69
|
+
return createLinuxScanner({
|
|
70
|
+
readdir: readDirectory,
|
|
71
|
+
readFile: (path) => readFileFromDisk(path, "utf8"),
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
throw new Error(`process scanning is not supported on ${platform}`);
|
|
75
|
+
}
|
|
76
|
+
export async function fileExists(path) {
|
|
77
|
+
try {
|
|
78
|
+
await stat(path);
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
if (error.code === "ENOENT")
|
|
83
|
+
return false;
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
export function killProcess(pid, signal) {
|
|
88
|
+
try {
|
|
89
|
+
process.kill(pid, signal);
|
|
90
|
+
return "signalled";
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
const code = error.code;
|
|
94
|
+
if (code === "ESRCH")
|
|
95
|
+
return "gone";
|
|
96
|
+
if (code === "EPERM")
|
|
97
|
+
return "denied";
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function waitFor(milliseconds) {
|
|
102
|
+
return new Promise((resolve) => {
|
|
103
|
+
setTimeout(resolve, milliseconds);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
export const executeGit = async (argv, options) => {
|
|
107
|
+
const [command, ...args] = argv;
|
|
108
|
+
if (command === undefined) {
|
|
109
|
+
throw new Error("git execution failed: empty command");
|
|
110
|
+
}
|
|
111
|
+
return await new Promise((resolve, reject) => {
|
|
112
|
+
const child = spawn(command, args, {
|
|
113
|
+
cwd: options?.cwd,
|
|
114
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
115
|
+
});
|
|
116
|
+
let stdout = "";
|
|
117
|
+
let stderr = "";
|
|
118
|
+
child.stdout.setEncoding("utf8").on("data", (chunk) => {
|
|
119
|
+
stdout += chunk;
|
|
120
|
+
});
|
|
121
|
+
child.stderr.setEncoding("utf8").on("data", (chunk) => {
|
|
122
|
+
stderr += chunk;
|
|
123
|
+
});
|
|
124
|
+
child.once("error", (cause) => {
|
|
125
|
+
reject(new Error(`git execution failed: ${argv.join(" ")}`, { cause }));
|
|
126
|
+
});
|
|
127
|
+
child.once("close", (code) => {
|
|
128
|
+
resolve({ exitCode: code ?? 1, stdout, stderr });
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
};
|
package/dist/skill.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Marks a skill file as ours to replace. Without it we cannot tell our own older text
|
|
3
|
+
* from something the user rewrote, and an upgrade would quietly eat their edits. Deleting
|
|
4
|
+
* the line is how a user says "leave this file alone".
|
|
5
|
+
*/
|
|
6
|
+
export declare const skillMarker = "<!-- managed by `termwire install`; delete this line to keep your own version -->";
|
|
7
|
+
/**
|
|
8
|
+
* The skill lives as Markdown in the package rather than as a string in the source: it is
|
|
9
|
+
* editable, reviewable in a diff, and readable by anyone who wants to know what their agent
|
|
10
|
+
* was told. `dist/skill.js` and `src/skill.ts` both sit one level below the package root, so
|
|
11
|
+
* the same specifier resolves in a published install and from a checkout.
|
|
12
|
+
*/
|
|
13
|
+
export declare function skillSourcePath(): string;
|