@synstack/path
Version:
Advanced path manipulation utilities
103 lines (101 loc) • 2.83 kB
JavaScript
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) {
return fsPath.resolve(...paths);
}
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);
return resolvedPath.startsWith(resolvedBasePath);
}
function relative2(basePath, path) {
return removeRelativeIndicator(
fsPath.relative(basePath, path)
);
}
function addMissingExtension(path, ext) {
return path.endsWith(ext) ? path : `${path}.${ext}`;
}
function join2(cwd, ...paths) {
return fsPath.join(cwd, ...paths);
}
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}`;
}
var importUrlToAbsolutePath = (importUrl) => {
return 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,
path_bundle_exports as path,
relative2 as relative,
removeRelativeIndicator,
resolve2 as resolve
};
//# sourceMappingURL=path.index.js.map