|
1 |
| -const getEscapeFunction = shell => { |
| 1 | +/** |
| 2 | + * @param {"bash" | "zsh" | "powershell"} shell |
| 3 | + * @param {string} command |
| 4 | + * @return {string} |
| 5 | + */ |
| 6 | +const escapeShellCommand = (shell, command) => { |
2 | 7 | switch (shell) {
|
3 | 8 | case 'powershell':
|
4 |
| - return string => { |
5 |
| - string = string |
6 |
| - .replace(/[\u009B\u001B\u0008\0]/gu, '') |
7 |
| - .replace(/\r(?!\n)/gu, '') |
8 |
| - .replace(/(['‛‘’‚])/gu, '$1$1'); |
| 9 | + command = command |
| 10 | + .replace(/[\u009B\u001B\u0008\0]/gu, '') |
| 11 | + .replace(/\r(?!\n)/gu, '') |
| 12 | + .replace(/(['‛‘’‚])/gu, '$1$1'); |
9 | 13 |
|
10 |
| - if (/[\u0085\s]/u.test(string)) { |
11 |
| - string = string.replace(/(?<!\\)(\\*)"/gu, '$1$1""').replace(/(?<!\\)(\\+)$/gu, '$1$1'); |
12 |
| - } else { |
13 |
| - string = string.replace(/(?<!\\)(\\*)"/gu, '$1$1\\"'); |
14 |
| - } |
15 |
| - return string; |
16 |
| - }; |
| 14 | + if (/[\u0085\s]/u.test(command)) { |
| 15 | + command = command.replace(/(?<!\\)(\\*)"/gu, '$1$1""').replace(/(?<!\\)(\\+)$/gu, '$1$1'); |
| 16 | + } else { |
| 17 | + command = command.replace(/(?<!\\)(\\*)"/gu, '$1$1\\"'); |
| 18 | + } |
| 19 | + return command; |
17 | 20 | case 'bash':
|
18 | 21 | case 'zsh':
|
19 |
| - return string => { |
20 |
| - return string |
21 |
| - .replace(/[\0\u0008\u001B\u009B]/gu, '') |
22 |
| - .replace(/\r(?!\n)/gu, '') |
23 |
| - .replace(/'/gu, "'\\''"); |
24 |
| - }; |
| 22 | + return command |
| 23 | + .replace(/[\0\u0008\u001B\u009B]/gu, '') |
| 24 | + .replace(/\r(?!\n)/gu, '') |
| 25 | + .replace(/'/gu, "'\\''"); |
| 26 | + |
25 | 27 | default:
|
26 |
| - return string => string; |
| 28 | + return command; |
27 | 29 | }
|
28 | 30 | };
|
29 | 31 |
|
30 |
| -const wrapInSingleQuotes = string => `'${string}'`; |
| 32 | +/** |
| 33 | + * @param {string} command |
| 34 | + * @return {string} |
| 35 | + */ |
| 36 | +const wrapInSingleQuotes = command => `'${command}'`; |
31 | 37 |
|
32 |
| -module.exports = { getEscapeFunction, wrapInSingleQuotes }; |
| 38 | +module.exports = { escapeShellCommand, wrapInSingleQuotes }; |
0 commit comments