utils.js 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.normalizePath = 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. function normalizePath(path, options) {
  20. const { resolvePaths, normalizePath, pathSeparator } = options;
  21. const pathNeedsCleaning = (process.platform === "win32" && path.includes("/")) ||
  22. path.startsWith(".");
  23. if (resolvePaths)
  24. path = (0, path_1.resolve)(path);
  25. if (normalizePath || pathNeedsCleaning)
  26. path = cleanPath(path);
  27. if (path === ".")
  28. return "";
  29. const needsSeperator = path[path.length - 1] !== pathSeparator;
  30. return convertSlashes(needsSeperator ? path + pathSeparator : path, pathSeparator);
  31. }
  32. exports.normalizePath = normalizePath;