path.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.toFilename = exports.convertPath = exports.ppath = exports.npath = exports.Filename = exports.PortablePath = void 0;
  4. const tslib_1 = require("tslib");
  5. const path_1 = tslib_1.__importDefault(require("path"));
  6. var PathType;
  7. (function (PathType) {
  8. PathType[PathType["File"] = 0] = "File";
  9. PathType[PathType["Portable"] = 1] = "Portable";
  10. PathType[PathType["Native"] = 2] = "Native";
  11. })(PathType || (PathType = {}));
  12. exports.PortablePath = {
  13. root: `/`,
  14. dot: `.`,
  15. parent: `..`,
  16. };
  17. exports.Filename = {
  18. nodeModules: `node_modules`,
  19. manifest: `package.json`,
  20. lockfile: `yarn.lock`,
  21. virtual: `__virtual__`,
  22. /**
  23. * @deprecated
  24. */
  25. pnpJs: `.pnp.js`,
  26. pnpCjs: `.pnp.cjs`,
  27. rc: `.yarnrc.yml`,
  28. };
  29. exports.npath = Object.create(path_1.default);
  30. exports.ppath = Object.create(path_1.default.posix);
  31. exports.npath.cwd = () => process.cwd();
  32. exports.ppath.cwd = () => toPortablePath(process.cwd());
  33. exports.ppath.resolve = (...segments) => {
  34. if (segments.length > 0 && exports.ppath.isAbsolute(segments[0])) {
  35. return path_1.default.posix.resolve(...segments);
  36. }
  37. else {
  38. return path_1.default.posix.resolve(exports.ppath.cwd(), ...segments);
  39. }
  40. };
  41. const contains = function (pathUtils, from, to) {
  42. from = pathUtils.normalize(from);
  43. to = pathUtils.normalize(to);
  44. if (from === to)
  45. return `.`;
  46. if (!from.endsWith(pathUtils.sep))
  47. from = (from + pathUtils.sep);
  48. if (to.startsWith(from)) {
  49. return to.slice(from.length);
  50. }
  51. else {
  52. return null;
  53. }
  54. };
  55. exports.npath.fromPortablePath = fromPortablePath;
  56. exports.npath.toPortablePath = toPortablePath;
  57. exports.npath.contains = (from, to) => contains(exports.npath, from, to);
  58. exports.ppath.contains = (from, to) => contains(exports.ppath, from, to);
  59. const WINDOWS_PATH_REGEXP = /^([a-zA-Z]:.*)$/;
  60. const UNC_WINDOWS_PATH_REGEXP = /^\/\/(\.\/)?(.*)$/;
  61. const PORTABLE_PATH_REGEXP = /^\/([a-zA-Z]:.*)$/;
  62. const UNC_PORTABLE_PATH_REGEXP = /^\/unc\/(\.dot\/)?(.*)$/;
  63. // Path should look like "/N:/berry/scripts/plugin-pack.js"
  64. // And transform to "N:\berry\scripts\plugin-pack.js"
  65. function fromPortablePath(p) {
  66. if (process.platform !== `win32`)
  67. return p;
  68. let portablePathMatch, uncPortablePathMatch;
  69. if ((portablePathMatch = p.match(PORTABLE_PATH_REGEXP)))
  70. p = portablePathMatch[1];
  71. else if ((uncPortablePathMatch = p.match(UNC_PORTABLE_PATH_REGEXP)))
  72. p = `\\\\${uncPortablePathMatch[1] ? `.\\` : ``}${uncPortablePathMatch[2]}`;
  73. else
  74. return p;
  75. return p.replace(/\//g, `\\`);
  76. }
  77. // Path should look like "N:/berry/scripts/plugin-pack.js"
  78. // And transform to "/N:/berry/scripts/plugin-pack.js"
  79. function toPortablePath(p) {
  80. if (process.platform !== `win32`)
  81. return p;
  82. p = p.replace(/\\/g, `/`);
  83. let windowsPathMatch, uncWindowsPathMatch;
  84. if ((windowsPathMatch = p.match(WINDOWS_PATH_REGEXP)))
  85. p = `/${windowsPathMatch[1]}`;
  86. else if ((uncWindowsPathMatch = p.match(UNC_WINDOWS_PATH_REGEXP)))
  87. p = `/unc/${uncWindowsPathMatch[1] ? `.dot/` : ``}${uncWindowsPathMatch[2]}`;
  88. return p;
  89. }
  90. function convertPath(targetPathUtils, sourcePath) {
  91. return (targetPathUtils === exports.npath ? fromPortablePath(sourcePath) : toPortablePath(sourcePath));
  92. }
  93. exports.convertPath = convertPath;
  94. function toFilename(filename) {
  95. if (exports.npath.parse(filename).dir !== `` || exports.ppath.parse(filename).dir !== ``)
  96. throw new Error(`Invalid filename: "${filename}"`);
  97. return filename;
  98. }
  99. exports.toFilename = toFilename;