* 将可能包含参数的命令字符串拆分为 [可执行文件, ...参数列表] * 例如: "/usr/bin/ghostty --gtk-single-instance=true" → ["/usr/bin/ghostty", "--gtk-single-instance=true"]
(cmd: string)
| 9 | * 例如: "/usr/bin/ghostty --gtk-single-instance=true" → ["/usr/bin/ghostty", "--gtk-single-instance=true"] |
| 10 | */ |
| 11 | function parseCommandString(cmd: string): [string, string[]] { |
| 12 | const parts: string[] = [] |
| 13 | let current = '' |
| 14 | let inQuote: string | null = null |
| 15 | |
| 16 | for (let i = 0; i < cmd.length; i++) { |
| 17 | const ch = cmd[i] |
| 18 | if (inQuote) { |
| 19 | if (ch === inQuote) { |
| 20 | inQuote = null |
| 21 | } else { |
| 22 | current += ch |
| 23 | } |
| 24 | } else if (ch === '"' || ch === "'") { |
| 25 | inQuote = ch |
| 26 | } else if (/\s/.test(ch)) { |
| 27 | if (current) { |
| 28 | parts.push(current) |
| 29 | current = '' |
| 30 | } |
| 31 | } else { |
| 32 | current += ch |
| 33 | } |
| 34 | } |
| 35 | if (current) parts.push(current) |
| 36 | |
| 37 | return [parts[0], parts.slice(1)] |
| 38 | } |
| 39 | |
| 40 | export async function launchApp( |
| 41 | appPath: string, |