utils.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.cleanPath = cleanPath;
  4. exports.convertSlashes = convertSlashes;
  5. exports.isRootDirectory = isRootDirectory;
  6. exports.normalizePath = normalizePath;
  7. const path_1 = require("path");
  8. function cleanPath(path) {
  9. let normalized = (0, path_1.normalize)(path);
  10. // we have to remove the last path separator
  11. // to account for / root path
  12. if (normalized.length > 1 && normalized[normalized.length - 1] === path_1.sep)
  13. normalized = normalized.substring(0, normalized.length - 1);
  14. return normalized;
  15. }
  16. const SLASHES_REGEX = /[\\/]/g;
  17. function convertSlashes(path, separator) {
  18. return path.replace(SLASHES_REGEX, separator);
  19. }
  20. const WINDOWS_ROOT_DIR_REGEX = /^[a-z]:[\\/]$/i;
  21. function isRootDirectory(path) {
  22. return path === "/" || WINDOWS_ROOT_DIR_REGEX.test(path);
  23. }
  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. }