send-to-pi 1.1.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,68 +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
- * - Windows Terminal (wt.exe)
6
- * - PowerShell (powershell.exe fallback)
7
- * - 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.
8
10
  */
9
11
 
10
12
  const { spawn } = require('child_process');
13
+ const fs = require('fs');
11
14
 
12
- function launchWin32(workDir) {
15
+ function spawnAndCheck(cmd, args) {
13
16
  return new Promise((resolve) => {
14
- // 1. 尝试 Windows Terminal (wt.exe)
15
17
  try {
16
- const wtArgs = ['-d', workDir, 'powershell', '-NoExit', '-Command', 'pi'];
17
- const child = spawn('wt.exe', wtArgs, {
18
+ const child = spawn(cmd, args, {
18
19
  detached: true,
19
20
  stdio: 'ignore',
20
21
  });
21
-
22
- child.on('error', (_err) => {
23
- // wt.exe 不存在或失败,回退到 powershell.exe
24
- fallbackPowerShell(workDir, resolve);
22
+ let failed = false;
23
+ child.on('error', () => {
24
+ failed = true;
25
+ resolve(false);
25
26
  });
26
-
27
27
  child.unref();
28
- // 在 Windows 上若成功触发 spawn 且无即时报错即视为已拉起
29
- setTimeout(() => resolve({ success: true, terminal: 'Windows Terminal' }), 300);
30
- } catch (_e) {
31
- fallbackPowerShell(workDir, resolve);
28
+ setTimeout(() => {
29
+ if (!failed) resolve(true);
30
+ }, 280);
31
+ } catch (_err) {
32
+ resolve(false);
32
33
  }
33
34
  });
34
35
  }
35
36
 
36
- function fallbackPowerShell(workDir, resolve) {
37
- try {
38
- const psArgs = ['-NoExit', '-Command', `Set-Location '${workDir}'; pi`];
39
- const child = spawn('powershell.exe', psArgs, {
40
- detached: true,
41
- stdio: 'ignore',
42
- });
37
+ function escapePsSingle(str) {
38
+ return String(str).replace(/'/g, "''");
39
+ }
43
40
 
44
- child.on('error', (_err) => {
45
- fallbackCmd(workDir, resolve);
46
- });
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
+ };
47
103
 
48
- child.unref();
49
- setTimeout(() => resolve({ success: true, terminal: 'PowerShell' }), 300);
50
- } catch (_e) {
51
- fallbackCmd(workDir, resolve);
52
- }
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 : '';
53
109
  }
54
110
 
55
- function fallbackCmd(workDir, resolve) {
56
- try {
57
- const child = spawn('cmd.exe', ['/k', `cd /d "${workDir}" && pi`], {
58
- detached: true,
59
- stdio: 'ignore',
60
- });
61
- child.unref();
62
- resolve({ success: true, terminal: 'cmd.exe' });
63
- } catch (err) {
64
- resolve({ success: false, error: err.message });
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'];
115
+
116
+ const ordered = [];
117
+ const pushUnique = (k) => {
118
+ if (k && ADAPTERS[k] && !ordered.includes(k)) ordered.push(k);
119
+ };
120
+
121
+ pushUnique(preferred);
122
+ pushUnique(lastKey);
123
+ for (const k of defaultOrder) pushUnique(k);
124
+
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
+ }
65
130
  }
131
+
132
+ return { success: false, error: 'Failed to launch any supported Windows terminal.' };
66
133
  }
67
134
 
68
135
  module.exports = launchWin32;