send-to-pi 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +57 -113
- package/README_CN.md +57 -116
- package/chrome-extension/background.js +237 -65
- package/chrome-extension/manifest.json +10 -1
- package/chrome-extension/options.html +8 -2
- package/chrome-extension/options.js +22 -11
- package/chrome-extension/popup.html +160 -0
- package/chrome-extension/popup.js +145 -0
- package/package.json +3 -2
- package/pi-extension/README.md +9 -17
- package/pi-extension/index.ts +309 -79
- package/server/bin/send-to-pi.js +97 -42
- package/server/src/clipboard.js +53 -0
- package/server/src/config.js +99 -11
- package/server/src/index.js +153 -67
- package/server/src/launchers/custom.js +55 -0
- package/server/src/launchers/darwin.js +289 -45
- package/server/src/launchers/index.js +51 -8
- package/server/src/launchers/linux.js +60 -36
- package/server/src/launchers/win32.js +112 -66
|
@@ -1,90 +1,334 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* macOS Terminal Launcher for send-to-pi
|
|
2
|
+
* macOS Adaptive Terminal Launcher for send-to-pi
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* Resolution order:
|
|
5
|
+
* 1. Explicit preference (PI_TERMINAL / config.json terminal)
|
|
6
|
+
* 2. Last terminal that actually ran Pi (~/.pi/send-to-pi/last-terminal.json)
|
|
7
|
+
* 3. Currently running supported terminal apps
|
|
8
|
+
* 4. Installed supported terminals (Rex, Ghostty, WezTerm, Warp, kitty, Alacritty, iTerm2)
|
|
9
|
+
* 5. macOS built-in Terminal.app fallback
|
|
8
10
|
*/
|
|
9
11
|
|
|
10
12
|
const { execFile, spawn } = require('child_process');
|
|
11
13
|
const fs = require('fs');
|
|
14
|
+
const os = require('os');
|
|
15
|
+
const path = require('path');
|
|
12
16
|
|
|
13
|
-
function
|
|
17
|
+
function runCommand(cmd, args, timeoutMs = 4000) {
|
|
14
18
|
return new Promise((resolve, reject) => {
|
|
15
|
-
execFile(
|
|
19
|
+
execFile(cmd, args, { timeout: timeoutMs }, (error, stdout, stderr) => {
|
|
16
20
|
if (error) {
|
|
17
|
-
reject(new Error(stderr || error.message));
|
|
21
|
+
reject(new Error((stderr || error.message || '').trim()));
|
|
18
22
|
} else {
|
|
19
|
-
resolve(stdout.trim());
|
|
23
|
+
resolve((stdout || '').trim());
|
|
20
24
|
}
|
|
21
25
|
});
|
|
22
26
|
});
|
|
23
27
|
}
|
|
24
28
|
|
|
25
|
-
|
|
26
|
-
|
|
29
|
+
function runAppleScript(script) {
|
|
30
|
+
return runCommand('osascript', ['-e', script], 5000);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function shellQuoteSingle(value) {
|
|
34
|
+
return "'" + String(value).replace(/'/g, "'\\''") + "'";
|
|
35
|
+
}
|
|
27
36
|
|
|
28
|
-
|
|
29
|
-
|
|
37
|
+
function spawnDetached(cmd, args) {
|
|
38
|
+
return new Promise((resolve) => {
|
|
30
39
|
try {
|
|
31
|
-
const child = spawn(
|
|
32
|
-
|
|
33
|
-
|
|
40
|
+
const child = spawn(cmd, args, { detached: true, stdio: 'ignore' });
|
|
41
|
+
let failed = false;
|
|
42
|
+
child.on('error', () => {
|
|
43
|
+
failed = true;
|
|
44
|
+
resolve(false);
|
|
34
45
|
});
|
|
35
46
|
child.unref();
|
|
36
|
-
|
|
37
|
-
|
|
47
|
+
setTimeout(() => {
|
|
48
|
+
if (!failed) resolve(true);
|
|
49
|
+
}, 200);
|
|
50
|
+
} catch (_err) {
|
|
51
|
+
resolve(false);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function findRexBinary(lastTerminal) {
|
|
57
|
+
const candidates = [];
|
|
58
|
+
if (lastTerminal && lastTerminal.rexBin) {
|
|
59
|
+
candidates.push(lastTerminal.rexBin);
|
|
60
|
+
}
|
|
61
|
+
if (process.env.REX_BIN_DIR) {
|
|
62
|
+
candidates.push(path.join(process.env.REX_BIN_DIR, 'rex'));
|
|
38
63
|
}
|
|
64
|
+
const home = os.homedir();
|
|
65
|
+
candidates.push(path.join(home, '.local', 'bin', 'rex'));
|
|
66
|
+
candidates.push('/usr/local/bin/rex');
|
|
39
67
|
|
|
40
|
-
|
|
68
|
+
// Scan RexServer Versions directory (prefer Current or highest numeric build).
|
|
69
|
+
const versionsDir = path.join(
|
|
70
|
+
home,
|
|
71
|
+
'Library',
|
|
72
|
+
'Application Support',
|
|
73
|
+
'com.superlogical.rex',
|
|
74
|
+
'RexServer',
|
|
75
|
+
'Versions'
|
|
76
|
+
);
|
|
77
|
+
if (fs.existsSync(versionsDir)) {
|
|
78
|
+
candidates.push(path.join(versionsDir, 'Current', 'Rex Server.app', 'Contents', 'MacOS', 'rex'));
|
|
41
79
|
try {
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
80
|
+
const entries = fs
|
|
81
|
+
.readdirSync(versionsDir)
|
|
82
|
+
.filter((name) => /^\d+$/.test(name))
|
|
83
|
+
.sort((a, b) => Number(b) - Number(a));
|
|
84
|
+
for (const ver of entries) {
|
|
85
|
+
candidates.push(path.join(versionsDir, ver, 'Rex Server.app', 'Contents', 'MacOS', 'rex'));
|
|
86
|
+
}
|
|
48
87
|
} catch (_err) {}
|
|
49
88
|
}
|
|
50
89
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
90
|
+
for (const binPath of candidates) {
|
|
91
|
+
if (binPath && fs.existsSync(binPath)) {
|
|
92
|
+
return binPath;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function getRunningAppNames() {
|
|
99
|
+
try {
|
|
100
|
+
const out = await runAppleScript(
|
|
101
|
+
'tell application "System Events" to get name of every application process whose background only is false'
|
|
102
|
+
);
|
|
103
|
+
return new Set(
|
|
104
|
+
out
|
|
105
|
+
.split(',')
|
|
106
|
+
.map((s) => s.trim().toLowerCase())
|
|
107
|
+
.filter(Boolean)
|
|
108
|
+
);
|
|
109
|
+
} catch (_err) {
|
|
110
|
+
return new Set();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function hasApp(appNames) {
|
|
115
|
+
const list = Array.isArray(appNames) ? appNames : [appNames];
|
|
116
|
+
const homeApps = path.join(os.homedir(), 'Applications');
|
|
117
|
+
return list.some(
|
|
118
|
+
(name) =>
|
|
119
|
+
fs.existsSync(path.join('/Applications', `${name}.app`)) ||
|
|
120
|
+
fs.existsSync(path.join(homeApps, `${name}.app`))
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const ADAPTERS = {
|
|
125
|
+
rex: {
|
|
126
|
+
label: 'Rex',
|
|
127
|
+
processNames: ['rex'],
|
|
128
|
+
isInstalled: (lastTerminal) => Boolean(findRexBinary(lastTerminal) || hasApp(['Rex', 'Rex Beta'])),
|
|
129
|
+
launch: async (workDir, lastTerminal) => {
|
|
130
|
+
const rexBin = findRexBinary(lastTerminal);
|
|
131
|
+
if (!rexBin) return false;
|
|
132
|
+
if (hasApp('Rex')) {
|
|
133
|
+
await spawnDetached('open', ['-a', 'Rex']);
|
|
134
|
+
} else if (hasApp('Rex Beta')) {
|
|
135
|
+
await spawnDetached('open', ['-a', 'Rex Beta']);
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
await runCommand(rexBin, ['run', '--interactive', '--cwd', workDir, '--keep-open', '--', 'pi'], 6000);
|
|
139
|
+
return true;
|
|
140
|
+
} catch (_err) {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
ghostty: {
|
|
146
|
+
label: 'Ghostty',
|
|
147
|
+
processNames: ['ghostty'],
|
|
148
|
+
isInstalled: () => hasApp('Ghostty'),
|
|
149
|
+
launch: async (workDir) => {
|
|
150
|
+
const cmd = `cd ${shellQuoteSingle(workDir)} && pi`;
|
|
151
|
+
return await spawnDetached('open', ['-na', 'Ghostty', '--args', '-e', 'sh', '-lc', cmd]);
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
wezterm: {
|
|
155
|
+
label: 'WezTerm',
|
|
156
|
+
processNames: ['wezterm', 'wezterm-gui'],
|
|
157
|
+
isInstalled: () => hasApp('WezTerm'),
|
|
158
|
+
launch: async (workDir) => {
|
|
159
|
+
return await spawnDetached('open', ['-a', 'WezTerm', '--args', 'start', '--cwd', workDir, 'pi']);
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
warp: {
|
|
163
|
+
label: 'Warp',
|
|
164
|
+
processNames: ['warp'],
|
|
165
|
+
isInstalled: () => hasApp('Warp'),
|
|
166
|
+
launch: async (workDir) => {
|
|
167
|
+
const uri = `warp://action/new_tab?path=${encodeURIComponent(workDir)}`;
|
|
168
|
+
const opened = await spawnDetached('open', [uri]);
|
|
169
|
+
if (!opened) return false;
|
|
170
|
+
// Launch pi via Warp tab; if AppleScript keystroke isn't permitted, Warp still opens in workDir.
|
|
171
|
+
try {
|
|
172
|
+
await new Promise((r) => setTimeout(r, 400));
|
|
173
|
+
await runAppleScript('tell application "Warp" to activate');
|
|
174
|
+
await runAppleScript('tell application "System Events" to keystroke "pi" & return');
|
|
175
|
+
} catch (_err) {}
|
|
176
|
+
return true;
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
kitty: {
|
|
180
|
+
label: 'kitty',
|
|
181
|
+
processNames: ['kitty'],
|
|
182
|
+
isInstalled: () => hasApp('kitty'),
|
|
183
|
+
launch: async (workDir) => {
|
|
184
|
+
return await spawnDetached('open', ['-a', 'kitty', '--args', '--directory', workDir, 'sh', '-lc', 'pi']);
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
alacritty: {
|
|
188
|
+
label: 'Alacritty',
|
|
189
|
+
processNames: ['alacritty'],
|
|
190
|
+
isInstalled: () => hasApp('Alacritty'),
|
|
191
|
+
launch: async (workDir) => {
|
|
192
|
+
return await spawnDetached('open', [
|
|
193
|
+
'-na',
|
|
194
|
+
'Alacritty',
|
|
195
|
+
'--args',
|
|
196
|
+
'--working-directory',
|
|
197
|
+
workDir,
|
|
198
|
+
'-e',
|
|
199
|
+
'sh',
|
|
200
|
+
'-lc',
|
|
201
|
+
'pi',
|
|
202
|
+
]);
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
iterm2: {
|
|
206
|
+
label: 'iTerm2',
|
|
207
|
+
processNames: ['iterm2', 'iterm'],
|
|
208
|
+
isInstalled: () => hasApp(['iTerm', 'iTerm2']),
|
|
209
|
+
launch: async (workDir) => {
|
|
55
210
|
const itermScript = `
|
|
56
211
|
tell application "iTerm"
|
|
57
212
|
activate
|
|
58
213
|
try
|
|
59
214
|
set newWindow to (create window with default profile)
|
|
60
215
|
tell current session of newWindow
|
|
61
|
-
write text "cd " & quoted form of "${workDir}" & " && pi"
|
|
216
|
+
write text "cd " & quoted form of "${workDir.replace(/"/g, '\\"')}" & " && pi"
|
|
62
217
|
end tell
|
|
63
218
|
on error
|
|
64
|
-
-- Fallback for older iTerm versions
|
|
65
219
|
tell current window
|
|
66
220
|
create tab with default profile
|
|
67
|
-
tell current session to write text "cd " & quoted form of "${workDir}" & " && pi"
|
|
221
|
+
tell current session to write text "cd " & quoted form of "${workDir.replace(/"/g, '\\"')}" & " && pi"
|
|
68
222
|
end tell
|
|
69
223
|
end try
|
|
70
224
|
end tell
|
|
71
225
|
`;
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
226
|
+
try {
|
|
227
|
+
await runAppleScript(itermScript);
|
|
228
|
+
return true;
|
|
229
|
+
} catch (_err) {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
},
|
|
234
|
+
terminal: {
|
|
235
|
+
label: 'Terminal.app',
|
|
236
|
+
processNames: ['terminal'],
|
|
237
|
+
isInstalled: () => true,
|
|
238
|
+
launch: async (workDir) => {
|
|
239
|
+
const terminalScript = `
|
|
240
|
+
tell application "Terminal"
|
|
241
|
+
activate
|
|
242
|
+
do script "cd " & quoted form of "${workDir.replace(/"/g, '\\"')}" & " && pi"
|
|
243
|
+
end tell
|
|
244
|
+
`;
|
|
245
|
+
try {
|
|
246
|
+
await runAppleScript(terminalScript);
|
|
247
|
+
return true;
|
|
248
|
+
} catch (_err) {
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
},
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
function normalizeTerminalKey(name) {
|
|
256
|
+
const key = (name || '').trim().toLowerCase();
|
|
257
|
+
if (!key) return '';
|
|
258
|
+
if (key === 'iterm' || key === 'iterm.app') return 'iterm2';
|
|
259
|
+
if (key === 'apple_terminal' || key === 'terminal.app') return 'terminal';
|
|
260
|
+
if (key === 'warpterminal') return 'warp';
|
|
261
|
+
return ADAPTERS[key] ? key : '';
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
async function launchDarwin(workDir, options = {}) {
|
|
265
|
+
const preferred = normalizeTerminalKey(options.preferredTerminal || process.env.PI_TERMINAL);
|
|
266
|
+
const lastKey = normalizeTerminalKey(options.lastTerminal && options.lastTerminal.terminal);
|
|
267
|
+
const defaultOrder = ['rex', 'ghostty', 'wezterm', 'warp', 'kitty', 'alacritty', 'iterm2', 'terminal'];
|
|
268
|
+
|
|
269
|
+
// Build prioritized, deduplicated candidate list.
|
|
270
|
+
const ordered = [];
|
|
271
|
+
const pushUnique = (k) => {
|
|
272
|
+
if (k && ADAPTERS[k] && !ordered.includes(k)) ordered.push(k);
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
// 1. Explicit user preference
|
|
276
|
+
pushUnique(preferred);
|
|
277
|
+
// 2. Last terminal that actually ran Pi
|
|
278
|
+
pushUnique(lastKey);
|
|
279
|
+
|
|
280
|
+
// 3. Currently running supported terminal apps
|
|
281
|
+
const runningApps = await getRunningAppNames();
|
|
282
|
+
for (const key of defaultOrder) {
|
|
283
|
+
const adapter = ADAPTERS[key];
|
|
284
|
+
if (adapter.processNames.some((p) => runningApps.has(p))) {
|
|
285
|
+
pushUnique(key);
|
|
76
286
|
}
|
|
77
287
|
}
|
|
78
288
|
|
|
79
|
-
//
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
289
|
+
// 4. Installed apps + Terminal.app fallback
|
|
290
|
+
for (const key of defaultOrder) {
|
|
291
|
+
pushUnique(key);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
for (const key of ordered) {
|
|
295
|
+
const adapter = ADAPTERS[key];
|
|
296
|
+
if (!adapter.isInstalled(options.lastTerminal)) continue;
|
|
297
|
+
const ok = await adapter.launch(workDir, options.lastTerminal);
|
|
298
|
+
if (ok) {
|
|
299
|
+
return { success: true, terminal: adapter.label, key };
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
return { success: false, error: 'Failed to launch any supported macOS terminal.' };
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const FOCUS_APP_MAP = {
|
|
307
|
+
rex: 'Rex',
|
|
308
|
+
ghostty: 'Ghostty',
|
|
309
|
+
iterm2: 'iTerm',
|
|
310
|
+
iterm: 'iTerm',
|
|
311
|
+
warp: 'Warp',
|
|
312
|
+
wezterm: 'WezTerm',
|
|
313
|
+
kitty: 'kitty',
|
|
314
|
+
alacritty: 'Alacritty',
|
|
315
|
+
terminal: 'Terminal',
|
|
316
|
+
vscode: 'Visual Studio Code',
|
|
317
|
+
cursor: 'Cursor',
|
|
318
|
+
zed: 'Zed',
|
|
319
|
+
tabby: 'Tabby',
|
|
320
|
+
hyper: 'Hyper',
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
async function focusDarwinApp(session) {
|
|
324
|
+
if (!session) return false;
|
|
325
|
+
const termKey = (session.terminal || '').toLowerCase();
|
|
326
|
+
const appName = FOCUS_APP_MAP[termKey] || (typeof session.appName === 'string' ? session.appName.trim() : '');
|
|
327
|
+
if (!appName || !/^[a-zA-Z0-9 ._-]+$/.test(appName)) return false;
|
|
328
|
+
return await spawnDetached('open', ['-a', appName]);
|
|
88
329
|
}
|
|
89
330
|
|
|
90
331
|
module.exports = launchDarwin;
|
|
332
|
+
module.exports.focusDarwinApp = focusDarwinApp;
|
|
333
|
+
module.exports.normalizeTerminalKey = normalizeTerminalKey;
|
|
334
|
+
module.exports.findRexBinary = findRexBinary;
|
|
@@ -1,22 +1,58 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Cross-Platform Terminal Launcher Dispatcher
|
|
2
|
+
* Cross-Platform Adaptive Terminal Launcher Dispatcher
|
|
3
|
+
*
|
|
4
|
+
* 4-Layer Strategy:
|
|
5
|
+
* 1. Custom launch command template (PI_LAUNCH_CMD / config.json launchCommand)
|
|
6
|
+
* 2. Last terminal that ran Pi (~/.pi/send-to-pi/last-terminal.json) or user preference
|
|
7
|
+
* 3. Running / installed terminal auto-detection with OS default fallback
|
|
8
|
+
* 4. Clipboard fallback (handled in server/src/index.js if launch fails)
|
|
3
9
|
*/
|
|
4
10
|
|
|
5
|
-
const os = require('os');
|
|
6
11
|
const launchDarwin = require('./darwin');
|
|
7
12
|
const launchWin32 = require('./win32');
|
|
8
13
|
const launchLinux = require('./linux');
|
|
14
|
+
const { launchCustomCommand } = require('./custom');
|
|
15
|
+
const {
|
|
16
|
+
getPreferredTerminal,
|
|
17
|
+
getCustomLaunchCommand,
|
|
18
|
+
readLastTerminal,
|
|
19
|
+
} = require('../config');
|
|
9
20
|
|
|
10
|
-
async function launchTerminal(workDir) {
|
|
11
|
-
const
|
|
21
|
+
async function launchTerminal(workDir, overrides = {}) {
|
|
22
|
+
const customCmd =
|
|
23
|
+
overrides.customLaunchCommand !== undefined
|
|
24
|
+
? overrides.customLaunchCommand
|
|
25
|
+
: getCustomLaunchCommand();
|
|
26
|
+
|
|
27
|
+
// Layer 1: Universal custom command escape hatch
|
|
28
|
+
if (customCmd) {
|
|
29
|
+
const customResult = await launchCustomCommand(customCmd, workDir);
|
|
30
|
+
if (customResult.success) {
|
|
31
|
+
return customResult;
|
|
32
|
+
}
|
|
33
|
+
console.warn(`[launcher] Custom launch command failed (${customResult.error}), falling back to platform adapters.`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const options = {
|
|
37
|
+
preferredTerminal:
|
|
38
|
+
overrides.preferredTerminal !== undefined
|
|
39
|
+
? overrides.preferredTerminal
|
|
40
|
+
: getPreferredTerminal(),
|
|
41
|
+
lastTerminal:
|
|
42
|
+
overrides.lastTerminal !== undefined
|
|
43
|
+
? overrides.lastTerminal
|
|
44
|
+
: readLastTerminal(),
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const platform = overrides.platform || process.platform;
|
|
12
48
|
|
|
13
49
|
switch (platform) {
|
|
14
50
|
case 'darwin':
|
|
15
|
-
return await launchDarwin(workDir);
|
|
51
|
+
return await launchDarwin(workDir, options);
|
|
16
52
|
case 'win32':
|
|
17
|
-
return await launchWin32(workDir);
|
|
53
|
+
return await launchWin32(workDir, options);
|
|
18
54
|
case 'linux':
|
|
19
|
-
return await launchLinux(workDir);
|
|
55
|
+
return await launchLinux(workDir, options);
|
|
20
56
|
default:
|
|
21
57
|
return {
|
|
22
58
|
success: false,
|
|
@@ -25,4 +61,11 @@ async function launchTerminal(workDir) {
|
|
|
25
61
|
}
|
|
26
62
|
}
|
|
27
63
|
|
|
28
|
-
|
|
64
|
+
async function focusTerminal(session) {
|
|
65
|
+
if (process.platform === 'darwin' && typeof launchDarwin.focusDarwinApp === 'function') {
|
|
66
|
+
return await launchDarwin.focusDarwinApp(session);
|
|
67
|
+
}
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
module.exports = { launchTerminal, focusTerminal };
|
|
@@ -1,47 +1,71 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Linux Terminal Launcher for send-to-pi
|
|
3
|
-
*
|
|
4
|
-
* Supports:
|
|
5
|
-
* - x-terminal-emulator
|
|
6
|
-
* - gnome-terminal
|
|
7
|
-
* - konsole
|
|
8
|
-
* - kitty / alacritty
|
|
9
|
-
* - xterm
|
|
2
|
+
* Linux Adaptive Terminal Launcher for send-to-pi
|
|
10
3
|
*/
|
|
11
4
|
|
|
12
5
|
const { spawn } = require('child_process');
|
|
13
6
|
|
|
14
|
-
function trySpawn(cmd, args
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
7
|
+
function trySpawn(cmd, args) {
|
|
8
|
+
return new Promise((resolve) => {
|
|
9
|
+
try {
|
|
10
|
+
const child = spawn(cmd, args, {
|
|
11
|
+
detached: true,
|
|
12
|
+
stdio: 'ignore',
|
|
13
|
+
});
|
|
14
|
+
let failed = false;
|
|
15
|
+
child.on('error', () => {
|
|
16
|
+
failed = true;
|
|
17
|
+
resolve(false);
|
|
18
|
+
});
|
|
19
|
+
child.unref();
|
|
20
|
+
setTimeout(() => {
|
|
21
|
+
if (!failed) resolve(true);
|
|
22
|
+
}, 220);
|
|
23
|
+
} catch (_err) {
|
|
24
|
+
resolve(false);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
26
27
|
}
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
{ cmd: '
|
|
36
|
-
{ cmd: '
|
|
37
|
-
{ cmd: '
|
|
38
|
-
{ cmd: '
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
29
|
+
function shellQuoteSingle(value) {
|
|
30
|
+
return "'" + String(value).replace(/'/g, "'\\''") + "'";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function buildCandidates(workDir) {
|
|
34
|
+
const quoted = shellQuoteSingle(workDir);
|
|
35
|
+
return {
|
|
36
|
+
ghostty: { cmd: 'ghostty', args: ['-e', 'sh', '-lc', `cd ${quoted} && pi; exec ${process.env.SHELL || 'sh'}`] },
|
|
37
|
+
wezterm: { cmd: 'wezterm', args: ['start', '--cwd', workDir, 'pi'] },
|
|
38
|
+
kitty: { cmd: 'kitty', args: ['--directory', workDir, 'sh', '-lc', 'pi; exec sh'] },
|
|
39
|
+
alacritty: { cmd: 'alacritty', args: ['--working-directory', workDir, '-e', 'sh', '-lc', 'pi; exec sh'] },
|
|
40
|
+
'gnome-terminal': { cmd: 'gnome-terminal', args: ['--working-directory', workDir, '--', 'sh', '-lc', 'pi; exec sh'] },
|
|
41
|
+
konsole: { cmd: 'konsole', args: ['--workdir', workDir, '-e', 'sh', '-lc', 'pi; exec sh'] },
|
|
42
|
+
'x-terminal-emulator': { cmd: 'x-terminal-emulator', args: ['-e', `sh -lc "cd ${quoted} && pi; exec sh"`] },
|
|
43
|
+
xterm: { cmd: 'xterm', args: ['-e', `cd ${quoted} && pi; exec sh`] },
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function launchLinux(workDir, options = {}) {
|
|
48
|
+
const candidates = buildCandidates(workDir);
|
|
49
|
+
const preferred = (options.preferredTerminal || process.env.PI_TERMINAL || '').trim().toLowerCase();
|
|
50
|
+
const lastKey = (options.lastTerminal && options.lastTerminal.terminal || '').trim().toLowerCase();
|
|
51
|
+
|
|
52
|
+
const order = [];
|
|
53
|
+
const pushUnique = (k) => {
|
|
54
|
+
if (k && !order.includes(k)) order.push(k);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
pushUnique(preferred);
|
|
58
|
+
pushUnique(lastKey);
|
|
59
|
+
for (const k of Object.keys(candidates)) pushUnique(k);
|
|
60
|
+
|
|
61
|
+
for (const key of order) {
|
|
62
|
+
const item = candidates[key] || {
|
|
63
|
+
cmd: key,
|
|
64
|
+
args: ['-e', `sh -lc "cd ${shellQuoteSingle(workDir)} && pi; exec sh"`],
|
|
65
|
+
};
|
|
66
|
+
const ok = await trySpawn(item.cmd, item.args);
|
|
43
67
|
if (ok) {
|
|
44
|
-
return { success: true, terminal: item.cmd };
|
|
68
|
+
return { success: true, terminal: item.cmd, key };
|
|
45
69
|
}
|
|
46
70
|
}
|
|
47
71
|
|