escape.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {platform} from 'node:process';
  2. import {stripVTControlCharacters} from 'node:util';
  3. // Compute `result.command` and `result.escapedCommand`
  4. export const joinCommand = (filePath, rawArguments) => {
  5. const fileAndArguments = [filePath, ...rawArguments];
  6. const command = fileAndArguments.join(' ');
  7. const escapedCommand = fileAndArguments
  8. .map(fileAndArgument => quoteString(escapeControlCharacters(fileAndArgument)))
  9. .join(' ');
  10. return {command, escapedCommand};
  11. };
  12. // Remove ANSI sequences and escape control characters and newlines
  13. export const escapeLines = lines => stripVTControlCharacters(lines)
  14. .split('\n')
  15. .map(line => escapeControlCharacters(line))
  16. .join('\n');
  17. const escapeControlCharacters = line => line.replaceAll(SPECIAL_CHAR_REGEXP, character => escapeControlCharacter(character));
  18. const escapeControlCharacter = character => {
  19. const commonEscape = COMMON_ESCAPES[character];
  20. if (commonEscape !== undefined) {
  21. return commonEscape;
  22. }
  23. const codepoint = character.codePointAt(0);
  24. const codepointHex = codepoint.toString(16);
  25. return codepoint <= ASTRAL_START
  26. ? `\\u${codepointHex.padStart(4, '0')}`
  27. : `\\U${codepointHex}`;
  28. };
  29. // Characters that would create issues when printed are escaped using the \u or \U notation.
  30. // Those include control characters and newlines.
  31. // The \u and \U notation is Bash specific, but there is no way to do this in a shell-agnostic way.
  32. // Some shells do not even have a way to print those characters in an escaped fashion.
  33. // Therefore, we prioritize printing those safely, instead of allowing those to be copy-pasted.
  34. // List of Unicode character categories: https://www.fileformat.info/info/unicode/category/index.htm
  35. const getSpecialCharRegExp = () => {
  36. try {
  37. // This throws when using Node.js without ICU support.
  38. // When using a RegExp literal, this would throw at parsing-time, instead of runtime.
  39. // eslint-disable-next-line prefer-regex-literals
  40. return new RegExp('\\p{Separator}|\\p{Other}', 'gu');
  41. } catch {
  42. // Similar to the above RegExp, but works even when Node.js has been built without ICU support.
  43. // Unlike the above RegExp, it only covers whitespaces and C0/C1 control characters.
  44. // It does not cover some edge cases, such as Unicode reserved characters.
  45. // See https://github.com/sindresorhus/execa/issues/1143
  46. // eslint-disable-next-line no-control-regex
  47. return /[\s\u0000-\u001F\u007F-\u009F\u00AD]/g;
  48. }
  49. };
  50. const SPECIAL_CHAR_REGEXP = getSpecialCharRegExp();
  51. // Accepted by $'...' in Bash.
  52. // Exclude \a \e \v which are accepted in Bash but not in JavaScript (except \v) and JSON.
  53. const COMMON_ESCAPES = {
  54. ' ': ' ',
  55. '\b': '\\b',
  56. '\f': '\\f',
  57. '\n': '\\n',
  58. '\r': '\\r',
  59. '\t': '\\t',
  60. };
  61. // Up until that codepoint, \u notation can be used instead of \U
  62. const ASTRAL_START = 65_535;
  63. // Some characters are shell-specific, i.e. need to be escaped when the command is copy-pasted then run.
  64. // Escaping is shell-specific. We cannot know which shell is used: `process.platform` detection is not enough.
  65. // For example, Windows users could be using `cmd.exe`, Powershell or Bash for Windows which all use different escaping.
  66. // We use '...' on Unix, which is POSIX shell compliant and escape all characters but ' so this is fairly safe.
  67. // On Windows, we assume cmd.exe is used and escape with "...", which also works with Powershell.
  68. const quoteString = escapedArgument => {
  69. if (NO_ESCAPE_REGEXP.test(escapedArgument)) {
  70. return escapedArgument;
  71. }
  72. return platform === 'win32'
  73. ? `"${escapedArgument.replaceAll('"', '""')}"`
  74. : `'${escapedArgument.replaceAll('\'', '\'\\\'\'')}'`;
  75. };
  76. const NO_ESCAPE_REGEXP = /^[\w./-]+$/;