mcp-ssh-manager 3.8.7 → 4.0.1
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/.eslintignore +5 -0
- package/.gitattributes +5 -0
- package/CHANGELOG.md +371 -6
- package/CLAUDE.md +6 -0
- package/CODE_OF_CONDUCT.md +18 -0
- package/CONTRIBUTING.md +30 -0
- package/Formula/ssh-manager.rb +60 -0
- package/README.md +484 -964
- package/ROADMAP-V4.md +451 -0
- package/brand/README.md +43 -0
- package/brand/icon.svg +32 -0
- package/brand/mark.svg +18 -0
- package/brand/wordmark.svg +30 -0
- package/cli/app.js +230 -0
- package/cli/control.js +116 -0
- package/cli/lib/colors.sh +15 -0
- package/cli/lib/menu.sh +145 -96
- package/cli/portability.js +186 -0
- package/cli/ssh-manager +197 -91
- package/cli/ssh-manager.js +14 -0
- package/cli/style.js +17 -0
- package/cli/vault.js +411 -0
- package/desktop/ControlPlaneApp.swift +219 -0
- package/desktop/build.sh +58 -0
- package/desktop/electron/after-pack.cjs +39 -0
- package/desktop/electron/electron-builder.yml +148 -0
- package/desktop/electron/finalize-artifacts.mjs +83 -0
- package/desktop/electron/main.js +815 -0
- package/desktop/electron/package-lock.json +3656 -0
- package/desktop/electron/package.json +30 -0
- package/desktop/electron/prepare-engine.mjs +84 -0
- package/desktop/electron/resources/entitlements.mac.plist +22 -0
- package/desktop/electron/resources/icon.png +0 -0
- package/desktop/electron/resources/trayTemplate.png +0 -0
- package/desktop/electron/resources/trayTemplate@2x.png +0 -0
- package/desktop/electron/smoke-test.mjs +184 -0
- package/dist/ui/app.css +1 -0
- package/dist/ui/app.js +501 -0
- package/dist/ui/assets/Forum-Regular.woff2 +0 -0
- package/dist/ui/assets/JetBrainsMono-Bold.woff2 +0 -0
- package/dist/ui/assets/JetBrainsMono-Regular.woff2 +0 -0
- package/dist/ui/assets/Karla-Variable.woff2 +0 -0
- package/dist/ui/index.html +16 -0
- package/docs/DISTRIBUTION.md +136 -0
- package/docs/FINAL-TEST-V4.md +209 -0
- package/docs/MIGRATION.md +334 -0
- package/docs/TESTING-V4.md +119 -0
- package/docs/superpowers/specs/2026-10-08-desktop-from-npm-and-cli-design.md +187 -0
- package/docs/videos/control-plane.mp4 +0 -0
- package/package.json +61 -30
- package/scripts/build-interface.mjs +73 -0
- package/scripts/capture-screenshots.mjs +191 -0
- package/scripts/demo-env.mjs +461 -0
- package/scripts/frame-screenshots.py +176 -0
- package/scripts/macbook-mockup.py +296 -0
- package/scripts/make-tray-icon.py +61 -0
- package/scripts/preview-candidate.mjs +291 -0
- package/scripts/publish-desktop.mjs +46 -0
- package/scripts/record-demo.mjs +229 -0
- package/scripts/release-version.mjs +32 -0
- package/scripts/render-cli-menu.py +157 -0
- package/scripts/requirements-dev.in +12 -0
- package/scripts/requirements-dev.txt +424 -0
- package/scripts/run-ui-regression.mjs +48 -0
- package/scripts/setup-hooks.sh +7 -13
- package/scripts/smoke-desktop.mjs +80 -0
- package/scripts/test-drag-and-drop.mjs +114 -0
- package/scripts/test-first-run.mjs +5 -0
- package/scripts/test-import-ui.mjs +5 -0
- package/scripts/test-preferences-persist.mjs +5 -0
- package/scripts/test-upgrade-from-published.mjs +226 -0
- package/scripts/verify-mac-build.sh +153 -0
- package/scripts/verify-windows-build.ps1 +15 -0
- package/server.json +3 -3
- package/skills/README.md +42 -0
- package/skills/ssh-incident/SKILL.md +55 -0
- package/skills/ssh-operations/SKILL.md +62 -0
- package/skills/ssh-restricted/SKILL.md +57 -0
- package/src/agent-clients.js +482 -0
- package/src/approval.js +291 -0
- package/src/audit.js +11 -1
- package/src/command-log.js +162 -0
- package/src/config-loader.js +119 -4
- package/src/config-paths.js +101 -0
- package/src/control-plane.js +2730 -0
- package/src/desktop-install.js +747 -0
- package/src/index.js +179 -213
- package/src/live-stream.js +261 -0
- package/src/saved-commands.js +150 -0
- package/src/secret-store.js +573 -0
- package/src/server-aliases.js +8 -7
- package/src/server-config-manager.js +33 -7
- package/src/server-export.js +106 -0
- package/src/server-groups.js +82 -21
- package/src/server-import.js +477 -0
- package/src/shell-quote.js +9 -2
- package/src/ssh-connection.js +155 -0
- package/src/ssh-manager.js +186 -13
- package/src/tunnel-manager.js +79 -0
- package/src/vault-recovery.js +206 -0
- package/src/xlsx.js +322 -0
- package/docs/images/social-preview.png +0 -0
- package/docs/images/social-preview@2x.png +0 -0
- package/docs/images/ssh-manager-cli-menu.png +0 -0
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
// Live command streaming — watching over the agent's shoulder.
|
|
2
|
+
//
|
|
3
|
+
// When a control plane is listening, the engine mirrors what it runs to it as
|
|
4
|
+
// it runs: the command, then its output, then the exit code. That is the one
|
|
5
|
+
// thing no CLI can offer and the reason to have a window at all — seeing what
|
|
6
|
+
// an agent is doing on a production box at the second it happens, rather than
|
|
7
|
+
// reading about it afterwards.
|
|
8
|
+
//
|
|
9
|
+
// The scrollback idea (a bounded circular buffer per stream, so a page opened
|
|
10
|
+
// late still shows recent context) is lifted from TransHub's PtyService —
|
|
11
|
+
// same author, relicensed here under MIT along with the rest of the engine.
|
|
12
|
+
//
|
|
13
|
+
// ## Two rules this module must never break
|
|
14
|
+
//
|
|
15
|
+
// 1. **It cannot slow a command down.** Every send is fire-and-forget over a
|
|
16
|
+
// datagram-style write; nothing here is awaited on the execution path, and
|
|
17
|
+
// a missing or wedged control plane costs one failed connect attempt.
|
|
18
|
+
// 2. **It is off unless someone is watching.** No socket, no work: not even
|
|
19
|
+
// the buffers are allocated.
|
|
20
|
+
|
|
21
|
+
import net from 'net';
|
|
22
|
+
import fs from 'fs';
|
|
23
|
+
import crypto from 'crypto';
|
|
24
|
+
import path from 'path';
|
|
25
|
+
import { defaultSocketPath } from './approval.js';
|
|
26
|
+
|
|
27
|
+
/** Bytes of recent output kept per stream, so a late viewer sees context. */
|
|
28
|
+
const SCROLLBACK_BYTES = 64 * 1024;
|
|
29
|
+
|
|
30
|
+
/** Suffix of the streaming socket, next to the approval one. */
|
|
31
|
+
const STREAM_SOCKET_SUFFIX = '.stream';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Where the control plane listens for stream events.
|
|
35
|
+
*
|
|
36
|
+
* A second socket rather than reusing the approval one: approval is a
|
|
37
|
+
* request/response that blocks a command, streaming is a one-way firehose. On
|
|
38
|
+
* one socket a slow reader of the firehose would delay a decision.
|
|
39
|
+
*
|
|
40
|
+
* @returns {string} Socket path
|
|
41
|
+
*/
|
|
42
|
+
export function streamSocketPath() {
|
|
43
|
+
return `${defaultSocketPath()}${STREAM_SOCKET_SUFFIX}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Is anyone watching?
|
|
48
|
+
*
|
|
49
|
+
* Checked per command rather than cached: the window can be opened and closed
|
|
50
|
+
* at any time, and a stale "yes" would mean a connect attempt per command with
|
|
51
|
+
* nothing on the other end.
|
|
52
|
+
*
|
|
53
|
+
* @returns {boolean} True when a control plane is listening for streams
|
|
54
|
+
*/
|
|
55
|
+
export function isWatching() {
|
|
56
|
+
if (process.platform === 'win32') return true;
|
|
57
|
+
try {
|
|
58
|
+
return fs.statSync(streamSocketPath()).isSocket();
|
|
59
|
+
} catch {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* A single command being mirrored to the control plane.
|
|
66
|
+
*
|
|
67
|
+
* Holds one connection for the life of the command. Output arrives in chunks
|
|
68
|
+
* from ssh2 and is forwarded as it comes.
|
|
69
|
+
*/
|
|
70
|
+
class LiveStream {
|
|
71
|
+
/**
|
|
72
|
+
* @param {string} server - Server name
|
|
73
|
+
* @param {string} command - The command being run, as sent to the host
|
|
74
|
+
*/
|
|
75
|
+
constructor(server, command) {
|
|
76
|
+
this.id = crypto.randomUUID();
|
|
77
|
+
this.server = server;
|
|
78
|
+
this.command = command;
|
|
79
|
+
/** @type {import('net').Socket|null} */
|
|
80
|
+
this.socket = null;
|
|
81
|
+
this.closed = false;
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
this.socket = net.createConnection(streamSocketPath());
|
|
85
|
+
// Errors here are expected and uninteresting: the window was closed, the
|
|
86
|
+
// socket went away. Streaming is a convenience, never a dependency.
|
|
87
|
+
this.socket.on('error', () => { this.closed = true; });
|
|
88
|
+
this.#send({ type: 'start', command, ts: new Date().toISOString() });
|
|
89
|
+
} catch {
|
|
90
|
+
this.closed = true;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @param {Record<string, any>} event - Event payload
|
|
96
|
+
*/
|
|
97
|
+
#send(event) {
|
|
98
|
+
if (this.closed || !this.socket) return;
|
|
99
|
+
try {
|
|
100
|
+
this.socket.write(`${JSON.stringify({ id: this.id, server: this.server, ...event })}\n`);
|
|
101
|
+
} catch {
|
|
102
|
+
this.closed = true;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Forward a chunk of output.
|
|
108
|
+
* @param {string} channel - 'stdout' or 'stderr'
|
|
109
|
+
* @param {string} chunk - The bytes, as text
|
|
110
|
+
*/
|
|
111
|
+
write(channel, chunk) {
|
|
112
|
+
if (!chunk) return;
|
|
113
|
+
this.#send({ type: 'data', channel, chunk });
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Close the stream and report how the command ended.
|
|
118
|
+
* @param {number|null} code - Exit code
|
|
119
|
+
*/
|
|
120
|
+
end(code) {
|
|
121
|
+
this.#send({ type: 'end', code, ts: new Date().toISOString() });
|
|
122
|
+
try { this.socket?.end(); } catch { /* already gone */ }
|
|
123
|
+
this.closed = true;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Open a stream for a command, or return null when nobody is watching.
|
|
129
|
+
*
|
|
130
|
+
* Callers do not need to branch: `stream?.write(...)` and `stream?.end(...)`
|
|
131
|
+
* are no-ops when this returns null.
|
|
132
|
+
*
|
|
133
|
+
* @param {string} server - Server name
|
|
134
|
+
* @param {string} command - Command being run
|
|
135
|
+
* @returns {LiveStream|null} The stream, or null when there is no viewer
|
|
136
|
+
*/
|
|
137
|
+
export function openStream(server, command) {
|
|
138
|
+
if (!isWatching()) return null;
|
|
139
|
+
return new LiveStream(server, command);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Server side: keeps recent output per stream so a page opened mid-command
|
|
144
|
+
* shows what came before, not just what follows.
|
|
145
|
+
*/
|
|
146
|
+
export class StreamRegistry {
|
|
147
|
+
constructor() {
|
|
148
|
+
/** @type {Map<string, {id: string, server: string, command: string, startedAt: string, scrollback: string, code: number|null}>} */
|
|
149
|
+
this.streams = new Map();
|
|
150
|
+
/** @type {Set<(event: any) => void>} */
|
|
151
|
+
this.listeners = new Set();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* @param {(event: any) => void} listener - Called for every event
|
|
156
|
+
* @returns {() => void} Unsubscribe
|
|
157
|
+
*/
|
|
158
|
+
subscribe(listener) {
|
|
159
|
+
this.listeners.add(listener);
|
|
160
|
+
return () => this.listeners.delete(listener);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Apply one event from the engine.
|
|
165
|
+
* @param {any} event - Parsed event
|
|
166
|
+
*/
|
|
167
|
+
apply(event) {
|
|
168
|
+
if (event.type === 'start') {
|
|
169
|
+
this.streams.set(event.id, {
|
|
170
|
+
id: event.id,
|
|
171
|
+
server: event.server,
|
|
172
|
+
command: event.command,
|
|
173
|
+
startedAt: event.ts,
|
|
174
|
+
scrollback: '',
|
|
175
|
+
code: null,
|
|
176
|
+
});
|
|
177
|
+
} else if (event.type === 'data') {
|
|
178
|
+
const stream = this.streams.get(event.id);
|
|
179
|
+
if (stream) {
|
|
180
|
+
stream.scrollback += event.chunk;
|
|
181
|
+
// Bounded, so a command that prints for an hour cannot grow without
|
|
182
|
+
// limit. Trimming from the front keeps the most recent output.
|
|
183
|
+
if (stream.scrollback.length > SCROLLBACK_BYTES) {
|
|
184
|
+
stream.scrollback = stream.scrollback.slice(-SCROLLBACK_BYTES);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
} else if (event.type === 'end') {
|
|
188
|
+
const stream = this.streams.get(event.id);
|
|
189
|
+
if (stream) stream.code = event.code;
|
|
190
|
+
// Finished streams are kept: the interesting one to read is usually the
|
|
191
|
+
// one that just ended. Trim the oldest so memory stays bounded.
|
|
192
|
+
this.#trimFinished();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
for (const listener of this.listeners) {
|
|
196
|
+
try { listener(event); } catch { /* a dead subscriber must not stop others */ }
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Keep at most 20 finished streams, oldest dropped first. */
|
|
201
|
+
#trimFinished() {
|
|
202
|
+
const finished = [...this.streams.values()].filter(s => s.code !== null);
|
|
203
|
+
if (finished.length <= 20) return;
|
|
204
|
+
for (const stream of finished.slice(0, finished.length - 20)) {
|
|
205
|
+
this.streams.delete(stream.id);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** @returns {any[]} Streams, most recent first */
|
|
210
|
+
list() {
|
|
211
|
+
return [...this.streams.values()].reverse();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* @param {string} id - Stream id
|
|
216
|
+
* @returns {any|undefined} One stream with its scrollback
|
|
217
|
+
*/
|
|
218
|
+
get(id) {
|
|
219
|
+
return this.streams.get(id);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Listen for stream events from the engine.
|
|
225
|
+
*
|
|
226
|
+
* @param {StreamRegistry} registry - Where events are applied
|
|
227
|
+
* @param {string} [socketPath] - Socket to create
|
|
228
|
+
* @returns {Promise<import('net').Server>} The listening server
|
|
229
|
+
*/
|
|
230
|
+
export function listenForStreams(registry, socketPath = streamSocketPath()) {
|
|
231
|
+
try {
|
|
232
|
+
if (fs.statSync(socketPath).isSocket()) fs.unlinkSync(socketPath);
|
|
233
|
+
} catch { /* nothing there */ }
|
|
234
|
+
fs.mkdirSync(path.dirname(socketPath), { recursive: true, mode: 0o700 });
|
|
235
|
+
|
|
236
|
+
const server = net.createServer(socket => {
|
|
237
|
+
let buffer = '';
|
|
238
|
+
socket.on('data', chunk => {
|
|
239
|
+
buffer += chunk.toString();
|
|
240
|
+
// Chunks arrive split at arbitrary points; only complete lines are events.
|
|
241
|
+
let newline;
|
|
242
|
+
while ((newline = buffer.indexOf('\n')) !== -1) {
|
|
243
|
+
const line = buffer.slice(0, newline);
|
|
244
|
+
buffer = buffer.slice(newline + 1);
|
|
245
|
+
if (!line.trim()) continue;
|
|
246
|
+
try {
|
|
247
|
+
registry.apply(JSON.parse(line));
|
|
248
|
+
} catch { /* partial or malformed: drop the line, keep the stream */ }
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
socket.on('error', () => { /* engine went away mid-command */ });
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
return new Promise((resolve, reject) => {
|
|
255
|
+
server.once('error', reject);
|
|
256
|
+
server.listen(socketPath, () => {
|
|
257
|
+
try { fs.chmodSync(socketPath, 0o600); } catch { /* best effort */ }
|
|
258
|
+
resolve(server);
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Commands you keep, so you stop retyping them.
|
|
3
|
+
*
|
|
4
|
+
* Distinct from `command-aliases.js`, which maps a short word to a longer
|
|
5
|
+
* command line for an *agent* to expand. These are for a person: a named,
|
|
6
|
+
* described command you pick from a list next to a terminal — "restart nginx",
|
|
7
|
+
* "tail the error log", "show me disk usage".
|
|
8
|
+
*
|
|
9
|
+
* The model follows TransHub's `SSHCommand`, because the shape earned its parts:
|
|
10
|
+
*
|
|
11
|
+
* - **`serverNames`** — empty means everywhere. A `systemctl restart nginx`
|
|
12
|
+
* offered on a database server is a mistake waiting for a tired evening.
|
|
13
|
+
* - **`confirmBeforeRun`** — some of these delete things. The flag lives with
|
|
14
|
+
* the command rather than being guessed from its text, because whoever wrote
|
|
15
|
+
* it knows, and a heuristic that misses once is worse than no heuristic.
|
|
16
|
+
* - **`workingDirectory`** — a command that only makes sense in one place
|
|
17
|
+
* should carry that place with it.
|
|
18
|
+
*/
|
|
19
|
+
import fs from 'fs';
|
|
20
|
+
import path from 'path';
|
|
21
|
+
import crypto from 'crypto';
|
|
22
|
+
import os from 'os';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Where the list lives. In the user's own directory rather than the project's:
|
|
26
|
+
* these are personal shortcuts, not repository configuration, and they should
|
|
27
|
+
* survive a reinstall and follow you between checkouts.
|
|
28
|
+
*
|
|
29
|
+
* @returns {string}
|
|
30
|
+
*/
|
|
31
|
+
export function savedCommandsPath() {
|
|
32
|
+
const home = process.env.SSH_MANAGER_HOME || path.join(os.homedir(), '.ssh-manager');
|
|
33
|
+
return path.join(home, 'commands.json');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @typedef {object} SavedCommand
|
|
38
|
+
* @property {string} id
|
|
39
|
+
* @property {string} name
|
|
40
|
+
* @property {string} command
|
|
41
|
+
* @property {string} [description]
|
|
42
|
+
* @property {string[]} serverNames - Empty means every server
|
|
43
|
+
* @property {boolean} confirmBeforeRun
|
|
44
|
+
* @property {string} [workingDirectory]
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A starting set, offered when the list is empty rather than written on
|
|
49
|
+
* install. Nothing is created behind the operator's back, and a list they never
|
|
50
|
+
* asked for is a list they have to clean up.
|
|
51
|
+
*
|
|
52
|
+
* @returns {Omit<SavedCommand, 'id'>[]}
|
|
53
|
+
*/
|
|
54
|
+
export function suggestedCommands() {
|
|
55
|
+
return [
|
|
56
|
+
{ name: 'Disk usage', command: 'df -h', description: 'Free space per filesystem', serverNames: [], confirmBeforeRun: false },
|
|
57
|
+
{ name: 'Memory', command: 'free -h', description: 'What is used and what is cached', serverNames: [], confirmBeforeRun: false },
|
|
58
|
+
{ name: 'Load', command: 'uptime', description: 'How long it has been up, and how busy', serverNames: [], confirmBeforeRun: false },
|
|
59
|
+
{ name: 'Biggest directories', command: 'du -sh */ 2>/dev/null | sort -rh | head -20', description: 'What is filling the disk', serverNames: [], confirmBeforeRun: false },
|
|
60
|
+
{ name: 'Failed services', command: 'systemctl --failed', description: 'Units that did not come up', serverNames: [], confirmBeforeRun: false },
|
|
61
|
+
{ name: 'Recent errors', command: 'journalctl -p err -n 50 --no-pager', description: 'The last 50 error-level log lines', serverNames: [], confirmBeforeRun: false },
|
|
62
|
+
{ name: 'Listening ports', command: 'ss -tlnp 2>/dev/null || netstat -tlnp', description: 'What is accepting connections', serverNames: [], confirmBeforeRun: false },
|
|
63
|
+
{ name: 'Reload nginx', command: 'systemctl reload nginx', description: 'Pick up a new configuration without dropping connections', serverNames: [], confirmBeforeRun: true },
|
|
64
|
+
];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Read the list. A missing file is an empty list, not an error — nobody has
|
|
69
|
+
* saved a command yet.
|
|
70
|
+
*
|
|
71
|
+
* @returns {SavedCommand[]}
|
|
72
|
+
*/
|
|
73
|
+
export function listSavedCommands() {
|
|
74
|
+
const file = savedCommandsPath();
|
|
75
|
+
if (!fs.existsSync(file)) return [];
|
|
76
|
+
try {
|
|
77
|
+
const parsed = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
78
|
+
return Array.isArray(parsed?.commands) ? parsed.commands : [];
|
|
79
|
+
} catch {
|
|
80
|
+
// A corrupt file must not take the interface down; the operator can see an
|
|
81
|
+
// empty list and re-save, which beats a screen that will not load.
|
|
82
|
+
return [];
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param {SavedCommand[]} commands - The full list
|
|
88
|
+
*/
|
|
89
|
+
function write(commands) {
|
|
90
|
+
const file = savedCommandsPath();
|
|
91
|
+
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
92
|
+
fs.writeFileSync(file, `${JSON.stringify({ version: 1, commands }, null, 2)}\n`, { mode: 0o600 });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Add or replace one. Validated here rather than trusted: this list is edited
|
|
97
|
+
* through an HTTP route, and a command with no command in it would be a row
|
|
98
|
+
* that does nothing and cannot be told apart from one that does.
|
|
99
|
+
*
|
|
100
|
+
* @param {Partial<SavedCommand>} input - What to save
|
|
101
|
+
* @returns {SavedCommand} The stored form
|
|
102
|
+
*/
|
|
103
|
+
export function saveCommand(input) {
|
|
104
|
+
const name = String(input.name ?? '').trim();
|
|
105
|
+
const command = String(input.command ?? '').trim();
|
|
106
|
+
if (!name) throw new Error('A name is required — it is what you will pick from the list');
|
|
107
|
+
if (!command) throw new Error('A command is required');
|
|
108
|
+
|
|
109
|
+
/** @type {SavedCommand} */
|
|
110
|
+
const stored = {
|
|
111
|
+
id: input.id || crypto.randomUUID(),
|
|
112
|
+
name,
|
|
113
|
+
command,
|
|
114
|
+
description: input.description ? String(input.description).trim() : undefined,
|
|
115
|
+
serverNames: Array.isArray(input.serverNames) ? input.serverNames.map(String) : [],
|
|
116
|
+
confirmBeforeRun: Boolean(input.confirmBeforeRun),
|
|
117
|
+
workingDirectory: input.workingDirectory ? String(input.workingDirectory).trim() : undefined,
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const commands = listSavedCommands();
|
|
121
|
+
const at = commands.findIndex(existing => existing.id === stored.id);
|
|
122
|
+
if (at >= 0) commands[at] = stored;
|
|
123
|
+
else commands.push(stored);
|
|
124
|
+
write(commands);
|
|
125
|
+
return stored;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* @param {string} id - Command to remove
|
|
130
|
+
* @returns {boolean} Whether it was there
|
|
131
|
+
*/
|
|
132
|
+
export function deleteCommand(id) {
|
|
133
|
+
const commands = listSavedCommands();
|
|
134
|
+
const remaining = commands.filter(command => command.id !== id);
|
|
135
|
+
if (remaining.length === commands.length) return false;
|
|
136
|
+
write(remaining);
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The commands offered for one server: the global ones plus those that name it.
|
|
142
|
+
*
|
|
143
|
+
* @param {string} serverName - The server in question
|
|
144
|
+
* @returns {SavedCommand[]}
|
|
145
|
+
*/
|
|
146
|
+
export function commandsForServer(serverName) {
|
|
147
|
+
return listSavedCommands().filter(
|
|
148
|
+
command => command.serverNames.length === 0 || command.serverNames.includes(serverName)
|
|
149
|
+
);
|
|
150
|
+
}
|