path.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.convertPosixPathToPattern = exports.convertWindowsPathToPattern = exports.convertPathToPattern = exports.escapePosixPath = exports.escapeWindowsPath = exports.escape = exports.removeLeadingDotSegment = exports.makeAbsolute = exports.unixify = void 0;
  4. const os = require("os");
  5. const path = require("path");
  6. const IS_WINDOWS_PLATFORM = os.platform() === 'win32';
  7. const LEADING_DOT_SEGMENT_CHARACTERS_COUNT = 2; // ./ or .\\
  8. /**
  9. * All non-escaped special characters.
  10. * Posix: ()*?[]{|}, !+@ before (, ! at the beginning, \\ before non-special characters.
  11. * Windows: (){}[], !+@ before (, ! at the beginning.
  12. */
  13. const POSIX_UNESCAPED_GLOB_SYMBOLS_RE = /(\\?)([()*?[\]{|}]|^!|[!+@](?=\()|\\(?![!()*+?@[\]{|}]))/g;
  14. const WINDOWS_UNESCAPED_GLOB_SYMBOLS_RE = /(\\?)([()[\]{}]|^!|[!+@](?=\())/g;
  15. /**
  16. * The device path (\\.\ or \\?\).
  17. * https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats#dos-device-paths
  18. */
  19. const DOS_DEVICE_PATH_RE = /^\\\\([.?])/;
  20. /**
  21. * All backslashes except those escaping special characters.
  22. * Windows: !()+@{}
  23. * https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions
  24. */
  25. const WINDOWS_BACKSLASHES_RE = /\\(?![!()+@[\]{}])/g;
  26. /**
  27. * Designed to work only with simple paths: `dir\\file`.
  28. */
  29. function unixify(filepath) {
  30. return filepath.replace(/\\/g, '/');
  31. }
  32. exports.unixify = unixify;
  33. function makeAbsolute(cwd, filepath) {
  34. return path.resolve(cwd, filepath);
  35. }
  36. exports.makeAbsolute = makeAbsolute;
  37. function removeLeadingDotSegment(entry) {
  38. // We do not use `startsWith` because this is 10x slower than current implementation for some cases.
  39. // eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
  40. if (entry.charAt(0) === '.') {
  41. const secondCharactery = entry.charAt(1);
  42. if (secondCharactery === '/' || secondCharactery === '\\') {
  43. return entry.slice(LEADING_DOT_SEGMENT_CHARACTERS_COUNT);
  44. }
  45. }
  46. return entry;
  47. }
  48. exports.removeLeadingDotSegment = removeLeadingDotSegment;
  49. exports.escape = IS_WINDOWS_PLATFORM ? escapeWindowsPath : escapePosixPath;
  50. function escapeWindowsPath(pattern) {
  51. return pattern.replace(WINDOWS_UNESCAPED_GLOB_SYMBOLS_RE, '\\$2');
  52. }
  53. exports.escapeWindowsPath = escapeWindowsPath;
  54. function escapePosixPath(pattern) {
  55. return pattern.replace(POSIX_UNESCAPED_GLOB_SYMBOLS_RE, '\\$2');
  56. }
  57. exports.escapePosixPath = escapePosixPath;
  58. exports.convertPathToPattern = IS_WINDOWS_PLATFORM ? convertWindowsPathToPattern : convertPosixPathToPattern;
  59. function convertWindowsPathToPattern(filepath) {
  60. return escapeWindowsPath(filepath)
  61. .replace(DOS_DEVICE_PATH_RE, '//$1')
  62. .replace(WINDOWS_BACKSLASHES_RE, '/');
  63. }
  64. exports.convertWindowsPathToPattern = convertWindowsPathToPattern;
  65. function convertPosixPathToPattern(filepath) {
  66. return escapePosixPath(filepath);
  67. }
  68. exports.convertPosixPathToPattern = convertPosixPathToPattern;