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.
@@ -1,89 +1,135 @@
1
1
  /**
2
- * Windows Terminal Launcher for send-to-pi
2
+ * Windows Adaptive Terminal Launcher for send-to-pi
3
3
  *
4
- * Supports:
5
- * - Tern Terminal (D:\Tern\tern.exe)
6
- * - Windows Terminal (wt.exe fallback)
7
- * - PowerShell (powershell.exe fallback)
8
- * - Command Prompt (cmd.exe fallback)
4
+ * Resolution order:
5
+ * 1. Explicit preference (PI_TERMINAL / config.json) or last-terminal.json
6
+ * 2. Windows Terminal (wt.exe)
7
+ * 3. PowerShell (pwsh.exe / powershell.exe)
8
+ * 4. Command Prompt (cmd.exe)
9
+ * Also supports WezTerm, Alacritty, and Tern when configured or detected.
9
10
  */
10
11
 
11
12
  const { spawn } = require('child_process');
12
13
  const fs = require('fs');
13
14
 
14
- function launchWin32(workDir) {
15
+ function spawnAndCheck(cmd, args) {
15
16
  return new Promise((resolve) => {
16
- // 1. 优先尝试 Tern Terminal (主力终端)
17
- const ternPath = 'D:\\Tern\\tern.exe';
18
- if (fs.existsSync(ternPath)) {
19
- try {
20
- const ternArgs = ['-e', 'powershell', '-NoExit', '-Command', `Set-Location '${workDir}'; pi`];
21
- const child = spawn(ternPath, ternArgs, {
22
- detached: true,
23
- stdio: 'ignore',
24
- });
25
- child.on('error', () => fallbackWT(workDir, resolve));
26
- child.unref();
27
- setTimeout(() => resolve({ success: true, terminal: 'Tern' }), 300);
28
- return;
29
- } catch (_e) {
30
- // continue
31
- }
17
+ try {
18
+ const child = spawn(cmd, args, {
19
+ detached: true,
20
+ stdio: 'ignore',
21
+ });
22
+ let failed = false;
23
+ child.on('error', () => {
24
+ failed = true;
25
+ resolve(false);
26
+ });
27
+ child.unref();
28
+ setTimeout(() => {
29
+ if (!failed) resolve(true);
30
+ }, 280);
31
+ } catch (_err) {
32
+ resolve(false);
32
33
  }
33
- fallbackWT(workDir, resolve);
34
34
  });
35
35
  }
36
36
 
37
- function fallbackWT(workDir, resolve) {
38
- // 2. 回退尝试 Windows Terminal (wt.exe)
39
- try {
40
- const wtArgs = ['-d', workDir, 'powershell', '-NoExit', '-Command', 'pi'];
41
- const child = spawn('wt.exe', wtArgs, {
42
- detached: true,
43
- stdio: 'ignore',
44
- });
37
+ function escapePsSingle(str) {
38
+ return String(str).replace(/'/g, "''");
39
+ }
45
40
 
46
- child.on('error', (_err) => {
47
- fallbackPowerShell(workDir, resolve);
48
- });
41
+ const ADAPTERS = {
42
+ wt: {
43
+ label: 'Windows Terminal',
44
+ launch: (workDir) =>
45
+ spawnAndCheck('wt.exe', ['-d', workDir, 'powershell', '-NoExit', '-Command', 'pi']),
46
+ },
47
+ wezterm: {
48
+ label: 'WezTerm',
49
+ launch: (workDir) =>
50
+ spawnAndCheck('wezterm-gui.exe', ['start', '--cwd', workDir, 'pi']),
51
+ },
52
+ alacritty: {
53
+ label: 'Alacritty',
54
+ launch: (workDir) =>
55
+ spawnAndCheck('alacritty.exe', [
56
+ '--working-directory',
57
+ workDir,
58
+ '-e',
59
+ 'powershell',
60
+ '-NoExit',
61
+ '-Command',
62
+ 'pi',
63
+ ]),
64
+ },
65
+ tern: {
66
+ label: 'Tern',
67
+ launch: (workDir) => {
68
+ const ternPath = process.env.TERN_PATH || 'D:\\Tern\\tern.exe';
69
+ if (!fs.existsSync(ternPath)) return Promise.resolve(false);
70
+ return spawnAndCheck(ternPath, [
71
+ '-e',
72
+ 'powershell',
73
+ '-NoExit',
74
+ '-Command',
75
+ `Set-Location '${escapePsSingle(workDir)}'; pi`,
76
+ ]);
77
+ },
78
+ },
79
+ pwsh: {
80
+ label: 'PowerShell 7',
81
+ launch: (workDir) =>
82
+ spawnAndCheck('pwsh.exe', [
83
+ '-NoExit',
84
+ '-Command',
85
+ `Set-Location '${escapePsSingle(workDir)}'; pi`,
86
+ ]),
87
+ },
88
+ powershell: {
89
+ label: 'PowerShell',
90
+ launch: (workDir) =>
91
+ spawnAndCheck('powershell.exe', [
92
+ '-NoExit',
93
+ '-Command',
94
+ `Set-Location '${escapePsSingle(workDir)}'; pi`,
95
+ ]),
96
+ },
97
+ cmd: {
98
+ label: 'cmd.exe',
99
+ launch: (workDir) =>
100
+ spawnAndCheck('cmd.exe', ['/k', `cd /d "${workDir}" && pi`]),
101
+ },
102
+ };
49
103
 
50
- child.unref();
51
- setTimeout(() => resolve({ success: true, terminal: 'Windows Terminal' }), 300);
52
- } catch (_e) {
53
- fallbackPowerShell(workDir, resolve);
54
- }
104
+ function normalizeKey(name) {
105
+ const k = (name || '').trim().toLowerCase();
106
+ if (!k) return '';
107
+ if (k === 'windows-terminal' || k === 'windowsterminal') return 'wt';
108
+ return ADAPTERS[k] ? k : '';
55
109
  }
56
110
 
57
- function fallbackPowerShell(workDir, resolve) {
58
- try {
59
- const psArgs = ['-NoExit', '-Command', `Set-Location '${workDir}'; pi`];
60
- const child = spawn('powershell.exe', psArgs, {
61
- detached: true,
62
- stdio: 'ignore',
63
- });
111
+ async function launchWin32(workDir, options = {}) {
112
+ const preferred = normalizeKey(options.preferredTerminal || process.env.PI_TERMINAL);
113
+ const lastKey = normalizeKey(options.lastTerminal && options.lastTerminal.terminal);
114
+ const defaultOrder = ['wt', 'pwsh', 'powershell', 'cmd'];
64
115
 
65
- child.on('error', (_err) => {
66
- fallbackCmd(workDir, resolve);
67
- });
116
+ const ordered = [];
117
+ const pushUnique = (k) => {
118
+ if (k && ADAPTERS[k] && !ordered.includes(k)) ordered.push(k);
119
+ };
68
120
 
69
- child.unref();
70
- setTimeout(() => resolve({ success: true, terminal: 'PowerShell' }), 300);
71
- } catch (_e) {
72
- fallbackCmd(workDir, resolve);
73
- }
74
- }
121
+ pushUnique(preferred);
122
+ pushUnique(lastKey);
123
+ for (const k of defaultOrder) pushUnique(k);
75
124
 
76
- function fallbackCmd(workDir, resolve) {
77
- try {
78
- const child = spawn('cmd.exe', ['/k', `cd /d "${workDir}" && pi`], {
79
- detached: true,
80
- stdio: 'ignore',
81
- });
82
- child.unref();
83
- resolve({ success: true, terminal: 'cmd.exe' });
84
- } catch (err) {
85
- resolve({ success: false, error: err.message });
125
+ for (const key of ordered) {
126
+ const ok = await ADAPTERS[key].launch(workDir);
127
+ if (ok) {
128
+ return { success: true, terminal: ADAPTERS[key].label, key };
129
+ }
86
130
  }
131
+
132
+ return { success: false, error: 'Failed to launch any supported Windows terminal.' };
87
133
  }
88
134
 
89
135
  module.exports = launchWin32;