UNPKG

@difizen/mana-common

Version:

274 lines (266 loc) 11.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Path = void 0; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var Path = exports.Path = /*#__PURE__*/function () { /** * The raw should be normalized, meaning that only '/' is allowed as a path separator. */ function Path(raw) { _classCallCheck(this, Path); _defineProperty(this, "isAbsolute", void 0); _defineProperty(this, "isRoot", void 0); _defineProperty(this, "isDrive", void 0); _defineProperty(this, "root", void 0); _defineProperty(this, "base", void 0); _defineProperty(this, "name", void 0); _defineProperty(this, "ext", void 0); _defineProperty(this, "_dir", void 0); _defineProperty(this, "raw", void 0); this.raw = Path.normalizePath(raw); var lastIndex = this.raw.lastIndexOf(Path.separator); this.isAbsolute = this.raw.indexOf(Path.separator) === 0; this.base = lastIndex === -1 ? this.raw : this.raw.substring(lastIndex + 1); this.isDrive = Path.isDrive(this.base); this.isRoot = this.isAbsolute && lastIndex === 0 && (!this.base || this.isDrive); this.root = Path.toRoot(this); var extIndex = this.base.lastIndexOf('.'); this.name = extIndex === -1 ? this.base : this.base.substring(0, extIndex); this.ext = extIndex === -1 ? '' : this.base.substring(extIndex); } /** * Returns the parent directory if it exists (`hasDir === true`) or `this` otherwise. */ _createClass(Path, [{ key: "isRelative", get: function get() { return !this.isAbsolute; } }, { key: "dir", get: function get() { if (this._dir === undefined) { this._dir = this.doGetDir(); } return this._dir; } /** * Returns `true` if this has a parent directory, `false` otherwise. * * _This implementation returns `true` if and only if this is not the root dir and * there is a path separator in the raw path._ */ }, { key: "hasDir", get: function get() { return !this.isRoot && this.raw.lastIndexOf(Path.separator) !== -1; } }, { key: "doGetDir", value: function doGetDir() { if (!this.hasDir) { return this; } var lastIndex = this.raw.lastIndexOf(Path.separator); if (this.isAbsolute) { var firstIndex = this.raw.indexOf(Path.separator); if (firstIndex === lastIndex) { return new Path(this.raw.substr(0, firstIndex + 1)); } } return new Path(this.raw.substr(0, lastIndex)); } }, { key: "includes", value: function includes(path) { return !!Path.relative(this, path); } }, { key: "toString", value: function toString() { return this.raw; } }], [{ key: "isDrive", value: function isDrive(segment) { return segment.endsWith(':'); } /** * vscode-uri always normalizes drive letters to lower case: * https://github.com/Microsoft/vscode-uri/blob/b1d3221579f97f28a839b6f996d76fc45e9964d8/src/index.ts#L1025 */ }, { key: "normalizeDrive", value: function normalizeDrive(path) { if (path.length > 3 && path[0] === '/' && path[2] === ':') { var code = path.charCodeAt(1); if (code >= 65 && code <= 90) { path = "/".concat(path[1].toLowerCase(), ":").concat(path.substring(3)); } return path; } if (path.length > 2 && path[1] === ':') { var _code = path.charCodeAt(0); if (_code >= 65 && _code <= 90) { path = "".concat(path[0].toLowerCase(), ":").concat(path.substring(2)); } if (path[0] !== '/') { path = "/".concat(path); } } return path; } /** * Normalize path separator to use Path.separator * @param Path candidate to normalize * @returns Normalized string path */ }, { key: "normalizePathSeparator", value: function normalizePathSeparator(path) { return path.split(/[\\]/).join(Path.separator); } }, { key: "normalizePath", value: function normalizePath(path) { return Path.normalizeDrive(Path.normalizePathSeparator(path)); } }, { key: "toRoot", value: function toRoot(path) { // '/' -> '/' // '/c:' -> '/c:' if (path.isRoot) { return path; } // 'foo/bar' -> `undefined` if (path.isRelative) { return undefined; } var index = path.raw.indexOf(Path.separator, Path.separator.length); if (index === -1) { // '/foo/bar' -> '/' return new Path(Path.separator); } // '/c:/foo/bar' -> '/c:' // '/foo/bar' -> '/' return new Path(path.raw.substring(0, index)).root; } }, { key: "join", value: function join(path) { for (var _len = arguments.length, paths = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { paths[_key - 1] = arguments[_key]; } var relativePath = paths.filter(function (s) { return !!s; }).join(Path.separator); if (!relativePath) { return path; } if (path.raw.endsWith(Path.separator)) { return new Path(path.raw + relativePath); } return new Path(path.raw + Path.separator + relativePath); } /** * * @param paths portions of a path * @returns a new Path if an absolute path can be computed from the segments passed in + this.raw * If no absolute path can be computed, returns undefined. * * Processes the path segments passed in from right to left (reverse order) concatenating until an * absolute path is found. */ }, { key: "resolve", value: function resolve(path) { for (var _len2 = arguments.length, paths = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { paths[_key2 - 1] = arguments[_key2]; } var segments = paths.slice().reverse(); // Don't mutate the caller's array. segments.push(path.raw); var result = new Path(''); var _iterator = _createForOfIteratorHelper(segments), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var segment = _step.value; if (segment) { var next = Path.join(new Path(segment), result.raw); if (next.isAbsolute) { return Path.normalize(next); } result = next; } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } return undefined; } }, { key: "relative", value: function relative(base, path) { if (base.raw === path.raw) { return new Path(''); } if (!base.raw || !path.raw) { return undefined; } var raw = base.base ? base.raw + Path.separator : base.raw; if (!path.raw.startsWith(raw)) { return undefined; } var relativePath = path.raw.substr(raw.length); return new Path(relativePath); } /* * return a normalized Path, resolving '..' and '.' segments */ }, { key: "normalize", value: function normalize(path) { var trailingSlash = path.raw.endsWith('/'); var pathArray = path.toString().split('/'); var resultArray = []; pathArray.forEach(function (value) { if (!value || value === '.') { return; } if (value === '..') { if (resultArray.length && resultArray[resultArray.length - 1] !== '..') { resultArray.pop(); } else if (path.isRelative) { resultArray.push('..'); } } else { resultArray.push(value); } }); if (resultArray.length === 0) { if (path.isRoot) { return new Path('/'); } else { return new Path('.'); } } return new Path((path.isAbsolute ? '/' : '') + resultArray.join('/') + (trailingSlash ? '/' : '')); } }]); return Path; }(); _defineProperty(Path, "separator", '/');