UNPKG

@synstack/path

Version:
120 lines (118 loc) 3.44 kB
var __defProp = Object.defineProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; // src/path.bundle.ts var path_bundle_exports = {}; __export(path_bundle_exports, { addMissingExtension: () => addMissingExtension, dirname: () => dirname2, ensureFileExtension: () => ensureFileExtension, fileExtension: () => fileExtension, filename: () => filename, filenameWithoutExtension: () => filenameWithoutExtension, importUrlToAbsolutePath: () => importUrlToAbsolutePath, isAbsolute: () => isAbsolute2, isInPath: () => isInPath, join: () => join2, mimeType: () => mimeType, relative: () => relative2, removeRelativeIndicator: () => removeRelativeIndicator, resolve: () => resolve2 }); // src/path.lib.ts import mime from "mime-types"; import * as fsPath from "path"; import { fileURLToPath } from "url"; var PathNotInCwdException = class extends Error { constructor(path, cwd) { super(`Path ${path} is not in cwd ${cwd}`); } }; function resolve2(...paths) { if (paths.length === 0) { throw new Error("No paths provided"); } const resolved = fsPath.resolve(...paths); if (process.platform === "win32" && /^[A-Z]:/.test(resolved)) { return resolved.charAt(0).toLowerCase() + resolved.slice(1); } return resolved; } function isAbsolute2(path) { return fsPath.isAbsolute(path); } function dirname2(path) { return fsPath.dirname(resolve2(path)); } function isInPath(basePath, path) { const resolvedBasePath = resolve2(basePath); const resolvedPath = resolve2(path); const relativePath = fsPath.relative(resolvedBasePath, resolvedPath); return relativePath !== "" && !relativePath.startsWith(".."); } function relative2(basePath, path) { const relativePath = fsPath.relative(resolve2(basePath), resolve2(path)); return removeRelativeIndicator( normalizeSeparators(relativePath) ); } function addMissingExtension(path, ext) { return path.endsWith(ext) ? path : `${path}.${ext}`; } function join2(cwd, ...paths) { const joined = fsPath.join(cwd, ...paths); if (fsPath.isAbsolute(joined)) { return resolve2(joined); } return joined; } function removeRelativeIndicator(path) { return path.replace(/^\.\//, ""); } function ensureFileExtension(filePath, extension) { const normalizedExt = extension.startsWith(".") ? extension : `.${extension}`; if (filePath.endsWith(normalizedExt)) return filePath; return `${filePath}${normalizedExt}`; } function normalizeSeparators(path) { return path.replace(/\\/g, "/"); } var importUrlToAbsolutePath = (importUrl) => { return resolve2(fsPath.dirname(fileURLToPath(importUrl))); }; var filename = (path) => { return fsPath.parse(path).base; }; var filenameWithoutExtension = (path) => { return fsPath.parse(path).name; }; var fileExtension = (path) => { return fsPath.parse(path).ext; }; var mimeType = (path) => { const type = mime.lookup(path); if (!type) return null; return type; }; export { PathNotInCwdException, addMissingExtension, dirname2 as dirname, ensureFileExtension, fileExtension, filename, filenameWithoutExtension, importUrlToAbsolutePath, isAbsolute2 as isAbsolute, isInPath, join2 as join, mimeType, normalizeSeparators, path_bundle_exports as path, relative2 as relative, removeRelativeIndicator, resolve2 as resolve }; //# sourceMappingURL=path.index.js.map