utils.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.normalizePath = exports.isRootDirectory = exports.convertSlashes = exports.cleanPath = void 0;
  4. const path_1 = require("path");
  5. function cleanPath(path) {
  6. let normalized = (0, path_1.normalize)(path);
  7. // we have to remove the last path separator
  8. // to account for / root path
  9. if (normalized.length > 1 && normalized[normalized.length - 1] === path_1.sep)
  10. normalized = normalized.substring(0, normalized.length - 1);
  11. return normalized;
  12. }
  13. exports.cleanPath = cleanPath;
  14. const SLASHES_REGEX = /[\\/]/g;
  15. function convertSlashes(path, separator) {
  16. return path.replace(SLASHES_REGEX, separator);
  17. }
  18. exports.convertSlashes = convertSlashes;
  19. const WINDOWS_ROOT_DIR_REGEX = /^[a-z]:[\\/]$/i;
  20. function isRootDirectory(path) {
  21. return path === "/" || WINDOWS_ROOT_DIR_REGEX.test(path);
  22. }
  23. exports.isRootDirectory = isRootDirectory;
  24. function normalizePath(path, options) {
  25. const { resolvePaths, normalizePath, pathSeparator } = options;
  26. const pathNeedsCleaning = (process.platform === "win32" && path.includes("/")) ||
  27. path.startsWith(".");
  28. if (resolvePaths)
  29. path = (0, path_1.resolve)(path);
  30. if (normalizePath || pathNeedsCleaning)
  31. path = cleanPath(path);
  32. if (path === ".")
  33. return "";
  34. const needsSeperator = path[path.length - 1] !== pathSeparator;
  35. return convertSlashes(needsSeperator ? path + pathSeparator : path, pathSeparator);
  36. }
  37. exports.normalizePath = normalizePath;