UNPKG

@stryke/path

Version:

A package containing various utilities that expand the functionality of NodeJs's built-in `path` module

100 lines (99 loc) 3.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.findFileExtension = findFileExtension; exports.findFileName = findFileName; exports.findFilePath = findFilePath; exports.findFolderName = findFolderName; exports.hasFileName = hasFileName; exports.hasFilePath = hasFilePath; exports.parsePath = parsePath; exports.relativePath = relativePath; exports.relativeToWorkspaceRoot = relativeToWorkspaceRoot; exports.renameFile = renameFile; exports.resolvePath = resolvePath; exports.resolvePaths = resolvePaths; var _base = require("@stryke/types/base"); var _nodePath = require("node:path"); var _correctPath = require("./correct-path.cjs"); var _getWorkspaceRoot = require("./get-workspace-root.cjs"); var _isFile = require("./is-file.cjs"); var _joinPaths = require("./join-paths.cjs"); function findFileName(t, e = {}) { const { requireExtension: n = !1, withExtension: r = !0 } = e, i = (0, _correctPath.normalizeWindowsPath)(t)?.split(t?.includes("\\") ? "\\" : "/")?.pop() ?? ""; return n === !0 && !i.includes(".") ? _base.EMPTY_STRING : r === !1 && i.includes(".") ? i.substring(0, i.lastIndexOf(".")) || _base.EMPTY_STRING : i; } function findFilePath(t) { const e = (0, _correctPath.normalizeWindowsPath)(t), n = e.replace(findFileName(e, { requireExtension: !0 }), ""); return n === "/" ? n : n.replace(/\/$/, ""); } function findFolderName(t) { const e = findFilePath(t).split("/"); let n = ""; for (let r = e.length - 1; r >= 0; r--) { const i = e[r]; if (i) { n = i; break; } } return n ?? _base.EMPTY_STRING; } function findFileExtension(t) { if (t === "..") return ""; const e = /.(\.[^./]+|\.)$/.exec((0, _correctPath.normalizeWindowsPath)(t)); return e && e[1] || _base.EMPTY_STRING; } function hasFileName(t) { return !!findFileName(t); } function hasFilePath(t) { return !!findFilePath(t); } function resolvePath(t, e = (0, _getWorkspaceRoot.getWorkspaceRoot)()) { const n = (0, _correctPath.normalizeWindowsPath)(t).split("/"); let r = "", i = !1; for (let s = n.length - 1; s >= -1 && !i; s--) { const o = s >= 0 ? n[s] : e; !o || o.length === 0 || (r = (0, _joinPaths.joinPaths)(o, r), i = (0, _isFile.isAbsolutePath)(o)); } return r = (0, _correctPath.normalizeString)(r, !i), i && !(0, _isFile.isAbsolutePath)(r) ? `/${r}` : r.length > 0 ? r : "."; } function resolvePaths(...t) { return resolvePath((0, _joinPaths.joinPaths)(...t.map(e => (0, _correctPath.normalizeWindowsPath)(e)))); } function relativePath(t, e, n = !1) { return (0, _nodePath.relative)(n !== !0 ? t.replace(/\/$/, "") : t, n !== !0 ? e.replace(/\/$/, "") : e); } function relativeToWorkspaceRoot(t) { return relativePath(t, (0, _getWorkspaceRoot.getWorkspaceRoot)()); } function parsePath(t) { const e = /^[/\\]|^[a-z]:[/\\]/i.exec(t)?.[0]?.replace(/\\/g, "/") || "", n = (0, _correctPath.normalizeWindowsPath)(t), r = n.replace(/\/$/, "").split("/").slice(0, -1); r.length === 1 && /^[A-Z]:$/i.test(r[0]) && (r[0] += "/"); const i = findFolderName(n), s = r.join("/") || ((0, _isFile.isAbsolutePath)(t) ? "/" : "."), o = findFileExtension(t); return { root: e, dir: s, base: i, ext: o, name: i.slice(0, i.length - o.length) }; } function renameFile(t, e) { const n = parsePath(t); return (0, _joinPaths.joinPaths)(n.dir, e.includes(".") ? e : e + n.ext); }