UNPKG

@storm-software/config-tools

Version:

⚡The Storm-Ops monorepo contains utility applications, tools, and various libraries to create modern and scalable web applications.

246 lines (227 loc) 8.71 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _chunkUSNT2KNTcjs = require('./chunk-USNT2KNT.cjs'); // src/utilities/correct-paths.ts var _DRIVE_LETTER_START_RE = /^[A-Za-z]:\//; function normalizeWindowsPath(input = "") { if (!input) { return input; } return input.replace(/\\/g, "/").replace(_DRIVE_LETTER_START_RE, (r) => r.toUpperCase()); } _chunkUSNT2KNTcjs.__name.call(void 0, normalizeWindowsPath, "normalizeWindowsPath"); var _UNC_REGEX = /^[/\\]{2}/; var _IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/; var _DRIVE_LETTER_RE = /^[A-Za-z]:$/; var _ROOT_FOLDER_RE = /^\/([A-Za-z]:)?$/; var _EXTNAME_RE = /.(\.[^./]+|\.)$/; var _PATH_ROOT_RE = /^[/\\]|^[a-zA-Z]:[/\\]/; var sep = "/"; var correctPaths = /* @__PURE__ */ _chunkUSNT2KNTcjs.__name.call(void 0, function(path) { if (!path || path.length === 0) { return "."; } path = normalizeWindowsPath(path); const isUNCPath = path.match(_UNC_REGEX); const isPathAbsolute = isAbsolute(path); const trailingSeparator = path[path.length - 1] === "/"; path = normalizeString(path, !isPathAbsolute); if (path.length === 0) { if (isPathAbsolute) { return "/"; } return trailingSeparator ? "./" : "."; } if (trailingSeparator) { path += "/"; } if (_DRIVE_LETTER_RE.test(path)) { path += "/"; } if (isUNCPath) { if (!isPathAbsolute) { return `//./${path}`; } return `//${path}`; } return isPathAbsolute && !isAbsolute(path) ? `/${path}` : path; }, "correctPaths"); var joinPaths = /* @__PURE__ */ _chunkUSNT2KNTcjs.__name.call(void 0, function(...segments) { let path = ""; for (const seg of segments) { if (!seg) { continue; } if (path.length > 0) { const pathTrailing = path[path.length - 1] === "/"; const segLeading = seg[0] === "/"; const both = pathTrailing && segLeading; if (both) { path += seg.slice(1); } else { path += pathTrailing || segLeading ? seg : `/${seg}`; } } else { path += seg; } } return correctPaths(path); }, "joinPaths"); function cwd() { if (typeof process !== "undefined" && typeof process.cwd === "function") { return process.cwd().replace(/\\/g, "/"); } return "/"; } _chunkUSNT2KNTcjs.__name.call(void 0, cwd, "cwd"); var resolve = /* @__PURE__ */ _chunkUSNT2KNTcjs.__name.call(void 0, function(...arguments_) { arguments_ = arguments_.map((argument) => normalizeWindowsPath(argument)); let resolvedPath = ""; let resolvedAbsolute = false; for (let index = arguments_.length - 1; index >= -1 && !resolvedAbsolute; index--) { const path = index >= 0 ? arguments_[index] : cwd(); if (!path || path.length === 0) { continue; } resolvedPath = `${path}/${resolvedPath}`; resolvedAbsolute = isAbsolute(path); } resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute); if (resolvedAbsolute && !isAbsolute(resolvedPath)) { return `/${resolvedPath}`; } return resolvedPath.length > 0 ? resolvedPath : "."; }, "resolve"); function normalizeString(path, allowAboveRoot) { let res = ""; let lastSegmentLength = 0; let lastSlash = -1; let dots = 0; let char = null; for (let index = 0; index <= path.length; ++index) { if (index < path.length) { char = path[index]; } else if (char === "/") { break; } else { char = "/"; } if (char === "/") { if (lastSlash === index - 1 || dots === 1) { } else if (dots === 2) { if (res.length < 2 || lastSegmentLength !== 2 || res[res.length - 1] !== "." || res[res.length - 2] !== ".") { if (res.length > 2) { const lastSlashIndex = res.lastIndexOf("/"); if (lastSlashIndex === -1) { res = ""; lastSegmentLength = 0; } else { res = res.slice(0, lastSlashIndex); lastSegmentLength = res.length - 1 - res.lastIndexOf("/"); } lastSlash = index; dots = 0; continue; } else if (res.length > 0) { res = ""; lastSegmentLength = 0; lastSlash = index; dots = 0; continue; } } if (allowAboveRoot) { res += res.length > 0 ? "/.." : ".."; lastSegmentLength = 2; } } else { if (res.length > 0) { res += `/${path.slice(lastSlash + 1, index)}`; } else { res = path.slice(lastSlash + 1, index); } lastSegmentLength = index - lastSlash - 1; } lastSlash = index; dots = 0; } else if (char === "." && dots !== -1) { ++dots; } else { dots = -1; } } return res; } _chunkUSNT2KNTcjs.__name.call(void 0, normalizeString, "normalizeString"); var isAbsolute = /* @__PURE__ */ _chunkUSNT2KNTcjs.__name.call(void 0, function(p) { return _IS_ABSOLUTE_RE.test(p); }, "isAbsolute"); var toNamespacedPath = /* @__PURE__ */ _chunkUSNT2KNTcjs.__name.call(void 0, function(p) { return normalizeWindowsPath(p); }, "toNamespacedPath"); var extname = /* @__PURE__ */ _chunkUSNT2KNTcjs.__name.call(void 0, function(p) { if (p === "..") return ""; const match = _EXTNAME_RE.exec(normalizeWindowsPath(p)); return match && match[1] || ""; }, "extname"); var relative = /* @__PURE__ */ _chunkUSNT2KNTcjs.__name.call(void 0, function(from, to) { const _from = resolve(from).replace(_ROOT_FOLDER_RE, "$1").split("/"); const _to = resolve(to).replace(_ROOT_FOLDER_RE, "$1").split("/"); if (_to[0][1] === ":" && _from[0][1] === ":" && _from[0] !== _to[0]) { return _to.join("/"); } const _fromCopy = [ ..._from ]; for (const segment of _fromCopy) { if (_to[0] !== segment) { break; } _from.shift(); _to.shift(); } return [ ..._from.map(() => ".."), ..._to ].join("/"); }, "relative"); var dirname = /* @__PURE__ */ _chunkUSNT2KNTcjs.__name.call(void 0, function(p) { const segments = normalizeWindowsPath(p).replace(/\/$/, "").split("/").slice(0, -1); if (segments.length === 1 && _DRIVE_LETTER_RE.test(segments[0])) { segments[0] += "/"; } return segments.join("/") || (isAbsolute(p) ? "/" : "."); }, "dirname"); var format = /* @__PURE__ */ _chunkUSNT2KNTcjs.__name.call(void 0, function(p) { const ext = p.ext ? p.ext.startsWith(".") ? p.ext : `.${p.ext}` : ""; const segments = [ p.root, p.dir, _nullishCoalesce(p.base, () => ( (_nullishCoalesce(p.name, () => ( ""))) + ext)) ].filter(Boolean); return normalizeWindowsPath(p.root ? resolve(...segments) : segments.join("/")); }, "format"); var basename = /* @__PURE__ */ _chunkUSNT2KNTcjs.__name.call(void 0, function(p, extension) { const segments = normalizeWindowsPath(p).split("/"); let lastSegment = ""; for (let i = segments.length - 1; i >= 0; i--) { const val = segments[i]; if (val) { lastSegment = val; break; } } return extension && lastSegment.endsWith(extension) ? lastSegment.slice(0, -extension.length) : lastSegment; }, "basename"); var parse = /* @__PURE__ */ _chunkUSNT2KNTcjs.__name.call(void 0, function(p) { const root = _optionalChain([_PATH_ROOT_RE, 'access', _ => _.exec, 'call', _2 => _2(p), 'optionalAccess', _3 => _3[0], 'optionalAccess', _4 => _4.replace, 'call', _5 => _5(/\\/g, "/")]) || ""; const base = basename(p); const extension = extname(base); return { root, dir: dirname(p), base, ext: extension, name: base.slice(0, base.length - extension.length) }; }, "parse"); exports.normalizeWindowsPath = normalizeWindowsPath; exports.sep = sep; exports.correctPaths = correctPaths; exports.joinPaths = joinPaths; exports.resolve = resolve; exports.normalizeString = normalizeString; exports.isAbsolute = isAbsolute; exports.toNamespacedPath = toNamespacedPath; exports.extname = extname; exports.relative = relative; exports.dirname = dirname; exports.format = format; exports.basename = basename; exports.parse = parse;